提交 9b3c92e8 编写于 作者: A Arjen Poutsma

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
上级 c329bad4
......@@ -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);
}
......
......@@ -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))))
......
......@@ -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");
......
......@@ -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);
}
......
......@@ -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");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册