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);
}
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();
}
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);
}
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();
}
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);
}
Aggregations