Browse Source

Improve MockHttpServletRequest/Response charset parsing

Issue: SPR-12677
pull/759/head
Rossen Stoyanchev 10 years ago
parent
commit
f06dffb714
  1. 18
      spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java
  2. 21
      spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java
  3. 11
      spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java
  4. 17
      spring-test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java
  5. 18
      spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java
  6. 22
      spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java

18
spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java

@ -1,5 +1,5 @@ @@ -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; @@ -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 { @@ -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();
}

21
spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java

@ -1,5 +1,5 @@ @@ -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; @@ -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 { @@ -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();
}

11
spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java

@ -100,6 +100,17 @@ public class MockHttpServletRequestTests { @@ -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");

17
spring-test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java

@ -90,6 +90,23 @@ public class MockHttpServletResponseTests { @@ -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");

18
spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java

@ -1,5 +1,5 @@ @@ -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; @@ -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 { @@ -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();
}

22
spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java

@ -1,5 +1,5 @@ @@ -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; @@ -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 { @@ -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();
}

Loading…
Cancel
Save