Merge branch '6.0.x'

This commit is contained in:
rstoyanchev
2023-09-07 15:27:56 +01:00
3 changed files with 109 additions and 105 deletions

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2022 the original author or authors. * Copyright 2002-2023 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -50,6 +50,7 @@ import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.MimeType; import org.springframework.util.MimeType;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
import org.springframework.util.ObjectUtils;
import org.springframework.web.reactive.function.BodyExtractor; import org.springframework.web.reactive.function.BodyExtractor;
import org.springframework.web.reactive.function.BodyExtractors; import org.springframework.web.reactive.function.BodyExtractors;
@@ -233,6 +234,9 @@ class DefaultClientResponse implements ClientResponse {
private Function<ResolvableType, ?> initDecodeFunction(byte[] body, @Nullable MediaType contentType) { private Function<ResolvableType, ?> initDecodeFunction(byte[] body, @Nullable MediaType contentType) {
return targetType -> { return targetType -> {
if (ObjectUtils.isEmpty(body)) {
return null;
}
Decoder<?> decoder = null; Decoder<?> decoder = null;
for (HttpMessageReader<?> reader : strategies().messageReaders()) { for (HttpMessageReader<?> reader : strategies().messageReaders()) {
if (reader.canRead(targetType, contentType)) { if (reader.canRead(targetType, contentType)) {

View File

@@ -233,22 +233,21 @@ public class WebClientResponseException extends WebClientException {
*/ */
@Nullable @Nullable
public <E> E getResponseBodyAs(Class<E> targetType) { public <E> E getResponseBodyAs(Class<E> targetType) {
return getResponseBodyAs(ResolvableType.forClass(targetType)); return decodeBody(ResolvableType.forClass(targetType));
} }
/** /**
* Variant of {@link #getResponseBodyAs(Class)} with * Variant of {@link #getResponseBodyAs(Class)} with {@link ParameterizedTypeReference}.
* {@link ParameterizedTypeReference}.
* @since 6.0 * @since 6.0
*/ */
@Nullable @Nullable
public <E> E getResponseBodyAs(ParameterizedTypeReference<E> targetType) { public <E> E getResponseBodyAs(ParameterizedTypeReference<E> targetType) {
return getResponseBodyAs(ResolvableType.forType(targetType.getType())); return decodeBody(ResolvableType.forType(targetType.getType()));
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Nullable @Nullable
private <E> E getResponseBodyAs(ResolvableType targetType) { private <E> E decodeBody(ResolvableType targetType) {
Assert.state(this.bodyDecodeFunction != null, "Decoder function not set"); Assert.state(this.bodyDecodeFunction != null, "Decoder function not set");
return (E) this.bodyDecodeFunction.apply(targetType); return (E) this.bodyDecodeFunction.apply(targetType);
} }

View File

@@ -17,10 +17,10 @@
package org.springframework.web.reactive.function.client; package org.springframework.web.reactive.function.client;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.OptionalLong; import java.util.OptionalLong;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
@@ -33,7 +33,6 @@ import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.codec.ByteArrayDecoder; import org.springframework.core.codec.ByteArrayDecoder;
import org.springframework.core.codec.StringDecoder; import org.springframework.core.codec.StringDecoder;
import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DefaultDataBuffer;
import org.springframework.core.io.buffer.DefaultDataBufferFactory; import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRange; import org.springframework.http.HttpRange;
@@ -44,7 +43,7 @@ import org.springframework.http.ResponseCookie;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.http.client.reactive.ClientHttpResponse; import org.springframework.http.client.reactive.ClientHttpResponse;
import org.springframework.http.codec.DecoderHttpMessageReader; import org.springframework.http.codec.DecoderHttpMessageReader;
import org.springframework.http.codec.HttpMessageReader; import org.springframework.http.codec.json.Jackson2JsonDecoder;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
@@ -60,11 +59,14 @@ import static org.springframework.web.reactive.function.BodyExtractors.toMono;
*/ */
class DefaultClientResponseTests { class DefaultClientResponseTests {
private ClientHttpResponse mockResponse = mock(); private static final ParameterizedTypeReference<String> STRING_TYPE = new ParameterizedTypeReference<>() {};
private final ClientHttpResponse mockResponse = mock();
private final HttpHeaders httpHeaders = new HttpHeaders(); private final HttpHeaders httpHeaders = new HttpHeaders();
private ExchangeStrategies mockExchangeStrategies = mock(); private final ExchangeStrategies mockExchangeStrategies = mock();
private DefaultClientResponse defaultClientResponse; private DefaultClientResponse defaultClientResponse;
@@ -72,7 +74,6 @@ class DefaultClientResponseTests {
@BeforeEach @BeforeEach
void configureMocks() { void configureMocks() {
given(mockResponse.getHeaders()).willReturn(this.httpHeaders); given(mockResponse.getHeaders()).willReturn(this.httpHeaders);
defaultClientResponse = new DefaultClientResponse(mockResponse, mockExchangeStrategies, "", "", () -> null); defaultClientResponse = new DefaultClientResponse(mockResponse, mockExchangeStrategies, "", "", () -> null);
} }
@@ -119,13 +120,11 @@ class DefaultClientResponseTests {
@Test @Test
void body() { void body() {
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8); byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes)); DataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(bytes);
Flux<DataBuffer> body = Flux.just(dataBuffer); mockTextPlainResponse(Flux.just(dataBuffer));
mockTextPlainResponse(body);
List<HttpMessageReader<?>> messageReaders = Collections given(mockExchangeStrategies.messageReaders()).willReturn(
.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())); Collections.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())));
given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);
Mono<String> resultMono = defaultClientResponse.body(toMono(String.class)); Mono<String> resultMono = defaultClientResponse.body(toMono(String.class));
assertThat(resultMono.block()).isEqualTo("foo"); assertThat(resultMono.block()).isEqualTo("foo");
@@ -134,13 +133,11 @@ class DefaultClientResponseTests {
@Test @Test
void bodyToMono() { void bodyToMono() {
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8); byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes)); DataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(bytes);
Flux<DataBuffer> body = Flux.just(dataBuffer); mockTextPlainResponse(Flux.just(dataBuffer));
mockTextPlainResponse(body);
List<HttpMessageReader<?>> messageReaders = Collections given(mockExchangeStrategies.messageReaders()).willReturn(
.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())); Collections.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())));
given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);
Mono<String> resultMono = defaultClientResponse.bodyToMono(String.class); Mono<String> resultMono = defaultClientResponse.bodyToMono(String.class);
assertThat(resultMono.block()).isEqualTo("foo"); assertThat(resultMono.block()).isEqualTo("foo");
@@ -149,30 +146,24 @@ class DefaultClientResponseTests {
@Test @Test
void bodyToMonoTypeReference() { void bodyToMonoTypeReference() {
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8); byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes)); DataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(bytes);
Flux<DataBuffer> body = Flux.just(dataBuffer); mockTextPlainResponse(Flux.just(dataBuffer));
mockTextPlainResponse(body);
List<HttpMessageReader<?>> messageReaders = Collections given(mockExchangeStrategies.messageReaders()).willReturn(
.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())); Collections.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())));
given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);
Mono<String> resultMono = Mono<String> resultMono = defaultClientResponse.bodyToMono(STRING_TYPE);
defaultClientResponse.bodyToMono(new ParameterizedTypeReference<String>() {
});
assertThat(resultMono.block()).isEqualTo("foo"); assertThat(resultMono.block()).isEqualTo("foo");
} }
@Test @Test
void bodyToFlux() { void bodyToFlux() {
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8); byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes)); DataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(bytes);
Flux<DataBuffer> body = Flux.just(dataBuffer); mockTextPlainResponse(Flux.just(dataBuffer));
mockTextPlainResponse(body);
List<HttpMessageReader<?>> messageReaders = Collections given(mockExchangeStrategies.messageReaders()).willReturn(
.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())); Collections.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())));
given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);
Flux<String> resultFlux = defaultClientResponse.bodyToFlux(String.class); Flux<String> resultFlux = defaultClientResponse.bodyToFlux(String.class);
Mono<List<String>> result = resultFlux.collectList(); Mono<List<String>> result = resultFlux.collectList();
@@ -182,17 +173,13 @@ class DefaultClientResponseTests {
@Test @Test
void bodyToFluxTypeReference() { void bodyToFluxTypeReference() {
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8); byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes)); DataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(bytes);
Flux<DataBuffer> body = Flux.just(dataBuffer); mockTextPlainResponse(Flux.just(dataBuffer));
mockTextPlainResponse(body);
List<HttpMessageReader<?>> messageReaders = Collections given(mockExchangeStrategies.messageReaders()).willReturn(
.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())); Collections.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())));
given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);
Flux<String> resultFlux = Flux<String> resultFlux = defaultClientResponse.bodyToFlux(STRING_TYPE);
defaultClientResponse.bodyToFlux(new ParameterizedTypeReference<String>() {
});
Mono<List<String>> result = resultFlux.collectList(); Mono<List<String>> result = resultFlux.collectList();
assertThat(result.block()).isEqualTo(Collections.singletonList("foo")); assertThat(result.block()).isEqualTo(Collections.singletonList("foo"));
} }
@@ -201,13 +188,11 @@ class DefaultClientResponseTests {
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
void toEntity() { void toEntity() {
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8); byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes)); DataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(bytes);
Flux<DataBuffer> body = Flux.just(dataBuffer); mockTextPlainResponse(Flux.just(dataBuffer));
mockTextPlainResponse(body);
List<HttpMessageReader<?>> messageReaders = Collections given(mockExchangeStrategies.messageReaders()).willReturn(
.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())); Collections.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())));
given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);
ResponseEntity<String> result = defaultClientResponse.toEntity(String.class).block(); ResponseEntity<String> result = defaultClientResponse.toEntity(String.class).block();
assertThat(result.getBody()).isEqualTo("foo"); assertThat(result.getBody()).isEqualTo("foo");
@@ -218,19 +203,17 @@ class DefaultClientResponseTests {
@Test @Test
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
void toEntityWithUnknownStatusCode() throws Exception { void toEntityWithUnknownStatusCode() {
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8); byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes)); DataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(bytes);
Flux<DataBuffer> body = Flux.just(dataBuffer);
httpHeaders.setContentType(MediaType.TEXT_PLAIN); httpHeaders.setContentType(MediaType.TEXT_PLAIN);
given(mockResponse.getHeaders()).willReturn(httpHeaders); given(mockResponse.getHeaders()).willReturn(httpHeaders);
given(mockResponse.getStatusCode()).willReturn(HttpStatusCode.valueOf(999)); given(mockResponse.getStatusCode()).willReturn(HttpStatusCode.valueOf(999));
given(mockResponse.getBody()).willReturn(body); given(mockResponse.getBody()).willReturn(Flux.just(dataBuffer));
List<HttpMessageReader<?>> messageReaders = Collections given(mockExchangeStrategies.messageReaders()).willReturn(
.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())); Collections.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())));
given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);
ResponseEntity<String> result = defaultClientResponse.toEntity(String.class).block(); ResponseEntity<String> result = defaultClientResponse.toEntity(String.class).block();
assertThat(result.getBody()).isEqualTo("foo"); assertThat(result.getBody()).isEqualTo("foo");
@@ -243,17 +226,13 @@ class DefaultClientResponseTests {
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
void toEntityTypeReference() { void toEntityTypeReference() {
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8); byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes)); DataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(bytes);
Flux<DataBuffer> body = Flux.just(dataBuffer); mockTextPlainResponse(Flux.just(dataBuffer));
mockTextPlainResponse(body);
List<HttpMessageReader<?>> messageReaders = Collections given(mockExchangeStrategies.messageReaders()).willReturn(
.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())); Collections.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())));
given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);
ResponseEntity<String> result = defaultClientResponse.toEntity( ResponseEntity<String> result = defaultClientResponse.toEntity(STRING_TYPE).block();
new ParameterizedTypeReference<String>() {
}).block();
assertThat(result.getBody()).isEqualTo("foo"); assertThat(result.getBody()).isEqualTo("foo");
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(result.getStatusCodeValue()).isEqualTo(HttpStatus.OK.value()); assertThat(result.getStatusCodeValue()).isEqualTo(HttpStatus.OK.value());
@@ -264,13 +243,11 @@ class DefaultClientResponseTests {
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
void toEntityList() { void toEntityList() {
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8); byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes)); DataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(bytes);
Flux<DataBuffer> body = Flux.just(dataBuffer); mockTextPlainResponse(Flux.just(dataBuffer));
mockTextPlainResponse(body);
List<HttpMessageReader<?>> messageReaders = Collections given(mockExchangeStrategies.messageReaders()).willReturn(
.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())); Collections.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())));
given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);
ResponseEntity<List<String>> result = defaultClientResponse.toEntityList(String.class).block(); ResponseEntity<List<String>> result = defaultClientResponse.toEntityList(String.class).block();
assertThat(result.getBody()).isEqualTo(Collections.singletonList("foo")); assertThat(result.getBody()).isEqualTo(Collections.singletonList("foo"));
@@ -283,17 +260,15 @@ class DefaultClientResponseTests {
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
void toEntityListWithUnknownStatusCode() { void toEntityListWithUnknownStatusCode() {
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8); byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes)); DataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(bytes);
Flux<DataBuffer> body = Flux.just(dataBuffer);
httpHeaders.setContentType(MediaType.TEXT_PLAIN); httpHeaders.setContentType(MediaType.TEXT_PLAIN);
given(mockResponse.getHeaders()).willReturn(httpHeaders); given(mockResponse.getHeaders()).willReturn(httpHeaders);
given(mockResponse.getStatusCode()).willReturn(HttpStatusCode.valueOf(999)); given(mockResponse.getStatusCode()).willReturn(HttpStatusCode.valueOf(999));
given(mockResponse.getBody()).willReturn(body); given(mockResponse.getBody()).willReturn(Flux.just(dataBuffer));
List<HttpMessageReader<?>> messageReaders = Collections given(mockExchangeStrategies.messageReaders()).willReturn(
.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())); Collections.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())));
given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);
ResponseEntity<List<String>> result = defaultClientResponse.toEntityList(String.class).block(); ResponseEntity<List<String>> result = defaultClientResponse.toEntityList(String.class).block();
assertThat(result.getBody()).isEqualTo(Collections.singletonList("foo")); assertThat(result.getBody()).isEqualTo(Collections.singletonList("foo"));
@@ -306,17 +281,14 @@ class DefaultClientResponseTests {
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
void toEntityListTypeReference() { void toEntityListTypeReference() {
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8); byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes)); DataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(bytes);
Flux<DataBuffer> body = Flux.just(dataBuffer);
mockTextPlainResponse(body); mockTextPlainResponse(Flux.just(dataBuffer));
List<HttpMessageReader<?>> messageReaders = Collections given(mockExchangeStrategies.messageReaders()).willReturn(
.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())); Collections.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())));
given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);
ResponseEntity<List<String>> result = defaultClientResponse.toEntityList( ResponseEntity<List<String>> result = defaultClientResponse.toEntityList(STRING_TYPE).block();
new ParameterizedTypeReference<String>() {}).block();
assertThat(result.getBody()).isEqualTo(Collections.singletonList("foo")); assertThat(result.getBody()).isEqualTo(Collections.singletonList("foo"));
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(result.getStatusCodeValue()).isEqualTo(HttpStatus.OK.value()); assertThat(result.getStatusCodeValue()).isEqualTo(HttpStatus.OK.value());
@@ -324,18 +296,16 @@ class DefaultClientResponseTests {
} }
@Test @Test
@SuppressWarnings("deprecation")
void createException() { void createException() {
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8); byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes)); DataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(bytes);
Flux<DataBuffer> body = Flux.just(dataBuffer);
httpHeaders.setContentType(MediaType.TEXT_PLAIN); httpHeaders.setContentType(MediaType.TEXT_PLAIN);
given(mockResponse.getStatusCode()).willReturn(HttpStatus.NOT_FOUND); given(mockResponse.getStatusCode()).willReturn(HttpStatus.NOT_FOUND);
given(mockResponse.getBody()).willReturn(body); given(mockResponse.getBody()).willReturn(Flux.just(dataBuffer));
List<HttpMessageReader<?>> messageReaders = Collections.singletonList( given(mockExchangeStrategies.messageReaders()).willReturn(
new DecoderHttpMessageReader<>(new ByteArrayDecoder())); Collections.singletonList(new DecoderHttpMessageReader<>(new ByteArrayDecoder())));
given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);
Mono<WebClientResponseException> resultMono = defaultClientResponse.createException(); Mono<WebClientResponseException> resultMono = defaultClientResponse.createException();
WebClientResponseException exception = resultMono.block(); WebClientResponseException exception = resultMono.block();
@@ -347,18 +317,50 @@ class DefaultClientResponseTests {
} }
@Test @Test
@SuppressWarnings("deprecation") void createExceptionAndDecodeContent() {
byte[] bytes = "{\"name\":\"Jason\"}".getBytes(StandardCharsets.UTF_8);
DataBuffer buffer = DefaultDataBufferFactory.sharedInstance.wrap(bytes);
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
given(mockResponse.getStatusCode()).willReturn(HttpStatus.NOT_FOUND);
given(mockResponse.getBody()).willReturn(Flux.just(buffer));
given(mockExchangeStrategies.messageReaders()).willReturn(List.of(
new DecoderHttpMessageReader<>(new ByteArrayDecoder()),
new DecoderHttpMessageReader<>(new Jackson2JsonDecoder())));
WebClientResponseException ex = defaultClientResponse.createException().block();
assertThat(ex.getResponseBodyAs(Map.class)).hasSize(1).containsEntry("name", "Jason");
}
@Test
void createExceptionAndDecodeWithoutContent() {
byte[] bytes = new byte[0];
DataBuffer buffer = DefaultDataBufferFactory.sharedInstance.wrap(bytes);
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
given(mockResponse.getStatusCode()).willReturn(HttpStatus.NOT_FOUND);
given(mockResponse.getBody()).willReturn(Flux.just(buffer));
given(mockExchangeStrategies.messageReaders()).willReturn(List.of(
new DecoderHttpMessageReader<>(new ByteArrayDecoder()),
new DecoderHttpMessageReader<>(new Jackson2JsonDecoder())));
WebClientResponseException ex = defaultClientResponse.createException().block();
assertThat(ex.getResponseBodyAs(Map.class)).isNull();
}
@Test
void createError() { void createError() {
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8); byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes)); DataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(bytes);
Flux<DataBuffer> body = Flux.just(dataBuffer);
httpHeaders.setContentType(MediaType.TEXT_PLAIN); httpHeaders.setContentType(MediaType.TEXT_PLAIN);
given(mockResponse.getStatusCode()).willReturn(HttpStatus.NOT_FOUND); given(mockResponse.getStatusCode()).willReturn(HttpStatus.NOT_FOUND);
given(mockResponse.getBody()).willReturn(body); given(mockResponse.getBody()).willReturn(Flux.just(dataBuffer));
List<HttpMessageReader<?>> messageReaders = Collections.singletonList( given(mockExchangeStrategies.messageReaders()).willReturn(
new DecoderHttpMessageReader<>(new ByteArrayDecoder())); Collections.singletonList(new DecoderHttpMessageReader<>(new ByteArrayDecoder())));
given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);
Mono<String> resultMono = defaultClientResponse.createError(); Mono<String> resultMono = defaultClientResponse.createError();
StepVerifier.create(resultMono) StepVerifier.create(resultMono)
@@ -376,7 +378,6 @@ class DefaultClientResponseTests {
} }
@SuppressWarnings("deprecation")
private void mockTextPlainResponse(Flux<DataBuffer> body) { private void mockTextPlainResponse(Flux<DataBuffer> body) {
httpHeaders.setContentType(MediaType.TEXT_PLAIN); httpHeaders.setContentType(MediaType.TEXT_PLAIN);
given(mockResponse.getStatusCode()).willReturn(HttpStatus.OK); given(mockResponse.getStatusCode()).willReturn(HttpStatus.OK);