Search in sources :

Example 31 with Mono

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

the class BodyExtractorsTests method toFormData.

@Test
public void toFormData() throws Exception {
    BodyExtractor<Mono<MultiValueMap<String, String>>, ServerHttpRequest> extractor = BodyExtractors.toFormData();
    DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
    DefaultDataBuffer dataBuffer = factory.wrap(ByteBuffer.wrap("name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3".getBytes(StandardCharsets.UTF_8)));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    MockServerHttpRequest request = MockServerHttpRequest.post("/").contentType(MediaType.APPLICATION_FORM_URLENCODED).body(body);
    Mono<MultiValueMap<String, String>> result = extractor.extract(request, this.context);
    StepVerifier.create(result).consumeNextWith(form -> {
        assertEquals("Invalid result", 3, form.size());
        assertEquals("Invalid result", "value 1", form.getFirst("name 1"));
        List<String> values = form.get("name 2");
        assertEquals("Invalid result", 2, values.size());
        assertEquals("Invalid result", "value 2+1", values.get(0));
        assertEquals("Invalid result", "value 2+2", values.get(1));
        assertNull("Invalid result", form.getFirst("name 3"));
    }).expectComplete().verify();
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) Mono(reactor.core.publisher.Mono) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) MultiValueMap(org.springframework.util.MultiValueMap) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 32 with Mono

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

the class BodyExtractorsTests method toMono.

@Test
public void toMono() throws Exception {
    BodyExtractor<Mono<String>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(String.class);
    DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
    DefaultDataBuffer dataBuffer = factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    MockServerHttpRequest request = MockServerHttpRequest.post("/").body(body);
    Mono<String> result = extractor.extract(request, this.context);
    StepVerifier.create(result).expectNext("foo").expectComplete().verify();
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) Mono(reactor.core.publisher.Mono) ReactiveHttpInputMessage(org.springframework.http.ReactiveHttpInputMessage) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 33 with Mono

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

the class BodyExtractorsTests method toMonoWithHints.

@Test
public void toMonoWithHints() throws Exception {
    BodyExtractor<Mono<User>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(User.class);
    this.hints.put(JSON_VIEW_HINT, SafeToDeserialize.class);
    DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
    DefaultDataBuffer dataBuffer = factory.wrap(ByteBuffer.wrap("{\"username\":\"foo\",\"password\":\"bar\"}".getBytes(StandardCharsets.UTF_8)));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    MockServerHttpRequest request = MockServerHttpRequest.post("/").contentType(MediaType.APPLICATION_JSON).body(body);
    Mono<User> result = extractor.extract(request, this.context);
    StepVerifier.create(result).consumeNextWith(user -> {
        assertEquals("foo", user.getUsername());
        assertNull(user.getPassword());
    }).expectComplete().verify();
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) Mono(reactor.core.publisher.Mono) ReactiveHttpInputMessage(org.springframework.http.ReactiveHttpInputMessage) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 34 with Mono

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

the class DefaultServerResponseBuilderTests method build.

@Test
public void build() throws Exception {
    Mono<ServerResponse> result = ServerResponse.status(HttpStatus.CREATED).header("MyKey", "MyValue").build();
    ServerWebExchange exchange = mock(ServerWebExchange.class);
    MockServerHttpResponse response = new MockServerHttpResponse();
    when(exchange.getResponse()).thenReturn(response);
    HandlerStrategies strategies = mock(HandlerStrategies.class);
    result.then(res -> res.writeTo(exchange, strategies)).block();
    assertEquals(HttpStatus.CREATED, response.getStatusCode());
    assertEquals("MyValue", response.getHeaders().getFirst("MyKey"));
    StepVerifier.create(response.getBody()).expectComplete().verify();
}
Also used : StepVerifier(reactor.test.StepVerifier) HttpHeaders(org.springframework.http.HttpHeaders) ZonedDateTime(java.time.ZonedDateTime) MediaType(org.springframework.http.MediaType) HttpMethod(org.springframework.http.HttpMethod) Set(java.util.Set) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Mockito.when(org.mockito.Mockito.when) CacheControl(org.springframework.http.CacheControl) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) URI(java.net.URI) Collections(java.util.Collections) EnumSet(java.util.EnumSet) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) Test(org.junit.Test)

Example 35 with Mono

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

the class RouterFunctionsTests method toHttpHandlerHandlerReturnResponseStatusExceptionInResponseWriteTo.

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

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

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

        @Override
        public Mono<Void> writeTo(ServerWebExchange exchange, HandlerStrategies strategies) {
            return Mono.error(new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found"));
        }
    });
    RouterFunction<ServerResponse> routerFunction = RouterFunctions.route(RequestPredicates.all(), handlerFunction);
    HttpHandler result = RouterFunctions.toHttpHandler(routerFunction);
    assertNotNull(result);
    MockServerHttpRequest httpRequest = MockServerHttpRequest.get("http://localhost").build();
    MockServerHttpResponse httpResponse = new MockServerHttpResponse();
    result.handle(httpRequest, httpResponse).block();
    assertEquals(HttpStatus.NOT_FOUND, httpResponse.getStatusCode());
}
Also used : HttpStatus(org.springframework.http.HttpStatus) Mockito(org.mockito.Mockito) StepVerifier(reactor.test.StepVerifier) HttpHandler(org.springframework.http.server.reactive.HttpHandler) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) ResponseStatusException(org.springframework.web.server.ResponseStatusException) HttpHeaders(org.springframework.http.HttpHeaders) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Assert(org.junit.Assert) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HttpHeaders(org.springframework.http.HttpHeaders) 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.mock.http.server.reactive.test.MockServerHttpResponse) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) ResponseStatusException(org.springframework.web.server.ResponseStatusException) Test(org.junit.Test)

Aggregations

Mono (reactor.core.publisher.Mono)40 Test (org.junit.Test)28 ServerWebExchange (org.springframework.web.server.ServerWebExchange)21 MockServerHttpRequest (org.springframework.mock.http.server.reactive.test.MockServerHttpRequest)18 HttpStatus (org.springframework.http.HttpStatus)17 StepVerifier (reactor.test.StepVerifier)17 List (java.util.List)16 Assert.assertEquals (org.junit.Assert.assertEquals)14 Collections (java.util.Collections)12 BindingContext (org.springframework.web.reactive.BindingContext)12 Matchers.is (org.hamcrest.Matchers.is)11 Assert.assertThat (org.junit.Assert.assertThat)11 MethodParameter (org.springframework.core.MethodParameter)11 Map (java.util.Map)10 ResolvableType (org.springframework.core.ResolvableType)10 ServerWebInputException (org.springframework.web.server.ServerWebInputException)10 Publisher (org.reactivestreams.Publisher)9 Assert (org.springframework.util.Assert)9 ResponseStatusException (org.springframework.web.server.ResponseStatusException)9 MediaType (org.springframework.http.MediaType)8