Introduce setDefaultCharacterEncoding() in MockHttpServletResponse

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 gh-27214
This commit is contained in:
Sam Brannen
2021-07-29 15:38:51 +02:00
parent 55e17ef306
commit e4b9b1fadb
3 changed files with 90 additions and 12 deletions

View File

@@ -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);