Search in sources :

Example 1 with RequestMappingHandlerMapping

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());
}
Also used : HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.Test)

Example 2 with RequestMappingHandlerMapping

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);
}
Also used : SitePreferenceHandlerInterceptor(org.springframework.mobile.device.site.SitePreferenceHandlerInterceptor) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.Test)

Example 3 with RequestMappingHandlerMapping

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);
}
Also used : DeviceResolverHandlerInterceptor(org.springframework.mobile.device.DeviceResolverHandlerInterceptor) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.Test)

Example 4 with RequestMappingHandlerMapping

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());
}
Also used : ResourceHttpRequestHandler(org.springframework.web.servlet.resource.ResourceHttpRequestHandler) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) Test(org.junit.Test)

Example 5 with RequestMappingHandlerMapping

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());
}
Also used : HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) AbstractHandlerMapping(org.springframework.web.servlet.handler.AbstractHandlerMapping) Test(org.junit.Test)

Aggregations

RequestMappingHandlerMapping (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping)25 Test (org.junit.Test)17 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)13 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)11 Bean (org.springframework.context.annotation.Bean)6 ConversionServiceExposingInterceptor (org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor)6 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)5 ContentNegotiationManager (org.springframework.web.accept.ContentNegotiationManager)4 NativeWebRequest (org.springframework.web.context.request.NativeWebRequest)4 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)4 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)4 LocaleChangeInterceptor (org.springframework.web.servlet.i18n.LocaleChangeInterceptor)4 RequestMappingHandlerAdapter (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter)4 ThemeChangeInterceptor (org.springframework.web.servlet.theme.ThemeChangeInterceptor)4 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)3 PathMatcher (org.springframework.util.PathMatcher)3 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)3 HandlerMethod (org.springframework.web.method.HandlerMethod)3 HandlerInterceptor (org.springframework.web.servlet.HandlerInterceptor)3 UrlPathHelper (org.springframework.web.util.UrlPathHelper)3