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());
}
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());
}
Aggregations