Polish MockHttpServletResponse

This commit is contained in:
Sam Brannen
2021-07-28 10:47:36 +02:00
parent 6c68419073
commit 403e04c0b4
2 changed files with 44 additions and 18 deletions

View File

@@ -83,7 +83,12 @@ public class MockHttpServletResponse implements HttpServletResponse {
@Nullable @Nullable
private String characterEncoding = WebUtils.DEFAULT_CHARACTER_ENCODING; private String characterEncoding = WebUtils.DEFAULT_CHARACTER_ENCODING;
private boolean charset = false; /**
* {@code true} if the character encoding has been explicitly set through
* {@link HttpServletResponse} methods or through a {@code charset} parameter
* on the {@code Content-Type}.
*/
private boolean characterEncodingSet = false;
private final ByteArrayOutputStream content = new ByteArrayOutputStream(1024); private final ByteArrayOutputStream content = new ByteArrayOutputStream(1024);
@@ -117,6 +122,11 @@ public class MockHttpServletResponse implements HttpServletResponse {
@Nullable @Nullable
private String errorMessage; private String errorMessage;
//---------------------------------------------------------------------
// Properties for MockRequestDispatcher
//---------------------------------------------------------------------
@Nullable @Nullable
private String forwardedUrl; private String forwardedUrl;
@@ -158,24 +168,27 @@ public class MockHttpServletResponse implements HttpServletResponse {
} }
/** /**
* Return whether the character encoding has been set. * Determine whether the character encoding has been explicitly set through
* <p>If {@code false}, {@link #getCharacterEncoding()} will return a default encoding value. * {@link HttpServletResponse} methods or through a {@code charset} parameter
* on the {@code Content-Type}.
* <p>If {@code false}, {@link #getCharacterEncoding()} will return a default
* encoding value.
*/ */
public boolean isCharset() { public boolean isCharset() {
return this.charset; return this.characterEncodingSet;
} }
@Override @Override
public void setCharacterEncoding(String characterEncoding) { public void setCharacterEncoding(String characterEncoding) {
this.characterEncoding = characterEncoding; this.characterEncoding = characterEncoding;
this.charset = true; this.characterEncodingSet = true;
updateContentTypePropertyAndHeader(); updateContentTypePropertyAndHeader();
} }
private void updateContentTypePropertyAndHeader() { private void updateContentTypePropertyAndHeader() {
if (this.contentType != null) { if (this.contentType != null) {
String value = this.contentType; String value = this.contentType;
if (this.charset && !this.contentType.toLowerCase().contains(CHARSET_PREFIX)) { if (this.characterEncodingSet && !this.contentType.toLowerCase().contains(CHARSET_PREFIX)) {
value = value + ';' + CHARSET_PREFIX + this.characterEncoding; value = value + ';' + CHARSET_PREFIX + this.characterEncoding;
this.contentType = value; this.contentType = value;
} }
@@ -270,7 +283,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
MediaType mediaType = MediaType.parseMediaType(contentType); MediaType mediaType = MediaType.parseMediaType(contentType);
if (mediaType.getCharset() != null) { if (mediaType.getCharset() != null) {
this.characterEncoding = mediaType.getCharset().name(); this.characterEncoding = mediaType.getCharset().name();
this.charset = true; this.characterEncodingSet = true;
} }
} }
catch (Exception ex) { catch (Exception ex) {
@@ -278,7 +291,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX); int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX);
if (charsetIndex != -1) { if (charsetIndex != -1) {
this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length()); this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
this.charset = true; this.characterEncodingSet = true;
} }
} }
updateContentTypePropertyAndHeader(); updateContentTypePropertyAndHeader();
@@ -332,7 +345,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
public void reset() { public void reset() {
resetBuffer(); resetBuffer();
this.characterEncoding = null; this.characterEncoding = null;
this.charset = false; this.characterEncodingSet = false;
this.contentLength = 0; this.contentLength = 0;
this.contentType = null; this.contentType = null;
this.locale = Locale.getDefault(); this.locale = Locale.getDefault();

View File

@@ -83,7 +83,12 @@ public class MockHttpServletResponse implements HttpServletResponse {
@Nullable @Nullable
private String characterEncoding = WebUtils.DEFAULT_CHARACTER_ENCODING; private String characterEncoding = WebUtils.DEFAULT_CHARACTER_ENCODING;
private boolean charset = false; /**
* {@code true} if the character encoding has been explicitly set through
* {@link HttpServletResponse} methods or through a {@code charset} parameter
* on the {@code Content-Type}.
*/
private boolean characterEncodingSet = false;
private final ByteArrayOutputStream content = new ByteArrayOutputStream(1024); private final ByteArrayOutputStream content = new ByteArrayOutputStream(1024);
@@ -117,6 +122,11 @@ public class MockHttpServletResponse implements HttpServletResponse {
@Nullable @Nullable
private String errorMessage; private String errorMessage;
//---------------------------------------------------------------------
// Properties for MockRequestDispatcher
//---------------------------------------------------------------------
@Nullable @Nullable
private String forwardedUrl; private String forwardedUrl;
@@ -158,24 +168,27 @@ public class MockHttpServletResponse implements HttpServletResponse {
} }
/** /**
* Return whether the character encoding has been set. * Determine whether the character encoding has been explicitly set through
* <p>If {@code false}, {@link #getCharacterEncoding()} will return a default encoding value. * {@link HttpServletResponse} methods or through a {@code charset} parameter
* on the {@code Content-Type}.
* <p>If {@code false}, {@link #getCharacterEncoding()} will return a default
* encoding value.
*/ */
public boolean isCharset() { public boolean isCharset() {
return this.charset; return this.characterEncodingSet;
} }
@Override @Override
public void setCharacterEncoding(String characterEncoding) { public void setCharacterEncoding(String characterEncoding) {
this.characterEncoding = characterEncoding; this.characterEncoding = characterEncoding;
this.charset = true; this.characterEncodingSet = true;
updateContentTypePropertyAndHeader(); updateContentTypePropertyAndHeader();
} }
private void updateContentTypePropertyAndHeader() { private void updateContentTypePropertyAndHeader() {
if (this.contentType != null) { if (this.contentType != null) {
String value = this.contentType; String value = this.contentType;
if (this.charset && !this.contentType.toLowerCase().contains(CHARSET_PREFIX)) { if (this.characterEncodingSet && !this.contentType.toLowerCase().contains(CHARSET_PREFIX)) {
value = value + ';' + CHARSET_PREFIX + this.characterEncoding; value = value + ';' + CHARSET_PREFIX + this.characterEncoding;
this.contentType = value; this.contentType = value;
} }
@@ -270,7 +283,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
MediaType mediaType = MediaType.parseMediaType(contentType); MediaType mediaType = MediaType.parseMediaType(contentType);
if (mediaType.getCharset() != null) { if (mediaType.getCharset() != null) {
this.characterEncoding = mediaType.getCharset().name(); this.characterEncoding = mediaType.getCharset().name();
this.charset = true; this.characterEncodingSet = true;
} }
} }
catch (Exception ex) { catch (Exception ex) {
@@ -278,7 +291,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX); int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX);
if (charsetIndex != -1) { if (charsetIndex != -1) {
this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length()); this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
this.charset = true; this.characterEncodingSet = true;
} }
} }
updateContentTypePropertyAndHeader(); updateContentTypePropertyAndHeader();
@@ -332,7 +345,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
public void reset() { public void reset() {
resetBuffer(); resetBuffer();
this.characterEncoding = null; this.characterEncoding = null;
this.charset = false; this.characterEncodingSet = false;
this.contentLength = 0; this.contentLength = 0;
this.contentType = null; this.contentType = null;
this.locale = Locale.getDefault(); this.locale = Locale.getDefault();