Search in sources :

Example 1 with DefaultDataBuffer

use of org.springframework.core.io.buffer.DefaultDataBuffer 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 2 with DefaultDataBuffer

use of org.springframework.core.io.buffer.DefaultDataBuffer 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 3 with DefaultDataBuffer

use of org.springframework.core.io.buffer.DefaultDataBuffer 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 4 with DefaultDataBuffer

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

Example 5 with DefaultDataBuffer

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

the class BodyExtractorsTests method toFormData.

@Test
public void toFormData() throws Exception {
    BodyExtractor<Mono<MultiValueMap<String, String>>, ServerHttpRequest> extractor = BodyExtractors.toFormData();
    DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
    DefaultDataBuffer dataBuffer = factory.wrap(ByteBuffer.wrap("name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3".getBytes(StandardCharsets.UTF_8)));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    MockServerHttpRequest request = MockServerHttpRequest.post("/").contentType(MediaType.APPLICATION_FORM_URLENCODED).body(body);
    Mono<MultiValueMap<String, String>> result = extractor.extract(request, this.context);
    StepVerifier.create(result).consumeNextWith(form -> {
        assertEquals("Invalid result", 3, form.size());
        assertEquals("Invalid result", "value 1", form.getFirst("name 1"));
        List<String> values = form.get("name 2");
        assertEquals("Invalid result", 2, values.size());
        assertEquals("Invalid result", "value 2+1", values.get(0));
        assertEquals("Invalid result", "value 2+2", values.get(1));
        assertNull("Invalid result", form.getFirst("name 3"));
    }).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) Mono(reactor.core.publisher.Mono) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) MultiValueMap(org.springframework.util.MultiValueMap) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)15 DataBuffer (org.springframework.core.io.buffer.DataBuffer)15 DefaultDataBuffer (org.springframework.core.io.buffer.DefaultDataBuffer)15 DefaultDataBufferFactory (org.springframework.core.io.buffer.DefaultDataBufferFactory)15 DecoderHttpMessageReader (org.springframework.http.codec.DecoderHttpMessageReader)8 HttpMessageReader (org.springframework.http.codec.HttpMessageReader)8 StringDecoder (org.springframework.core.codec.StringDecoder)7 HttpHeaders (org.springframework.http.HttpHeaders)7 MockServerHttpRequest (org.springframework.mock.http.server.reactive.test.MockServerHttpRequest)7 ReactiveHttpInputMessage (org.springframework.http.ReactiveHttpInputMessage)6 Flux (reactor.core.publisher.Flux)5 Mono (reactor.core.publisher.Mono)4 List (java.util.List)3 Stream (java.util.stream.Stream)2 ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)2 MultiValueMap (org.springframework.util.MultiValueMap)2 JsonView (com.fasterxml.jackson.annotation.JsonView)1 ByteBuffer (java.nio.ByteBuffer)1 StandardCharsets (java.nio.charset.StandardCharsets)1 ArrayList (java.util.ArrayList)1