use of org.springframework.web.servlet.handler.SimpleUrlHandlerMapping in project spring-framework by spring-projects.
the class WebMvcConfigurationSupportExtensionTests method contentNegotiation.
@Test
public void contentNegotiation() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.json");
NativeWebRequest webRequest = new ServletWebRequest(request);
RequestMappingHandlerMapping mapping = this.config.requestMappingHandlerMapping();
ContentNegotiationManager manager = mapping.getContentNegotiationManager();
assertEquals(Collections.singletonList(APPLICATION_JSON), manager.resolveMediaTypes(webRequest));
request.setRequestURI("/foo.xml");
assertEquals(Collections.singletonList(APPLICATION_XML), manager.resolveMediaTypes(webRequest));
request.setRequestURI("/foo.rss");
assertEquals(Collections.singletonList(MediaType.valueOf("application/rss+xml")), manager.resolveMediaTypes(webRequest));
request.setRequestURI("/foo.atom");
assertEquals(Collections.singletonList(APPLICATION_ATOM_XML), manager.resolveMediaTypes(webRequest));
request.setRequestURI("/foo");
request.setParameter("f", "json");
assertEquals(Collections.singletonList(APPLICATION_JSON), manager.resolveMediaTypes(webRequest));
request.setRequestURI("/resources/foo.gif");
SimpleUrlHandlerMapping handlerMapping = (SimpleUrlHandlerMapping) this.config.resourceHandlerMapping();
handlerMapping.setApplicationContext(this.context);
HandlerExecutionChain chain = handlerMapping.getHandler(request);
assertNotNull(chain);
ResourceHttpRequestHandler handler = (ResourceHttpRequestHandler) chain.getHandler();
assertNotNull(handler);
assertSame(manager, handler.getContentNegotiationManager());
}
use of org.springframework.web.servlet.handler.SimpleUrlHandlerMapping in project spring-framework by spring-projects.
the class MvcNamespaceTests method testResources.
@Test
public void testResources() throws Exception {
loadBeanDefinitions("mvc-config-resources.xml", 20);
HttpRequestHandlerAdapter adapter = appContext.getBean(HttpRequestHandlerAdapter.class);
assertNotNull(adapter);
RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
ContentNegotiationManager manager = mapping.getContentNegotiationManager();
ResourceHttpRequestHandler handler = appContext.getBean(ResourceHttpRequestHandler.class);
assertNotNull(handler);
assertSame(manager, handler.getContentNegotiationManager());
SimpleUrlHandlerMapping resourceMapping = appContext.getBean(SimpleUrlHandlerMapping.class);
assertNotNull(resourceMapping);
assertEquals(Ordered.LOWEST_PRECEDENCE - 1, resourceMapping.getOrder());
BeanNameUrlHandlerMapping beanNameMapping = appContext.getBean(BeanNameUrlHandlerMapping.class);
assertNotNull(beanNameMapping);
assertEquals(2, beanNameMapping.getOrder());
ResourceUrlProvider urlProvider = appContext.getBean(ResourceUrlProvider.class);
assertNotNull(urlProvider);
Map<String, MappedInterceptor> beans = appContext.getBeansOfType(MappedInterceptor.class);
List<Class<?>> interceptors = beans.values().stream().map(mappedInterceptor -> mappedInterceptor.getInterceptor().getClass()).collect(Collectors.toList());
assertThat(interceptors, containsInAnyOrder(ConversionServiceExposingInterceptor.class, ResourceUrlProviderExposingInterceptor.class));
MockHttpServletRequest request = new MockHttpServletRequest();
request.setRequestURI("/resources/foo.css");
request.setMethod("GET");
HandlerExecutionChain chain = resourceMapping.getHandler(request);
assertNotNull(chain);
assertTrue(chain.getHandler() instanceof ResourceHttpRequestHandler);
MockHttpServletResponse response = new MockHttpServletResponse();
for (HandlerInterceptor interceptor : chain.getInterceptors()) {
interceptor.preHandle(request, response, chain.getHandler());
}
ModelAndView mv = adapter.handle(request, response, chain.getHandler());
assertNull(mv);
}
use of org.springframework.web.servlet.handler.SimpleUrlHandlerMapping in project spring-framework by spring-projects.
the class MvcNamespaceTests method testDefaultServletHandlerWithOptionalAttributes.
@Test
public void testDefaultServletHandlerWithOptionalAttributes() throws Exception {
loadBeanDefinitions("mvc-config-default-servlet-optional-attrs.xml", 6);
HttpRequestHandlerAdapter adapter = appContext.getBean(HttpRequestHandlerAdapter.class);
assertNotNull(adapter);
DefaultServletHttpRequestHandler handler = appContext.getBean(DefaultServletHttpRequestHandler.class);
assertNotNull(handler);
SimpleUrlHandlerMapping mapping = appContext.getBean(SimpleUrlHandlerMapping.class);
assertNotNull(mapping);
assertEquals(Ordered.LOWEST_PRECEDENCE, mapping.getOrder());
MockHttpServletRequest request = new MockHttpServletRequest();
request.setRequestURI("/foo.css");
request.setMethod("GET");
HandlerExecutionChain chain = mapping.getHandler(request);
assertTrue(chain.getHandler() instanceof DefaultServletHttpRequestHandler);
MockHttpServletResponse response = new MockHttpServletResponse();
ModelAndView mv = adapter.handle(request, response, chain.getHandler());
assertNull(mv);
}
use of org.springframework.web.servlet.handler.SimpleUrlHandlerMapping in project spring-framework by spring-projects.
the class DefaultServletHandlerConfigurerTests method enableWithServletName.
@Test
public void enableWithServletName() throws Exception {
configurer.enable("defaultServlet");
SimpleUrlHandlerMapping handlerMapping = getHandlerMapping();
DefaultServletHttpRequestHandler handler = (DefaultServletHttpRequestHandler) handlerMapping.getUrlMap().get("/**");
assertNotNull(handler);
assertEquals(Integer.MAX_VALUE, handlerMapping.getOrder());
handler.handleRequest(new MockHttpServletRequest(), response);
String expected = "defaultServlet";
assertEquals("The ServletContext was not called with the default servlet name", expected, servletContext.url);
assertEquals("The request was not forwarded", expected, response.getForwardedUrl());
}
use of org.springframework.web.servlet.handler.SimpleUrlHandlerMapping in project spring-framework by spring-projects.
the class DefaultServletHandlerConfigurerTests method enable.
@Test
public void enable() throws Exception {
configurer.enable();
SimpleUrlHandlerMapping getHandlerMapping = getHandlerMapping();
SimpleUrlHandlerMapping handlerMapping = getHandlerMapping;
DefaultServletHttpRequestHandler handler = (DefaultServletHttpRequestHandler) handlerMapping.getUrlMap().get("/**");
assertNotNull(handler);
assertEquals(Integer.MAX_VALUE, handlerMapping.getOrder());
handler.handleRequest(new MockHttpServletRequest(), response);
String expected = "default";
assertEquals("The ServletContext was not called with the default servlet name", expected, servletContext.url);
assertEquals("The request was not forwarded", expected, response.getForwardedUrl());
}
Aggregations