Search in sources :

Example 1 with DefaultDataBufferFactory

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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) MockAsyncContext(org.springframework.mock.web.test.MockAsyncContext) MockAsyncContext(org.springframework.mock.web.test.MockAsyncContext) AsyncContext(javax.servlet.AsyncContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse)

Example 2 with DefaultDataBufferFactory

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));
}
Also used : DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 3 with DefaultDataBufferFactory

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();
}
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) Flux(reactor.core.publisher.Flux) ReactiveHttpInputMessage(org.springframework.http.ReactiveHttpInputMessage) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 4 with DefaultDataBufferFactory

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();
}
Also used : ReactiveHttpOutputMessage(org.springframework.http.ReactiveHttpOutputMessage) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) Flux(reactor.core.publisher.Flux) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) ByteBuffer(java.nio.ByteBuffer) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 5 with DefaultDataBufferFactory

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

Aggregations

DefaultDataBufferFactory (org.springframework.core.io.buffer.DefaultDataBufferFactory)22 DataBuffer (org.springframework.core.io.buffer.DataBuffer)20 Test (org.junit.Test)19 DefaultDataBuffer (org.springframework.core.io.buffer.DefaultDataBuffer)17 HttpHeaders (org.springframework.http.HttpHeaders)8 DecoderHttpMessageReader (org.springframework.http.codec.DecoderHttpMessageReader)8 HttpMessageReader (org.springframework.http.codec.HttpMessageReader)8 MockServerHttpRequest (org.springframework.mock.http.server.reactive.test.MockServerHttpRequest)8 StringDecoder (org.springframework.core.codec.StringDecoder)7 Flux (reactor.core.publisher.Flux)7 Mono (reactor.core.publisher.Mono)6 ByteBuffer (java.nio.ByteBuffer)5 List (java.util.List)5 ReactiveHttpInputMessage (org.springframework.http.ReactiveHttpInputMessage)5 ReactiveHttpOutputMessage (org.springframework.http.ReactiveHttpOutputMessage)3 MockServerHttpResponse (org.springframework.mock.http.server.reactive.test.MockServerHttpResponse)3 StepVerifier (reactor.test.StepVerifier)3 StandardCharsets (java.nio.charset.StandardCharsets)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2