Search in sources :

Example 6 with DefaultDataBufferFactory

use of org.springframework.core.io.buffer.DefaultDataBufferFactory in project spring-framework by spring-projects.

the class DefaultServerRequestTests method body.

@Test
public void body() throws Exception {
    DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
    DefaultDataBuffer dataBuffer = factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.TEXT_PLAIN);
    when(mockRequest.getHeaders()).thenReturn(httpHeaders);
    when(mockRequest.getBody()).thenReturn(body);
    Set<HttpMessageReader<?>> messageReaders = Collections.singleton(new DecoderHttpMessageReader<String>(new StringDecoder()));
    when(mockHandlerStrategies.messageReaders()).thenReturn(messageReaders::stream);
    Mono<String> resultMono = defaultRequest.body(toMono(String.class));
    assertEquals("foo", resultMono.block());
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) HttpHeaders(org.springframework.http.HttpHeaders) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) StringDecoder(org.springframework.core.codec.StringDecoder) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 7 with DefaultDataBufferFactory

use of org.springframework.core.io.buffer.DefaultDataBufferFactory in project spring-framework by spring-projects.

the class DefaultServerRequestTests method bodyUnacceptable.

@Test
public void bodyUnacceptable() throws Exception {
    DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
    DefaultDataBuffer dataBuffer = factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.TEXT_PLAIN);
    when(mockRequest.getHeaders()).thenReturn(httpHeaders);
    when(mockRequest.getBody()).thenReturn(body);
    Set<HttpMessageReader<?>> messageReaders = Collections.emptySet();
    when(mockHandlerStrategies.messageReaders()).thenReturn(messageReaders::stream);
    Flux<String> resultFlux = defaultRequest.bodyToFlux(String.class);
    StepVerifier.create(resultFlux).expectError(UnsupportedMediaTypeStatusException.class).verify();
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) HttpHeaders(org.springframework.http.HttpHeaders) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) UnsupportedMediaTypeStatusException(org.springframework.web.server.UnsupportedMediaTypeStatusException) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 8 with DefaultDataBufferFactory

use of org.springframework.core.io.buffer.DefaultDataBufferFactory in project spring-framework by spring-projects.

the class BodyInsertersTests method ofString.

@Test
public void ofString() throws Exception {
    String body = "foo";
    BodyInserter<String, ReactiveHttpOutputMessage> inserter = BodyInserters.fromObject(body);
    MockServerHttpResponse response = new MockServerHttpResponse();
    Mono<Void> result = inserter.insert(response, this.context);
    StepVerifier.create(result).expectComplete().verify();
    ByteBuffer byteBuffer = ByteBuffer.wrap(body.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) 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 9 with DefaultDataBufferFactory

use of org.springframework.core.io.buffer.DefaultDataBufferFactory 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)

Example 10 with DefaultDataBufferFactory

use of org.springframework.core.io.buffer.DefaultDataBufferFactory in project spring-framework by spring-projects.

the class DefaultClientResponseTests method body.

@Test
public void body() throws Exception {
    DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
    DefaultDataBuffer dataBuffer = factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.TEXT_PLAIN);
    when(mockResponse.getHeaders()).thenReturn(httpHeaders);
    when(mockResponse.getBody()).thenReturn(body);
    Set<HttpMessageReader<?>> messageReaders = Collections.singleton(new DecoderHttpMessageReader<String>(new StringDecoder()));
    when(mockExchangeStrategies.messageReaders()).thenReturn(messageReaders::stream);
    Mono<String> resultMono = defaultClientResponse.body(toMono(String.class));
    assertEquals("foo", resultMono.block());
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) HttpHeaders(org.springframework.http.HttpHeaders) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) StringDecoder(org.springframework.core.codec.StringDecoder) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) 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