Search in sources :

Example 1 with PathPatternsParameterizedTest

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

the class CrossOriginTests method classLevel.

@PathPatternsParameterizedTest
void classLevel(TestRequestMappingInfoHandlerMapping mapping) throws Exception {
    mapping.registerHandler(new ClassLevelController());
    this.request.setRequestURI("/foo");
    HandlerExecutionChain chain = mapping.getHandler(request);
    CorsConfiguration config = getCorsConfiguration(chain, false);
    assertThat(config).isNotNull();
    assertThat(config.getAllowedMethods()).containsExactly("GET");
    assertThat(config.getAllowedOrigins()).containsExactly("*");
    assertThat(config.getAllowCredentials()).isFalse();
    this.request.setRequestURI("/bar");
    chain = mapping.getHandler(request);
    config = getCorsConfiguration(chain, false);
    assertThat(config).isNotNull();
    assertThat(config.getAllowedMethods()).containsExactly("GET");
    assertThat(config.getAllowedOrigins()).containsExactly("*");
    assertThat(config.getAllowCredentials()).isFalse();
    this.request.setRequestURI("/baz");
    chain = mapping.getHandler(request);
    config = getCorsConfiguration(chain, false);
    assertThat(config).isNotNull();
    assertThat(config.getAllowedMethods()).containsExactly("GET");
    assertThat(config.getAllowedOrigins()).isNull();
    assertThat(config.getAllowedOriginPatterns()).containsExactly("*");
    assertThat(config.getAllowCredentials()).isTrue();
}
Also used : CorsConfiguration(org.springframework.web.cors.CorsConfiguration) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 2 with PathPatternsParameterizedTest

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

the class CrossOriginTests method preFlightRequest.

@PathPatternsParameterizedTest
void preFlightRequest(TestRequestMappingInfoHandlerMapping mapping) throws Exception {
    mapping.registerHandler(new MethodLevelController());
    this.request.setMethod("OPTIONS");
    this.request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
    this.request.setRequestURI("/default");
    HandlerExecutionChain chain = mapping.getHandler(request);
    CorsConfiguration config = getCorsConfiguration(chain, true);
    assertThat(config).isNotNull();
    assertThat(config.getAllowedMethods()).containsExactly("GET");
    assertThat(config.getAllowedOrigins()).containsExactly("*");
    assertThat(config.getAllowCredentials()).isNull();
    assertThat(config.getAllowedHeaders()).containsExactly("*");
    assertThat(CollectionUtils.isEmpty(config.getExposedHeaders())).isTrue();
    assertThat(config.getMaxAge()).isEqualTo(Long.valueOf(1800));
}
Also used : CorsConfiguration(org.springframework.web.cors.CorsConfiguration) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 3 with PathPatternsParameterizedTest

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

the class CrossOriginTests method ambiguousHeaderPreFlightRequest.

@PathPatternsParameterizedTest
void ambiguousHeaderPreFlightRequest(TestRequestMappingInfoHandlerMapping mapping) throws Exception {
    mapping.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 = mapping.getHandler(request);
    CorsConfiguration config = getCorsConfiguration(chain, true);
    assertThat(config).isNotNull();
    assertThat(config.getAllowedMethods()).containsExactly("*");
    assertThat(config.getAllowedOrigins()).isNull();
    assertThat(config.getAllowedOriginPatterns()).containsExactly("*");
    assertThat(config.getAllowedHeaders()).containsExactly("*");
    assertThat(config.getAllowCredentials()).isTrue();
    assertThat(CollectionUtils.isEmpty(config.getExposedHeaders())).isTrue();
    assertThat(config.getMaxAge()).isNull();
}
Also used : CorsConfiguration(org.springframework.web.cors.CorsConfiguration) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 4 with PathPatternsParameterizedTest

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

the class CrossOriginTests method customOriginDefinedViaValueAttribute.

@PathPatternsParameterizedTest
void customOriginDefinedViaValueAttribute(TestRequestMappingInfoHandlerMapping mapping) throws Exception {
    mapping.registerHandler(new MethodLevelController());
    this.request.setRequestURI("/customOrigin");
    HandlerExecutionChain chain = mapping.getHandler(request);
    CorsConfiguration config = getCorsConfiguration(chain, false);
    assertThat(config).isNotNull();
    assertThat(config.getAllowedOrigins()).isEqualTo(Collections.singletonList("https://example.com"));
    assertThat(config.getAllowCredentials()).isNull();
}
Also used : CorsConfiguration(org.springframework.web.cors.CorsConfiguration) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 5 with PathPatternsParameterizedTest

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

the class CrossOriginTests method preFlightRequestWithoutRequestMethodHeader.

@PathPatternsParameterizedTest
void preFlightRequestWithoutRequestMethodHeader(TestRequestMappingInfoHandlerMapping mapping) throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("OPTIONS", "/default");
    request.addHeader(HttpHeaders.ORIGIN, "https://domain2.com");
    assertThat(mapping.getHandler(request)).isNull();
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Aggregations

PathPatternsParameterizedTest (org.springframework.web.servlet.handler.PathPatternsParameterizedTest)195 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)171 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)142 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)37 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)19 CorsConfiguration (org.springframework.web.cors.CorsConfiguration)13 ModelAndView (org.springframework.web.servlet.ModelAndView)12 HttpSession (jakarta.servlet.http.HttpSession)7 Map (java.util.Map)7 MultiValueMap (org.springframework.util.MultiValueMap)7 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)6 ModelMap (org.springframework.ui.ModelMap)5 HandlerMethod (org.springframework.web.method.HandlerMethod)5 InvocableHandlerMethod (org.springframework.web.method.support.InvocableHandlerMethod)5 ArrayList (java.util.ArrayList)4 MarshallingHttpMessageConverter (org.springframework.http.converter.xml.MarshallingHttpMessageConverter)4 ExtendedModelMap (org.springframework.ui.ExtendedModelMap)4 RequestMappingInfo (org.springframework.web.servlet.mvc.method.RequestMappingInfo)4 MockMultipartFile (org.springframework.web.testfixture.servlet.MockMultipartFile)4 MockMultipartHttpServletRequest (org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest)4