Search in sources :

Example 1 with DecoderHttpMessageReader

use of cn.taketoday.http.codec.DecoderHttpMessageReader 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 2 with DecoderHttpMessageReader

use of cn.taketoday.http.codec.DecoderHttpMessageReader 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 3 with DecoderHttpMessageReader

use of cn.taketoday.http.codec.DecoderHttpMessageReader 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 4 with DecoderHttpMessageReader

use of cn.taketoday.http.codec.DecoderHttpMessageReader 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)

Example 5 with DecoderHttpMessageReader

use of cn.taketoday.http.codec.DecoderHttpMessageReader in project today-infrastructure by TAKETODAY.

the class DefaultClientResponseTests method bodyToMonoTypeReference.

@Test
public void bodyToMonoTypeReference() {
    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(new TypeReference<String>() {
    });
    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)

Aggregations

DecoderHttpMessageReader (cn.taketoday.http.codec.DecoderHttpMessageReader)30 HttpMessageReader (cn.taketoday.http.codec.HttpMessageReader)30 Test (org.junit.jupiter.api.Test)28 DataBuffer (cn.taketoday.core.io.buffer.DataBuffer)26 DefaultDataBuffer (cn.taketoday.core.io.buffer.DefaultDataBuffer)26 List (java.util.List)16 TypeReference (cn.taketoday.core.TypeReference)6 ByteArrayDecoder (cn.taketoday.core.codec.ByteArrayDecoder)6 ByteBufferDecoder (cn.taketoday.core.codec.ByteBufferDecoder)4 StringDecoder (cn.taketoday.core.codec.StringDecoder)4 MediaType (cn.taketoday.http.MediaType)4 FormHttpMessageReader (cn.taketoday.http.codec.FormHttpMessageReader)4 Jackson2JsonDecoder (cn.taketoday.http.codec.json.Jackson2JsonDecoder)4 WebClientResponseException (cn.taketoday.web.reactive.function.client.WebClientResponseException)4 MultiValueMap (cn.taketoday.core.MultiValueMap)3 Optional (java.util.Optional)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 BDDMockito.given (org.mockito.BDDMockito.given)3 Mockito.mock (org.mockito.Mockito.mock)3