Skip to content

Commit 5e5723c

Browse files
committed
Reset charset field in MockHttpServletResponse
Closes gh-25501
1 parent 9fcdaa7 commit 5e5723c

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-2017 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(CONTENT_TYPE_HEADER, sb.toString(), true);
174+
doAddHeaderValue(CONTENT_TYPE_HEADER, value, true);
175175
}
176176
}
177177

@@ -195,7 +195,8 @@ public PrintWriter getWriter() throws UnsupportedEncodingException {
195195
}
196196
if (this.writer == null) {
197197
Writer targetWriter = (this.characterEncoding != null ?
198-
new OutputStreamWriter(this.content, this.characterEncoding) : new OutputStreamWriter(this.content));
198+
new OutputStreamWriter(this.content, this.characterEncoding) :
199+
new OutputStreamWriter(this.content));
199200
this.writer = new ResponsePrintWriter(targetWriter);
200201
}
201202
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 = null;
@@ -353,7 +355,7 @@ public boolean containsHeader(String name) {
353355

354356
/**
355357
* Return the names of all specified headers as a Set of Strings.
356-
* <p>As of Servlet 3.0, this method is also defined HttpServletResponse.
358+
* <p>As of Servlet 3.0, this method is also defined in {@link HttpServletResponse}.
357359
* @return the {@code Set} of header name {@code Strings}, or an empty {@code Set} if none
358360
*/
359361
@Override
@@ -364,7 +366,7 @@ public Collection<String> getHeaderNames() {
364366
/**
365367
* Return the primary value for the given header as a String, if any.
366368
* Will return the first value in case of multiple values.
367-
* <p>As of Servlet 3.0, this method is also defined in HttpServletResponse.
369+
* <p>As of Servlet 3.0, this method is also defined in {@link HttpServletResponse}.
368370
* As of Spring 3.1, it returns a stringified value for Servlet 3.0 compatibility.
369371
* Consider using {@link #getHeaderValue(String)} for raw Object access.
370372
* @param name the name of the header
@@ -378,7 +380,7 @@ public String getHeader(String name) {
378380

379381
/**
380382
* Return all values for the given header as a List of Strings.
381-
* <p>As of Servlet 3.0, this method is also defined in HttpServletResponse.
383+
* <p>As of Servlet 3.0, this method is also defined in {@link HttpServletResponse}.
382384
* As of Spring 3.1, it returns a List of stringified values for Servlet 3.0 compatibility.
383385
* Consider using {@link #getHeaderValues(String)} for raw Object access.
384386
* @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-2017 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.
@@ -166,11 +166,11 @@ public void setCharacterEncoding(String characterEncoding) {
166166

167167
private void updateContentTypeHeader() {
168168
if (this.contentType != null) {
169-
StringBuilder sb = new StringBuilder(this.contentType);
170-
if (!this.contentType.toLowerCase().contains(CHARSET_PREFIX) && this.charset) {
171-
sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding);
169+
String value = this.contentType;
170+
if (this.charset && !this.contentType.toLowerCase().contains(CHARSET_PREFIX)) {
171+
value = value + ';' + CHARSET_PREFIX + this.characterEncoding;
172172
}
173-
doAddHeaderValue(CONTENT_TYPE_HEADER, sb.toString(), true);
173+
doAddHeaderValue(CONTENT_TYPE_HEADER, value, true);
174174
}
175175
}
176176

@@ -194,7 +194,8 @@ public PrintWriter getWriter() throws UnsupportedEncodingException {
194194
}
195195
if (this.writer == null) {
196196
Writer targetWriter = (this.characterEncoding != null ?
197-
new OutputStreamWriter(this.content, this.characterEncoding) : new OutputStreamWriter(this.content));
197+
new OutputStreamWriter(this.content, this.characterEncoding) :
198+
new OutputStreamWriter(this.content));
198199
this.writer = new ResponsePrintWriter(targetWriter);
199200
}
200201
return this.writer;
@@ -301,6 +302,7 @@ public boolean isCommitted() {
301302
public void reset() {
302303
resetBuffer();
303304
this.characterEncoding = null;
305+
this.charset = false;
304306
this.contentLength = 0;
305307
this.contentType = null;
306308
this.locale = null;
@@ -352,7 +354,7 @@ public boolean containsHeader(String name) {
352354

353355
/**
354356
* Return the names of all specified headers as a Set of Strings.
355-
* <p>As of Servlet 3.0, this method is also defined HttpServletResponse.
357+
* <p>As of Servlet 3.0, this method is also defined in {@link HttpServletResponse}.
356358
* @return the {@code Set} of header name {@code Strings}, or an empty {@code Set} if none
357359
*/
358360
@Override
@@ -363,7 +365,7 @@ public Collection<String> getHeaderNames() {
363365
/**
364366
* Return the primary value for the given header as a String, if any.
365367
* Will return the first value in case of multiple values.
366-
* <p>As of Servlet 3.0, this method is also defined in HttpServletResponse.
368+
* <p>As of Servlet 3.0, this method is also defined in {@link HttpServletResponse}.
367369
* As of Spring 3.1, it returns a stringified value for Servlet 3.0 compatibility.
368370
* Consider using {@link #getHeaderValue(String)} for raw Object access.
369371
* @param name the name of the header
@@ -377,7 +379,7 @@ public String getHeader(String name) {
377379

378380
/**
379381
* Return all values for the given header as a List of Strings.
380-
* <p>As of Servlet 3.0, this method is also defined in HttpServletResponse.
382+
* <p>As of Servlet 3.0, this method is also defined in {@link HttpServletResponse}.
381383
* As of Spring 3.1, it returns a List of stringified values for Servlet 3.0 compatibility.
382384
* Consider using {@link #getHeaderValues(String)} for raw Object access.
383385
* @param name the name of the header

0 commit comments

Comments
 (0)