Support for multiple default content types

Enhance FixedContentNegotiationStrategy and places where it is exposed
to also accept a list of media types.

Issue: SPR-15367
This commit is contained in:
Ryan O'Meara
2017-03-21 00:53:26 -04:00
committed by Rossen Stoyanchev
parent 95e78b16f7
commit 4a890226ea
5 changed files with 60 additions and 17 deletions

View File

@@ -16,13 +16,13 @@
package org.springframework.web.accept;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.http.MediaType;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockServletContext;
@@ -161,10 +161,10 @@ public class ContentNegotiationManagerFactoryBeanTests {
assertEquals(Collections.<MediaType>emptyList(), manager.resolveMediaTypes(this.webRequest));
}
@Test
public void setDefaultContentType() throws Exception {
this.factoryBean.setDefaultContentType(MediaType.APPLICATION_JSON);
this.factoryBean.setDefaultContentType(Arrays.asList(MediaType.APPLICATION_JSON));
this.factoryBean.afterPropertiesSet();
ContentNegotiationManager manager = this.factoryBean.getObject();
@@ -176,6 +176,21 @@ public class ContentNegotiationManagerFactoryBeanTests {
assertEquals(Collections.singletonList(MediaType.APPLICATION_JSON),
manager.resolveMediaTypes(this.webRequest));
}
@Test
public void setMultipleDefaultContentTypess() throws Exception {
this.factoryBean.setDefaultContentType(Arrays.asList(MediaType.APPLICATION_JSON, MediaType.ALL));
this.factoryBean.afterPropertiesSet();
ContentNegotiationManager manager = this.factoryBean.getObject();
assertEquals(Arrays.asList(MediaType.APPLICATION_JSON, MediaType.ALL),
manager.resolveMediaTypes(this.webRequest));
// SPR-15367
this.servletRequest.addHeader("Accept", MediaType.ALL_VALUE);
assertEquals(Arrays.asList(MediaType.APPLICATION_JSON, MediaType.ALL),
manager.resolveMediaTypes(this.webRequest));
}
@Test // SPR-12286
public void setDefaultContentTypeWithStrategy() throws Exception {