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:
parent
55e17ef306
commit
e4b9b1fadb
|
@ -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 <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;
|
||||
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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 <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;
|
||||
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue