Consistently accept empty Content-Type header and empty character encoding

Issue: SPR-12173
(cherry picked from commit a1c0905)
This commit is contained in:
Juergen Hoeller
2014-10-22 01:04:22 +02:00
parent de3ea5dad6
commit 4ab27d83a1
4 changed files with 65 additions and 31 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 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.
@@ -47,6 +47,7 @@ import javax.servlet.http.HttpSession;
import org.springframework.util.Assert;
import org.springframework.util.LinkedCaseInsensitiveMap;
import org.springframework.util.StringUtils;
/**
* Mock implementation of the {@link javax.servlet.http.HttpServletRequest} interface.
@@ -210,8 +211,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
/**
* Create a new {@code MockHttpServletRequest} with the supplied {@link ServletContext}.
* @param servletContext the ServletContext that the request runs in (may be
* {@code null} to use a default {@link MockServletContext})
* @param servletContext the ServletContext that the request runs in
* (may be {@code null} to use a default {@link MockServletContext})
* @see #MockHttpServletRequest(ServletContext, String, String)
*/
public MockHttpServletRequest(ServletContext servletContext) {
@@ -308,9 +309,10 @@ public class MockHttpServletRequest implements HttpServletRequest {
}
private void updateContentTypeHeader() {
if (this.contentType != null) {
if (StringUtils.hasLength(this.contentType)) {
StringBuilder sb = new StringBuilder(this.contentType);
if (!this.contentType.toLowerCase().contains(CHARSET_PREFIX) && this.characterEncoding != null) {
if (!this.contentType.toLowerCase().contains(CHARSET_PREFIX) &&
StringUtils.hasLength(this.characterEncoding)) {
sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding);
}
doAddHeaderValue(CONTENT_TYPE_HEADER, sb.toString(), true);
@@ -330,8 +332,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
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.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
}
updateContentTypeHeader();
}
@@ -715,8 +716,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
return ((Number) value).longValue();
}
else if (value != null) {
throw new IllegalArgumentException("Value for header '" + name + "' is neither a Date nor a Number: "
+ value);
throw new IllegalArgumentException(
"Value for header '" + name + "' is neither a Date nor a Number: " + value);
}
else {
return -1L;