use of org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping in project spring-framework by spring-projects.
the class StandaloneMockMvcBuilderTests method placeHoldersInRequestMapping.
// SPR-10825
@Test
public void placeHoldersInRequestMapping() throws Exception {
TestStandaloneMockMvcBuilder builder = new TestStandaloneMockMvcBuilder(new PlaceholderController());
builder.addPlaceholderValue("sys.login.ajax", "/foo");
builder.build();
RequestMappingHandlerMapping hm = builder.wac.getBean(RequestMappingHandlerMapping.class);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
HandlerExecutionChain chain = hm.getHandler(request);
assertNotNull(chain);
assertEquals("handleWithPlaceholders", ((HandlerMethod) chain.getHandler()).getMethod().getName());
}
use of org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping in project spring-boot by spring-projects.
the class SitePreferenceAutoConfigurationTests method sitePreferenceHandlerInterceptorRegistered.
@Test
public void sitePreferenceHandlerInterceptorRegistered() throws Exception {
this.context = new AnnotationConfigWebApplicationContext();
this.context.setServletContext(new MockServletContext());
this.context.register(Config.class, WebMvcAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, SitePreferenceAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
RequestMappingHandlerMapping mapping = this.context.getBean(RequestMappingHandlerMapping.class);
HandlerInterceptor[] interceptors = mapping.getHandler(new MockHttpServletRequest()).getInterceptors();
assertThat(interceptors).hasAtLeastOneElementOfType(SitePreferenceHandlerInterceptor.class);
}
use of org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping in project spring-boot by spring-projects.
the class DeviceResolverAutoConfigurationTests method deviceResolverHandlerInterceptorRegistered.
@Test
public void deviceResolverHandlerInterceptorRegistered() throws Exception {
this.context = new AnnotationConfigWebApplicationContext();
this.context.setServletContext(new MockServletContext());
this.context.register(Config.class);
this.context.refresh();
RequestMappingHandlerMapping mapping = this.context.getBean(RequestMappingHandlerMapping.class);
HandlerInterceptor[] interceptors = mapping.getHandler(new MockHttpServletRequest()).getInterceptors();
assertThat(interceptors).hasAtLeastOneElementOfType(DeviceResolverHandlerInterceptor.class);
}
use of org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping 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.mvc.method.annotation.RequestMappingHandlerMapping in project spring-framework by spring-projects.
the class WebMvcConfigurationSupportExtensionTests method handlerMappings.
@Test
public void handlerMappings() throws Exception {
RequestMappingHandlerMapping rmHandlerMapping = this.config.requestMappingHandlerMapping();
rmHandlerMapping.setApplicationContext(this.context);
rmHandlerMapping.afterPropertiesSet();
assertEquals(TestPathHelper.class, rmHandlerMapping.getUrlPathHelper().getClass());
assertEquals(TestPathMatcher.class, rmHandlerMapping.getPathMatcher().getClass());
HandlerExecutionChain chain = rmHandlerMapping.getHandler(new MockHttpServletRequest("GET", "/"));
assertNotNull(chain);
assertNotNull(chain.getInterceptors());
assertEquals(3, chain.getInterceptors().length);
assertEquals(LocaleChangeInterceptor.class, chain.getInterceptors()[0].getClass());
assertEquals(ConversionServiceExposingInterceptor.class, chain.getInterceptors()[1].getClass());
assertEquals(ResourceUrlProviderExposingInterceptor.class, chain.getInterceptors()[2].getClass());
AbstractHandlerMapping handlerMapping = (AbstractHandlerMapping) this.config.viewControllerHandlerMapping();
handlerMapping.setApplicationContext(this.context);
assertNotNull(handlerMapping);
assertEquals(1, handlerMapping.getOrder());
assertEquals(TestPathHelper.class, handlerMapping.getUrlPathHelper().getClass());
assertEquals(TestPathMatcher.class, handlerMapping.getPathMatcher().getClass());
chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/path"));
assertNotNull(chain);
assertNotNull(chain.getHandler());
chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/bad"));
assertNotNull(chain);
assertNotNull(chain.getHandler());
chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/old"));
assertNotNull(chain);
assertNotNull(chain.getHandler());
handlerMapping = (AbstractHandlerMapping) this.config.resourceHandlerMapping();
handlerMapping.setApplicationContext(this.context);
assertNotNull(handlerMapping);
assertEquals(Integer.MAX_VALUE - 1, handlerMapping.getOrder());
assertEquals(TestPathHelper.class, handlerMapping.getUrlPathHelper().getClass());
assertEquals(TestPathMatcher.class, handlerMapping.getPathMatcher().getClass());
chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/resources/foo.gif"));
assertNotNull(chain);
assertNotNull(chain.getHandler());
assertEquals(Arrays.toString(chain.getInterceptors()), 2, chain.getInterceptors().length);
// PathExposingHandlerInterceptor at chain.getInterceptors()[0]
assertEquals(ResourceUrlProviderExposingInterceptor.class, chain.getInterceptors()[1].getClass());
handlerMapping = (AbstractHandlerMapping) this.config.defaultServletHandlerMapping();
handlerMapping.setApplicationContext(this.context);
assertNotNull(handlerMapping);
assertEquals(Integer.MAX_VALUE, handlerMapping.getOrder());
chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/anyPath"));
assertNotNull(chain);
assertNotNull(chain.getHandler());
}
Aggregations