Search in sources :

Example 21 with DecoderHttpMessageReader

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

Example 22 with DecoderHttpMessageReader

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

the class DefaultClientResponseTests method bodyToFlux.

@Test
public void bodyToFlux() {
    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);
    Flux<String> resultFlux = defaultClientResponse.bodyToFlux(String.class);
    Mono<List<String>> result = resultFlux.collectList();
    assertThat(result.block()).isEqualTo(Collections.singletonList("foo"));
}
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 23 with DecoderHttpMessageReader

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

the class DefaultClientResponseTests method body.

@Test
public void body() {
    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.body(toMono(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 24 with DecoderHttpMessageReader

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

the class BodyExtractorsTests method createContext.

@BeforeEach
public void createContext() {
    final List<HttpMessageReader<?>> messageReaders = new ArrayList<>();
    messageReaders.add(new DecoderHttpMessageReader<>(new ByteBufferDecoder()));
    messageReaders.add(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes()));
    // messageReaders.add(new DecoderHttpMessageReader<>(new Jaxb2XmlDecoder()));
    messageReaders.add(new DecoderHttpMessageReader<>(new Jackson2JsonDecoder()));
    messageReaders.add(new FormHttpMessageReader());
    DefaultPartHttpMessageReader partReader = new DefaultPartHttpMessageReader();
    messageReaders.add(partReader);
    messageReaders.add(new MultipartHttpMessageReader(partReader));
    messageReaders.add(new FormHttpMessageReader());
    this.context = new BodyExtractor.Context() {

        @Override
        public List<HttpMessageReader<?>> messageReaders() {
            return messageReaders;
        }

        @Override
        public Optional<ServerHttpResponse> serverResponse() {
            return serverResponse;
        }

        @Override
        public Map<String, Object> hints() {
            return hints;
        }
    };
    this.hints = new HashMap<>();
}
Also used : MultipartHttpMessageReader(cn.taketoday.http.codec.multipart.MultipartHttpMessageReader) DefaultPartHttpMessageReader(cn.taketoday.http.codec.multipart.DefaultPartHttpMessageReader) Optional(java.util.Optional) Jackson2JsonDecoder(cn.taketoday.http.codec.json.Jackson2JsonDecoder) ArrayList(java.util.ArrayList) ByteBufferDecoder(cn.taketoday.core.codec.ByteBufferDecoder) DefaultPartHttpMessageReader(cn.taketoday.http.codec.multipart.DefaultPartHttpMessageReader) FormHttpMessageReader(cn.taketoday.http.codec.FormHttpMessageReader) MultipartHttpMessageReader(cn.taketoday.http.codec.multipart.MultipartHttpMessageReader) DecoderHttpMessageReader(cn.taketoday.http.codec.DecoderHttpMessageReader) HttpMessageReader(cn.taketoday.http.codec.HttpMessageReader) FormHttpMessageReader(cn.taketoday.http.codec.FormHttpMessageReader) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) MultiValueMap(cn.taketoday.core.MultiValueMap) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 25 with DecoderHttpMessageReader

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

the class DefaultClientResponseTests method bodyToFluxTypeReference.

@Test
public void bodyToFluxTypeReference() {
    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);
    Flux<String> resultFlux = defaultClientResponse.bodyToFlux(new TypeReference<String>() {
    });
    Mono<List<String>> result = resultFlux.collectList();
    assertThat(result.block()).isEqualTo(Collections.singletonList("foo"));
}
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

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