From f16122d53385759157998928a6c7d2d35ccc470e Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Tue, 31 Oct 2023 15:28:27 +0000 Subject: [PATCH] Polishing and minor refactoring Closes gh-31202 --- .../springframework/http/HttpStatusCode.java | 8 ------ .../function/client/DefaultWebClient.java | 12 +++----- .../client/WebClientIntegrationTests.java | 28 +++++++++---------- 3 files changed, 18 insertions(+), 30 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/HttpStatusCode.java b/spring-web/src/main/java/org/springframework/http/HttpStatusCode.java index 30acaefb9c1..3275eb8d966 100644 --- a/spring-web/src/main/java/org/springframework/http/HttpStatusCode.java +++ b/spring-web/src/main/java/org/springframework/http/HttpStatusCode.java @@ -88,14 +88,6 @@ public sealed interface HttpStatusCode extends Serializable permits DefaultHttpS return value() == other.value(); } - /** - * Checks whether this status code is a well-known HTTP status code or not - * @return {@code true} if the status code corresponds to a standard HTTP status code, {@code false} otherwise - */ - default boolean isWellKnown() { - return HttpStatus.resolve(this.value()) != null; - } - /** * Return an {@code HttpStatusCode} object for the given integer value. * @param code the status code as integer diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java index c3d54fe22d7..44192837d28 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java @@ -515,12 +515,9 @@ final class DefaultWebClient implements WebClient { private static class DefaultResponseSpec implements ResponseSpec { - private static final Predicate STATUS_CODE_ERROR = HttpStatusCode::isError; - private static final Predicate STATUS_CODE_UNKNOWN = status -> !status.isWellKnown(); - private static final StatusHandler DEFAULT_ERROR_STATUS_HANDLER = - new StatusHandler(STATUS_CODE_ERROR, ClientResponse::createException); - private static final StatusHandler DEFAULT_UNKNOWN_STATUS_HANDLER = - new StatusHandler(STATUS_CODE_UNKNOWN, ClientResponse::createException); + private static final StatusHandler DEFAULT_STATUS_HANDLER = + new StatusHandler(code -> code.value() >= 400, ClientResponse::createException); + private final HttpMethod httpMethod; @@ -539,8 +536,7 @@ final class DefaultWebClient implements WebClient { this.uri = uri; this.responseMono = responseMono; this.statusHandlers.addAll(defaultStatusHandlers); - this.statusHandlers.add(DEFAULT_ERROR_STATUS_HANDLER); - this.statusHandlers.add(DEFAULT_UNKNOWN_STATUS_HANDLER); + this.statusHandlers.add(DEFAULT_STATUS_HANDLER); this.defaultStatusHandlerCount = this.statusHandlers.size(); } diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java index ad402809f2d..8f1499f0dee 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java @@ -644,14 +644,14 @@ class WebClientIntegrationTests { int errorStatus = 555; assertThat(HttpStatus.resolve(errorStatus)).isNull(); + String errorMessage = "Something went wrong"; - prepareResponse(response -> response.setResponseCode(errorStatus) - .setHeader("Content-Type", "text/plain").setBody(errorMessage)); + prepareResponse(response -> + response.setResponseCode(errorStatus) + .setHeader("Content-Type", "text/plain") + .setBody(errorMessage)); - Mono result = this.webClient.get() - .uri("/unknownPage") - .retrieve() - .bodyToMono(String.class); + Mono result = this.webClient.get().uri("/unknownPage").retrieve().bodyToMono(String.class); StepVerifier.create(result) .expectErrorSatisfies(throwable -> { @@ -672,20 +672,20 @@ class WebClientIntegrationTests { }); } - @ParameterizedWebClientTest - void retrieve929CustomUnknownStatus(ClientHttpConnector connector) { + @ParameterizedWebClientTest // gh-31202 + void retrieve929UnknownStatusCode(ClientHttpConnector connector) { startServer(connector); int errorStatus = 929; assertThat(HttpStatus.resolve(errorStatus)).isNull(); + String errorMessage = "Something went wrong"; - prepareResponse(response -> response.setResponseCode(errorStatus) - .setHeader("Content-Type", "text/plain").setBody(errorMessage)); + prepareResponse(response -> + response.setResponseCode(errorStatus) + .setHeader("Content-Type", "text/plain") + .setBody(errorMessage)); - Mono result = this.webClient.get() - .uri("/unknownPage") - .retrieve() - .bodyToMono(String.class); + Mono result = this.webClient.get().uri("/unknownPage").retrieve().bodyToMono(String.class); StepVerifier.create(result) .expectErrorSatisfies(throwable -> {