diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java index 40546ecb33..6c2873c4c0 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -50,6 +50,7 @@ import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import javax.servlet.http.Part; +import org.springframework.http.MediaType; import org.springframework.util.Assert; import org.springframework.util.LinkedCaseInsensitiveMap; import org.springframework.util.StringUtils; @@ -368,9 +369,18 @@ 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) { - this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length()); + try { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (mediaType.getCharSet() != null) { + this.characterEncoding = mediaType.getCharSet().name(); + } + } + catch (Exception ex) { + // Try to get charset value anyway + int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX); + if (charsetIndex != -1) { + this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length()); + } } updateContentTypeHeader(); } diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java index 525197336a..ae42cb4c12 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,6 +33,7 @@ import javax.servlet.ServletOutputStream; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletResponse; +import org.springframework.http.MediaType; import org.springframework.util.Assert; import org.springframework.util.LinkedCaseInsensitiveMap; import org.springframework.web.util.WebUtils; @@ -216,10 +217,20 @@ public class MockHttpServletResponse implements HttpServletResponse { public void setContentType(String contentType) { this.contentType = contentType; if (contentType != null) { - int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX); - if (charsetIndex != -1) { - this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length()); - this.charset = true; + try { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (mediaType.getCharSet() != null) { + this.characterEncoding = mediaType.getCharSet().name(); + this.charset = true; + } + } + catch (Exception ex) { + // Try to get charset value anyway + int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX); + if (charsetIndex != -1) { + this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length()); + this.charset = true; + } } updateContentTypeHeader(); } diff --git a/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java b/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java index 90f301a890..f0cd2866e6 100644 --- a/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java +++ b/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java @@ -100,6 +100,17 @@ public class MockHttpServletRequestTests { assertEquals("UTF-8", request.getCharacterEncoding()); } + // SPR-12677 + + @Test + public void setContentTypeHeaderWithMoreComplexCharsetSyntax() { + String contentType = "test/plain;charset=\"utf-8\";foo=\"charset=bar\";foocharset=bar;foo=bar"; + request.addHeader("Content-Type", contentType); + assertEquals(contentType, request.getContentType()); + assertEquals(contentType, request.getHeader("Content-Type")); + assertEquals("UTF-8", request.getCharacterEncoding()); + } + @Test public void setContentTypeThenCharacterEncoding() { request.setContentType("test/plain"); diff --git a/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java b/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java index 0f1cbc8666..3ed21c536c 100644 --- a/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java +++ b/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java @@ -90,6 +90,23 @@ public class MockHttpServletResponseTests { assertEquals("UTF-8", response.getCharacterEncoding()); } + // SPR-12677 + + @Test + public void contentTypeHeaderWithMoreComplexCharsetSyntax() { + String contentType = "test/plain;charset=\"utf-8\";foo=\"charset=bar\";foocharset=bar;foo=bar"; + 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, response.getContentType()); + assertEquals(contentType, response.getHeader("Content-Type")); + assertEquals("UTF-8", response.getCharacterEncoding()); + } + @Test public void setContentTypeThenCharacterEncoding() { response.setContentType("test/plain"); diff --git a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java index 2df697d72c..144977e6e4 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java +++ b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -50,6 +50,7 @@ import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import javax.servlet.http.Part; +import org.springframework.http.MediaType; import org.springframework.util.Assert; import org.springframework.util.LinkedCaseInsensitiveMap; import org.springframework.util.LinkedMultiValueMap; @@ -361,9 +362,18 @@ 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) { - this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length()); + try { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (mediaType.getCharSet() != null) { + this.characterEncoding = mediaType.getCharSet().name(); + } + } + catch (Exception ex) { + // Try to get charset value anyway + int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX); + if (charsetIndex != -1) { + this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length()); + } } updateContentTypeHeader(); } diff --git a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java index 080428883b..a1e7e243e1 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java +++ b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,6 +33,7 @@ import javax.servlet.ServletOutputStream; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletResponse; +import org.springframework.http.MediaType; import org.springframework.util.Assert; import org.springframework.util.LinkedCaseInsensitiveMap; import org.springframework.web.util.WebUtils; @@ -216,11 +217,20 @@ public class MockHttpServletResponse implements HttpServletResponse { 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; - this.charset = true; + try { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (mediaType.getCharSet() != null) { + this.characterEncoding = mediaType.getCharSet().name(); + this.charset = true; + } + } + catch (Exception ex) { + // Try to get charset value anyway + int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX); + if (charsetIndex != -1) { + this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length()); + this.charset = true; + } } updateContentTypeHeader(); }