SPR-8750 Update MockHttpServletRequest/Response handling of contentType.
The Content-Type header and the contentType field in HttpServletRequest/Response are now always in sync. When a header is added the contentType field is updated as well and vice versa. Similarly when the Content-Type header or the contentType field includes a charset field, the character encoding is updated and vice versa.
This commit is contained in:
@@ -101,6 +101,10 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
*/
|
||||
public static final String DEFAULT_REMOTE_HOST = "localhost";
|
||||
|
||||
private static final String CONTENT_TYPE_HEADER = "Content-Type";
|
||||
|
||||
private static final String CHARSET_PREFIX = "charset=";
|
||||
|
||||
|
||||
private boolean active = true;
|
||||
|
||||
@@ -306,6 +310,18 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
|
||||
public void setCharacterEncoding(String characterEncoding) {
|
||||
this.characterEncoding = characterEncoding;
|
||||
if (this.contentType != null) {
|
||||
String type = removeCharset(this.contentType);
|
||||
setContentType(type);
|
||||
}
|
||||
}
|
||||
|
||||
private String removeCharset(String contentType) {
|
||||
int index = contentType.toLowerCase().indexOf(CHARSET_PREFIX);
|
||||
if (index != -1) {
|
||||
contentType = contentType.substring(0, contentType.lastIndexOf(';', index));
|
||||
}
|
||||
return contentType;
|
||||
}
|
||||
|
||||
public void setContent(byte[] content) {
|
||||
@@ -318,6 +334,17 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
|
||||
public void setContentType(String contentType) {
|
||||
this.contentType = contentType;
|
||||
if (contentType != null) {
|
||||
int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX);
|
||||
if (charsetIndex != -1) {
|
||||
String encoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
|
||||
this.characterEncoding = encoding;
|
||||
}
|
||||
else if (this.characterEncoding != null) {
|
||||
this.contentType += ";" + CHARSET_PREFIX + this.characterEncoding;
|
||||
}
|
||||
doAddHeaderValue(CONTENT_TYPE_HEADER, this.contentType, true);
|
||||
}
|
||||
}
|
||||
|
||||
public String getContentType() {
|
||||
@@ -650,9 +677,18 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
* @see #getIntHeader
|
||||
*/
|
||||
public void addHeader(String name, Object value) {
|
||||
if (CONTENT_TYPE_HEADER.equalsIgnoreCase(name)) {
|
||||
setContentType((String) value);
|
||||
return;
|
||||
}
|
||||
doAddHeaderValue(name, value, false);
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
private void doAddHeaderValue(String name, Object value, boolean replace) {
|
||||
HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
|
||||
Assert.notNull(value, "Header value must not be null");
|
||||
if (header == null) {
|
||||
if (header == null || replace) {
|
||||
header = new HeaderValueHolder();
|
||||
this.headers.put(name, header);
|
||||
}
|
||||
|
||||
@@ -55,6 +55,10 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
|
||||
private static final String CHARSET_PREFIX = "charset=";
|
||||
|
||||
private static final String CONTENT_TYPE_HEADER = "Content-Type";
|
||||
|
||||
private static final String CONTENT_LENGTH_HEADER = "Content-Length";
|
||||
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
// ServletResponse properties
|
||||
@@ -66,6 +70,8 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
|
||||
private String characterEncoding = WebUtils.DEFAULT_CHARACTER_ENCODING;
|
||||
|
||||
private boolean charset = false;
|
||||
|
||||
private final ByteArrayOutputStream content = new ByteArrayOutputStream();
|
||||
|
||||
private final ServletOutputStream outputStream = new ResponseServletOutputStream(this.content);
|
||||
@@ -138,6 +144,19 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
|
||||
public void setCharacterEncoding(String characterEncoding) {
|
||||
this.characterEncoding = characterEncoding;
|
||||
this.charset = true;
|
||||
if (this.contentType != null) {
|
||||
String type = removeCharset(this.contentType);
|
||||
setContentType(type);
|
||||
}
|
||||
}
|
||||
|
||||
private String removeCharset(String contentType) {
|
||||
int index = contentType.toLowerCase().indexOf(CHARSET_PREFIX);
|
||||
if (index != -1) {
|
||||
contentType = contentType.substring(0, contentType.lastIndexOf(';', index));
|
||||
}
|
||||
return contentType;
|
||||
}
|
||||
|
||||
public String getCharacterEncoding() {
|
||||
@@ -176,6 +195,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
|
||||
public void setContentLength(int contentLength) {
|
||||
this.contentLength = contentLength;
|
||||
doAddHeaderValue(CONTENT_LENGTH_HEADER, contentLength, true);
|
||||
}
|
||||
|
||||
public int getContentLength() {
|
||||
@@ -188,8 +208,12 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX);
|
||||
if (charsetIndex != -1) {
|
||||
String encoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
|
||||
setCharacterEncoding(encoding);
|
||||
this.characterEncoding = encoding;
|
||||
}
|
||||
else if (this.charset) {
|
||||
this.contentType += ";" + CHARSET_PREFIX + this.characterEncoding;
|
||||
}
|
||||
doAddHeaderValue(CONTENT_TYPE_HEADER, this.contentType, true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -391,12 +415,32 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
}
|
||||
|
||||
private void setHeaderValue(String name, Object value) {
|
||||
if (setSpecialHeader(name, value)) {
|
||||
return;
|
||||
}
|
||||
doAddHeaderValue(name, value, true);
|
||||
}
|
||||
|
||||
private void addHeaderValue(String name, Object value) {
|
||||
if (setSpecialHeader(name, value)) {
|
||||
return;
|
||||
}
|
||||
doAddHeaderValue(name, value, false);
|
||||
}
|
||||
|
||||
private boolean setSpecialHeader(String name, Object value) {
|
||||
if (CONTENT_TYPE_HEADER.equalsIgnoreCase(name)) {
|
||||
setContentType((String) value);
|
||||
return true;
|
||||
}
|
||||
else if (CONTENT_LENGTH_HEADER.equalsIgnoreCase(name)) {
|
||||
setContentLength(Integer.parseInt((String) value));
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void doAddHeaderValue(String name, Object value, boolean replace) {
|
||||
HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
|
||||
|
||||
@@ -52,7 +52,6 @@ import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.mock.web.MockMultipartHttpServletRequest;
|
||||
import org.springframework.mock.web.MockPart;
|
||||
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
||||
import org.springframework.web.bind.ServletRequestBindingException;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RequestPart;
|
||||
@@ -235,7 +234,6 @@ public class RequestPartMethodArgumentResolverTests {
|
||||
|
||||
private void testResolveArgument(SimpleBean argValue, MethodParameter parameter) throws IOException, Exception {
|
||||
MediaType contentType = MediaType.TEXT_PLAIN;
|
||||
multipartRequest.addHeader("Content-Type", contentType.toString());
|
||||
|
||||
expect(messageConverter.canRead(SimpleBean.class, contentType)).andReturn(true);
|
||||
expect(messageConverter.read(eq(SimpleBean.class), isA(RequestPartServletServerHttpRequest.class))).andReturn(argValue);
|
||||
|
||||
@@ -92,7 +92,7 @@ public class MappingJacksonJsonViewTest {
|
||||
assertEquals("no-cache, no-store, max-age=0", response.getHeader("Cache-Control"));
|
||||
assertNotNull(response.getHeader("Expires"));
|
||||
|
||||
assertEquals(MappingJacksonJsonView.DEFAULT_CONTENT_TYPE, response.getContentType());
|
||||
assertEquals(MappingJacksonJsonView.DEFAULT_CONTENT_TYPE + ";charset=UTF-8", response.getContentType());
|
||||
|
||||
String jsonResult = response.getContentAsString();
|
||||
assertTrue(jsonResult.length() > 0);
|
||||
|
||||
Reference in New Issue
Block a user