Search in sources :

Example 21 with HandlerExecutionChain

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

the class CrossOriginTests method ambiguousProducesPreFlightRequest.

@Test
public void ambiguousProducesPreFlightRequest() throws Exception {
    this.handlerMapping.registerHandler(new MethodLevelController());
    this.request.setMethod("OPTIONS");
    this.request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
    this.request.setRequestURI("/ambiguous-produces");
    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 22 with HandlerExecutionChain

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

the class CrossOriginTests method methodLevelComposedAnnotation.

// SPR-13468
@Test
public void methodLevelComposedAnnotation() throws Exception {
    this.handlerMapping.registerHandler(new MethodLevelMappingWithComposedAnnotation());
    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[] { "http://foo.com" }, 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 23 with HandlerExecutionChain

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

the class CrossOriginTests method customOriginDefinedViaPlaceholder.

@Test
public void customOriginDefinedViaPlaceholder() throws Exception {
    this.handlerMapping.registerHandler(new MethodLevelController());
    this.request.setRequestURI("/someOrigin");
    HandlerExecutionChain chain = this.handlerMapping.getHandler(request);
    CorsConfiguration config = getCorsConfiguration(chain, false);
    assertNotNull(config);
    assertEquals(Arrays.asList("http://example.com"), config.getAllowedOrigins());
    assertTrue(config.getAllowCredentials());
}
Also used : CorsConfiguration(org.springframework.web.cors.CorsConfiguration) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) Test(org.junit.Test)

Example 24 with HandlerExecutionChain

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

the class HandlerMethodAnnotationDetectionTests method testRequestMappingMethod.

@Test
public void testRequestMappingMethod() throws Exception {
    String datePattern = "MM:dd:yyyy";
    SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern);
    String dateA = "11:01:2011";
    String dateB = "11:02:2011";
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/path1/path2");
    request.setParameter("datePattern", datePattern);
    request.addHeader("header1", dateA);
    request.addHeader("header2", dateB);
    HandlerExecutionChain chain = handlerMapping.getHandler(request);
    assertNotNull(chain);
    ModelAndView mav = handlerAdapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
    assertEquals("model attr1:", dateFormat.parse(dateA), mav.getModel().get("attr1"));
    assertEquals("model attr2:", dateFormat.parse(dateB), mav.getModel().get("attr2"));
    MockHttpServletResponse response = new MockHttpServletResponse();
    exceptionResolver.resolveException(request, response, chain.getHandler(), new Exception("failure"));
    assertEquals("text/plain;charset=ISO-8859-1", response.getHeader("Content-Type"));
    assertEquals("failure", response.getContentAsString());
}
Also used : HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ModelAndView(org.springframework.web.servlet.ModelAndView) SimpleDateFormat(java.text.SimpleDateFormat) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 25 with HandlerExecutionChain

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

the class RequestMappingInfoHandlerMappingTests method getHandlerMappedInterceptors.

@Test
public void getHandlerMappedInterceptors() throws Exception {
    String path = "/foo";
    HandlerInterceptor interceptor = new HandlerInterceptorAdapter() {
    };
    MappedInterceptor mappedInterceptor = new MappedInterceptor(new String[] { path }, interceptor);
    TestRequestMappingInfoHandlerMapping mapping = new TestRequestMappingInfoHandlerMapping();
    mapping.registerHandler(new TestController());
    mapping.setInterceptors(new Object[] { mappedInterceptor });
    mapping.setApplicationContext(new StaticWebApplicationContext());
    HandlerExecutionChain chain = mapping.getHandler(new MockHttpServletRequest("GET", path));
    assertNotNull(chain);
    assertNotNull(chain.getInterceptors());
    assertSame(interceptor, chain.getInterceptors()[0]);
    chain = mapping.getHandler(new MockHttpServletRequest("GET", "/invalid"));
    assertNull(chain);
}
Also used : MappedInterceptor(org.springframework.web.servlet.handler.MappedInterceptor) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) HandlerInterceptorAdapter(org.springframework.web.servlet.handler.HandlerInterceptorAdapter) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) 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