use of org.springframework.web.cors.CorsConfiguration in project spring-framework by spring-projects.
the class HandlerMethodMappingTests method registerMapping.
@Test
public void registerMapping() throws Exception {
String key1 = "/foo";
String key2 = "/foo*";
this.mapping.registerMapping(key1, this.handler, this.method1);
this.mapping.registerMapping(key2, this.handler, this.method2);
// Direct URL lookup
List directUrlMatches = this.mapping.getMappingRegistry().getMappingsByUrl(key1);
assertNotNull(directUrlMatches);
assertEquals(1, directUrlMatches.size());
assertEquals(key1, directUrlMatches.get(0));
// Mapping name lookup
HandlerMethod handlerMethod1 = new HandlerMethod(this.handler, this.method1);
HandlerMethod handlerMethod2 = new HandlerMethod(this.handler, this.method2);
String name1 = this.method1.getName();
List<HandlerMethod> handlerMethods = this.mapping.getMappingRegistry().getHandlerMethodsByMappingName(name1);
assertNotNull(handlerMethods);
assertEquals(1, handlerMethods.size());
assertEquals(handlerMethod1, handlerMethods.get(0));
String name2 = this.method2.getName();
handlerMethods = this.mapping.getMappingRegistry().getHandlerMethodsByMappingName(name2);
assertNotNull(handlerMethods);
assertEquals(1, handlerMethods.size());
assertEquals(handlerMethod2, handlerMethods.get(0));
// CORS lookup
CorsConfiguration config = this.mapping.getMappingRegistry().getCorsConfiguration(handlerMethod1);
assertNotNull(config);
assertEquals("http://" + handler.hashCode() + name1, config.getAllowedOrigins().get(0));
config = this.mapping.getMappingRegistry().getCorsConfiguration(handlerMethod2);
assertNotNull(config);
assertEquals("http://" + handler.hashCode() + name2, config.getAllowedOrigins().get(0));
}
use of org.springframework.web.cors.CorsConfiguration in project spring-framework by spring-projects.
the class CorsAbstractHandlerMappingTests method preflightRequestWithMappedCorsConfiguration.
@Test
public void preflightRequestWithMappedCorsConfiguration() throws Exception {
CorsConfiguration config = new CorsConfiguration();
config.addAllowedOrigin("*");
this.handlerMapping.setCorsConfigurations(Collections.singletonMap("/foo", config));
this.request.setMethod(RequestMethod.OPTIONS.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);
assertNotNull(chain.getHandler());
assertTrue(chain.getHandler().getClass().getSimpleName().equals("PreFlightHandler"));
config = getCorsConfiguration(chain, true);
assertNotNull(config);
assertArrayEquals(config.getAllowedOrigins().toArray(), new String[] { "*" });
}
use of org.springframework.web.cors.CorsConfiguration in project spring-framework by spring-projects.
the class CorsAbstractHandlerMappingTests method actualRequestWithCorsConfigurationProvider.
@Test
public void actualRequestWithCorsConfigurationProvider() throws Exception {
this.request.setMethod(RequestMethod.GET.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);
assertTrue(chain.getHandler() instanceof CorsAwareHandler);
CorsConfiguration config = getCorsConfiguration(chain, false);
assertNotNull(config);
assertArrayEquals(config.getAllowedOrigins().toArray(), new String[] { "*" });
}
use of org.springframework.web.cors.CorsConfiguration in project spring-framework by spring-projects.
the class CorsAbstractHandlerMappingTests method getCorsConfiguration.
private CorsConfiguration getCorsConfiguration(HandlerExecutionChain chain, boolean isPreFlightRequest) {
if (isPreFlightRequest) {
Object handler = chain.getHandler();
assertTrue(handler.getClass().getSimpleName().equals("PreFlightHandler"));
DirectFieldAccessor accessor = new DirectFieldAccessor(handler);
return (CorsConfiguration) accessor.getPropertyValue("config");
} else {
HandlerInterceptor[] interceptors = chain.getInterceptors();
if (interceptors != null) {
for (HandlerInterceptor interceptor : interceptors) {
if (interceptor.getClass().getSimpleName().equals("CorsInterceptor")) {
DirectFieldAccessor accessor = new DirectFieldAccessor(interceptor);
return (CorsConfiguration) accessor.getPropertyValue("config");
}
}
}
}
return null;
}
use of org.springframework.web.cors.CorsConfiguration in project spring-framework by spring-projects.
the class CrossOriginTests method customOriginDefinedViaValueAttribute.
@Test
public void customOriginDefinedViaValueAttribute() throws Exception {
this.handlerMapping.registerHandler(new MethodLevelController());
this.request.setRequestURI("/customOrigin");
HandlerExecutionChain chain = this.handlerMapping.getHandler(request);
CorsConfiguration config = getCorsConfiguration(chain, false);
assertNotNull(config);
assertEquals(Arrays.asList("http://example.com"), config.getAllowedOrigins());
assertTrue(config.getAllowCredentials());
}
Aggregations