From 1d589eb6396531674d36cc80a62c912b5e004cb3 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Thu, 16 Feb 2017 10:08:56 +0100 Subject: [PATCH] Add pathPrefix predicate Added the `pathPrefix` predicate that tests the start of the request path against a given pattern. Issue: SPR-14954 --- .../function/server/RequestPredicates.java | 19 ++++++++++++++++++- .../server/RequestPredicatesTests.java | 16 ++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) 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 4f4c36ac8b..c74d6f22cd 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 @@ -70,7 +70,7 @@ public abstract class RequestPredicates { } /** - * Return a {@code RequestPredicate} that tests against the given path pattern. + * Return a {@code RequestPredicate} that tests the request path against the given path pattern. * * @param pattern the pattern to match to * @return a predicate that tests against the given path pattern @@ -256,6 +256,23 @@ public abstract class RequestPredicates { }; } + /** + * Return a {@code RequestPredicate} that tests the beginning of the request path against the + * given path pattern. This predicate is effectively identical to a + * {@linkplain #path(String) standard path predicate} with path {@code pathPrefixPattern + "/**"}. + * @param pathPrefixPattern the pattern to match against the start of the request path + * @return a predicate that matches if the given predicate matches against the beginning of + * the request's path + */ + public static RequestPredicate pathPrefix(String pathPrefixPattern) { + Assert.notNull(pathPrefixPattern, "'pathPrefixPattern' must not be null"); + + if (!pathPrefixPattern.endsWith("/**")) { + pathPrefixPattern += "/**"; + } + return path(pathPrefixPattern); + } + /** * Return a {@code RequestPredicate} that tests the request's query parameter of the given name * against the given predicate. 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 2b1dec7498..018987bc29 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 @@ -162,7 +162,23 @@ public class RequestPredicatesTests { uri = URI.create("http://localhost/file.foo"); request = MockServerRequest.builder().uri(uri).build(); assertFalse(predicate.test(request)); + } + @Test + public void pathPrefix() throws Exception { + RequestPredicate predicate = RequestPredicates.pathPrefix("/foo"); + + URI uri = URI.create("http://localhost/foo/bar"); + MockServerRequest request = MockServerRequest.builder().uri(uri).build(); + assertTrue(predicate.test(request)); + + uri = URI.create("http://localhost/foo"); + request = MockServerRequest.builder().uri(uri).build(); + assertTrue(predicate.test(request)); + + uri = URI.create("http://localhost/bar"); + request = MockServerRequest.builder().uri(uri).build(); + assertFalse(predicate.test(request)); } @Test