Search in sources :

Example 26 with MockServerHttpRequest

use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest in project spring-framework by spring-projects.

the class RequestPredicatesTests method pathPredicates.

@Test
public void pathPredicates() {
    PathPatternParser parser = new PathPatternParser();
    parser.setCaseSensitive(false);
    Function<String, RequestPredicate> pathPredicates = RequestPredicates.pathPredicates(parser);
    RequestPredicate predicate = pathPredicates.apply("/P*");
    MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://example.com/path").build();
    ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
    assertThat(predicate.test(request)).isTrue();
}
Also used : MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) PathPatternParser(org.springframework.web.util.pattern.PathPatternParser) Test(org.junit.jupiter.api.Test)

Example 27 with MockServerHttpRequest

use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest 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)

Example 28 with MockServerHttpRequest

use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest in project spring-framework by spring-projects.

the class RouterFunctionBuilderTests method filters.

@Test
public void filters() {
    AtomicInteger filterCount = new AtomicInteger();
    RouterFunction<?> route = RouterFunctions.route().GET("/foo", request -> ServerResponse.ok().build()).GET("/bar", request -> Mono.error(new IllegalStateException())).before(request -> {
        int count = filterCount.getAndIncrement();
        assertThat(count).isEqualTo(0);
        return request;
    }).after((request, response) -> {
        int count = filterCount.getAndIncrement();
        assertThat(count).isEqualTo(3);
        return response;
    }).filter((request, next) -> {
        int count = filterCount.getAndIncrement();
        assertThat(count).isEqualTo(1);
        Mono<ServerResponse> responseMono = next.handle(request);
        count = filterCount.getAndIncrement();
        assertThat(count).isEqualTo(2);
        return responseMono;
    }).onError(IllegalStateException.class, (e, request) -> ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR).build()).build();
    MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://localhost/foo").build();
    ServerRequest fooRequest = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
    Mono<ServerResponse> fooResponseMono = route.route(fooRequest).flatMap(handlerFunction -> handlerFunction.handle(fooRequest));
    StepVerifier.create(fooResponseMono).consumeNextWith(serverResponse -> assertThat(filterCount.get()).isEqualTo(4)).verifyComplete();
    filterCount.set(0);
    mockRequest = MockServerHttpRequest.get("https://localhost/bar").build();
    ServerRequest barRequest = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
    Mono<Integer> barResponseMono = route.route(barRequest).flatMap(handlerFunction -> handlerFunction.handle(barRequest)).map(ServerResponse::statusCode).map(HttpStatus::value);
    StepVerifier.create(barResponseMono).expectNext(500).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) HttpStatus(org.springframework.http.HttpStatus) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) Test(org.junit.jupiter.api.Test)

Example 29 with MockServerHttpRequest

use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest in project spring-framework by spring-projects.

the class RouterFunctionsTests method routeNoMatch.

@Test
public void routeNoMatch() {
    HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.ok().build();
    MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://example.com").build();
    ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
    RequestPredicate requestPredicate = mock(RequestPredicate.class);
    given(requestPredicate.test(request)).willReturn(false);
    RouterFunction<ServerResponse> result = RouterFunctions.route(requestPredicate, handlerFunction);
    assertThat(result).isNotNull();
    Mono<HandlerFunction<ServerResponse>> resultHandlerFunction = result.route(request);
    StepVerifier.create(resultHandlerFunction).expectComplete().verify();
}
Also used : StepVerifier(reactor.test.StepVerifier) ResponseStatusException(org.springframework.web.server.ResponseStatusException) HttpHeaders(org.springframework.http.HttpHeaders) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MultiValueMap(org.springframework.util.MultiValueMap) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) HttpHandler(org.springframework.http.server.reactive.HttpHandler) WebFilter(org.springframework.web.server.WebFilter) BDDMockito.given(org.mockito.BDDMockito.given) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Optional(java.util.Optional) Collections(java.util.Collections) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ResponseCookie(org.springframework.http.ResponseCookie) Mockito.mock(org.mockito.Mockito.mock) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) Test(org.junit.jupiter.api.Test)

Example 30 with MockServerHttpRequest

use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest in project spring-framework by spring-projects.

the class RouterFunctionsTests method toHttpHandlerHandlerThrowResponseStatusExceptionInResponseWriteTo.

@Test
public void toHttpHandlerHandlerThrowResponseStatusExceptionInResponseWriteTo() {
    HandlerFunction<ServerResponse> handlerFunction = // Mono.<ServerResponse> is required for compilation in Eclipse
    request -> Mono.just(new ServerResponse() {

        @Override
        public HttpStatus statusCode() {
            return HttpStatus.OK;
        }

        @Override
        public int rawStatusCode() {
            return 200;
        }

        @Override
        public HttpHeaders headers() {
            return new HttpHeaders();
        }

        @Override
        public MultiValueMap<String, ResponseCookie> cookies() {
            return new LinkedMultiValueMap<>();
        }

        @Override
        public Mono<Void> writeTo(ServerWebExchange exchange, Context context) {
            throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found");
        }
    });
    RouterFunction<ServerResponse> routerFunction = RouterFunctions.route(RequestPredicates.all(), handlerFunction);
    HttpHandler result = RouterFunctions.toHttpHandler(routerFunction);
    assertThat(result).isNotNull();
    MockServerHttpRequest httpRequest = MockServerHttpRequest.get("https://localhost").build();
    MockServerHttpResponse httpResponse = new MockServerHttpResponse();
    result.handle(httpRequest, httpResponse).block();
    assertThat(httpResponse.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
}
Also used : StepVerifier(reactor.test.StepVerifier) ResponseStatusException(org.springframework.web.server.ResponseStatusException) HttpHeaders(org.springframework.http.HttpHeaders) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MultiValueMap(org.springframework.util.MultiValueMap) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) HttpHandler(org.springframework.http.server.reactive.HttpHandler) WebFilter(org.springframework.web.server.WebFilter) BDDMockito.given(org.mockito.BDDMockito.given) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Optional(java.util.Optional) Collections(java.util.Collections) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ResponseCookie(org.springframework.http.ResponseCookie) Mockito.mock(org.mockito.Mockito.mock) HttpHeaders(org.springframework.http.HttpHeaders) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HttpHandler(org.springframework.http.server.reactive.HttpHandler) HttpStatus(org.springframework.http.HttpStatus) Mono(reactor.core.publisher.Mono) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) ResponseStatusException(org.springframework.web.server.ResponseStatusException) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Test(org.junit.jupiter.api.Test)

Aggregations

MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)182 Test (org.junit.jupiter.api.Test)160 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)101 Mono (reactor.core.publisher.Mono)52 DataBuffer (org.springframework.core.io.buffer.DataBuffer)51 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)50 Collections (java.util.Collections)48 StepVerifier (reactor.test.StepVerifier)46 HttpHeaders (org.springframework.http.HttpHeaders)39 Flux (reactor.core.publisher.Flux)37 MediaType (org.springframework.http.MediaType)34 ServerWebExchange (org.springframework.web.server.ServerWebExchange)34 MultiValueMap (org.springframework.util.MultiValueMap)33 HttpStatus (org.springframework.http.HttpStatus)31 StandardCharsets (java.nio.charset.StandardCharsets)29 MockServerHttpResponse (org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse)29 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)27 ClassPathResource (org.springframework.core.io.ClassPathResource)25 DefaultDataBuffer (org.springframework.core.io.buffer.DefaultDataBuffer)24 Optional (java.util.Optional)23