SPR-8750 Refine 'Content-Type' update in MockHttpServletRequest/Response.

The initial solution kept these three in full sync at all times:
contentType field, characterEncoding field, 'Content-Type' header.
That is correct behavior, however it breaks existing tests that rely
on contentType and characterEncoding being equal to exactly what 
they were set to.

For example, consider:
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");

Ideally both contentType and the 'Content-Type' header would now be
"text/plain;charset=UTF-8". However, existing tests would expect 
that contentType is equal to "text/plain".

To avoid breaking existing tests, contentType and characterEncoding
will continue to be equal to exactly what they were set to while
the 'Content-Type' header will always include both the content 
type and the charset.

The only exception to this rule is when a 'Content-Type' header
is set explicitly, the contentType and characterEncoding fields will 
be updated accordingly, possibly overriding the existing values.
This commit is contained in:
Rossen Stoyanchev
2011-11-17 15:07:15 +00:00
parent 56608d6bd6
commit 63e235f215
11 changed files with 124 additions and 167 deletions

View File

@@ -47,7 +47,7 @@ public class MockHttpServletRequestTests extends TestCase {
assertEquals("UTF-8", request.getCharacterEncoding());
}
public void testSetContentTypeHeader() {
public void testContentTypeHeader() {
String contentType = "test/plain";
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("Content-Type", contentType);
@@ -56,7 +56,7 @@ public class MockHttpServletRequestTests extends TestCase {
assertNull(request.getCharacterEncoding());
}
public void testSetContentTypeHeaderUTF8() {
public void testContentTypeHeaderUTF8() {
String contentType = "test/plain;charset=UTF-8";
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("Content-Type", contentType);
@@ -65,31 +65,20 @@ public class MockHttpServletRequestTests extends TestCase {
assertEquals("UTF-8", request.getCharacterEncoding());
}
public void testSetCharacterEncoding() {
String contentType = "test/plain;charset=UTF-8";
public void testSetContentTypeThenCharacterEncoding() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setContentType("test/plain");
request.setCharacterEncoding("UTF-8");
assertEquals(contentType, request.getContentType());
assertEquals(contentType, request.getHeader("Content-Type"));
assertEquals("test/plain", request.getContentType());
assertEquals("test/plain;charset=UTF-8", request.getHeader("Content-Type"));
assertEquals("UTF-8", request.getCharacterEncoding());
}
public void testSetCharacterEncodingOppositeOrder() {
String contentType = "test/plain;charset=UTF-8";
public void testSetCharacterEncodingThenContentType() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setCharacterEncoding("UTF-8");
request.setContentType("test/plain");
assertEquals(contentType, request.getContentType());
assertEquals(contentType, request.getHeader("Content-Type"));
assertEquals("UTF-8", request.getCharacterEncoding());
}
public void testReplaceCharacterEncoding() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setContentType("test/plain;charset=ISO-8859-1");
request.setCharacterEncoding("UTF-8");
assertEquals("test/plain;charset=UTF-8", request.getContentType());
assertEquals("test/plain", request.getContentType());
assertEquals("test/plain;charset=UTF-8", request.getHeader("Content-Type"));
assertEquals("UTF-8", request.getCharacterEncoding());
}

View File

@@ -32,88 +32,84 @@ import org.springframework.web.util.WebUtils;
*/
public class MockHttpServletResponseTests extends TestCase {
public void testSetContentTypeWithNoEncoding() {
public void testSetContentType() {
String contentType = "test/plain";
MockHttpServletResponse response = new MockHttpServletResponse();
response.setContentType(contentType);
assertEquals("Character encoding should be the default", WebUtils.DEFAULT_CHARACTER_ENCODING,
response.getCharacterEncoding());
assertEquals("Content-Type header not set", contentType, response.getHeader("Content-Type"));
assertEquals(contentType, response.getContentType());
assertEquals(contentType, response.getHeader("Content-Type"));
assertEquals(WebUtils.DEFAULT_CHARACTER_ENCODING, response.getCharacterEncoding());
}
public void testSetContentTypeWithUTF8() {
String contentType = "test/plain; charset=UTF-8";
public void testSetContentTypeUTF8() {
String contentType = "test/plain;charset=UTF-8";
MockHttpServletResponse response = new MockHttpServletResponse();
response.setContentType(contentType);
assertEquals("Character encoding should be 'UTF-8'", "UTF-8", response.getCharacterEncoding());
assertEquals("Content-Type header not set", contentType, response.getHeader("Content-Type"));
assertEquals("UTF-8", response.getCharacterEncoding());
assertEquals(contentType, response.getContentType());
assertEquals(contentType, response.getHeader("Content-Type"));
}
public void testContentTypeHeaderWithNoEncoding() {
public void testContentTypeHeader() {
String contentType = "test/plain";
MockHttpServletResponse response = new MockHttpServletResponse();
response.addHeader("Content-Type", contentType);
assertEquals("contentType field not set", contentType, response.getContentType());
assertEquals(contentType, response.getContentType());
assertEquals(contentType, response.getHeader("Content-Type"));
assertEquals(WebUtils.DEFAULT_CHARACTER_ENCODING, response.getCharacterEncoding());
response = new MockHttpServletResponse();
response.setHeader("Content-Type", contentType);
assertEquals("contentType field not set", contentType, response.getContentType());
assertEquals(contentType, response.getContentType());
assertEquals(contentType, response.getHeader("Content-Type"));
assertEquals(WebUtils.DEFAULT_CHARACTER_ENCODING, response.getCharacterEncoding());
}
public void testContentTypeHeaderWithUTF8() {
String contentType = "test/plain; charset=UTF-8";
public void testContentTypeHeaderUTF8() {
String contentType = "test/plain;charset=UTF-8";
MockHttpServletResponse response = new MockHttpServletResponse();
response.setHeader("Content-Type", contentType);
assertEquals(contentType, response.getContentType());
assertEquals(contentType, response.getHeader("Content-Type"));
assertEquals("UTF-8", response.getCharacterEncoding());
response = new MockHttpServletResponse();
response.addHeader("Content-Type", contentType);
assertEquals("contentType field not set", contentType, response.getContentType());
assertEquals("Character encoding should be 'UTF-8'", "UTF-8", response.getCharacterEncoding());
assertEquals("Content-Type header not set", contentType, response.getHeader("Content-Type"));
response = new MockHttpServletResponse();
response.setHeader("Content-Type", contentType);
assertEquals("contentType field not set", contentType, response.getContentType());
assertEquals("Character encoding should be 'UTF-8'", "UTF-8", response.getCharacterEncoding());
assertEquals("Content-Type header not set", contentType, response.getHeader("Content-Type"));
assertEquals(contentType, response.getContentType());
assertEquals(contentType, response.getHeader("Content-Type"));
assertEquals("UTF-8", response.getCharacterEncoding());
}
public void testSetCharacterEncoding() {
public void testSetContentTypeThenCharacterEncoding() {
MockHttpServletResponse response = new MockHttpServletResponse();
response.setContentType("test/plain");
response.setCharacterEncoding("UTF-8");
assertEquals("Character encoding not set", "UTF-8", response.getCharacterEncoding());
assertEquals("contentType field not set", "test/plain;charset=UTF-8", response.getContentType());
assertEquals("Content-Type header not set", "test/plain;charset=UTF-8", response.getHeader("Content-Type"));
assertEquals("test/plain", response.getContentType());
assertEquals("test/plain;charset=UTF-8", response.getHeader("Content-Type"));
assertEquals("UTF-8", response.getCharacterEncoding());
}
public void testSetCharacterEncodingOppositeOrder() {
public void testSetCharacterEncodingThenContentType() {
MockHttpServletResponse response = new MockHttpServletResponse();
response.setCharacterEncoding("UTF-8");
response.setContentType("test/plain");
assertEquals("Character encoding not set", "UTF-8", response.getCharacterEncoding());
assertEquals("contentType field not set", "test/plain;charset=UTF-8", response.getContentType());
assertEquals("Content-Type header not set", "test/plain;charset=UTF-8", response.getHeader("Content-Type"));
assertEquals("test/plain", response.getContentType());
assertEquals("test/plain;charset=UTF-8", response.getHeader("Content-Type"));
assertEquals("UTF-8", response.getCharacterEncoding());
}
public void testReplaceCharacterEncoding() {
MockHttpServletResponse response = new MockHttpServletResponse();
response.setContentType("test/plain;charset=ISO-8859-1");
response.setCharacterEncoding("UTF-8");
assertEquals("Character encoding not set", "UTF-8", response.getCharacterEncoding());
assertEquals("contentType field not set", "test/plain;charset=UTF-8", response.getContentType());
assertEquals("Content-Type header not set", "test/plain;charset=UTF-8", response.getHeader("Content-Type"));
}
public void testContentLength() {
MockHttpServletResponse response = new MockHttpServletResponse();
response.setContentLength(66);
assertEquals("Content length field not set", 66, response.getContentLength());
assertEquals("Content-Length header not set", "66", response.getHeader("Content-Length"));
assertEquals(66, response.getContentLength());
assertEquals("66", response.getHeader("Content-Length"));
}
public void testContentLengthHeader() {
MockHttpServletResponse response = new MockHttpServletResponse();
response.addHeader("Content-Length", "66");
assertEquals("Content length field not set", 66, response.getContentLength());
assertEquals("Content-Length header not set", "66", response.getHeader("Content-Length"));
assertEquals(66,response.getContentLength());
assertEquals("66", response.getHeader("Content-Length"));
}
public void testHttpHeaderNameCasingIsPreserved() throws Exception {