Skip to content

Commit

Permalink
Reset charset field in MockHttpServletResponse
Browse files Browse the repository at this point in the history
Prior to this commit, calling reset() on MockHttpServletResponse did not
reset the `charset` field to `false` which could result in the
"Content-Type" header containing `;charset=null` which in turn would
result in errors when parsing the "Content-Type" header.

This commit resets the charset field to `false` in
MockHttpServletResponse's reset() method to avoid such errors.

Closes gh-25501
  • Loading branch information
sbrannen committed Aug 1, 2020
1 parent 785ab57 commit 5576321
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
Expand Up @@ -331,6 +331,7 @@ public boolean isCommitted() {
public void reset() {
resetBuffer();
this.characterEncoding = null;
this.charset = false;
this.contentLength = 0;
this.contentType = null;
this.locale = Locale.getDefault();
Expand Down
Expand Up @@ -458,4 +458,27 @@ private void assertPrimarySessionCookie(String expectedValue) {
assertThat(((MockCookie) cookie).getSameSite()).isEqualTo("Lax");
}

@Test // gh-25501
void resetResetsCharset() {
assertThat(response.isCharset()).isFalse();
response.setCharacterEncoding("UTF-8");
assertThat(response.isCharset()).isTrue();
assertThat(response.getCharacterEncoding()).isEqualTo("UTF-8");
response.setContentType("text/plain");
assertThat(response.getContentType()).isEqualTo("text/plain");
String contentTypeHeader = response.getHeader(CONTENT_TYPE);
assertThat(contentTypeHeader).isEqualTo("text/plain;charset=UTF-8");

response.reset();

assertThat(response.isCharset()).isFalse();
// Do not invoke setCharacterEncoding() since that sets the charset flag to true.
// response.setCharacterEncoding("UTF-8");
response.setContentType("text/plain");
assertThat(response.isCharset()).isFalse(); // should still be false
assertThat(response.getContentType()).isEqualTo("text/plain");
contentTypeHeader = response.getHeader(CONTENT_TYPE);
assertThat(contentTypeHeader).isEqualTo("text/plain");
}

}
Expand Up @@ -331,6 +331,7 @@ public boolean isCommitted() {
public void reset() {
resetBuffer();
this.characterEncoding = null;
this.charset = false;
this.contentLength = 0;
this.contentType = null;
this.locale = Locale.getDefault();
Expand Down

0 comments on commit 5576321

Please sign in to comment.