Add ContentNegotiationManagerFactoryBean

The new FactoryBean facilitates the creation of a
ContentNegotiationManager in XML configuration.

Issue: SPR-8420
This commit is contained in:
Rossen Stoyanchev
2012-07-21 06:16:42 -04:00
parent 028e15faa3
commit 64d939bb16
6 changed files with 342 additions and 39 deletions

View File

@@ -0,0 +1,125 @@
/*
* 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.accept;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Collections;
import java.util.Properties;
import org.junit.Before;
import org.junit.Test;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.ServletWebRequest;
/**
* Test fixture for {@link ContentNegotiationManagerFactoryBean} tests.
* @author Rossen Stoyanchev
*/
public class ContentNegotiationManagerFactoryBeanTests {
private ContentNegotiationManagerFactoryBean factoryBean;
private NativeWebRequest webRequest;
private MockHttpServletRequest servletRequest;
@Before
public void setup() {
this.factoryBean = new ContentNegotiationManagerFactoryBean();
this.servletRequest = new MockHttpServletRequest();
this.webRequest = new ServletWebRequest(this.servletRequest);
}
@Test
public void defaultSettings() throws Exception {
this.factoryBean.afterPropertiesSet();
ContentNegotiationManager manager = this.factoryBean.getObject();
this.servletRequest.setRequestURI("/flower.gif");
assertEquals("Should be able to resolve file extensions by default",
Arrays.asList(MediaType.IMAGE_GIF), manager.resolveMediaTypes(this.webRequest));
this.servletRequest.setRequestURI("/flower?format=gif");
this.servletRequest.addParameter("format", "gif");
assertEquals("Should not resolve request parameters by default",
Collections.emptyList(), manager.resolveMediaTypes(this.webRequest));
this.servletRequest.setRequestURI("/flower");
this.servletRequest.addHeader("Accept", MediaType.IMAGE_GIF_VALUE);
assertEquals("Should resolve Accept header by default",
Arrays.asList(MediaType.IMAGE_GIF), manager.resolveMediaTypes(this.webRequest));
}
@Test
public void addMediaTypes() throws Exception {
Properties mediaTypes = new Properties();
mediaTypes.put("json", MediaType.APPLICATION_JSON_VALUE);
this.factoryBean.setMediaTypes(mediaTypes);
this.factoryBean.afterPropertiesSet();
ContentNegotiationManager manager = this.factoryBean.getObject();
this.servletRequest.setRequestURI("/flower.json");
assertEquals(Arrays.asList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(this.webRequest));
}
@Test
public void favorParameter() throws Exception {
this.factoryBean.setFavorParameter(true);
this.factoryBean.setParameterName("f");
Properties mediaTypes = new Properties();
mediaTypes.put("json", MediaType.APPLICATION_JSON_VALUE);
this.factoryBean.setMediaTypes(mediaTypes);
this.factoryBean.afterPropertiesSet();
ContentNegotiationManager manager = this.factoryBean.getObject();
this.servletRequest.setRequestURI("/flower");
this.servletRequest.addParameter("f", "json");
assertEquals(Arrays.asList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(this.webRequest));
}
@Test
public void ignoreAcceptHeader() throws Exception {
this.factoryBean.setIgnoreAcceptHeader(true);
this.factoryBean.afterPropertiesSet();
ContentNegotiationManager manager = this.factoryBean.getObject();
this.servletRequest.setRequestURI("/flower");
this.servletRequest.addHeader("Accept", MediaType.IMAGE_GIF_VALUE);
assertEquals(Collections.emptyList(), manager.resolveMediaTypes(this.webRequest));
}
@Test
public void setDefaultContentType() throws Exception {
this.factoryBean.setDefaultContentType(MediaType.APPLICATION_JSON);
this.factoryBean.afterPropertiesSet();
ContentNegotiationManager manager = this.factoryBean.getObject();
assertEquals(Arrays.asList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(this.webRequest));
}
}