/** * Return declared HTTP methods. */ public Set<HttpMethod> getAllowedMethods() { return this.partialMatches.stream(). flatMap(m -> m.getInfo().getMethodsCondition().getMethods().stream()). map(requestMethod -> HttpMethod.resolve(requestMethod.name())). collect(Collectors.toSet()); }
/** * Return declared HTTP methods. */ public Set<String> getAllowedMethods() { Set<String> result = new LinkedHashSet<>(); for (PartialMatch match : this.partialMatches) { for (RequestMethod method : match.getInfo().getMethodsCondition().getMethods()) { result.add(method.name()); } } return result; }
@Nullable private RequestMethodsRequestCondition matchRequestMethod(@Nullable HttpMethod httpMethod) { if (httpMethod != null) { for (RequestMethod method : getMethods()) { if (httpMethod.matches(method.name())) { return new RequestMethodsRequestCondition(method); } } if (httpMethod == HttpMethod.HEAD && getMethods().contains(RequestMethod.GET)) { return GET_CONDITION; } } return null; }
private RequestMappingInfo assertComposedAnnotationMapping(RequestMethod requestMethod) throws Exception { String methodName = requestMethod.name().toLowerCase(); String path = "/" + methodName; return assertComposedAnnotationMapping(methodName, path, requestMethod); }
private RequestMappingInfo assertComposedAnnotationMapping(RequestMethod requestMethod) throws Exception { String methodName = requestMethod.name().toLowerCase(); String path = "/" + methodName; return assertComposedAnnotationMapping(methodName, path, requestMethod); }
@Nullable private RequestMethodsRequestCondition matchRequestMethod(String httpMethodValue) { HttpMethod httpMethod = HttpMethod.resolve(httpMethodValue); if (httpMethod != null) { for (RequestMethod method : getMethods()) { if (httpMethod.matches(method.name())) { return new RequestMethodsRequestCondition(method); } } if (httpMethod == HttpMethod.HEAD && getMethods().contains(RequestMethod.GET)) { return GET_CONDITION; } } return null; }
private void testNoMatch(RequestMethodsRequestCondition condition, RequestMethod method) { MockHttpServletRequest request = new MockHttpServletRequest(method.name(), ""); assertNull(condition.getMatchingCondition(request)); }
private void testNoMatch(RequestMethodsRequestCondition condition, RequestMethod method) throws Exception { ServerWebExchange exchange = getExchange(method.name()); assertNull(condition.getMatchingCondition(exchange)); }
private void testMatch(RequestMethodsRequestCondition condition, RequestMethod method) { MockHttpServletRequest request = new MockHttpServletRequest(method.name(), ""); RequestMethodsRequestCondition actual = condition.getMatchingCondition(request); assertNotNull(actual); assertEquals(Collections.singleton(method), actual.getContent()); }
private void testMatch(RequestMethodsRequestCondition condition, RequestMethod method) throws Exception { ServerWebExchange exchange = getExchange(method.name()); RequestMethodsRequestCondition actual = condition.getMatchingCondition(exchange); assertNotNull(actual); assertEquals(Collections.singleton(method), actual.getContent()); }
@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 getMatchingConditionWithEmptyConditions() throws Exception { RequestMethodsRequestCondition condition = new RequestMethodsRequestCondition(); for (RequestMethod method : RequestMethod.values()) { if (method != OPTIONS) { ServerWebExchange exchange = getExchange(method.name()); assertNotNull(condition.getMatchingCondition(exchange)); } } testNoMatch(condition, OPTIONS); }
@Test public void getMatchingConditionWithEmptyConditions() { RequestMethodsRequestCondition condition = new RequestMethodsRequestCondition(); for (RequestMethod method : RequestMethod.values()) { if (method != OPTIONS) { HttpServletRequest request = new MockHttpServletRequest(method.name(), ""); assertNotNull(condition.getMatchingCondition(request)); } } testNoMatch(condition, OPTIONS); }
@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[]{"*"}); }
@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()); }