diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java index 52a7ab354f7..a1b3eca3b08 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java @@ -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 @@ -166,12 +168,33 @@ public class MockHttpServletResponse implements HttpServletResponse { return this.writerAccessAllowed; } + /** + * Set the default character encoding for the response. + *
If this method is not invoked, {@code ISO-8859-1} will be used as the + * default character encoding. + *
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}. - *
If {@code false}, {@link #getCharacterEncoding()} will return the default - * character encoding. + *
If {@code false}, {@link #getCharacterEncoding()} will return the + * {@linkplain #setDefaultCharacterEncoding(String) default character encoding}. */ public boolean isCharset() { return this.characterEncodingSet; @@ -229,8 +252,9 @@ public class MockHttpServletResponse implements HttpServletResponse { * 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) @@ -346,7 +370,7 @@ public class MockHttpServletResponse implements HttpServletResponse { @Override public void reset() { resetBuffer(); - this.characterEncoding = WebUtils.DEFAULT_CHARACTER_ENCODING; + this.characterEncoding = this.defaultCharacterEncoding; this.characterEncodingSet = false; this.contentLength = 0; this.contentType = null; diff --git a/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java b/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java index e8ac11b36b3..fe0f90f7b50 100644 --- a/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java +++ b/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java @@ -195,6 +195,36 @@ class MockHttpServletResponseTests { 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); diff --git a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletResponse.java b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletResponse.java index 7418a287160..4a343c8344c 100644 --- a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletResponse.java +++ b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletResponse.java @@ -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 @@ -166,12 +168,33 @@ public class MockHttpServletResponse implements HttpServletResponse { return this.writerAccessAllowed; } + /** + * Set the default character encoding for the response. + *
If this method is not invoked, {@code ISO-8859-1} will be used as the + * default character encoding. + *
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}. - *
If {@code false}, {@link #getCharacterEncoding()} will return the default - * character encoding. + *
If {@code false}, {@link #getCharacterEncoding()} will return the + * {@linkplain #setDefaultCharacterEncoding(String) default character encoding}. */ public boolean isCharset() { return this.characterEncodingSet; @@ -229,8 +252,9 @@ public class MockHttpServletResponse implements HttpServletResponse { * 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) @@ -346,7 +370,7 @@ public class MockHttpServletResponse implements HttpServletResponse { @Override public void reset() { resetBuffer(); - this.characterEncoding = WebUtils.DEFAULT_CHARACTER_ENCODING; + this.characterEncoding = this.defaultCharacterEncoding; this.characterEncodingSet = false; this.contentLength = 0; this.contentType = null;