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

@@ -111,6 +111,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

@@ -52,7 +52,7 @@ public class NestedRouteIntegrationTests extends AbstractRouterFunctionIntegrati
return nest(path("/foo/"),
route(GET("/bar"), nestedHandler::pattern)
.andRoute(GET("/baz"), nestedHandler::pattern))
.andNest(GET("/{foo}"),
.andNest(GET("{foo}"),
route(GET("/bar"), nestedHandler::variables).and(
nest(GET("/{bar}"),
route(GET("/{baz}"), nestedHandler::variables))))

View File

@@ -109,6 +109,14 @@ public class RequestPredicatesTests {
assertFalse(predicate.test(request));
}
@Test
public void pathNoLeadingSlash() {
URI uri = URI.create("http://localhost/path");
RequestPredicate predicate = RequestPredicates.path("p*");
MockServerRequest request = MockServerRequest.builder().uri(uri).build();
assertTrue(predicate.test(request));
}
@Test
public void pathEncoded() {
URI uri = URI.create("http://localhost/foo%20bar");

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");