Skip to content

Commit

Permalink
Introduce setDefaultCharacterEncoding() in MockHttpServletResponse
Browse files Browse the repository at this point in the history
Prior to this commit, it was possible to set the character encoding
in MockHttpServletResponse via setCharacterEncoding() or
setContentType(); however, those methods append "charset=..." to the
Content-Type header which may not be an acceptable side effect.

This commit addresses this shortcoming by introducing a new
setDefaultCharacterEncoding() in MockHttpServletResponse which allows
one to override the previously hard coded value of "ISO-8859-1". In
addition, setDefaultCharacterEncoding() does not modify the Content-Type
header.

The reset() method has also been updated to reset the character encoding
to the configured default character encoding.

Closes spring-projectsgh-27214
  • Loading branch information
sbrannen authored and lxbzmy committed Mar 26, 2022
1 parent 973833d commit dd8579e
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ public class MockHttpServletResponse implements HttpServletResponse {

private boolean writerAccessAllowed = true;

private String characterEncoding = WebUtils.DEFAULT_CHARACTER_ENCODING;
private String defaultCharacterEncoding = WebUtils.DEFAULT_CHARACTER_ENCODING;

private String characterEncoding = this.defaultCharacterEncoding;

/**
* {@code true} if the character encoding has been explicitly set through
Expand Down Expand Up @@ -166,12 +168,33 @@ public boolean isWriterAccessAllowed() {
return this.writerAccessAllowed;
}

/**
* Set the <em>default</em> character encoding for the response.
* <p>If this method is not invoked, {@code ISO-8859-1} will be used as the
* default character encoding.
* <p>If the {@linkplain #getCharacterEncoding() character encoding} for the
* response has not already been explicitly set via {@link #setCharacterEncoding(String)}
* or {@link #setContentType(String)}, the character encoding for the response
* will be set to the supplied default character encoding.
* @param characterEncoding the default character encoding
* @since 5.3.10
* @see #setCharacterEncoding(String)
* @see #setContentType(String)
*/
public void setDefaultCharacterEncoding(String characterEncoding) {
Assert.notNull(characterEncoding, "'characterEncoding' must not be null");
this.defaultCharacterEncoding = characterEncoding;
if (!this.characterEncodingSet) {
this.characterEncoding = characterEncoding;
}
}

/**
* Determine whether the character encoding has been explicitly set through
* {@link HttpServletResponse} methods or through a {@code charset} parameter
* on the {@code Content-Type}.
* <p>If {@code false}, {@link #getCharacterEncoding()} will return the default
* character encoding.
* <p>If {@code false}, {@link #getCharacterEncoding()} will return the
* {@linkplain #setDefaultCharacterEncoding(String) default character encoding}.
*/
public boolean isCharset() {
return this.characterEncodingSet;
Expand Down Expand Up @@ -229,8 +252,9 @@ public byte[] getContentAsByteArray() {
* Get the content of the response body as a {@code String}, using the charset
* specified for the response by the application, either through
* {@link HttpServletResponse} methods or through a charset parameter on the
* {@code Content-Type}. If no charset has been explicitly defined, the default
* character encoding will be used.
* {@code Content-Type}. If no charset has been explicitly defined, the
* {@linkplain #setDefaultCharacterEncoding(String) default character encoding}
* will be used.
* @return the content as a {@code String}
* @throws UnsupportedEncodingException if the character encoding is not supported
* @see #getContentAsString(Charset)
Expand Down Expand Up @@ -346,7 +370,7 @@ public boolean isCommitted() {
@Override
public void reset() {
resetBuffer();
this.characterEncoding = WebUtils.DEFAULT_CHARACTER_ENCODING;
this.characterEncoding = this.defaultCharacterEncoding;
this.characterEncodingSet = false;
this.contentLength = 0;
this.contentType = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,36 @@ void setCharacterEncodingThenContentType() {
assertThat(response.getCharacterEncoding()).isEqualTo("UTF-8");
}

@Test
void defaultCharacterEncoding() {
assertThat(response.isCharset()).isFalse();
assertThat(response.getContentType()).isNull();
assertThat(response.getCharacterEncoding()).isEqualTo("ISO-8859-1");

response.setDefaultCharacterEncoding("UTF-8");
assertThat(response.isCharset()).isFalse();
assertThat(response.getContentType()).isNull();
assertThat(response.getCharacterEncoding()).isEqualTo("UTF-8");

response.setContentType("text/plain;charset=UTF-16");
assertThat(response.isCharset()).isTrue();
assertThat(response.getContentType()).isEqualTo("text/plain;charset=UTF-16");
assertThat(response.getCharacterEncoding()).isEqualTo("UTF-16");

response.reset();
assertThat(response.isCharset()).isFalse();
assertThat(response.getContentType()).isNull();
assertThat(response.getCharacterEncoding()).isEqualTo("UTF-8");

response.setCharacterEncoding("FOXTROT");
assertThat(response.isCharset()).isTrue();
assertThat(response.getContentType()).isNull();
assertThat(response.getCharacterEncoding()).isEqualTo("FOXTROT");

response.setDefaultCharacterEncoding("ENIGMA");
assertThat(response.getCharacterEncoding()).isEqualTo("FOXTROT");
}

@Test
void contentLength() {
response.setContentLength(66);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ public class MockHttpServletResponse implements HttpServletResponse {

private boolean writerAccessAllowed = true;

private String characterEncoding = WebUtils.DEFAULT_CHARACTER_ENCODING;
private String defaultCharacterEncoding = WebUtils.DEFAULT_CHARACTER_ENCODING;

private String characterEncoding = this.defaultCharacterEncoding;

/**
* {@code true} if the character encoding has been explicitly set through
Expand Down Expand Up @@ -166,12 +168,33 @@ public boolean isWriterAccessAllowed() {
return this.writerAccessAllowed;
}

/**
* Set the <em>default</em> character encoding for the response.
* <p>If this method is not invoked, {@code ISO-8859-1} will be used as the
* default character encoding.
* <p>If the {@linkplain #getCharacterEncoding() character encoding} for the
* response has not already been explicitly set via {@link #setCharacterEncoding(String)}
* or {@link #setContentType(String)}, the character encoding for the response
* will be set to the supplied default character encoding.
* @param characterEncoding the default character encoding
* @since 5.3.10
* @see #setCharacterEncoding(String)
* @see #setContentType(String)
*/
public void setDefaultCharacterEncoding(String characterEncoding) {
Assert.notNull(characterEncoding, "'characterEncoding' must not be null");
this.defaultCharacterEncoding = characterEncoding;
if (!this.characterEncodingSet) {
this.characterEncoding = characterEncoding;
}
}

/**
* Determine whether the character encoding has been explicitly set through
* {@link HttpServletResponse} methods or through a {@code charset} parameter
* on the {@code Content-Type}.
* <p>If {@code false}, {@link #getCharacterEncoding()} will return the default
* character encoding.
* <p>If {@code false}, {@link #getCharacterEncoding()} will return the
* {@linkplain #setDefaultCharacterEncoding(String) default character encoding}.
*/
public boolean isCharset() {
return this.characterEncodingSet;
Expand Down Expand Up @@ -229,8 +252,9 @@ public byte[] getContentAsByteArray() {
* Get the content of the response body as a {@code String}, using the charset
* specified for the response by the application, either through
* {@link HttpServletResponse} methods or through a charset parameter on the
* {@code Content-Type}. If no charset has been explicitly defined, the default
* character encoding will be used.
* {@code Content-Type}. If no charset has been explicitly defined, the
* {@linkplain #setDefaultCharacterEncoding(String) default character encoding}
* will be used.
* @return the content as a {@code String}
* @throws UnsupportedEncodingException if the character encoding is not supported
* @see #getContentAsString(Charset)
Expand Down Expand Up @@ -346,7 +370,7 @@ public boolean isCommitted() {
@Override
public void reset() {
resetBuffer();
this.characterEncoding = WebUtils.DEFAULT_CHARACTER_ENCODING;
this.characterEncoding = this.defaultCharacterEncoding;
this.characterEncodingSet = false;
this.contentLength = 0;
this.contentType = null;
Expand Down

0 comments on commit dd8579e

Please sign in to comment.