diff --git a/org.springframework.orm/src/test/java/org/springframework/mock/web/MockHttpServletRequest.java b/org.springframework.orm/src/test/java/org/springframework/mock/web/MockHttpServletRequest.java index f3b01519712..12a952ac06c 100644 --- a/org.springframework.orm/src/test/java/org/springframework/mock/web/MockHttpServletRequest.java +++ b/org.springframework.orm/src/test/java/org/springframework/mock/web/MockHttpServletRequest.java @@ -300,18 +300,17 @@ public class MockHttpServletRequest implements HttpServletRequest { public void setCharacterEncoding(String characterEncoding) { this.characterEncoding = characterEncoding; - if (this.contentType != null) { - String type = removeCharset(this.contentType); - setContentType(type); - } + updateContentTypeHeader(); } - private String removeCharset(String contentType) { - int index = contentType.toLowerCase().indexOf(CHARSET_PREFIX); - if (index != -1) { - contentType = contentType.substring(0, contentType.lastIndexOf(';', index)); + private void updateContentTypeHeader() { + if (this.contentType != null) { + StringBuilder sb = new StringBuilder(this.contentType); + if (this.contentType.toLowerCase().indexOf(CHARSET_PREFIX) == -1 && this.characterEncoding != null) { + sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding); + } + doAddHeaderValue(CONTENT_TYPE_HEADER, sb.toString(), true); } - return contentType; } public void setContent(byte[] content) { @@ -330,10 +329,7 @@ public class MockHttpServletRequest implements HttpServletRequest { 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); + updateContentTypeHeader(); } } diff --git a/org.springframework.orm/src/test/java/org/springframework/mock/web/MockHttpServletResponse.java b/org.springframework.orm/src/test/java/org/springframework/mock/web/MockHttpServletResponse.java index 5c1070b30ed..fa6d227709f 100644 --- a/org.springframework.orm/src/test/java/org/springframework/mock/web/MockHttpServletResponse.java +++ b/org.springframework.orm/src/test/java/org/springframework/mock/web/MockHttpServletResponse.java @@ -140,18 +140,17 @@ 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); - } + updateContentTypeHeader(); } - private String removeCharset(String contentType) { - int index = contentType.toLowerCase().indexOf(CHARSET_PREFIX); - if (index != -1) { - contentType = contentType.substring(0, contentType.lastIndexOf(';', index)); + private void updateContentTypeHeader() { + if (this.contentType != null) { + StringBuilder sb = new StringBuilder(this.contentType); + if (this.contentType.toLowerCase().indexOf(CHARSET_PREFIX) == -1 && this.charset) { + sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding); + } + doAddHeaderValue(CONTENT_TYPE_HEADER, sb.toString(), true); } - return contentType; } public String getCharacterEncoding() { @@ -204,11 +203,9 @@ public class MockHttpServletResponse implements HttpServletResponse { if (charsetIndex != -1) { String encoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length()); this.characterEncoding = encoding; + this.charset = true; } - else if (this.charset) { - this.contentType += ";" + CHARSET_PREFIX + this.characterEncoding; - } - doAddHeaderValue(CONTENT_TYPE_HEADER, this.contentType, true); + updateContentTypeHeader(); } } diff --git a/org.springframework.test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java b/org.springframework.test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java index 1134d0261fa..aa06a23214b 100644 --- a/org.springframework.test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java +++ b/org.springframework.test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java @@ -300,18 +300,17 @@ public class MockHttpServletRequest implements HttpServletRequest { public void setCharacterEncoding(String characterEncoding) { this.characterEncoding = characterEncoding; - if (this.contentType != null) { - String type = removeCharset(this.contentType); - setContentType(type); - } + updateContentTypeHeader(); } - private String removeCharset(String contentType) { - int index = contentType.toLowerCase().indexOf(CHARSET_PREFIX); - if (index != -1) { - contentType = contentType.substring(0, contentType.lastIndexOf(';', index)); + private void updateContentTypeHeader() { + if (this.contentType != null) { + StringBuilder sb = new StringBuilder(this.contentType); + if (this.contentType.toLowerCase().indexOf(CHARSET_PREFIX) == -1 && this.characterEncoding != null) { + sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding); + } + doAddHeaderValue(CONTENT_TYPE_HEADER, sb.toString(), true); } - return contentType; } public void setContent(byte[] content) { @@ -330,10 +329,7 @@ public class MockHttpServletRequest implements HttpServletRequest { 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); + updateContentTypeHeader(); } } diff --git a/org.springframework.test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java b/org.springframework.test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java index 08b11d9ddab..b0b2db17065 100644 --- a/org.springframework.test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java +++ b/org.springframework.test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java @@ -139,18 +139,17 @@ 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); - } + updateContentTypeHeader(); } - private String removeCharset(String contentType) { - int index = contentType.toLowerCase().indexOf(CHARSET_PREFIX); - if (index != -1) { - contentType = contentType.substring(0, contentType.lastIndexOf(';', index)); + private void updateContentTypeHeader() { + if (this.contentType != null) { + StringBuilder sb = new StringBuilder(this.contentType); + if (this.contentType.toLowerCase().indexOf(CHARSET_PREFIX) == -1 && this.charset) { + sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding); + } + doAddHeaderValue(CONTENT_TYPE_HEADER, sb.toString(), true); } - return contentType; } public String getCharacterEncoding() { @@ -203,11 +202,9 @@ public class MockHttpServletResponse implements HttpServletResponse { if (charsetIndex != -1) { String encoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length()); this.characterEncoding = encoding; + this.charset = true; } - else if (this.charset) { - this.contentType += ";" + CHARSET_PREFIX + this.characterEncoding; - } - doAddHeaderValue(CONTENT_TYPE_HEADER, this.contentType, true); + updateContentTypeHeader(); } } diff --git a/org.springframework.test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java b/org.springframework.test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java index 509333204ed..4bad4f0d273 100644 --- a/org.springframework.test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java +++ b/org.springframework.test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java @@ -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()); } diff --git a/org.springframework.test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java b/org.springframework.test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java index e282de6894e..0a037c8f2c3 100644 --- a/org.springframework.test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java +++ b/org.springframework.test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java @@ -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.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.setHeader("Content-Type", contentType); + assertEquals(contentType, response.getContentType()); + assertEquals(contentType, response.getHeader("Content-Type")); + assertEquals("UTF-8", response.getCharacterEncoding()); 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")); + response.addHeader("Content-Type", contentType); + 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")); - } - - 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")); + assertEquals("test/plain", response.getContentType()); + assertEquals("test/plain;charset=UTF-8", response.getHeader("Content-Type")); + assertEquals("UTF-8", response.getCharacterEncoding()); } - + 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 { diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/mock/web/MockHttpServletRequest.java b/org.springframework.web.servlet/src/test/java/org/springframework/mock/web/MockHttpServletRequest.java index 952f3a5137e..f493d553cf1 100644 --- a/org.springframework.web.servlet/src/test/java/org/springframework/mock/web/MockHttpServletRequest.java +++ b/org.springframework.web.servlet/src/test/java/org/springframework/mock/web/MockHttpServletRequest.java @@ -310,18 +310,17 @@ public class MockHttpServletRequest implements HttpServletRequest { public void setCharacterEncoding(String characterEncoding) { this.characterEncoding = characterEncoding; - if (this.contentType != null) { - String type = removeCharset(this.contentType); - setContentType(type); - } + updateContentTypeHeader(); } - private String removeCharset(String contentType) { - int index = contentType.toLowerCase().indexOf(CHARSET_PREFIX); - if (index != -1) { - contentType = contentType.substring(0, contentType.lastIndexOf(';', index)); + private void updateContentTypeHeader() { + if (this.contentType != null) { + StringBuilder sb = new StringBuilder(this.contentType); + if (this.contentType.toLowerCase().indexOf(CHARSET_PREFIX) == -1 && this.characterEncoding != null) { + sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding); + } + doAddHeaderValue(CONTENT_TYPE_HEADER, sb.toString(), true); } - return contentType; } public void setContent(byte[] content) { @@ -340,10 +339,7 @@ public class MockHttpServletRequest implements HttpServletRequest { 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); + updateContentTypeHeader(); } } diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/mock/web/MockHttpServletResponse.java b/org.springframework.web.servlet/src/test/java/org/springframework/mock/web/MockHttpServletResponse.java index 4ad4e08d0b0..0762220a774 100644 --- a/org.springframework.web.servlet/src/test/java/org/springframework/mock/web/MockHttpServletResponse.java +++ b/org.springframework.web.servlet/src/test/java/org/springframework/mock/web/MockHttpServletResponse.java @@ -145,18 +145,17 @@ 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); - } + updateContentTypeHeader(); } - private String removeCharset(String contentType) { - int index = contentType.toLowerCase().indexOf(CHARSET_PREFIX); - if (index != -1) { - contentType = contentType.substring(0, contentType.lastIndexOf(';', index)); + private void updateContentTypeHeader() { + if (this.contentType != null) { + StringBuilder sb = new StringBuilder(this.contentType); + if (this.contentType.toLowerCase().indexOf(CHARSET_PREFIX) == -1 && this.charset) { + sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding); + } + doAddHeaderValue(CONTENT_TYPE_HEADER, sb.toString(), true); } - return contentType; } public String getCharacterEncoding() { @@ -209,11 +208,9 @@ public class MockHttpServletResponse implements HttpServletResponse { if (charsetIndex != -1) { String encoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length()); this.characterEncoding = encoding; + this.charset = true; } - else if (this.charset) { - this.contentType += ";" + CHARSET_PREFIX + this.characterEncoding; - } - doAddHeaderValue(CONTENT_TYPE_HEADER, this.contentType, true); + updateContentTypeHeader(); } } diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/json/MappingJacksonJsonViewTest.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/json/MappingJacksonJsonViewTest.java index 76990195432..7b0a8e0c7d2 100644 --- a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/json/MappingJacksonJsonViewTest.java +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/json/MappingJacksonJsonViewTest.java @@ -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 + ";charset=UTF-8", response.getContentType()); + assertEquals(MappingJacksonJsonView.DEFAULT_CONTENT_TYPE, response.getContentType()); String jsonResult = response.getContentAsString(); assertTrue(jsonResult.length() > 0); diff --git a/org.springframework.web/src/test/java/org/springframework/mock/web/MockHttpServletRequest.java b/org.springframework.web/src/test/java/org/springframework/mock/web/MockHttpServletRequest.java index 952f3a5137e..f493d553cf1 100644 --- a/org.springframework.web/src/test/java/org/springframework/mock/web/MockHttpServletRequest.java +++ b/org.springframework.web/src/test/java/org/springframework/mock/web/MockHttpServletRequest.java @@ -310,18 +310,17 @@ public class MockHttpServletRequest implements HttpServletRequest { public void setCharacterEncoding(String characterEncoding) { this.characterEncoding = characterEncoding; - if (this.contentType != null) { - String type = removeCharset(this.contentType); - setContentType(type); - } + updateContentTypeHeader(); } - private String removeCharset(String contentType) { - int index = contentType.toLowerCase().indexOf(CHARSET_PREFIX); - if (index != -1) { - contentType = contentType.substring(0, contentType.lastIndexOf(';', index)); + private void updateContentTypeHeader() { + if (this.contentType != null) { + StringBuilder sb = new StringBuilder(this.contentType); + if (this.contentType.toLowerCase().indexOf(CHARSET_PREFIX) == -1 && this.characterEncoding != null) { + sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding); + } + doAddHeaderValue(CONTENT_TYPE_HEADER, sb.toString(), true); } - return contentType; } public void setContent(byte[] content) { @@ -340,10 +339,7 @@ public class MockHttpServletRequest implements HttpServletRequest { 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); + updateContentTypeHeader(); } } diff --git a/org.springframework.web/src/test/java/org/springframework/mock/web/MockHttpServletResponse.java b/org.springframework.web/src/test/java/org/springframework/mock/web/MockHttpServletResponse.java index ed2f8e23ffd..fa07a8e9541 100644 --- a/org.springframework.web/src/test/java/org/springframework/mock/web/MockHttpServletResponse.java +++ b/org.springframework.web/src/test/java/org/springframework/mock/web/MockHttpServletResponse.java @@ -144,18 +144,17 @@ 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); - } + updateContentTypeHeader(); } - private String removeCharset(String contentType) { - int index = contentType.toLowerCase().indexOf(CHARSET_PREFIX); - if (index != -1) { - contentType = contentType.substring(0, contentType.lastIndexOf(';', index)); + private void updateContentTypeHeader() { + if (this.contentType != null) { + StringBuilder sb = new StringBuilder(this.contentType); + if (this.contentType.toLowerCase().indexOf(CHARSET_PREFIX) == -1 && this.charset) { + sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding); + } + doAddHeaderValue(CONTENT_TYPE_HEADER, sb.toString(), true); } - return contentType; } public String getCharacterEncoding() { @@ -208,11 +207,9 @@ public class MockHttpServletResponse implements HttpServletResponse { if (charsetIndex != -1) { String encoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length()); this.characterEncoding = encoding; + this.charset = true; } - else if (this.charset) { - this.contentType += ";" + CHARSET_PREFIX + this.characterEncoding; - } - doAddHeaderValue(CONTENT_TYPE_HEADER, this.contentType, true); + updateContentTypeHeader(); } }