Skip to content

Commit 9197f15

Browse files
committed
Use ByteArrayDecoder in DefaultClientResponse::createException
This commit changes DefaultClientResponse::createException to use the ByteArrayDecoder, instead of converting to DataBuffers and turning these into a byte array. Closes gh-27666
1 parent 15a6373 commit 9197f15

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponse.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,7 @@ public <T> Mono<ResponseEntity<List<T>>> toEntityList(ParameterizedTypeReference
195195

196196
@Override
197197
public Mono<WebClientResponseException> createException() {
198-
return DataBufferUtils.join(body(BodyExtractors.toDataBuffers()))
199-
.map(dataBuffer -> {
200-
byte[] bytes = new byte[dataBuffer.readableByteCount()];
201-
dataBuffer.read(bytes);
202-
DataBufferUtils.release(dataBuffer);
203-
return bytes;
204-
})
198+
return bodyToMono(byte[].class)
205199
.defaultIfEmpty(EMPTY)
206200
.onErrorReturn(ex -> !(ex instanceof Error), EMPTY)
207201
.map(bodyBytes -> {

spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultClientResponseTests.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import reactor.core.publisher.Mono;
3131

3232
import org.springframework.core.ParameterizedTypeReference;
33+
import org.springframework.core.codec.ByteArrayDecoder;
3334
import org.springframework.core.codec.StringDecoder;
3435
import org.springframework.core.io.buffer.DataBuffer;
3536
import org.springframework.core.io.buffer.DefaultDataBuffer;
@@ -48,6 +49,7 @@
4849

4950
import static org.assertj.core.api.Assertions.assertThat;
5051
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
52+
import static org.assertj.core.api.Assertions.entry;
5153
import static org.mockito.BDDMockito.given;
5254
import static org.mockito.Mockito.mock;
5355
import static org.springframework.web.reactive.function.BodyExtractors.toMono;
@@ -328,6 +330,29 @@ public void toEntityListTypeReference() {
328330
assertThat(result.getHeaders().getContentType()).isEqualTo(MediaType.TEXT_PLAIN);
329331
}
330332

333+
@Test
334+
public void createException() {
335+
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
336+
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
337+
Flux<DataBuffer> body = Flux.just(dataBuffer);
338+
httpHeaders.setContentType(MediaType.TEXT_PLAIN);
339+
given(mockResponse.getStatusCode()).willReturn(HttpStatus.NOT_FOUND);
340+
given(mockResponse.getRawStatusCode()).willReturn(HttpStatus.NOT_FOUND.value());
341+
given(mockResponse.getBody()).willReturn(body);
342+
343+
List<HttpMessageReader<?>> messageReaders = Collections.singletonList(
344+
new DecoderHttpMessageReader<>(new ByteArrayDecoder()));
345+
given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);
346+
347+
Mono<WebClientResponseException> resultMono = defaultClientResponse.createException();
348+
WebClientResponseException exception = resultMono.block();
349+
assertThat(exception.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
350+
assertThat(exception.getMessage()).isEqualTo("404 Not Found");
351+
assertThat(exception.getHeaders()).containsExactly(entry("Content-Type",
352+
Collections.singletonList("text/plain")));
353+
assertThat(exception.getResponseBodyAsByteArray()).isEqualTo(bytes);
354+
}
355+
331356

332357
private void mockTextPlainResponse(Flux<DataBuffer> body) {
333358
httpHeaders.setContentType(MediaType.TEXT_PLAIN);

0 commit comments

Comments
 (0)