Search in sources :

Example 1 with DefaultDataBuffer

use of cn.taketoday.core.io.buffer.DefaultDataBuffer in project today-infrastructure by TAKETODAY.

the class BodyInsertersTests method ofDataBuffers.

@Test
public void ofDataBuffers() {
    byte[] bytes = "foo".getBytes(UTF_8);
    DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    BodyInserter<Flux<DataBuffer>, ReactiveHttpOutputMessage> inserter = BodyInserters.fromDataBuffers(body);
    MockServerHttpResponse response = new MockServerHttpResponse();
    Mono<Void> result = inserter.insert(response, this.context);
    StepVerifier.create(result).expectComplete().verify();
    StepVerifier.create(response.getBody()).expectNext(dataBuffer).expectComplete().verify();
}
Also used : DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) ReactiveHttpOutputMessage(cn.taketoday.http.ReactiveHttpOutputMessage) Flux(reactor.core.publisher.Flux) MockServerHttpResponse(cn.taketoday.web.testfixture.http.server.reactive.MockServerHttpResponse) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) Test(org.junit.jupiter.api.Test)

Example 2 with DefaultDataBuffer

use of cn.taketoday.core.io.buffer.DefaultDataBuffer in project today-infrastructure by TAKETODAY.

the class DefaultClientResponseTests method toEntity.

@Test
public void toEntity() {
    byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
    DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    mockTextPlainResponse(body);
    List<HttpMessageReader<?>> messageReaders = Collections.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes()));
    given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);
    ResponseEntity<String> result = defaultClientResponse.toEntity(String.class).block();
    assertThat(result.getBody()).isEqualTo("foo");
    assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
    assertThat(result.getStatusCodeValue()).isEqualTo(HttpStatus.OK.value());
    assertThat(result.getHeaders().getContentType()).isEqualTo(MediaType.TEXT_PLAIN);
}
Also used : DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) DecoderHttpMessageReader(cn.taketoday.http.codec.DecoderHttpMessageReader) HttpMessageReader(cn.taketoday.http.codec.HttpMessageReader) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) Test(org.junit.jupiter.api.Test)

Example 3 with DefaultDataBuffer

use of cn.taketoday.core.io.buffer.DefaultDataBuffer in project today-infrastructure by TAKETODAY.

the class DefaultClientResponseTests method createException.

@Test
public void createException() {
    byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
    DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    httpHeaders.setContentType(MediaType.TEXT_PLAIN);
    given(mockResponse.getStatusCode()).willReturn(HttpStatus.NOT_FOUND);
    given(mockResponse.getRawStatusCode()).willReturn(HttpStatus.NOT_FOUND.value());
    given(mockResponse.getBody()).willReturn(body);
    List<HttpMessageReader<?>> messageReaders = Collections.singletonList(new DecoderHttpMessageReader<>(new ByteArrayDecoder()));
    given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);
    Mono<WebClientResponseException> resultMono = defaultClientResponse.createException();
    WebClientResponseException exception = resultMono.block();
    assertThat(exception.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
    assertThat(exception.getMessage()).isEqualTo("404 Not Found");
    assertThat(exception.getHeaders()).containsExactly(entry("Content-Type", Collections.singletonList("text/plain")));
    assertThat(exception.getResponseBodyAsByteArray()).isEqualTo(bytes);
}
Also used : DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) DecoderHttpMessageReader(cn.taketoday.http.codec.DecoderHttpMessageReader) HttpMessageReader(cn.taketoday.http.codec.HttpMessageReader) WebClientResponseException(cn.taketoday.web.reactive.function.client.WebClientResponseException) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) ByteArrayDecoder(cn.taketoday.core.codec.ByteArrayDecoder) Test(org.junit.jupiter.api.Test)

Example 4 with DefaultDataBuffer

use of cn.taketoday.core.io.buffer.DefaultDataBuffer in project today-infrastructure by TAKETODAY.

the class DefaultClientResponseTests method bodyToMono.

@Test
public void bodyToMono() {
    byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
    DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    mockTextPlainResponse(body);
    List<HttpMessageReader<?>> messageReaders = Collections.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes()));
    given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);
    Mono<String> resultMono = defaultClientResponse.bodyToMono(String.class);
    assertThat(resultMono.block()).isEqualTo("foo");
}
Also used : DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) DecoderHttpMessageReader(cn.taketoday.http.codec.DecoderHttpMessageReader) HttpMessageReader(cn.taketoday.http.codec.HttpMessageReader) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) Test(org.junit.jupiter.api.Test)

Example 5 with DefaultDataBuffer

use of cn.taketoday.core.io.buffer.DefaultDataBuffer in project today-infrastructure by TAKETODAY.

the class DefaultClientResponseTests method toEntityList.

@Test
public void toEntityList() {
    byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
    DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    mockTextPlainResponse(body);
    List<HttpMessageReader<?>> messageReaders = Collections.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes()));
    given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);
    ResponseEntity<List<String>> result = defaultClientResponse.toEntityList(String.class).block();
    assertThat(result.getBody()).isEqualTo(Collections.singletonList("foo"));
    assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
    assertThat(result.getStatusCodeValue()).isEqualTo(HttpStatus.OK.value());
    assertThat(result.getHeaders().getContentType()).isEqualTo(MediaType.TEXT_PLAIN);
}
Also used : DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) DecoderHttpMessageReader(cn.taketoday.http.codec.DecoderHttpMessageReader) HttpMessageReader(cn.taketoday.http.codec.HttpMessageReader) List(java.util.List) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

DataBuffer (cn.taketoday.core.io.buffer.DataBuffer)48 DefaultDataBuffer (cn.taketoday.core.io.buffer.DefaultDataBuffer)48 Test (org.junit.jupiter.api.Test)48 DecoderHttpMessageReader (cn.taketoday.http.codec.DecoderHttpMessageReader)32 HttpMessageReader (cn.taketoday.http.codec.HttpMessageReader)32 NettyDataBuffer (cn.taketoday.core.io.buffer.NettyDataBuffer)20 ReactiveHttpInputMessage (cn.taketoday.http.ReactiveHttpInputMessage)18 MockServerHttpRequest (cn.taketoday.web.testfixture.http.server.reactive.MockServerHttpRequest)18 List (java.util.List)16 Flux (reactor.core.publisher.Flux)14 Mono (reactor.core.publisher.Mono)14 MultiValueMap (cn.taketoday.core.MultiValueMap)10 TypeReference (cn.taketoday.core.TypeReference)10 DefaultDataBufferFactory (cn.taketoday.core.io.buffer.DefaultDataBufferFactory)8 StringDecoder (cn.taketoday.core.codec.StringDecoder)6 HttpStatus (cn.taketoday.http.HttpStatus)6 MediaType (cn.taketoday.http.MediaType)6 FormHttpMessageReader (cn.taketoday.http.codec.FormHttpMessageReader)6 DefaultPartHttpMessageReader (cn.taketoday.http.codec.multipart.DefaultPartHttpMessageReader)6 MultipartHttpMessageReader (cn.taketoday.http.codec.multipart.MultipartHttpMessageReader)6