Search in sources :

Example 76 with Mono

use of reactor.core.publisher.Mono in project spring-framework by spring-projects.

the class ServerHttpResponseTests method monoResponseShouldNotLeakIfCancelled.

// gh-26232
@Test
void monoResponseShouldNotLeakIfCancelled() {
    LeakAwareDataBufferFactory bufferFactory = new LeakAwareDataBufferFactory();
    MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
    MockServerHttpResponse response = new MockServerHttpResponse(bufferFactory);
    response.setWriteHandler(flux -> {
        throw AbortedException.beforeSend();
    });
    HttpMessageWriter<Object> messageWriter = new EncoderHttpMessageWriter<>(new Jackson2JsonEncoder());
    Mono<Void> result = messageWriter.write(Mono.just(Collections.singletonMap("foo", "bar")), ResolvableType.forClass(Mono.class), ResolvableType.forClass(Map.class), null, request, response, Collections.emptyMap());
    StepVerifier.create(result).expectError(AbortedException.class).verify();
    bufferFactory.checkForLeaks();
}
Also used : LeakAwareDataBufferFactory(org.springframework.core.testfixture.io.buffer.LeakAwareDataBufferFactory) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) AbortedException(reactor.netty.channel.AbortedException) Mono(reactor.core.publisher.Mono) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Map(java.util.Map) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) Test(org.junit.jupiter.api.Test)

Example 77 with Mono

use of reactor.core.publisher.Mono in project spring-framework by spring-projects.

the class RouterFunctionTests method andRoute.

@Test
public void andRoute() {
    RouterFunction<ServerResponse> routerFunction1 = request -> Mono.empty();
    RequestPredicate requestPredicate = request -> true;
    RouterFunction<ServerResponse> result = routerFunction1.andRoute(requestPredicate, this::handlerMethod);
    assertThat(result).isNotNull();
    MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://example.com").build();
    ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
    Mono<? extends HandlerFunction<?>> resultHandlerFunction = result.route(request);
    StepVerifier.create(resultHandlerFunction).expectNextCount(1).expectComplete().verify();
}
Also used : Test(org.junit.jupiter.api.Test) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) StepVerifier(reactor.test.StepVerifier) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) GET(org.springframework.web.reactive.function.server.RequestPredicates.GET) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Mono(reactor.core.publisher.Mono) Collections(java.util.Collections) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) Test(org.junit.jupiter.api.Test)

Example 78 with Mono

use of reactor.core.publisher.Mono in project spring-framework by spring-projects.

the class RouterFunctionTests method andOther.

@Test
public void andOther() {
    HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.ok().bodyValue("42");
    RouterFunction<?> routerFunction1 = request -> Mono.empty();
    RouterFunction<ServerResponse> routerFunction2 = request -> Mono.just(handlerFunction);
    RouterFunction<?> result = routerFunction1.andOther(routerFunction2);
    assertThat(result).isNotNull();
    MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://example.com").build();
    ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
    Mono<? extends HandlerFunction<?>> resultHandlerFunction = result.route(request);
    StepVerifier.create(resultHandlerFunction).expectNextMatches(o -> o.equals(handlerFunction)).expectComplete().verify();
}
Also used : Test(org.junit.jupiter.api.Test) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) StepVerifier(reactor.test.StepVerifier) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) GET(org.springframework.web.reactive.function.server.RequestPredicates.GET) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Mono(reactor.core.publisher.Mono) Collections(java.util.Collections) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) Test(org.junit.jupiter.api.Test)

Example 79 with Mono

use of reactor.core.publisher.Mono 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 80 with Mono

use of reactor.core.publisher.Mono 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)

Aggregations

Mono (reactor.core.publisher.Mono)308 Test (org.junit.jupiter.api.Test)143 StepVerifier (reactor.test.StepVerifier)117 List (java.util.List)116 Flux (reactor.core.publisher.Flux)110 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)102 Collections (java.util.Collections)85 Map (java.util.Map)75 HttpStatus (org.springframework.http.HttpStatus)66 ServerWebExchange (org.springframework.web.server.ServerWebExchange)65 Duration (java.time.Duration)60 ArrayList (java.util.ArrayList)58 Test (org.junit.Test)53 MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)52 DataBuffer (org.springframework.core.io.buffer.DataBuffer)51 Assert (org.springframework.util.Assert)49 Mockito.mock (org.mockito.Mockito.mock)48 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)46 Optional (java.util.Optional)44 StandardCharsets (java.nio.charset.StandardCharsets)43