Skip to content

Commit 98b6491

Browse files
committed
Send error signal also for empty server responses
Prior to that commit, the `ResponseSpec` `WebClient` would process error responses (4xx, 5xx HTTP status) and transform those into error signals with a `WebClientResponseException`. But this would only work if the HTTP response would have a non-empty response body. An empty error response would not send an error signal and only translate in an `onComplete` signal. This commit fixes this behavior and makes sure that this error signal is sent in all cases. Issue: SPR-15946
1 parent aa19912 commit 98b6491

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,7 @@ private static Mono<WebClientResponseException> createResponseException(ClientRe
483483
DataBufferUtils.release(dataBuffer);
484484
return bytes;
485485
})
486+
.defaultIfEmpty(new byte[0])
486487
.map(bodyBytes -> {
487488
String msg = String.format("ClientResponse has erroneous status code: %d %s", response.statusCode().value(),
488489
response.statusCode().getReasonPhrase());

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,26 @@ public void shouldGetErrorSignalOn404() throws Exception {
377377
});
378378
}
379379

380+
@Test // SPR-15946
381+
public void shouldGetErrorSignalOnEmptyErrorResponse() throws Exception {
382+
prepareResponse(response -> response.setResponseCode(404)
383+
.setHeader("Content-Type", "text/plain"));
384+
385+
Mono<String> result = this.webClient.get().uri("/greeting")
386+
.retrieve()
387+
.bodyToMono(String.class);
388+
389+
StepVerifier.create(result)
390+
.expectError(WebClientException.class)
391+
.verify(Duration.ofSeconds(3));
392+
393+
expectRequestCount(1);
394+
expectRequest(request -> {
395+
assertEquals("*/*", request.getHeader(HttpHeaders.ACCEPT));
396+
assertEquals("/greeting", request.getPath());
397+
});
398+
}
399+
380400
@Test
381401
public void shouldGetInternalServerErrorSignal() throws Exception {
382402
String errorMessage = "Internal Server error";
@@ -473,9 +493,7 @@ public void shouldApplyExchangeFilter() throws Exception {
473493
.verify(Duration.ofSeconds(3));
474494

475495
expectRequestCount(1);
476-
expectRequest(request -> {
477-
assertEquals("bar", request.getHeader("foo"));
478-
});
496+
expectRequest(request -> assertEquals("bar", request.getHeader("foo")));
479497
}
480498

481499
@Test
@@ -563,7 +581,7 @@ private void expectRequestCount(int count) {
563581
@SuppressWarnings("serial")
564582
private static class MyException extends RuntimeException {
565583

566-
public MyException(String message) {
584+
MyException(String message) {
567585
super(message);
568586
}
569587
}

0 commit comments

Comments
 (0)