Skip to content

Commit 14b1d20

Browse files
committed
Reset charset field in MockHttpServletResponse
Closes gh-25501
1 parent ce0832a commit 14b1d20

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
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-2018 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.
@@ -167,11 +167,11 @@ public void setCharacterEncoding(String characterEncoding) {
167167

168168
private void updateContentTypeHeader() {
169169
if (this.contentType != null) {
170-
StringBuilder sb = new StringBuilder(this.contentType);
171-
if (!this.contentType.toLowerCase().contains(CHARSET_PREFIX) && this.charset) {
172-
sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding);
170+
String value = this.contentType;
171+
if (this.charset && !this.contentType.toLowerCase().contains(CHARSET_PREFIX)) {
172+
value = value + ';' + CHARSET_PREFIX + this.characterEncoding;
173173
}
174-
doAddHeaderValue(HttpHeaders.CONTENT_TYPE, sb.toString(), true);
174+
doAddHeaderValue(HttpHeaders.CONTENT_TYPE, value, true);
175175
}
176176
}
177177

@@ -192,7 +192,8 @@ public PrintWriter getWriter() throws UnsupportedEncodingException {
192192
Assert.state(this.writerAccessAllowed, "Writer access not allowed");
193193
if (this.writer == null) {
194194
Writer targetWriter = (this.characterEncoding != null ?
195-
new OutputStreamWriter(this.content, this.characterEncoding) : new OutputStreamWriter(this.content));
195+
new OutputStreamWriter(this.content, this.characterEncoding) :
196+
new OutputStreamWriter(this.content));
196197
this.writer = new ResponsePrintWriter(targetWriter);
197198
}
198199
return this.writer;
@@ -297,6 +298,7 @@ public boolean isCommitted() {
297298
public void reset() {
298299
resetBuffer();
299300
this.characterEncoding = null;
301+
this.charset = false;
300302
this.contentLength = 0;
301303
this.contentType = null;
302304
this.locale = Locale.getDefault();
@@ -378,7 +380,7 @@ public boolean containsHeader(String name) {
378380

379381
/**
380382
* Return the names of all specified headers as a Set of Strings.
381-
* <p>As of Servlet 3.0, this method is also defined HttpServletResponse.
383+
* <p>As of Servlet 3.0, this method is also defined in {@link HttpServletResponse}.
382384
* @return the {@code Set} of header name {@code Strings}, or an empty {@code Set} if none
383385
*/
384386
@Override
@@ -389,7 +391,7 @@ public Collection<String> getHeaderNames() {
389391
/**
390392
* Return the primary value for the given header as a String, if any.
391393
* Will return the first value in case of multiple values.
392-
* <p>As of Servlet 3.0, this method is also defined in HttpServletResponse.
394+
* <p>As of Servlet 3.0, this method is also defined in {@link HttpServletResponse}.
393395
* As of Spring 3.1, it returns a stringified value for Servlet 3.0 compatibility.
394396
* Consider using {@link #getHeaderValue(String)} for raw Object access.
395397
* @param name the name of the header
@@ -404,7 +406,7 @@ public String getHeader(String name) {
404406

405407
/**
406408
* Return all values for the given header as a List of Strings.
407-
* <p>As of Servlet 3.0, this method is also defined in HttpServletResponse.
409+
* <p>As of Servlet 3.0, this method is also defined in {@link HttpServletResponse}.
408410
* As of Spring 3.1, it returns a List of stringified values for Servlet 3.0 compatibility.
409411
* Consider using {@link #getHeaderValues(String)} for raw Object access.
410412
* @param name the name of the header

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-2018 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.
@@ -161,11 +161,11 @@ public void setCharacterEncoding(String characterEncoding) {
161161

162162
private void updateContentTypeHeader() {
163163
if (this.contentType != null) {
164-
StringBuilder sb = new StringBuilder(this.contentType);
165-
if (!this.contentType.toLowerCase().contains(CHARSET_PREFIX) && this.charset) {
166-
sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding);
164+
String value = this.contentType;
165+
if (this.charset && !this.contentType.toLowerCase().contains(CHARSET_PREFIX)) {
166+
value = value + ';' + CHARSET_PREFIX + this.characterEncoding;
167167
}
168-
doAddHeaderValue(HttpHeaders.CONTENT_TYPE, sb.toString(), true);
168+
doAddHeaderValue(HttpHeaders.CONTENT_TYPE, value, true);
169169
}
170170
}
171171

@@ -185,7 +185,8 @@ public PrintWriter getWriter() throws UnsupportedEncodingException {
185185
Assert.state(this.writerAccessAllowed, "Writer access not allowed");
186186
if (this.writer == null) {
187187
Writer targetWriter = (this.characterEncoding != null ?
188-
new OutputStreamWriter(this.content, this.characterEncoding) : new OutputStreamWriter(this.content));
188+
new OutputStreamWriter(this.content, this.characterEncoding) :
189+
new OutputStreamWriter(this.content));
189190
this.writer = new ResponsePrintWriter(targetWriter);
190191
}
191192
return this.writer;
@@ -289,6 +290,7 @@ public boolean isCommitted() {
289290
public void reset() {
290291
resetBuffer();
291292
this.characterEncoding = null;
293+
this.charset = false;
292294
this.contentLength = 0;
293295
this.contentType = null;
294296
this.locale = Locale.getDefault();
@@ -369,7 +371,7 @@ public boolean containsHeader(String name) {
369371

370372
/**
371373
* Return the names of all specified headers as a Set of Strings.
372-
* <p>As of Servlet 3.0, this method is also defined HttpServletResponse.
374+
* <p>As of Servlet 3.0, this method is also defined in {@link HttpServletResponse}.
373375
* @return the {@code Set} of header name {@code Strings}, or an empty {@code Set} if none
374376
*/
375377
@Override
@@ -380,7 +382,7 @@ public Collection<String> getHeaderNames() {
380382
/**
381383
* Return the primary value for the given header as a String, if any.
382384
* Will return the first value in case of multiple values.
383-
* <p>As of Servlet 3.0, this method is also defined in HttpServletResponse.
385+
* <p>As of Servlet 3.0, this method is also defined in {@link HttpServletResponse}.
384386
* As of Spring 3.1, it returns a stringified value for Servlet 3.0 compatibility.
385387
* Consider using {@link #getHeaderValue(String)} for raw Object access.
386388
* @param name the name of the header
@@ -394,7 +396,7 @@ public String getHeader(String name) {
394396

395397
/**
396398
* Return all values for the given header as a List of Strings.
397-
* <p>As of Servlet 3.0, this method is also defined in HttpServletResponse.
399+
* <p>As of Servlet 3.0, this method is also defined in {@link HttpServletResponse}.
398400
* As of Spring 3.1, it returns a List of stringified values for Servlet 3.0 compatibility.
399401
* Consider using {@link #getHeaderValues(String)} for raw Object access.
400402
* @param name the name of the header

0 commit comments

Comments
 (0)