Add pathPrefix predicate

Added the `pathPrefix` predicate that tests the start of the request
path against a given pattern.

Issue: SPR-14954
This commit is contained in:
Arjen Poutsma 2017-02-16 10:08:56 +01:00
parent 285ba7d391
commit 1d589eb639
2 changed files with 34 additions and 1 deletions

View File

@ -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.

View File

@ -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