- 
                Notifications
    
You must be signed in to change notification settings  - Fork 38.8k
 
Description
David Rabinowitz opened SPR-1717 and commented
In the servlet specification it determines that HttpServletResponse.setContentType is called before getWriter() it should determine the encoding of the content (pending that a charset was set). In MockHttpServletResponse.setContentType() is just a trivial setter, and does not perform this task.
Here is a fix:
private static final String CHARSET_STRING = "charset=";
private static final int CHARSET_STRING_LENGTH = CHARSET_STRING.length();
public void setContentType(String contentType) {
super.setContentType(contentType);
int position = contentType.toLowerCase().indexOf(CHARSET_STRING);
if(position != -1) {
String encoding = contentType.substring(position + CHARSET_STRING_LENGTH);
setCharacterEncoding(encoding);
}
}
Here is a test:
public void testSetContentTypeWithNoEncoding() {
MockHttpServletResponse response = new MmMockHttpServletResponse();
response.setContentType("test/plain");
assertEquals("Character encoding should be the default",
WebUtils.DEFAULT_CHARACTER_ENCODING, response
.getCharacterEncoding());
}
public void testSetContentTypeWithUTF8() {
MockHttpServletResponse response = new MmMockHttpServletResponse();
response.setContentType("test/plain; charset=UTF-8");
assertEquals("Character encoding should be 'UTF-8'", "UTF-8", response
.getCharacterEncoding());
}
Affects: 1.2.6