use of org.springframework.core.io.buffer.DefaultDataBufferFactory in project spring-framework by spring-projects.
the class ServerHttpRequestTests method createHttpRequest.
private ServerHttpRequest createHttpRequest(String path) throws Exception {
HttpServletRequest request = new MockHttpServletRequest("GET", path) {
@Override
public ServletInputStream getInputStream() {
return new TestServletInputStream();
}
};
AsyncContext asyncContext = new MockAsyncContext(request, new MockHttpServletResponse());
return new ServletServerHttpRequest(request, asyncContext, new DefaultDataBufferFactory(), 1024);
}
use of org.springframework.core.io.buffer.DefaultDataBufferFactory in project spring-framework by spring-projects.
the class WebHttpHandlerBuilderTests method writeToResponse.
private static Mono<Void> writeToResponse(ServerWebExchange exchange, String value) {
byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
DataBuffer buffer = new DefaultDataBufferFactory().wrap(bytes);
return exchange.getResponse().writeWith(Flux.just(buffer));
}
use of org.springframework.core.io.buffer.DefaultDataBufferFactory in project spring-framework by spring-projects.
the class BodyExtractorsTests method toDataBuffers.
@Test
public void toDataBuffers() throws Exception {
BodyExtractor<Flux<DataBuffer>, ReactiveHttpInputMessage> extractor = BodyExtractors.toDataBuffers();
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);
Flux<DataBuffer> result = extractor.extract(request, this.context);
StepVerifier.create(result).expectNext(dataBuffer).expectComplete().verify();
}
use of org.springframework.core.io.buffer.DefaultDataBufferFactory in project spring-framework by spring-projects.
the class BodyInsertersTests method ofPublisher.
@Test
public void ofPublisher() throws Exception {
Flux<String> body = Flux.just("foo");
BodyInserter<Flux<String>, ReactiveHttpOutputMessage> inserter = BodyInserters.fromPublisher(body, String.class);
MockServerHttpResponse response = new MockServerHttpResponse();
Mono<Void> result = inserter.insert(response, this.context);
StepVerifier.create(result).expectComplete().verify();
ByteBuffer byteBuffer = ByteBuffer.wrap("foo".getBytes(UTF_8));
DataBuffer buffer = new DefaultDataBufferFactory().wrap(byteBuffer);
StepVerifier.create(response.getBody()).expectNext(buffer).expectComplete().verify();
}
use of org.springframework.core.io.buffer.DefaultDataBufferFactory 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());
}
Aggregations