Use request contentType/encoding in ServletServetHttpRequest/Response
ServletServerHttpRequest now falls back on the contentType and the the characterEncoding of the ServletRequest, if the headers of the incoming request don't specify one. Similary ServletServerHttpResponse sets the contentType and the characterEncoding of the ServletResponse if not already set. This allows using the CharacterEncodingFilter to set a character encoding where the request doesn't specify one and have it be used in HttpMessageConverter's. SPR-9096
This commit is contained in:
committed by
Rossen Stoyanchev
parent
ff44c9132c
commit
edc80ffa95
@@ -1,11 +1,11 @@
|
||||
/*
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
* Copyright 2002-2012 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.http.server;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
@@ -24,6 +25,7 @@ import org.junit.Test;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
|
||||
@@ -67,6 +69,8 @@ public class ServletServerHttpRequestTests {
|
||||
mockRequest.addHeader(headerName, headerValue1);
|
||||
String headerValue2 = "value2";
|
||||
mockRequest.addHeader(headerName, headerValue2);
|
||||
mockRequest.setContentType("text/plain");
|
||||
mockRequest.setCharacterEncoding("UTF-8");
|
||||
|
||||
HttpHeaders headers = request.getHeaders();
|
||||
assertNotNull("No HttpHeaders returned", headers);
|
||||
@@ -75,6 +79,8 @@ public class ServletServerHttpRequestTests {
|
||||
assertEquals("Invalid header values returned", 2, headerValues.size());
|
||||
assertTrue("Invalid header values returned", headerValues.contains(headerValue1));
|
||||
assertTrue("Invalid header values returned", headerValues.contains(headerValue2));
|
||||
assertEquals("Invalid Content-Type", new MediaType("text", "plain", Charset.forName("UTF-8")),
|
||||
headers.getContentType());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
* Copyright 2002-2012 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.
|
||||
@@ -16,16 +16,19 @@
|
||||
|
||||
package org.springframework.http.server;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
@@ -56,12 +59,16 @@ public class ServletServerHttpResponseTests {
|
||||
headers.add(headerName, headerValue1);
|
||||
String headerValue2 = "value2";
|
||||
headers.add(headerName, headerValue2);
|
||||
headers.setContentType(new MediaType("text", "plain", Charset.forName("UTF-8")));
|
||||
|
||||
response.close();
|
||||
assertTrue("Header not set", mockResponse.getHeaderNames().contains(headerName));
|
||||
List headerValues = mockResponse.getHeaders(headerName);
|
||||
assertTrue("Header not set", headerValues.contains(headerValue1));
|
||||
assertTrue("Header not set", headerValues.contains(headerValue2));
|
||||
assertEquals("Invalid Content-Type", "text/plain;charset=UTF-8", mockResponse.getHeader("Content-Type"));
|
||||
assertEquals("Invalid Content-Type", "text/plain;charset=UTF-8", mockResponse.getContentType());
|
||||
assertEquals("Invalid Content-Type", "UTF-8", mockResponse.getCharacterEncoding());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user