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:
parent
285ba7d391
commit
1d589eb639
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue