Skip to content

Commit f16122d

Browse files
committed
Polishing and minor refactoring
Closes gh-31202
1 parent d54a694 commit f16122d

File tree

3 files changed

+18
-30
lines changed

3 files changed

+18
-30
lines changed

spring-web/src/main/java/org/springframework/http/HttpStatusCode.java

-8
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,6 @@ default boolean isSameCodeAs(HttpStatusCode other) {
8888
return value() == other.value();
8989
}
9090

91-
/**
92-
* Checks whether this status code is a well-known HTTP status code or not
93-
* @return {@code true} if the status code corresponds to a standard HTTP status code, {@code false} otherwise
94-
*/
95-
default boolean isWellKnown() {
96-
return HttpStatus.resolve(this.value()) != null;
97-
}
98-
9991
/**
10092
* Return an {@code HttpStatusCode} object for the given integer value.
10193
* @param code the status code as integer

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

+4-8
Original file line numberDiff line numberDiff line change
@@ -515,12 +515,9 @@ private void initCookies(MultiValueMap<String, String> out) {
515515

516516
private static class DefaultResponseSpec implements ResponseSpec {
517517

518-
private static final Predicate<HttpStatusCode> STATUS_CODE_ERROR = HttpStatusCode::isError;
519-
private static final Predicate<HttpStatusCode> STATUS_CODE_UNKNOWN = status -> !status.isWellKnown();
520-
private static final StatusHandler DEFAULT_ERROR_STATUS_HANDLER =
521-
new StatusHandler(STATUS_CODE_ERROR, ClientResponse::createException);
522-
private static final StatusHandler DEFAULT_UNKNOWN_STATUS_HANDLER =
523-
new StatusHandler(STATUS_CODE_UNKNOWN, ClientResponse::createException);
518+
private static final StatusHandler DEFAULT_STATUS_HANDLER =
519+
new StatusHandler(code -> code.value() >= 400, ClientResponse::createException);
520+
524521

525522
private final HttpMethod httpMethod;
526523

@@ -539,8 +536,7 @@ private static class DefaultResponseSpec implements ResponseSpec {
539536
this.uri = uri;
540537
this.responseMono = responseMono;
541538
this.statusHandlers.addAll(defaultStatusHandlers);
542-
this.statusHandlers.add(DEFAULT_ERROR_STATUS_HANDLER);
543-
this.statusHandlers.add(DEFAULT_UNKNOWN_STATUS_HANDLER);
539+
this.statusHandlers.add(DEFAULT_STATUS_HANDLER);
544540
this.defaultStatusHandlerCount = this.statusHandlers.size();
545541
}
546542

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

+14-14
Original file line numberDiff line numberDiff line change
@@ -644,14 +644,14 @@ void retrieve555UnknownStatus(ClientHttpConnector connector) {
644644

645645
int errorStatus = 555;
646646
assertThat(HttpStatus.resolve(errorStatus)).isNull();
647+
647648
String errorMessage = "Something went wrong";
648-
prepareResponse(response -> response.setResponseCode(errorStatus)
649-
.setHeader("Content-Type", "text/plain").setBody(errorMessage));
649+
prepareResponse(response ->
650+
response.setResponseCode(errorStatus)
651+
.setHeader("Content-Type", "text/plain")
652+
.setBody(errorMessage));
650653

651-
Mono<String> result = this.webClient.get()
652-
.uri("/unknownPage")
653-
.retrieve()
654-
.bodyToMono(String.class);
654+
Mono<String> result = this.webClient.get().uri("/unknownPage").retrieve().bodyToMono(String.class);
655655

656656
StepVerifier.create(result)
657657
.expectErrorSatisfies(throwable -> {
@@ -672,20 +672,20 @@ void retrieve555UnknownStatus(ClientHttpConnector connector) {
672672
});
673673
}
674674

675-
@ParameterizedWebClientTest
676-
void retrieve929CustomUnknownStatus(ClientHttpConnector connector) {
675+
@ParameterizedWebClientTest // gh-31202
676+
void retrieve929UnknownStatusCode(ClientHttpConnector connector) {
677677
startServer(connector);
678678

679679
int errorStatus = 929;
680680
assertThat(HttpStatus.resolve(errorStatus)).isNull();
681+
681682
String errorMessage = "Something went wrong";
682-
prepareResponse(response -> response.setResponseCode(errorStatus)
683-
.setHeader("Content-Type", "text/plain").setBody(errorMessage));
683+
prepareResponse(response ->
684+
response.setResponseCode(errorStatus)
685+
.setHeader("Content-Type", "text/plain")
686+
.setBody(errorMessage));
684687

685-
Mono<String> result = this.webClient.get()
686-
.uri("/unknownPage")
687-
.retrieve()
688-
.bodyToMono(String.class);
688+
Mono<String> result = this.webClient.get().uri("/unknownPage").retrieve().bodyToMono(String.class);
689689

690690
StepVerifier.create(result)
691691
.expectErrorSatisfies(throwable -> {

0 commit comments

Comments
 (0)