From 490302ebf8933e4c451bb0e347df63b3d8f47db4 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Fri, 6 Jul 2018 11:56:39 +0200 Subject: [PATCH] Added RequestPredicates.methods(HttpMethod...) Added a predicate that tests for multiple HTTP methods. --- .../function/server/RequestPredicates.java | 38 +++++++++++++++---- .../server/RequestPredicatesTests.java | 15 +++++++- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java index 94eed917be..f7d2bde143 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java @@ -22,6 +22,7 @@ import java.security.Principal; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.EnumSet; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; @@ -79,14 +80,26 @@ public abstract class RequestPredicates { /** - * Return a {@code RequestPredicate} that tests against the given HTTP method. - * @param httpMethod the HTTP method to match to + * Return a {@code RequestPredicate} that matches if the request's HTTP method is equal to the + * given method + * @param httpMethod the HTTP method to match against * @return a predicate that tests against the given HTTP method */ public static RequestPredicate method(HttpMethod httpMethod) { return new HttpMethodPredicate(httpMethod); } + /** + * Return a {@code RequestPredicate} that matches if the request's HTTP method is equal to one + * the of the given methods. + * @param httpMethods the HTTP methods to match against + * @return a predicate that tests against the given HTTP methods + * @since 5.1 + */ + public static RequestPredicate methods(HttpMethod... httpMethods) { + return new HttpMethodPredicate(httpMethods); + } + /** * Return a {@code RequestPredicate} that tests the request path against the given path pattern. * @param pattern the pattern to match to @@ -336,23 +349,34 @@ public abstract class RequestPredicates { private static class HttpMethodPredicate implements RequestPredicate { - private final HttpMethod httpMethod; + private final Set httpMethods; public HttpMethodPredicate(HttpMethod httpMethod) { Assert.notNull(httpMethod, "HttpMethod must not be null"); - this.httpMethod = httpMethod; + this.httpMethods = EnumSet.of(httpMethod); + } + + public HttpMethodPredicate(HttpMethod... httpMethods) { + Assert.notEmpty(httpMethods, "HttpMethods must not be empty"); + + this.httpMethods = EnumSet.copyOf(Arrays.asList(httpMethods)); } @Override public boolean test(ServerRequest request) { - boolean match = this.httpMethod == request.method(); - traceMatch("Method", this.httpMethod, request.method(), match); + boolean match = this.httpMethods.contains(request.method()); + traceMatch("Method", this.httpMethods, request.method(), match); return match; } @Override public String toString() { - return this.httpMethod.toString(); + if (this.httpMethods.size() == 1) { + return this.httpMethods.iterator().next().toString(); + } + else { + return this.httpMethods.toString(); + } } } diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RequestPredicatesTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RequestPredicatesTests.java index 57600be6c5..3e727598e0 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RequestPredicatesTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RequestPredicatesTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -53,6 +53,19 @@ public class RequestPredicatesTests { @Test public void methods() { + RequestPredicate predicate = RequestPredicates.methods(HttpMethod.GET, HttpMethod.HEAD); + MockServerRequest request = MockServerRequest.builder().method(HttpMethod.GET).build(); + assertTrue(predicate.test(request)); + + request = MockServerRequest.builder().method(HttpMethod.HEAD).build(); + assertTrue(predicate.test(request)); + + request = MockServerRequest.builder().method(HttpMethod.POST).build(); + assertFalse(predicate.test(request)); + } + + @Test + public void allMethods() { URI uri = URI.create("http://localhost/path"); RequestPredicate predicate = RequestPredicates.GET("/p*");