Use PathPattern.getPathRemaining in WebFlux fn
This commit uses the newly introduced `PathPattern.getPathRemaining(String)` in the functional web framework. With this change, all path predicates can be used for nested router functions, so the `pathPrefix` predicate is no longer required and has been removed. Issue: SPR-15336
This commit is contained in:
@@ -18,6 +18,7 @@ package org.springframework.web.reactive.function.server;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
@@ -25,9 +26,11 @@ import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.web.reactive.function.BodyInserters.*;
|
||||
import static org.springframework.web.reactive.function.server.RequestPredicates.*;
|
||||
import static org.springframework.web.reactive.function.server.RouterFunctions.*;
|
||||
import static org.springframework.web.reactive.function.BodyInserters.fromObject;
|
||||
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
|
||||
import static org.springframework.web.reactive.function.server.RequestPredicates.path;
|
||||
import static org.springframework.web.reactive.function.server.RouterFunctions.nest;
|
||||
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
@@ -40,9 +43,12 @@ public class NestedRouteIntegrationTests extends AbstractRouterFunctionIntegrati
|
||||
@Override
|
||||
protected RouterFunction<?> routerFunction() {
|
||||
NestedHandler nestedHandler = new NestedHandler();
|
||||
return nest(pathPrefix("/foo"),
|
||||
return nest(path("/foo/"),
|
||||
route(GET("/bar"), nestedHandler::bar)
|
||||
.andRoute(GET("/baz"), nestedHandler::baz));
|
||||
.andRoute(GET("/baz"), nestedHandler::baz))
|
||||
.andNest(GET("/{foo}"),
|
||||
nest(GET("/{bar}"),
|
||||
route(GET("/{baz}"), nestedHandler::variables)));
|
||||
}
|
||||
|
||||
|
||||
@@ -56,7 +62,6 @@ public class NestedRouteIntegrationTests extends AbstractRouterFunctionIntegrati
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void baz() throws Exception {
|
||||
ResponseEntity<String> result =
|
||||
restTemplate.getForEntity("http://localhost:" + port + "/foo/baz", String.class);
|
||||
@@ -65,6 +70,16 @@ public class NestedRouteIntegrationTests extends AbstractRouterFunctionIntegrati
|
||||
assertEquals("baz", result.getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore("SPR-15419")
|
||||
public void variables() throws Exception {
|
||||
ResponseEntity<String> result =
|
||||
restTemplate.getForEntity("http://localhost:" + port + "/1/2/3", String.class);
|
||||
|
||||
assertEquals(HttpStatus.OK, result.getStatusCode());
|
||||
assertEquals("1-2-3", result.getBody());
|
||||
}
|
||||
|
||||
|
||||
private static class NestedHandler {
|
||||
|
||||
@@ -75,6 +90,13 @@ public class NestedRouteIntegrationTests extends AbstractRouterFunctionIntegrati
|
||||
public Mono<ServerResponse> baz(ServerRequest request) {
|
||||
return ServerResponse.ok().body(fromObject("baz"));
|
||||
}
|
||||
|
||||
public Mono<ServerResponse> variables(ServerRequest request) {
|
||||
Flux<String> responseBody =
|
||||
Flux.just(request.pathVariable("foo"), "-", request.pathVariable("bar"), "-",
|
||||
request.pathVariable("baz"));
|
||||
return ServerResponse.ok().body(responseBody, String.class);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -163,23 +163,6 @@ public class RequestPredicatesTests {
|
||||
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
|
||||
public void queryParam() throws Exception {
|
||||
MockServerRequest request = MockServerRequest.builder().queryParam("foo", "bar").build();
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.web.reactive.function.server;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
@@ -82,7 +84,7 @@ public class RouterFunctionsTests {
|
||||
|
||||
MockServerRequest request = MockServerRequest.builder().build();
|
||||
RequestPredicate requestPredicate = mock(RequestPredicate.class);
|
||||
when(requestPredicate.test(request)).thenReturn(true);
|
||||
when(requestPredicate.nest(request)).thenReturn(Optional.of(request));
|
||||
|
||||
RouterFunction<ServerResponse> result = RouterFunctions.nest(requestPredicate, routerFunction);
|
||||
assertNotNull(result);
|
||||
@@ -101,7 +103,7 @@ public class RouterFunctionsTests {
|
||||
|
||||
MockServerRequest request = MockServerRequest.builder().build();
|
||||
RequestPredicate requestPredicate = mock(RequestPredicate.class);
|
||||
when(requestPredicate.test(request)).thenReturn(false);
|
||||
when(requestPredicate.nest(request)).thenReturn(Optional.empty());
|
||||
|
||||
RouterFunction<ServerResponse> result = RouterFunctions.nest(requestPredicate, routerFunction);
|
||||
assertNotNull(result);
|
||||
|
||||
Reference in New Issue
Block a user