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
This commit is contained in:
@@ -483,6 +483,7 @@ class DefaultWebClient implements WebClient {
|
|||||||
DataBufferUtils.release(dataBuffer);
|
DataBufferUtils.release(dataBuffer);
|
||||||
return bytes;
|
return bytes;
|
||||||
})
|
})
|
||||||
|
.defaultIfEmpty(new byte[0])
|
||||||
.map(bodyBytes -> {
|
.map(bodyBytes -> {
|
||||||
String msg = String.format("ClientResponse has erroneous status code: %d %s", response.statusCode().value(),
|
String msg = String.format("ClientResponse has erroneous status code: %d %s", response.statusCode().value(),
|
||||||
response.statusCode().getReasonPhrase());
|
response.statusCode().getReasonPhrase());
|
||||||
|
|||||||
@@ -377,6 +377,26 @@ public class WebClientIntegrationTests {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // SPR-15946
|
||||||
|
public void shouldGetErrorSignalOnEmptyErrorResponse() throws Exception {
|
||||||
|
prepareResponse(response -> response.setResponseCode(404)
|
||||||
|
.setHeader("Content-Type", "text/plain"));
|
||||||
|
|
||||||
|
Mono<String> result = this.webClient.get().uri("/greeting")
|
||||||
|
.retrieve()
|
||||||
|
.bodyToMono(String.class);
|
||||||
|
|
||||||
|
StepVerifier.create(result)
|
||||||
|
.expectError(WebClientException.class)
|
||||||
|
.verify(Duration.ofSeconds(3));
|
||||||
|
|
||||||
|
expectRequestCount(1);
|
||||||
|
expectRequest(request -> {
|
||||||
|
assertEquals("*/*", request.getHeader(HttpHeaders.ACCEPT));
|
||||||
|
assertEquals("/greeting", request.getPath());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldGetInternalServerErrorSignal() throws Exception {
|
public void shouldGetInternalServerErrorSignal() throws Exception {
|
||||||
String errorMessage = "Internal Server error";
|
String errorMessage = "Internal Server error";
|
||||||
@@ -473,9 +493,7 @@ public class WebClientIntegrationTests {
|
|||||||
.verify(Duration.ofSeconds(3));
|
.verify(Duration.ofSeconds(3));
|
||||||
|
|
||||||
expectRequestCount(1);
|
expectRequestCount(1);
|
||||||
expectRequest(request -> {
|
expectRequest(request -> assertEquals("bar", request.getHeader("foo")));
|
||||||
assertEquals("bar", request.getHeader("foo"));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -563,7 +581,7 @@ public class WebClientIntegrationTests {
|
|||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
private static class MyException extends RuntimeException {
|
private static class MyException extends RuntimeException {
|
||||||
|
|
||||||
public MyException(String message) {
|
MyException(String message) {
|
||||||
super(message);
|
super(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user