Search in sources :

Example 6 with HttpMessageReader

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

the class DefaultClientResponseTests method toEntityListWithUnknownStatusCode.

@Test
public void toEntityListWithUnknownStatusCode() {
    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.getHeaders()).willReturn(httpHeaders);
    given(mockResponse.getStatusCode()).willThrow(new IllegalArgumentException("999"));
    given(mockResponse.getRawStatusCode()).willReturn(999);
    given(mockResponse.getBody()).willReturn(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"));
    assertThatIllegalArgumentException().isThrownBy(result::getStatusCode);
    assertThat(result.getStatusCodeValue()).isEqualTo(999);
    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) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) Test(org.junit.jupiter.api.Test)

Example 7 with HttpMessageReader

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

the class DefaultClientResponseTests method createError.

@Test
public void createError() {
    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<String> resultMono = defaultClientResponse.createError();
    StepVerifier.create(resultMono).consumeErrorWith(t -> {
        assertThat(t).isInstanceOf(WebClientResponseException.class);
        WebClientResponseException exception = (WebClientResponseException) t;
        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);
    }).verify();
}
Also used : DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) BeforeEach(org.junit.jupiter.api.BeforeEach) LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) StepVerifier(reactor.test.StepVerifier) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ResponseCookie(cn.taketoday.http.ResponseCookie) ByteBuffer(java.nio.ByteBuffer) OptionalLong(java.util.OptionalLong) HttpHeaders(cn.taketoday.http.HttpHeaders) ClientHttpResponse(cn.taketoday.http.client.reactive.ClientHttpResponse) BDDMockito.given(org.mockito.BDDMockito.given) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBufferFactory(cn.taketoday.core.io.buffer.DefaultDataBufferFactory) MultiValueMap(cn.taketoday.core.MultiValueMap) TypeReference(cn.taketoday.core.TypeReference) StringDecoder(cn.taketoday.core.codec.StringDecoder) ExchangeStrategies(cn.taketoday.web.reactive.function.client.ExchangeStrategies) ClientResponse(cn.taketoday.web.reactive.function.client.ClientResponse) ByteArrayDecoder(cn.taketoday.core.codec.ByteArrayDecoder) Mono(reactor.core.publisher.Mono) ResponseEntity(cn.taketoday.http.ResponseEntity) InetSocketAddress(java.net.InetSocketAddress) Assertions.entry(org.assertj.core.api.Assertions.entry) StandardCharsets(java.nio.charset.StandardCharsets) BodyExtractors.toMono(cn.taketoday.web.reactive.function.BodyExtractors.toMono) Test(org.junit.jupiter.api.Test) HttpRange(cn.taketoday.http.HttpRange) Flux(reactor.core.publisher.Flux) List(java.util.List) DefaultClientResponse(cn.taketoday.web.reactive.function.client.DefaultClientResponse) WebClientResponseException(cn.taketoday.web.reactive.function.client.WebClientResponseException) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) HttpStatus(cn.taketoday.http.HttpStatus) DecoderHttpMessageReader(cn.taketoday.http.codec.DecoderHttpMessageReader) Optional(java.util.Optional) HttpMessageReader(cn.taketoday.http.codec.HttpMessageReader) MediaType(cn.taketoday.http.MediaType) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) 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 8 with HttpMessageReader

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

the class ServerCodecConfigurerTests method maxInMemorySizeWithCustomCodecs.

@Test
public void maxInMemorySizeWithCustomCodecs() {
    int size = 99;
    this.configurer.defaultCodecs().maxInMemorySize(size);
    this.configurer.registerDefaults(false);
    CodecConfigurer.CustomCodecs customCodecs = this.configurer.customCodecs();
    customCodecs.register(new ByteArrayDecoder());
    customCodecs.registerWithDefaultConfig(new ByteArrayDecoder());
    customCodecs.register(new Jackson2JsonDecoder());
    customCodecs.registerWithDefaultConfig(new Jackson2JsonDecoder());
    this.configurer.defaultCodecs().enableLoggingRequestDetails(true);
    List<HttpMessageReader<?>> readers = this.configurer.getReaders();
    assertThat(((ByteArrayDecoder) getNextDecoder(readers)).getMaxInMemorySize()).isEqualTo(256 * 1024);
    assertThat(((ByteArrayDecoder) getNextDecoder(readers)).getMaxInMemorySize()).isEqualTo(size);
    assertThat(((Jackson2JsonDecoder) getNextDecoder(readers)).getMaxInMemorySize()).isEqualTo(256 * 1024);
    assertThat(((Jackson2JsonDecoder) getNextDecoder(readers)).getMaxInMemorySize()).isEqualTo(size);
}
Also used : ResourceHttpMessageReader(cn.taketoday.http.codec.ResourceHttpMessageReader) 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) PartEventHttpMessageReader(cn.taketoday.http.codec.multipart.PartEventHttpMessageReader) Jackson2JsonDecoder(cn.taketoday.http.codec.json.Jackson2JsonDecoder) CodecConfigurer(cn.taketoday.http.codec.CodecConfigurer) ServerCodecConfigurer(cn.taketoday.http.codec.ServerCodecConfigurer) ByteArrayDecoder(cn.taketoday.core.codec.ByteArrayDecoder) Test(org.junit.jupiter.api.Test)

Example 9 with HttpMessageReader

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

the class ServerCodecConfigurerTests method enableRequestLoggingDetailsWithCustomCodecs.

@Test
public void enableRequestLoggingDetailsWithCustomCodecs() {
    this.configurer.registerDefaults(false);
    this.configurer.defaultCodecs().enableLoggingRequestDetails(true);
    CodecConfigurer.CustomCodecs customCodecs = this.configurer.customCodecs();
    customCodecs.register(new FormHttpMessageReader());
    customCodecs.registerWithDefaultConfig(new FormHttpMessageReader());
    List<HttpMessageReader<?>> readers = this.configurer.getReaders();
    assertThat(((FormHttpMessageReader) readers.get(0)).isEnableLoggingRequestDetails()).isFalse();
    assertThat(((FormHttpMessageReader) readers.get(1)).isEnableLoggingRequestDetails()).isTrue();
}
Also used : FormHttpMessageReader(cn.taketoday.http.codec.FormHttpMessageReader) ResourceHttpMessageReader(cn.taketoday.http.codec.ResourceHttpMessageReader) 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) PartEventHttpMessageReader(cn.taketoday.http.codec.multipart.PartEventHttpMessageReader) CodecConfigurer(cn.taketoday.http.codec.CodecConfigurer) ServerCodecConfigurer(cn.taketoday.http.codec.ServerCodecConfigurer) Test(org.junit.jupiter.api.Test)

Example 10 with HttpMessageReader

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

the class ServerCodecConfigurerTests method jackson2EncoderOverride.

@Test
public void jackson2EncoderOverride() {
    Jackson2JsonDecoder decoder = new Jackson2JsonDecoder();
    Jackson2JsonEncoder encoder = new Jackson2JsonEncoder();
    this.configurer.defaultCodecs().jackson2JsonDecoder(decoder);
    this.configurer.defaultCodecs().jackson2JsonEncoder(encoder);
    List<HttpMessageReader<?>> readers = this.configurer.getReaders();
    Jackson2JsonDecoder actualDecoder = findCodec(readers, Jackson2JsonDecoder.class);
    assertThat(actualDecoder).isSameAs(decoder);
    List<HttpMessageWriter<?>> writers = this.configurer.getWriters();
    Jackson2JsonEncoder actualEncoder = findCodec(writers, Jackson2JsonEncoder.class);
    assertThat(actualEncoder).isSameAs(encoder);
    assertThat(findCodec(writers, ServerSentEventHttpMessageWriter.class).getEncoder()).isSameAs(encoder);
}
Also used : ResourceHttpMessageReader(cn.taketoday.http.codec.ResourceHttpMessageReader) 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) PartEventHttpMessageReader(cn.taketoday.http.codec.multipart.PartEventHttpMessageReader) ServerSentEventHttpMessageWriter(cn.taketoday.http.codec.ServerSentEventHttpMessageWriter) PartHttpMessageWriter(cn.taketoday.http.codec.multipart.PartHttpMessageWriter) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) ProtobufHttpMessageWriter(cn.taketoday.http.codec.protobuf.ProtobufHttpMessageWriter) Jackson2JsonDecoder(cn.taketoday.http.codec.json.Jackson2JsonDecoder) ServerSentEventHttpMessageWriter(cn.taketoday.http.codec.ServerSentEventHttpMessageWriter) Jackson2JsonEncoder(cn.taketoday.http.codec.json.Jackson2JsonEncoder) Test(org.junit.jupiter.api.Test)

Aggregations

DecoderHttpMessageReader (cn.taketoday.http.codec.DecoderHttpMessageReader)46 HttpMessageReader (cn.taketoday.http.codec.HttpMessageReader)46 Test (org.junit.jupiter.api.Test)44 DataBuffer (cn.taketoday.core.io.buffer.DataBuffer)28 DefaultDataBuffer (cn.taketoday.core.io.buffer.DefaultDataBuffer)28 FormHttpMessageReader (cn.taketoday.http.codec.FormHttpMessageReader)20 ResourceHttpMessageReader (cn.taketoday.http.codec.ResourceHttpMessageReader)16 Jackson2JsonDecoder (cn.taketoday.http.codec.json.Jackson2JsonDecoder)14 DefaultPartHttpMessageReader (cn.taketoday.http.codec.multipart.DefaultPartHttpMessageReader)14 MultipartHttpMessageReader (cn.taketoday.http.codec.multipart.MultipartHttpMessageReader)14 List (java.util.List)14 ByteArrayDecoder (cn.taketoday.core.codec.ByteArrayDecoder)10 PartEventHttpMessageReader (cn.taketoday.http.codec.multipart.PartEventHttpMessageReader)10 TypeReference (cn.taketoday.core.TypeReference)6 ByteBufferDecoder (cn.taketoday.core.codec.ByteBufferDecoder)6 StringDecoder (cn.taketoday.core.codec.StringDecoder)6 ServerSentEventHttpMessageReader (cn.taketoday.http.codec.ServerSentEventHttpMessageReader)6 EncoderHttpMessageWriter (cn.taketoday.http.codec.EncoderHttpMessageWriter)5 HttpMessageWriter (cn.taketoday.http.codec.HttpMessageWriter)5 ResourceHttpMessageWriter (cn.taketoday.http.codec.ResourceHttpMessageWriter)5