|
20 | 20 | import java.util.Collections; |
21 | 21 | import java.util.HashMap; |
22 | 22 | import java.util.Map; |
| 23 | +import java.util.function.Predicate; |
23 | 24 |
|
24 | 25 | import org.junit.jupiter.api.BeforeEach; |
25 | 26 | import org.junit.jupiter.api.Test; |
|
33 | 34 |
|
34 | 35 | import org.springframework.core.NamedThreadLocal; |
35 | 36 | import org.springframework.http.HttpHeaders; |
| 37 | +import org.springframework.http.HttpStatus; |
36 | 38 | import org.springframework.http.MediaType; |
37 | 39 |
|
38 | 40 | import static org.assertj.core.api.Assertions.assertThat; |
@@ -309,6 +311,48 @@ public void shouldApplyFiltersAtSubscription() { |
309 | 311 | assertThat(request.headers().getFirst("Custom")).isEqualTo("value"); |
310 | 312 | } |
311 | 313 |
|
| 314 | + @Test // gh-23880 |
| 315 | + public void onStatusHandlersOrderIsPreserved() { |
| 316 | + |
| 317 | + ClientResponse response = ClientResponse.create(HttpStatus.BAD_REQUEST).build(); |
| 318 | + given(exchangeFunction.exchange(any())).willReturn(Mono.just(response)); |
| 319 | + |
| 320 | + Mono<Void> result = this.builder.build().get() |
| 321 | + .uri("/path") |
| 322 | + .retrieve() |
| 323 | + .onStatus(HttpStatus::is4xxClientError, resp -> Mono.error(new IllegalStateException("1"))) |
| 324 | + .onStatus(HttpStatus::is4xxClientError, resp -> Mono.error(new IllegalStateException("2"))) |
| 325 | + .bodyToMono(Void.class); |
| 326 | + |
| 327 | + StepVerifier.create(result).expectErrorMessage("1").verify(); |
| 328 | + } |
| 329 | + |
| 330 | + @Test // gh-23880 |
| 331 | + @SuppressWarnings("unchecked") |
| 332 | + public void onStatusHandlersDefaultHandlerIsLast() { |
| 333 | + |
| 334 | + ClientResponse response = ClientResponse.create(HttpStatus.BAD_REQUEST).build(); |
| 335 | + given(exchangeFunction.exchange(any())).willReturn(Mono.just(response)); |
| 336 | + |
| 337 | + Predicate<HttpStatus> predicate1 = mock(Predicate.class); |
| 338 | + Predicate<HttpStatus> predicate2 = mock(Predicate.class); |
| 339 | + |
| 340 | + given(predicate1.test(HttpStatus.BAD_REQUEST)).willReturn(false); |
| 341 | + given(predicate2.test(HttpStatus.BAD_REQUEST)).willReturn(false); |
| 342 | + |
| 343 | + Mono<Void> result = this.builder.build().get() |
| 344 | + .uri("/path") |
| 345 | + .retrieve() |
| 346 | + .onStatus(predicate1, resp -> Mono.error(new IllegalStateException())) |
| 347 | + .onStatus(predicate2, resp -> Mono.error(new IllegalStateException())) |
| 348 | + .bodyToMono(Void.class); |
| 349 | + |
| 350 | + StepVerifier.create(result).expectError(WebClientResponseException.class).verify(); |
| 351 | + |
| 352 | + verify(predicate1).test(HttpStatus.BAD_REQUEST); |
| 353 | + verify(predicate2).test(HttpStatus.BAD_REQUEST); |
| 354 | + } |
| 355 | + |
312 | 356 | private ClientRequest verifyAndGetRequest() { |
313 | 357 | ClientRequest request = this.captor.getValue(); |
314 | 358 | verify(this.exchangeFunction).exchange(request); |
|
0 commit comments