Search in sources :

Example 11 with HandlerExecutionChain

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

the class CorsAbstractHandlerMappingTests method preflightRequestWithCorsConfigurationProvider.

@Test
public void preflightRequestWithCorsConfigurationProvider() throws Exception {
    this.request.setMethod(RequestMethod.OPTIONS.name());
    this.request.setRequestURI("/cors");
    this.request.addHeader(HttpHeaders.ORIGIN, "http://domain2.com");
    this.request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
    HandlerExecutionChain chain = handlerMapping.getHandler(this.request);
    assertNotNull(chain);
    assertNotNull(chain.getHandler());
    assertTrue(chain.getHandler().getClass().getSimpleName().equals("PreFlightHandler"));
    CorsConfiguration config = getCorsConfiguration(chain, true);
    assertNotNull(config);
    assertArrayEquals(config.getAllowedOrigins().toArray(), new String[] { "*" });
}
Also used : CorsConfiguration(org.springframework.web.cors.CorsConfiguration) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) Test(org.junit.Test)

Example 12 with HandlerExecutionChain

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

the class CorsAbstractHandlerMappingTests method actualRequestWithMappedCorsConfiguration.

@Test
public void actualRequestWithMappedCorsConfiguration() throws Exception {
    CorsConfiguration config = new CorsConfiguration();
    config.addAllowedOrigin("*");
    this.handlerMapping.setCorsConfigurations(Collections.singletonMap("/foo", config));
    this.request.setMethod(RequestMethod.GET.name());
    this.request.setRequestURI("/foo");
    this.request.addHeader(HttpHeaders.ORIGIN, "http://domain2.com");
    this.request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
    HandlerExecutionChain chain = handlerMapping.getHandler(this.request);
    assertNotNull(chain);
    assertTrue(chain.getHandler() instanceof SimpleHandler);
    config = getCorsConfiguration(chain, false);
    assertNotNull(config);
    assertArrayEquals(config.getAllowedOrigins().toArray(), new String[] { "*" });
}
Also used : CorsConfiguration(org.springframework.web.cors.CorsConfiguration) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) Test(org.junit.Test)

Example 13 with HandlerExecutionChain

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

the class HandlerMappingTests method orderedInterceptors.

@Test
public void orderedInterceptors() throws Exception {
    MappedInterceptor firstMappedInterceptor = new MappedInterceptor(new String[] { "/**" }, Mockito.mock(HandlerInterceptor.class));
    HandlerInterceptor secondHandlerInterceptor = Mockito.mock(HandlerInterceptor.class);
    MappedInterceptor thirdMappedInterceptor = new MappedInterceptor(new String[] { "/**" }, Mockito.mock(HandlerInterceptor.class));
    HandlerInterceptor fourthHandlerInterceptor = Mockito.mock(HandlerInterceptor.class);
    this.handlerMapping.setInterceptors(new Object[] { firstMappedInterceptor, secondHandlerInterceptor, thirdMappedInterceptor, fourthHandlerInterceptor });
    this.handlerMapping.setApplicationContext(this.context);
    HandlerExecutionChain chain = this.handlerMapping.getHandlerExecutionChain(new SimpleHandler(), this.request);
    Assert.assertThat(chain.getInterceptors(), Matchers.arrayContaining(firstMappedInterceptor.getInterceptor(), secondHandlerInterceptor, thirdMappedInterceptor.getInterceptor(), fourthHandlerInterceptor));
}
Also used : HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) Test(org.junit.Test)

Example 14 with HandlerExecutionChain

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

the class PathMatchingUrlHandlerMappingTests method getHandler.

private HandlerExecutionChain getHandler(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 15 with HandlerExecutionChain

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

the class PathMatchingUrlHandlerMappingTests method actualPathMatching.

@Test
public void actualPathMatching() throws Exception {
    // there a couple of mappings defined with which we can test the
    // path matching, let's do that...
    Object bean = wac.getBean("mainController");
    Object defaultBean = wac.getBean("starController");
    // testing some normal behavior
    MockHttpServletRequest req = new MockHttpServletRequest("GET", "/pathmatchingTest.html");
    HandlerExecutionChain hec = getHandler(req);
    assertTrue("Handler is null", hec != null);
    assertTrue("Handler is correct bean", hec.getHandler() == bean);
    assertEquals("/pathmatchingTest.html", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
    // no match, no forward slash included
    req = new MockHttpServletRequest("GET", "welcome.html");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
    assertEquals("welcome.html", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
    // testing some ????? behavior
    req = new MockHttpServletRequest("GET", "/pathmatchingAA.html");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    assertEquals("pathmatchingAA.html", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
    // testing some ????? behavior
    req = new MockHttpServletRequest("GET", "/pathmatchingA.html");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
    assertEquals("/pathmatchingA.html", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
    // testing some ????? behavior
    req = new MockHttpServletRequest("GET", "/administrator/pathmatching.html");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    // testing simple /**/behavior
    req = new MockHttpServletRequest("GET", "/administrator/test/pathmatching.html");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    // this should not match because of the administratorT
    req = new MockHttpServletRequest("GET", "/administratort/pathmatching.html");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
    // this should match because of *.jsp
    req = new MockHttpServletRequest("GET", "/bla.jsp");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    // should match because exact pattern is there
    req = new MockHttpServletRequest("GET", "/administrator/another/bla.xml");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    // should not match, because there's not .gif extension in there
    req = new MockHttpServletRequest("GET", "/administrator/another/bla.gif");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
    // should match because there testlast* in there
    req = new MockHttpServletRequest("GET", "/administrator/test/testlastbit");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    // but this not, because it's testlast and not testla
    req = new MockHttpServletRequest("GET", "/administrator/test/testla");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
    req = new MockHttpServletRequest("GET", "/administrator/testing/longer/bla");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/administrator/testing/longer/test.jsp");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/administrator/testing/longer2/notmatching/notmatching");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
    req = new MockHttpServletRequest("GET", "/shortpattern/testing/toolong");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
    req = new MockHttpServletRequest("GET", "/XXpathXXmatching.html");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/pathXXmatching.html");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/XpathXXmatching.html");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
    req = new MockHttpServletRequest("GET", "/XXpathmatching.html");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
    req = new MockHttpServletRequest("GET", "/show12.html");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/show123.html");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/show1.html");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/reallyGood-test-is-this.jpeg");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/reallyGood-tst-is-this.jpeg");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
    req = new MockHttpServletRequest("GET", "/testing/test.jpeg");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/testing/test.jpg");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
    req = new MockHttpServletRequest("GET", "/anotherTest");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/stillAnotherTest");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
    // there outofpattern*yeah in the pattern, so this should fail
    req = new MockHttpServletRequest("GET", "/outofpattern*ye");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
    req = new MockHttpServletRequest("GET", "/test't est/path'm atching.html");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
    req = new MockHttpServletRequest("GET", "/test%26t%20est/path%26m%20atching.html");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
}
Also used : HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) 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