Search in sources :

Example 81 with Mono

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

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

Example 83 with Mono

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

the class DefaultServerRequestTests method checkNotModifiedTimestamp.

@ParameterizedHttpMethodTest
void checkNotModifiedTimestamp(String method) throws Exception {
    Instant now = Instant.now().truncatedTo(ChronoUnit.SECONDS);
    HttpHeaders headers = new HttpHeaders();
    headers.setIfModifiedSince(now);
    MockServerHttpRequest mockRequest = MockServerHttpRequest.method(HttpMethod.valueOf(method), "/").headers(headers).build();
    DefaultServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
    Mono<ServerResponse> result = request.checkNotModified(now);
    StepVerifier.create(result).assertNext(serverResponse -> {
        assertThat(serverResponse.statusCode()).isEqualTo(HttpStatus.NOT_MODIFIED);
        assertThat(serverResponse.headers().getLastModified()).isEqualTo(now.toEpochMilli());
    }).verifyComplete();
}
Also used : Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) URISyntaxException(java.net.URISyntaxException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ByteBuffer(java.nio.ByteBuffer) HttpCookie(org.springframework.http.HttpCookie) UnsupportedMediaTypeStatusException(org.springframework.web.server.UnsupportedMediaTypeStatusException) Part(org.springframework.http.codec.multipart.Part) Map(java.util.Map) URI(java.net.URI) Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) BodyExtractors.toMono(org.springframework.web.reactive.function.BodyExtractors.toMono) StringDecoder(org.springframework.core.codec.StringDecoder) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) Instant(java.time.Instant) InetSocketAddress(java.net.InetSocketAddress) StandardCharsets(java.nio.charset.StandardCharsets) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) Test(org.junit.jupiter.api.Test) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) List(java.util.List) Optional(java.util.Optional) FormFieldPart(org.springframework.http.codec.multipart.FormFieldPart) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) ServerWebInputException(org.springframework.web.server.ServerWebInputException) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) Retention(java.lang.annotation.Retention) OptionalLong(java.util.OptionalLong) Charset(java.nio.charset.Charset) ValueSource(org.junit.jupiter.params.provider.ValueSource) HttpMethod(org.springframework.http.HttpMethod) MultiValueMap(org.springframework.util.MultiValueMap) Target(java.lang.annotation.Target) Mono(reactor.core.publisher.Mono) HttpRange(org.springframework.http.HttpRange) ElementType(java.lang.annotation.ElementType) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ChronoUnit(java.time.temporal.ChronoUnit) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Collections(java.util.Collections) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) RetentionPolicy(java.lang.annotation.RetentionPolicy) HttpHeaders(org.springframework.http.HttpHeaders) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) Instant(java.time.Instant)

Example 84 with Mono

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

the class DefaultServerRequestTests method checkNotModifiedETagAndTimestamp.

@ParameterizedHttpMethodTest
void checkNotModifiedETagAndTimestamp(String method) {
    String eTag = "\"Foo\"";
    Instant now = Instant.now().truncatedTo(ChronoUnit.SECONDS);
    HttpHeaders headers = new HttpHeaders();
    headers.setIfNoneMatch(eTag);
    headers.setIfModifiedSince(now);
    MockServerHttpRequest mockRequest = MockServerHttpRequest.method(HttpMethod.valueOf(method), "/").headers(headers).build();
    DefaultServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
    Mono<ServerResponse> result = request.checkNotModified(now, eTag);
    StepVerifier.create(result).assertNext(serverResponse -> {
        assertThat(serverResponse.statusCode()).isEqualTo(HttpStatus.NOT_MODIFIED);
        assertThat(serverResponse.headers().getETag()).isEqualTo(eTag);
        assertThat(serverResponse.headers().getLastModified()).isEqualTo(now.toEpochMilli());
    }).verifyComplete();
}
Also used : Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) URISyntaxException(java.net.URISyntaxException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ByteBuffer(java.nio.ByteBuffer) HttpCookie(org.springframework.http.HttpCookie) UnsupportedMediaTypeStatusException(org.springframework.web.server.UnsupportedMediaTypeStatusException) Part(org.springframework.http.codec.multipart.Part) Map(java.util.Map) URI(java.net.URI) Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) BodyExtractors.toMono(org.springframework.web.reactive.function.BodyExtractors.toMono) StringDecoder(org.springframework.core.codec.StringDecoder) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) Instant(java.time.Instant) InetSocketAddress(java.net.InetSocketAddress) StandardCharsets(java.nio.charset.StandardCharsets) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) Test(org.junit.jupiter.api.Test) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) List(java.util.List) Optional(java.util.Optional) FormFieldPart(org.springframework.http.codec.multipart.FormFieldPart) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) ServerWebInputException(org.springframework.web.server.ServerWebInputException) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) Retention(java.lang.annotation.Retention) OptionalLong(java.util.OptionalLong) Charset(java.nio.charset.Charset) ValueSource(org.junit.jupiter.params.provider.ValueSource) HttpMethod(org.springframework.http.HttpMethod) MultiValueMap(org.springframework.util.MultiValueMap) Target(java.lang.annotation.Target) Mono(reactor.core.publisher.Mono) HttpRange(org.springframework.http.HttpRange) ElementType(java.lang.annotation.ElementType) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ChronoUnit(java.time.temporal.ChronoUnit) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Collections(java.util.Collections) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) RetentionPolicy(java.lang.annotation.RetentionPolicy) HttpHeaders(org.springframework.http.HttpHeaders) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) Instant(java.time.Instant)

Example 85 with Mono

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

the class DefaultServerRequestTests method multipartData.

@Test
public void multipartData() {
    String data = "--12345\r\n" + "Content-Disposition: form-data; name=\"foo\"\r\n" + "\r\n" + "bar\r\n" + "--12345\r\n" + "Content-Disposition: form-data; name=\"baz\"\r\n" + "\r\n" + "qux\r\n" + "--12345--\r\n";
    byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
    DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.set(HttpHeaders.CONTENT_TYPE, "multipart/form-data; boundary=12345");
    MockServerHttpRequest mockRequest = MockServerHttpRequest.method(HttpMethod.GET, "https://example.com").headers(httpHeaders).body(body);
    DefaultServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
    Mono<MultiValueMap<String, Part>> resultData = request.multipartData();
    StepVerifier.create(resultData).consumeNextWith(formData -> {
        assertThat(formData.size()).isEqualTo(2);
        Part part = formData.getFirst("foo");
        boolean condition1 = part instanceof FormFieldPart;
        assertThat(condition1).isTrue();
        FormFieldPart formFieldPart = (FormFieldPart) part;
        assertThat(formFieldPart.value()).isEqualTo("bar");
        part = formData.getFirst("baz");
        boolean condition = part instanceof FormFieldPart;
        assertThat(condition).isTrue();
        formFieldPart = (FormFieldPart) part;
        assertThat(formFieldPart.value()).isEqualTo("qux");
    }).verifyComplete();
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) URISyntaxException(java.net.URISyntaxException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ByteBuffer(java.nio.ByteBuffer) HttpCookie(org.springframework.http.HttpCookie) UnsupportedMediaTypeStatusException(org.springframework.web.server.UnsupportedMediaTypeStatusException) Part(org.springframework.http.codec.multipart.Part) Map(java.util.Map) URI(java.net.URI) Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) BodyExtractors.toMono(org.springframework.web.reactive.function.BodyExtractors.toMono) StringDecoder(org.springframework.core.codec.StringDecoder) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) Instant(java.time.Instant) InetSocketAddress(java.net.InetSocketAddress) StandardCharsets(java.nio.charset.StandardCharsets) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) Test(org.junit.jupiter.api.Test) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) List(java.util.List) Optional(java.util.Optional) FormFieldPart(org.springframework.http.codec.multipart.FormFieldPart) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) ServerWebInputException(org.springframework.web.server.ServerWebInputException) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) Retention(java.lang.annotation.Retention) OptionalLong(java.util.OptionalLong) Charset(java.nio.charset.Charset) ValueSource(org.junit.jupiter.params.provider.ValueSource) HttpMethod(org.springframework.http.HttpMethod) MultiValueMap(org.springframework.util.MultiValueMap) Target(java.lang.annotation.Target) Mono(reactor.core.publisher.Mono) HttpRange(org.springframework.http.HttpRange) ElementType(java.lang.annotation.ElementType) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ChronoUnit(java.time.temporal.ChronoUnit) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Collections(java.util.Collections) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) RetentionPolicy(java.lang.annotation.RetentionPolicy) HttpHeaders(org.springframework.http.HttpHeaders) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) Part(org.springframework.http.codec.multipart.Part) FormFieldPart(org.springframework.http.codec.multipart.FormFieldPart) FormFieldPart(org.springframework.http.codec.multipart.FormFieldPart) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

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