Skip to content

Commit 5576321

Browse files
committed
Reset charset field in MockHttpServletResponse
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
1 parent 785ab57 commit 5576321

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ public boolean isCommitted() {
331331
public void reset() {
332332
resetBuffer();
333333
this.characterEncoding = null;
334+
this.charset = false;
334335
this.contentLength = 0;
335336
this.contentType = null;
336337
this.locale = Locale.getDefault();

spring-test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,4 +458,27 @@ private void assertPrimarySessionCookie(String expectedValue) {
458458
assertThat(((MockCookie) cookie).getSameSite()).isEqualTo("Lax");
459459
}
460460

461+
@Test // gh-25501
462+
void resetResetsCharset() {
463+
assertThat(response.isCharset()).isFalse();
464+
response.setCharacterEncoding("UTF-8");
465+
assertThat(response.isCharset()).isTrue();
466+
assertThat(response.getCharacterEncoding()).isEqualTo("UTF-8");
467+
response.setContentType("text/plain");
468+
assertThat(response.getContentType()).isEqualTo("text/plain");
469+
String contentTypeHeader = response.getHeader(CONTENT_TYPE);
470+
assertThat(contentTypeHeader).isEqualTo("text/plain;charset=UTF-8");
471+
472+
response.reset();
473+
474+
assertThat(response.isCharset()).isFalse();
475+
// Do not invoke setCharacterEncoding() since that sets the charset flag to true.
476+
// response.setCharacterEncoding("UTF-8");
477+
response.setContentType("text/plain");
478+
assertThat(response.isCharset()).isFalse(); // should still be false
479+
assertThat(response.getContentType()).isEqualTo("text/plain");
480+
contentTypeHeader = response.getHeader(CONTENT_TYPE);
481+
assertThat(contentTypeHeader).isEqualTo("text/plain");
482+
}
483+
461484
}

spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletResponse.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ public boolean isCommitted() {
331331
public void reset() {
332332
resetBuffer();
333333
this.characterEncoding = null;
334+
this.charset = false;
334335
this.contentLength = 0;
335336
this.contentType = null;
336337
this.locale = Locale.getDefault();

0 commit comments

Comments
 (0)