Search in sources :

Example 1 with BodyInserter

use of org.springframework.web.reactive.function.BodyInserter in project spring-framework by spring-projects.

the class DefaultEntityResponseBuilderTests method bodyInserter.

@Test
public void bodyInserter() throws Exception {
    String body = "foo";
    Publisher<String> publisher = Mono.just(body);
    BiFunction<ServerHttpResponse, BodyInserter.Context, Mono<Void>> writer = (response, strategies) -> {
        byte[] bodyBytes = body.getBytes(UTF_8);
        ByteBuffer byteBuffer = ByteBuffer.wrap(bodyBytes);
        DataBuffer buffer = new DefaultDataBufferFactory().wrap(byteBuffer);
        return response.writeWith(Mono.just(buffer));
    };
    Mono<EntityResponse<Publisher<String>>> result = EntityResponse.fromPublisher(publisher, String.class).build();
    MockServerWebExchange exchange = MockServerHttpRequest.get("http://localhost").toExchange();
    HandlerStrategies strategies = HandlerStrategies.empty().messageWriter(new EncoderHttpMessageWriter<>(new CharSequenceEncoder())).build();
    StepVerifier.create(result).consumeNextWith(response -> {
        StepVerifier.create(response.entity()).expectNext(body).expectComplete().verify();
        response.writeTo(exchange, strategies);
    }).expectComplete().verify();
    assertNotNull(exchange.getResponse().getBody());
}
Also used : CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) StepVerifier(reactor.test.StepVerifier) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) ZonedDateTime(java.time.ZonedDateTime) BiFunction(java.util.function.BiFunction) CacheControl(org.springframework.http.CacheControl) ByteBuffer(java.nio.ByteBuffer) BodyInserter(org.springframework.web.reactive.function.BodyInserter) Assert.assertSame(org.junit.Assert.assertSame) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) ResolvableType(org.springframework.core.ResolvableType) EnumSet(java.util.EnumSet) HttpHeaders(org.springframework.http.HttpHeaders) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Assert.assertNotNull(org.junit.Assert.assertNotNull) Publisher(org.reactivestreams.Publisher) MediaType(org.springframework.http.MediaType) HttpMethod(org.springframework.http.HttpMethod) Set(java.util.Set) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) Collections(java.util.Collections) Mono(reactor.core.publisher.Mono) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) ByteBuffer(java.nio.ByteBuffer) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 2 with BodyInserter

use of org.springframework.web.reactive.function.BodyInserter in project spring-framework by spring-projects.

the class DefaultClientRequestBuilderTests method bodyInserter.

@Test
public void bodyInserter() throws Exception {
    String body = "foo";
    BodyInserter<String, ClientHttpRequest> inserter = (response, strategies) -> {
        byte[] bodyBytes = body.getBytes(UTF_8);
        ByteBuffer byteBuffer = ByteBuffer.wrap(bodyBytes);
        DataBuffer buffer = new DefaultDataBufferFactory().wrap(byteBuffer);
        return response.writeWith(Mono.just(buffer));
    };
    ClientRequest result = ClientRequest.method(POST, URI.create("http://example.com")).body(inserter).build();
    List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
    messageWriters.add(new EncoderHttpMessageWriter<>(new CharSequenceEncoder()));
    ExchangeStrategies strategies = mock(ExchangeStrategies.class);
    when(strategies.messageWriters()).thenReturn(messageWriters::stream);
    MockClientHttpRequest request = new MockClientHttpRequest(GET, "/");
    result.writeTo(request, strategies).block();
    assertNotNull(request.getBody());
}
Also used : CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) StepVerifier(reactor.test.StepVerifier) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) MockClientHttpRequest(org.springframework.mock.http.client.reactive.test.MockClientHttpRequest) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) ByteBuffer(java.nio.ByteBuffer) BodyInserter(org.springframework.web.reactive.function.BodyInserter) StandardCharsets(java.nio.charset.StandardCharsets) ArrayList(java.util.ArrayList) Mockito(org.mockito.Mockito) List(java.util.List) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) ClientHttpRequest(org.springframework.http.client.reactive.ClientHttpRequest) URI(java.net.URI) Assert(org.junit.Assert) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) ArrayList(java.util.ArrayList) MockClientHttpRequest(org.springframework.mock.http.client.reactive.test.MockClientHttpRequest) MockClientHttpRequest(org.springframework.mock.http.client.reactive.test.MockClientHttpRequest) ClientHttpRequest(org.springframework.http.client.reactive.ClientHttpRequest) ByteBuffer(java.nio.ByteBuffer) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Aggregations

ByteBuffer (java.nio.ByteBuffer)2 List (java.util.List)2 Test (org.junit.Test)2 CharSequenceEncoder (org.springframework.core.codec.CharSequenceEncoder)2 DataBuffer (org.springframework.core.io.buffer.DataBuffer)2 DefaultDataBufferFactory (org.springframework.core.io.buffer.DefaultDataBufferFactory)2 HttpMethod (org.springframework.http.HttpMethod)2 EncoderHttpMessageWriter (org.springframework.http.codec.EncoderHttpMessageWriter)2 BodyInserter (org.springframework.web.reactive.function.BodyInserter)2 Mono (reactor.core.publisher.Mono)2 StepVerifier (reactor.test.StepVerifier)2 URI (java.net.URI)1 StandardCharsets (java.nio.charset.StandardCharsets)1 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)1 ZonedDateTime (java.time.ZonedDateTime)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 EnumSet (java.util.EnumSet)1 Set (java.util.Set)1 BiFunction (java.util.function.BiFunction)1