use of org.springframework.web.reactive.function.server.RequestPredicates.HEAD in project spring-framework by spring-projects.
the class RouterFunctionBuilderTests method route.
@Test
public void route() {
RouterFunction<ServerResponse> route = RouterFunctions.route().GET("/foo", request -> ServerResponse.ok().build()).POST("/", RequestPredicates.contentType(MediaType.TEXT_PLAIN), request -> ServerResponse.noContent().build()).route(HEAD("/foo"), request -> ServerResponse.accepted().build()).build();
MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://example.com/foo").build();
ServerRequest getRequest = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
Mono<Integer> responseMono = route.route(getRequest).flatMap(handlerFunction -> handlerFunction.handle(getRequest)).map(ServerResponse::statusCode).map(HttpStatus::value);
StepVerifier.create(responseMono).expectNext(200).verifyComplete();
mockRequest = MockServerHttpRequest.head("https://example.com/foo").build();
ServerRequest headRequest = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
responseMono = route.route(headRequest).flatMap(handlerFunction -> handlerFunction.handle(headRequest)).map(ServerResponse::statusCode).map(HttpStatus::value);
StepVerifier.create(responseMono).expectNext(202).verifyComplete();
mockRequest = MockServerHttpRequest.post("https://example.com/").contentType(MediaType.TEXT_PLAIN).build();
ServerRequest barRequest = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
responseMono = route.route(barRequest).flatMap(handlerFunction -> handlerFunction.handle(barRequest)).map(ServerResponse::statusCode).map(HttpStatus::value);
StepVerifier.create(responseMono).expectNext(204).verifyComplete();
mockRequest = MockServerHttpRequest.post("https://example.com/").build();
ServerRequest invalidRequest = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
responseMono = route.route(invalidRequest).flatMap(handlerFunction -> handlerFunction.handle(invalidRequest)).map(ServerResponse::statusCode).map(HttpStatus::value);
StepVerifier.create(responseMono).verifyComplete();
}
Aggregations