Browse Source

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
pull/23013/head
Arjen Poutsma 6 years ago
parent
commit
9b3c92e8d2
  1. 3
      spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java
  2. 2
      spring-webflux/src/test/java/org/springframework/web/reactive/function/server/NestedRouteIntegrationTests.java
  3. 8
      spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RequestPredicatesTests.java
  4. 3
      spring-webmvc/src/main/java/org/springframework/web/servlet/function/RequestPredicates.java
  5. 8
      spring-webmvc/src/test/java/org/springframework/web/servlet/function/RequestPredicatesTests.java

3
spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java

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

2
spring-webflux/src/test/java/org/springframework/web/reactive/function/server/NestedRouteIntegrationTests.java

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

8
spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RequestPredicatesTests.java

@ -109,6 +109,14 @@ public class RequestPredicatesTests {
assertFalse(predicate.test(request)); 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 @Test
public void pathEncoded() { public void pathEncoded() {
URI uri = URI.create("http://localhost/foo%20bar"); URI uri = URI.create("http://localhost/foo%20bar");

3
spring-webmvc/src/main/java/org/springframework/web/servlet/function/RequestPredicates.java

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

8
spring-webmvc/src/test/java/org/springframework/web/servlet/function/RequestPredicatesTests.java

@ -117,6 +117,14 @@ public class RequestPredicatesTests {
assertFalse(predicate.test(request)); 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 @Test
public void pathEncoded() { public void pathEncoded() {
MockHttpServletRequest servletRequest = new MockHttpServletRequest("GET", "/foo%20bar"); MockHttpServletRequest servletRequest = new MockHttpServletRequest("GET", "/foo%20bar");

Loading…
Cancel
Save