@Test public void preflightRequestWithoutCorsConfigurationProvider() throws Exception { 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")); }
@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[]{"*"}); }
@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[]{"*"}); }
@Test public void preflightRequestWithCorsConfigurationSource() throws Exception { this.handlerMapping.setCorsConfigurationSource(new CustomCorsConfigurationSource()); 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")); CorsConfiguration config = getCorsConfiguration(chain, true); assertNotNull(config); assertArrayEquals(new String[]{"*"}, config.getAllowedOrigins().toArray()); assertEquals(true, config.getAllowCredentials()); }
@Test public void actualRequestWithoutCorsConfigurationProvider() throws Exception { 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); }
@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[]{"*"}); }
@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[]{"*"}); }
assertEquals(TestPathHelper.class, handlerMapping.getUrlPathHelper().getClass()); assertEquals(TestPathMatcher.class, handlerMapping.getPathMatcher().getClass()); chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/path")); assertNotNull(chain); assertNotNull(chain.getHandler()); chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/bad")); assertNotNull(chain); assertNotNull(chain.getHandler()); chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/old")); assertNotNull(chain); assertNotNull(chain.getHandler()); assertEquals(TestPathHelper.class, handlerMapping.getUrlPathHelper().getClass()); assertEquals(TestPathMatcher.class, handlerMapping.getPathMatcher().getClass()); chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/resources/foo.gif")); assertNotNull(chain); assertNotNull(chain.getHandler()); assertNotNull(handlerMapping); assertEquals(Integer.MAX_VALUE, handlerMapping.getOrder()); chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/anyPath")); assertNotNull(chain); assertNotNull(chain.getHandler());
@Test public void actualRequestWithCorsConfigurationSource() throws Exception { this.handlerMapping.setCorsConfigurationSource(new CustomCorsConfigurationSource()); 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); CorsConfiguration config = getCorsConfiguration(chain, false); assertNotNull(config); assertArrayEquals(new String[]{"*"}, config.getAllowedOrigins().toArray()); assertEquals(true, config.getAllowCredentials()); }