use of org.springframework.web.accept.ContentNegotiationManager in project spring-framework by spring-projects.
the class ContentNegotiationConfigurerTests method defaultSettings.
@Test
public void defaultSettings() throws Exception {
ContentNegotiationManager manager = this.configurer.getContentNegotiationManager();
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));
}
use of org.springframework.web.accept.ContentNegotiationManager in project spring-framework by spring-projects.
the class ContentNegotiationConfigurerTests method ignoreAcceptHeader.
@Test
public void ignoreAcceptHeader() throws Exception {
this.configurer.ignoreAcceptHeader(true);
ContentNegotiationManager manager = this.configurer.getContentNegotiationManager();
this.servletRequest.setRequestURI("/flower");
this.servletRequest.addHeader("Accept", MediaType.IMAGE_GIF_VALUE);
assertEquals(Collections.emptyList(), manager.resolveMediaTypes(this.webRequest));
}
use of org.springframework.web.accept.ContentNegotiationManager in project spring-framework by spring-projects.
the class MvcNamespaceTests method testContentNegotiationManager.
@Test
public void testContentNegotiationManager() throws Exception {
loadBeanDefinitions("mvc-config-content-negotiation-manager.xml", 15);
RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
ContentNegotiationManager manager = mapping.getContentNegotiationManager();
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.xml");
NativeWebRequest webRequest = new ServletWebRequest(request);
assertEquals(Collections.singletonList(MediaType.valueOf("application/rss+xml")), manager.resolveMediaTypes(webRequest));
ViewResolverComposite compositeResolver = this.appContext.getBean(ViewResolverComposite.class);
assertNotNull(compositeResolver);
assertEquals("Actual: " + compositeResolver.getViewResolvers(), 1, compositeResolver.getViewResolvers().size());
ViewResolver resolver = compositeResolver.getViewResolvers().get(0);
assertEquals(ContentNegotiatingViewResolver.class, resolver.getClass());
ContentNegotiatingViewResolver cnvr = (ContentNegotiatingViewResolver) resolver;
assertSame(manager, cnvr.getContentNegotiationManager());
}
use of org.springframework.web.accept.ContentNegotiationManager in project spring-framework by spring-projects.
the class RequestMappingHandlerMappingTests method useRegisteredSuffixPatternMatch.
@Test
public void useRegisteredSuffixPatternMatch() {
assertTrue(this.handlerMapping.useSuffixPatternMatch());
assertFalse(this.handlerMapping.useRegisteredSuffixPatternMatch());
Map<String, MediaType> fileExtensions = Collections.singletonMap("json", MediaType.APPLICATION_JSON);
PathExtensionContentNegotiationStrategy strategy = new PathExtensionContentNegotiationStrategy(fileExtensions);
ContentNegotiationManager manager = new ContentNegotiationManager(strategy);
this.handlerMapping.setContentNegotiationManager(manager);
this.handlerMapping.setUseRegisteredSuffixPatternMatch(true);
this.handlerMapping.afterPropertiesSet();
assertTrue(this.handlerMapping.useSuffixPatternMatch());
assertTrue(this.handlerMapping.useRegisteredSuffixPatternMatch());
assertEquals(Arrays.asList("json"), this.handlerMapping.getFileExtensions());
}
use of org.springframework.web.accept.ContentNegotiationManager in project spring-framework by spring-projects.
the class ResourceHttpRequestHandlerTests method getMediaTypeWithFavorPathExtensionOff.
// SPR-14577
@Test
public void getMediaTypeWithFavorPathExtensionOff() throws Exception {
ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean();
factory.setFavorPathExtension(false);
factory.afterPropertiesSet();
ContentNegotiationManager manager = factory.getObject();
List<Resource> paths = Collections.singletonList(new ClassPathResource("test/", getClass()));
ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
handler.setServletContext(new MockServletContext());
handler.setLocations(paths);
handler.setContentNegotiationManager(manager);
handler.afterPropertiesSet();
this.request.addHeader("Accept", "application/json,text/plain,*/*");
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.html");
handler.handleRequest(this.request, this.response);
assertEquals("text/html", this.response.getContentType());
}
Aggregations