Search in sources :

Example 16 with HandlerExecutionChain

use of org.springframework.web.servlet.HandlerExecutionChain in project spring-framework by spring-projects.

the class PathMatchingUrlHandlerMappingTests method mappingExposedInRequest.

@Test
public void mappingExposedInRequest() throws Exception {
    Object bean = wac.getBean("mainController");
    MockHttpServletRequest req = new MockHttpServletRequest("GET", "/show.html");
    HandlerExecutionChain hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    assertEquals("Mapping not exposed", "show.html", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
}
Also used : HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

Example 17 with HandlerExecutionChain

use of org.springframework.web.servlet.HandlerExecutionChain in project spring-framework by spring-projects.

the class SimpleUrlHandlerMappingTests method getHandler.

private HandlerExecutionChain getHandler(HandlerMapping hm, MockHttpServletRequest req) throws Exception {
    HandlerExecutionChain hec = hm.getHandler(req);
    HandlerInterceptor[] interceptors = hec.getInterceptors();
    if (interceptors != null) {
        for (HandlerInterceptor interceptor : interceptors) {
            interceptor.preHandle(req, null, hec.getHandler());
        }
    }
    return hec;
}
Also used : HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor)

Example 18 with HandlerExecutionChain

use of org.springframework.web.servlet.HandlerExecutionChain in project spring-framework by spring-projects.

the class CrossOriginTests method classLevel.

@Test
public void classLevel() throws Exception {
    this.handlerMapping.registerHandler(new ClassLevelController());
    this.request.setRequestURI("/foo");
    HandlerExecutionChain chain = this.handlerMapping.getHandler(request);
    CorsConfiguration config = getCorsConfiguration(chain, false);
    assertNotNull(config);
    assertArrayEquals(new String[] { "GET" }, config.getAllowedMethods().toArray());
    assertArrayEquals(new String[] { "*" }, config.getAllowedOrigins().toArray());
    assertFalse(config.getAllowCredentials());
    this.request.setRequestURI("/bar");
    chain = this.handlerMapping.getHandler(request);
    config = getCorsConfiguration(chain, false);
    assertNotNull(config);
    assertArrayEquals(new String[] { "GET" }, config.getAllowedMethods().toArray());
    assertArrayEquals(new String[] { "*" }, config.getAllowedOrigins().toArray());
    assertFalse(config.getAllowCredentials());
    this.request.setRequestURI("/baz");
    chain = this.handlerMapping.getHandler(request);
    config = getCorsConfiguration(chain, false);
    assertNotNull(config);
    assertArrayEquals(new String[] { "GET" }, config.getAllowedMethods().toArray());
    assertArrayEquals(new String[] { "*" }, config.getAllowedOrigins().toArray());
    assertTrue(config.getAllowCredentials());
}
Also used : CorsConfiguration(org.springframework.web.cors.CorsConfiguration) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) Test(org.junit.Test)

Example 19 with HandlerExecutionChain

use of org.springframework.web.servlet.HandlerExecutionChain in project spring-framework by spring-projects.

the class CrossOriginTests method ambiguousHeaderPreFlightRequest.

@Test
public void ambiguousHeaderPreFlightRequest() throws Exception {
    this.handlerMapping.registerHandler(new MethodLevelController());
    this.request.setMethod("OPTIONS");
    this.request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
    this.request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS, "header1");
    this.request.setRequestURI("/ambiguous-header");
    HandlerExecutionChain chain = this.handlerMapping.getHandler(request);
    CorsConfiguration config = getCorsConfiguration(chain, true);
    assertNotNull(config);
    assertArrayEquals(new String[] { "*" }, config.getAllowedMethods().toArray());
    assertArrayEquals(new String[] { "*" }, config.getAllowedOrigins().toArray());
    assertArrayEquals(new String[] { "*" }, config.getAllowedHeaders().toArray());
    assertTrue(config.getAllowCredentials());
    assertTrue(CollectionUtils.isEmpty(config.getExposedHeaders()));
    assertNull(config.getMaxAge());
}
Also used : CorsConfiguration(org.springframework.web.cors.CorsConfiguration) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) Test(org.junit.Test)

Example 20 with HandlerExecutionChain

use of org.springframework.web.servlet.HandlerExecutionChain in project spring-framework by spring-projects.

the class CrossOriginTests method noAnnotationPostWithOrigin.

// SPR-12931
@Test
public void noAnnotationPostWithOrigin() throws Exception {
    this.handlerMapping.registerHandler(new MethodLevelController());
    this.request.setMethod("POST");
    this.request.setRequestURI("/no");
    HandlerExecutionChain chain = this.handlerMapping.getHandler(request);
    assertNull(getCorsConfiguration(chain, false));
}
Also used : HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) Test(org.junit.Test)

Aggregations

HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)61 Test (org.junit.Test)47 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)28 CorsConfiguration (org.springframework.web.cors.CorsConfiguration)16 RequestMappingHandlerMapping (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping)11 HandlerInterceptor (org.springframework.web.servlet.HandlerInterceptor)9 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)8 ConversionServiceExposingInterceptor (org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor)7 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)7 HandlerMethod (org.springframework.web.method.HandlerMethod)6 ModelAndView (org.springframework.web.servlet.ModelAndView)6 LocaleChangeInterceptor (org.springframework.web.servlet.i18n.LocaleChangeInterceptor)5 ThemeChangeInterceptor (org.springframework.web.servlet.theme.ThemeChangeInterceptor)5 Mock (org.jmock.Mock)4 ContentNegotiationManager (org.springframework.web.accept.ContentNegotiationManager)3 NativeWebRequest (org.springframework.web.context.request.NativeWebRequest)3 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)3 HttpRequestHandlerAdapter (org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ApplicationContext (org.springframework.context.ApplicationContext)2