Search in sources :

Example 1 with HEAD

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();
}
Also used : StepVerifier(reactor.test.StepVerifier) MediaType(org.springframework.http.MediaType) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ClassPathResource(org.springframework.core.io.ClassPathResource) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) HEAD(org.springframework.web.reactive.function.server.RequestPredicates.HEAD) Test(org.junit.jupiter.api.Test) HttpStatus(org.springframework.http.HttpStatus) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Collections(java.util.Collections) Resource(org.springframework.core.io.Resource) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) HttpStatus(org.springframework.http.HttpStatus) Test(org.junit.jupiter.api.Test)

Aggregations

IOException (java.io.IOException)1 Collections (java.util.Collections)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Test (org.junit.jupiter.api.Test)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1 Resource (org.springframework.core.io.Resource)1 HttpStatus (org.springframework.http.HttpStatus)1 MediaType (org.springframework.http.MediaType)1 HEAD (org.springframework.web.reactive.function.server.RequestPredicates.HEAD)1 MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)1 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)1 Mono (reactor.core.publisher.Mono)1 StepVerifier (reactor.test.StepVerifier)1