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);
|
||||
}
|
||||
|
||||
@@ -54,6 +54,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
|
||||
@@ -65,6 +69,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);
|
||||
@@ -137,6 +143,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() {
|
||||
@@ -175,6 +194,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
|
||||
public void setContentLength(int contentLength) {
|
||||
this.contentLength = contentLength;
|
||||
doAddHeaderValue(CONTENT_LENGTH_HEADER, contentLength, true);
|
||||
}
|
||||
|
||||
public int getContentLength() {
|
||||
@@ -187,8 +207,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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -390,12 +414,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);
|
||||
|
||||
Reference in New Issue
Block a user