Add leading slash for path predicate if not present

This commit adds a leading slash for path predicates in both
WebFlux.fn and WebMvc.fn.

Closes gh-22795
This commit is contained in:
Arjen Poutsma
2019-05-21 12:07:57 +02:00
parent c329bad42a
commit 9b3c92e8d2
5 changed files with 23 additions and 1 deletions

View File

@@ -107,6 +107,9 @@ public abstract class RequestPredicates {
*/
public static RequestPredicate path(String pattern) {
Assert.notNull(pattern, "'pattern' must not be null");
if (!pattern.isEmpty() && !pattern.startsWith("/")) {
pattern = "/" + pattern;
}
return pathPredicates(DEFAULT_PATTERN_PARSER).apply(pattern);
}

View File

@@ -117,6 +117,14 @@ public class RequestPredicatesTests {
assertFalse(predicate.test(request));
}
@Test
public void pathNoLeadingSlash() {
MockHttpServletRequest servletRequest = new MockHttpServletRequest("GET", "/path");
ServerRequest request = new DefaultServerRequest(servletRequest, emptyList());
RequestPredicate predicate = RequestPredicates.path("p*");
assertTrue(predicate.test(request));
}
@Test
public void pathEncoded() {
MockHttpServletRequest servletRequest = new MockHttpServletRequest("GET", "/foo%20bar");