Skip to content

Commit b254777

Browse files
committed
Reset charset field in MockHttpServletResponse
Closes gh-25501
1 parent d9f9f8b commit b254777

File tree

3 files changed

+46
-19
lines changed

3 files changed

+46
-19
lines changed

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -172,11 +172,11 @@ public void setCharacterEncoding(String characterEncoding) {
172172

173173
private void updateContentTypeHeader() {
174174
if (this.contentType != null) {
175-
StringBuilder sb = new StringBuilder(this.contentType);
176-
if (!this.contentType.toLowerCase().contains(CHARSET_PREFIX) && this.charset) {
177-
sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding);
175+
String value = this.contentType;
176+
if (this.charset && !this.contentType.toLowerCase().contains(CHARSET_PREFIX)) {
177+
value = value + ';' + CHARSET_PREFIX + this.characterEncoding;
178178
}
179-
doAddHeaderValue(HttpHeaders.CONTENT_TYPE, sb.toString(), true);
179+
doAddHeaderValue(HttpHeaders.CONTENT_TYPE, value, true);
180180
}
181181
}
182182

@@ -197,7 +197,8 @@ public PrintWriter getWriter() throws UnsupportedEncodingException {
197197
Assert.state(this.writerAccessAllowed, "Writer access not allowed");
198198
if (this.writer == null) {
199199
Writer targetWriter = (this.characterEncoding != null ?
200-
new OutputStreamWriter(this.content, this.characterEncoding) : new OutputStreamWriter(this.content));
200+
new OutputStreamWriter(this.content, this.characterEncoding) :
201+
new OutputStreamWriter(this.content));
201202
this.writer = new ResponsePrintWriter(targetWriter);
202203
}
203204
return this.writer;
@@ -302,6 +303,7 @@ public boolean isCommitted() {
302303
public void reset() {
303304
resetBuffer();
304305
this.characterEncoding = null;
306+
this.charset = false;
305307
this.contentLength = 0;
306308
this.contentType = null;
307309
this.locale = Locale.getDefault();
@@ -395,7 +397,7 @@ public boolean containsHeader(String name) {
395397

396398
/**
397399
* Return the names of all specified headers as a Set of Strings.
398-
* <p>As of Servlet 3.0, this method is also defined HttpServletResponse.
400+
* <p>As of Servlet 3.0, this method is also defined in {@link HttpServletResponse}.
399401
* @return the {@code Set} of header name {@code Strings}, or an empty {@code Set} if none
400402
*/
401403
@Override
@@ -406,7 +408,7 @@ public Collection<String> getHeaderNames() {
406408
/**
407409
* Return the primary value for the given header as a String, if any.
408410
* Will return the first value in case of multiple values.
409-
* <p>As of Servlet 3.0, this method is also defined in HttpServletResponse.
411+
* <p>As of Servlet 3.0, this method is also defined in {@link HttpServletResponse}.
410412
* As of Spring 3.1, it returns a stringified value for Servlet 3.0 compatibility.
411413
* Consider using {@link #getHeaderValue(String)} for raw Object access.
412414
* @param name the name of the header
@@ -421,7 +423,7 @@ public String getHeader(String name) {
421423

422424
/**
423425
* Return all values for the given header as a List of Strings.
424-
* <p>As of Servlet 3.0, this method is also defined in HttpServletResponse.
426+
* <p>As of Servlet 3.0, this method is also defined in {@link HttpServletResponse}.
425427
* As of Spring 3.1, it returns a List of stringified values for Servlet 3.0 compatibility.
426428
* Consider using {@link #getHeaderValues(String)} for raw Object access.
427429
* @param name the name of the header

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -446,4 +446,27 @@ private void assertPrimarySessionCookie(String expectedValue) {
446446
assertEquals("Lax", ((MockCookie) cookie).getSameSite());
447447
}
448448

449+
@Test // gh-25501
450+
public void resetResetsCharset() {
451+
assertFalse(response.isCharset());
452+
response.setCharacterEncoding("UTF-8");
453+
assertTrue(response.isCharset());
454+
assertEquals(response.getCharacterEncoding(), "UTF-8");
455+
response.setContentType("text/plain");
456+
assertEquals(response.getContentType(), "text/plain");
457+
String contentTypeHeader = response.getHeader(HttpHeaders.CONTENT_TYPE);
458+
assertEquals(contentTypeHeader, "text/plain;charset=UTF-8");
459+
460+
response.reset();
461+
462+
assertFalse(response.isCharset());
463+
// Do not invoke setCharacterEncoding() since that sets the charset flag to true.
464+
// response.setCharacterEncoding("UTF-8");
465+
response.setContentType("text/plain");
466+
assertFalse(response.isCharset()); // should still be false
467+
assertEquals(response.getContentType(), "text/plain");
468+
contentTypeHeader = response.getHeader(HttpHeaders.CONTENT_TYPE);
469+
assertEquals(contentTypeHeader, "text/plain");
470+
}
471+
449472
}

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -172,11 +172,11 @@ public void setCharacterEncoding(String characterEncoding) {
172172

173173
private void updateContentTypeHeader() {
174174
if (this.contentType != null) {
175-
StringBuilder sb = new StringBuilder(this.contentType);
176-
if (!this.contentType.toLowerCase().contains(CHARSET_PREFIX) && this.charset) {
177-
sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding);
175+
String value = this.contentType;
176+
if (this.charset && !this.contentType.toLowerCase().contains(CHARSET_PREFIX)) {
177+
value = value + ';' + CHARSET_PREFIX + this.characterEncoding;
178178
}
179-
doAddHeaderValue(HttpHeaders.CONTENT_TYPE, sb.toString(), true);
179+
doAddHeaderValue(HttpHeaders.CONTENT_TYPE, value, true);
180180
}
181181
}
182182

@@ -197,7 +197,8 @@ public PrintWriter getWriter() throws UnsupportedEncodingException {
197197
Assert.state(this.writerAccessAllowed, "Writer access not allowed");
198198
if (this.writer == null) {
199199
Writer targetWriter = (this.characterEncoding != null ?
200-
new OutputStreamWriter(this.content, this.characterEncoding) : new OutputStreamWriter(this.content));
200+
new OutputStreamWriter(this.content, this.characterEncoding) :
201+
new OutputStreamWriter(this.content));
201202
this.writer = new ResponsePrintWriter(targetWriter);
202203
}
203204
return this.writer;
@@ -302,6 +303,7 @@ public boolean isCommitted() {
302303
public void reset() {
303304
resetBuffer();
304305
this.characterEncoding = null;
306+
this.charset = false;
305307
this.contentLength = 0;
306308
this.contentType = null;
307309
this.locale = Locale.getDefault();
@@ -395,7 +397,7 @@ public boolean containsHeader(String name) {
395397

396398
/**
397399
* Return the names of all specified headers as a Set of Strings.
398-
* <p>As of Servlet 3.0, this method is also defined HttpServletResponse.
400+
* <p>As of Servlet 3.0, this method is also defined in {@link HttpServletResponse}.
399401
* @return the {@code Set} of header name {@code Strings}, or an empty {@code Set} if none
400402
*/
401403
@Override
@@ -406,7 +408,7 @@ public Collection<String> getHeaderNames() {
406408
/**
407409
* Return the primary value for the given header as a String, if any.
408410
* Will return the first value in case of multiple values.
409-
* <p>As of Servlet 3.0, this method is also defined in HttpServletResponse.
411+
* <p>As of Servlet 3.0, this method is also defined in {@link HttpServletResponse}.
410412
* As of Spring 3.1, it returns a stringified value for Servlet 3.0 compatibility.
411413
* Consider using {@link #getHeaderValue(String)} for raw Object access.
412414
* @param name the name of the header
@@ -421,7 +423,7 @@ public String getHeader(String name) {
421423

422424
/**
423425
* Return all values for the given header as a List of Strings.
424-
* <p>As of Servlet 3.0, this method is also defined in HttpServletResponse.
426+
* <p>As of Servlet 3.0, this method is also defined in {@link HttpServletResponse}.
425427
* As of Spring 3.1, it returns a List of stringified values for Servlet 3.0 compatibility.
426428
* Consider using {@link #getHeaderValues(String)} for raw Object access.
427429
* @param name the name of the header

0 commit comments

Comments
 (0)