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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user