Consistent support for multiple Accept headers
Issue: SPR-14506
(cherry picked from commit e59a599)
This commit is contained in:
@@ -32,7 +32,7 @@ import java.util.TimeZone;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
@@ -67,13 +67,22 @@ public class HttpHeadersTests {
|
||||
}
|
||||
|
||||
@Test // SPR-9655
|
||||
public void acceptIPlanet() {
|
||||
public void acceptWithMultipleHeaderValues() {
|
||||
headers.add("Accept", "text/html");
|
||||
headers.add("Accept", "text/plain");
|
||||
List<MediaType> expected = Arrays.asList(new MediaType("text", "html"), new MediaType("text", "plain"));
|
||||
assertEquals("Invalid Accept header", expected, headers.getAccept());
|
||||
}
|
||||
|
||||
@Test // SPR-14506
|
||||
public void acceptWithMultipleCommaSeparatedHeaderValues() {
|
||||
headers.add("Accept", "text/html,text/pdf");
|
||||
headers.add("Accept", "text/plain,text/csv");
|
||||
List<MediaType> expected = Arrays.asList(new MediaType("text", "html"), new MediaType("text", "pdf"),
|
||||
new MediaType("text", "plain"), new MediaType("text", "csv"));
|
||||
assertEquals("Invalid Accept header", expected, headers.getAccept());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void acceptCharsets() {
|
||||
Charset charset1 = Charset.forName("UTF-8");
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -138,7 +138,7 @@ public class MediaTypeTests {
|
||||
assertNotNull("No media types returned", mediaTypes);
|
||||
assertEquals("Invalid amount of media types", 4, mediaTypes.size());
|
||||
|
||||
mediaTypes = MediaType.parseMediaTypes(null);
|
||||
mediaTypes = MediaType.parseMediaTypes("");
|
||||
assertNotNull("No media types returned", mediaTypes);
|
||||
assertEquals("Invalid amount of media types", 0, mediaTypes.size());
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -13,11 +13,11 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.accept;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
@@ -32,21 +32,16 @@ import static org.junit.Assert.*;
|
||||
* Test fixture for HeaderContentNegotiationStrategy tests.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class HeaderContentNegotiationStrategyTests {
|
||||
|
||||
private HeaderContentNegotiationStrategy strategy;
|
||||
private final HeaderContentNegotiationStrategy strategy = new HeaderContentNegotiationStrategy();
|
||||
|
||||
private NativeWebRequest webRequest;
|
||||
private final MockHttpServletRequest servletRequest = new MockHttpServletRequest();
|
||||
|
||||
private MockHttpServletRequest servletRequest;
|
||||
private final NativeWebRequest webRequest = new ServletWebRequest(this.servletRequest);
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.strategy = new HeaderContentNegotiationStrategy();
|
||||
this.servletRequest = new MockHttpServletRequest();
|
||||
this.webRequest = new ServletWebRequest(servletRequest );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveMediaTypes() throws Exception {
|
||||
@@ -60,7 +55,20 @@ public class HeaderContentNegotiationStrategyTests {
|
||||
assertEquals("text/plain;q=0.5", mediaTypes.get(3).toString());
|
||||
}
|
||||
|
||||
@Test(expected=HttpMediaTypeNotAcceptableException.class)
|
||||
@Test // SPR-14506
|
||||
public void resolveMediaTypesFromMultipleHeaderValues() throws Exception {
|
||||
this.servletRequest.addHeader("Accept", "text/plain; q=0.5, text/html");
|
||||
this.servletRequest.addHeader("Accept", "text/x-dvi; q=0.8, text/x-c");
|
||||
List<MediaType> mediaTypes = this.strategy.resolveMediaTypes(this.webRequest);
|
||||
|
||||
assertEquals(4, mediaTypes.size());
|
||||
assertEquals("text/html", mediaTypes.get(0).toString());
|
||||
assertEquals("text/x-c", mediaTypes.get(1).toString());
|
||||
assertEquals("text/x-dvi;q=0.8", mediaTypes.get(2).toString());
|
||||
assertEquals("text/plain;q=0.5", mediaTypes.get(3).toString());
|
||||
}
|
||||
|
||||
@Test(expected = HttpMediaTypeNotAcceptableException.class)
|
||||
public void resolveMediaTypesParseError() throws Exception {
|
||||
this.servletRequest.addHeader("Accept", "textplain; q=0.5");
|
||||
this.strategy.resolveMediaTypes(this.webRequest);
|
||||
|
||||
Reference in New Issue
Block a user