Browse Source

Reset charset field in MockHttpServletResponse

Closes gh-25501
pull/23967/head
Juergen Hoeller 5 years ago
parent
commit
b254777744
  1. 20
      spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java
  2. 25
      spring-test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java
  3. 20
      spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -172,11 +172,11 @@ public class MockHttpServletResponse implements HttpServletResponse { @@ -172,11 +172,11 @@ public class MockHttpServletResponse implements HttpServletResponse {
private void updateContentTypeHeader() {
if (this.contentType != null) {
StringBuilder sb = new StringBuilder(this.contentType);
if (!this.contentType.toLowerCase().contains(CHARSET_PREFIX) && this.charset) {
sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding);
String value = this.contentType;
if (this.charset && !this.contentType.toLowerCase().contains(CHARSET_PREFIX)) {
value = value + ';' + CHARSET_PREFIX + this.characterEncoding;
}
doAddHeaderValue(HttpHeaders.CONTENT_TYPE, sb.toString(), true);
doAddHeaderValue(HttpHeaders.CONTENT_TYPE, value, true);
}
}
@ -197,7 +197,8 @@ public class MockHttpServletResponse implements HttpServletResponse { @@ -197,7 +197,8 @@ public class MockHttpServletResponse implements HttpServletResponse {
Assert.state(this.writerAccessAllowed, "Writer access not allowed");
if (this.writer == null) {
Writer targetWriter = (this.characterEncoding != null ?
new OutputStreamWriter(this.content, this.characterEncoding) : new OutputStreamWriter(this.content));
new OutputStreamWriter(this.content, this.characterEncoding) :
new OutputStreamWriter(this.content));
this.writer = new ResponsePrintWriter(targetWriter);
}
return this.writer;
@ -302,6 +303,7 @@ public class MockHttpServletResponse implements HttpServletResponse { @@ -302,6 +303,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
public void reset() {
resetBuffer();
this.characterEncoding = null;
this.charset = false;
this.contentLength = 0;
this.contentType = null;
this.locale = Locale.getDefault();
@ -395,7 +397,7 @@ public class MockHttpServletResponse implements HttpServletResponse { @@ -395,7 +397,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
/**
* Return the names of all specified headers as a Set of Strings.
* <p>As of Servlet 3.0, this method is also defined HttpServletResponse.
* <p>As of Servlet 3.0, this method is also defined in {@link HttpServletResponse}.
* @return the {@code Set} of header name {@code Strings}, or an empty {@code Set} if none
*/
@Override
@ -406,7 +408,7 @@ public class MockHttpServletResponse implements HttpServletResponse { @@ -406,7 +408,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
/**
* Return the primary value for the given header as a String, if any.
* Will return the first value in case of multiple values.
* <p>As of Servlet 3.0, this method is also defined in HttpServletResponse.
* <p>As of Servlet 3.0, this method is also defined in {@link HttpServletResponse}.
* As of Spring 3.1, it returns a stringified value for Servlet 3.0 compatibility.
* Consider using {@link #getHeaderValue(String)} for raw Object access.
* @param name the name of the header
@ -421,7 +423,7 @@ public class MockHttpServletResponse implements HttpServletResponse { @@ -421,7 +423,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
/**
* Return all values for the given header as a List of Strings.
* <p>As of Servlet 3.0, this method is also defined in HttpServletResponse.
* <p>As of Servlet 3.0, this method is also defined in {@link HttpServletResponse}.
* As of Spring 3.1, it returns a List of stringified values for Servlet 3.0 compatibility.
* Consider using {@link #getHeaderValues(String)} for raw Object access.
* @param name the name of the header

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -446,4 +446,27 @@ public class MockHttpServletResponseTests { @@ -446,4 +446,27 @@ public class MockHttpServletResponseTests {
assertEquals("Lax", ((MockCookie) cookie).getSameSite());
}
@Test // gh-25501
public void resetResetsCharset() {
assertFalse(response.isCharset());
response.setCharacterEncoding("UTF-8");
assertTrue(response.isCharset());
assertEquals(response.getCharacterEncoding(), "UTF-8");
response.setContentType("text/plain");
assertEquals(response.getContentType(), "text/plain");
String contentTypeHeader = response.getHeader(HttpHeaders.CONTENT_TYPE);
assertEquals(contentTypeHeader, "text/plain;charset=UTF-8");
response.reset();
assertFalse(response.isCharset());
// Do not invoke setCharacterEncoding() since that sets the charset flag to true.
// response.setCharacterEncoding("UTF-8");
response.setContentType("text/plain");
assertFalse(response.isCharset()); // should still be false
assertEquals(response.getContentType(), "text/plain");
contentTypeHeader = response.getHeader(HttpHeaders.CONTENT_TYPE);
assertEquals(contentTypeHeader, "text/plain");
}
}

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -172,11 +172,11 @@ public class MockHttpServletResponse implements HttpServletResponse { @@ -172,11 +172,11 @@ public class MockHttpServletResponse implements HttpServletResponse {
private void updateContentTypeHeader() {
if (this.contentType != null) {
StringBuilder sb = new StringBuilder(this.contentType);
if (!this.contentType.toLowerCase().contains(CHARSET_PREFIX) && this.charset) {
sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding);
String value = this.contentType;
if (this.charset && !this.contentType.toLowerCase().contains(CHARSET_PREFIX)) {
value = value + ';' + CHARSET_PREFIX + this.characterEncoding;
}
doAddHeaderValue(HttpHeaders.CONTENT_TYPE, sb.toString(), true);
doAddHeaderValue(HttpHeaders.CONTENT_TYPE, value, true);
}
}
@ -197,7 +197,8 @@ public class MockHttpServletResponse implements HttpServletResponse { @@ -197,7 +197,8 @@ public class MockHttpServletResponse implements HttpServletResponse {
Assert.state(this.writerAccessAllowed, "Writer access not allowed");
if (this.writer == null) {
Writer targetWriter = (this.characterEncoding != null ?
new OutputStreamWriter(this.content, this.characterEncoding) : new OutputStreamWriter(this.content));
new OutputStreamWriter(this.content, this.characterEncoding) :
new OutputStreamWriter(this.content));
this.writer = new ResponsePrintWriter(targetWriter);
}
return this.writer;
@ -302,6 +303,7 @@ public class MockHttpServletResponse implements HttpServletResponse { @@ -302,6 +303,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
public void reset() {
resetBuffer();
this.characterEncoding = null;
this.charset = false;
this.contentLength = 0;
this.contentType = null;
this.locale = Locale.getDefault();
@ -395,7 +397,7 @@ public class MockHttpServletResponse implements HttpServletResponse { @@ -395,7 +397,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
/**
* Return the names of all specified headers as a Set of Strings.
* <p>As of Servlet 3.0, this method is also defined HttpServletResponse.
* <p>As of Servlet 3.0, this method is also defined in {@link HttpServletResponse}.
* @return the {@code Set} of header name {@code Strings}, or an empty {@code Set} if none
*/
@Override
@ -406,7 +408,7 @@ public class MockHttpServletResponse implements HttpServletResponse { @@ -406,7 +408,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
/**
* Return the primary value for the given header as a String, if any.
* Will return the first value in case of multiple values.
* <p>As of Servlet 3.0, this method is also defined in HttpServletResponse.
* <p>As of Servlet 3.0, this method is also defined in {@link HttpServletResponse}.
* As of Spring 3.1, it returns a stringified value for Servlet 3.0 compatibility.
* Consider using {@link #getHeaderValue(String)} for raw Object access.
* @param name the name of the header
@ -421,7 +423,7 @@ public class MockHttpServletResponse implements HttpServletResponse { @@ -421,7 +423,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
/**
* Return all values for the given header as a List of Strings.
* <p>As of Servlet 3.0, this method is also defined in HttpServletResponse.
* <p>As of Servlet 3.0, this method is also defined in {@link HttpServletResponse}.
* As of Spring 3.1, it returns a List of stringified values for Servlet 3.0 compatibility.
* Consider using {@link #getHeaderValues(String)} for raw Object access.
* @param name the name of the header

Loading…
Cancel
Save