|
1 | 1 | /* |
2 | | - * Copyright 2017-2019 the original author or authors. |
| 2 | + * Copyright 2017-2021 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
18 | 18 |
|
19 | 19 | import static org.assertj.core.api.Assertions.assertThat; |
20 | 20 |
|
| 21 | +import java.nio.charset.StandardCharsets; |
21 | 22 | import java.time.Duration; |
22 | 23 |
|
23 | 24 | import org.junit.jupiter.api.Test; |
|
47 | 48 | /** |
48 | 49 | * @author Shiliang Li |
49 | 50 | * @author Artem Bilan |
| 51 | + * @author David Graff |
50 | 52 | * |
51 | 53 | * @since 5.0 |
52 | 54 | */ |
@@ -108,6 +110,8 @@ void testReactiveErrorOneWay() { |
108 | 110 | assertThat(errorMessage).isNotNull(); |
109 | 111 | assertThat(errorMessage).isInstanceOf(ErrorMessage.class); |
110 | 112 | Throwable throwable = (Throwable) errorMessage.getPayload(); |
| 113 | + assertThat(throwable).isInstanceOf(MessageHandlingException.class); |
| 114 | + assertThat(throwable.getCause()).isInstanceOf(WebClientResponseException.Unauthorized.class); |
111 | 115 | assertThat(throwable.getMessage()).contains("401 Unauthorized"); |
112 | 116 | } |
113 | 117 |
|
@@ -173,7 +177,8 @@ void testServiceUnavailableWithoutBody() { |
173 | 177 | assertThat(payload).isInstanceOf(MessageHandlingException.class); |
174 | 178 |
|
175 | 179 | Exception exception = (Exception) payload; |
176 | | - assertThat(exception.getCause()).isInstanceOf(WebClientResponseException.class); |
| 180 | + assertThat(exception).isInstanceOf(MessageHandlingException.class); |
| 181 | + assertThat(exception.getCause()).isInstanceOf(WebClientResponseException.ServiceUnavailable.class); |
177 | 182 | assertThat(exception.getMessage()).contains("503 Service Unavailable"); |
178 | 183 |
|
179 | 184 | Message<?> replyMessage = errorChannel.receive(10); |
@@ -273,4 +278,57 @@ void testClientHttpResponseAsReply() { |
273 | 278 | .verifyComplete(); |
274 | 279 | } |
275 | 280 |
|
| 281 | + |
| 282 | + @Test |
| 283 | + void testClientHttpResponseErrorAsReply() { |
| 284 | + ClientHttpConnector httpConnector = new HttpHandlerConnector((request, response) -> { |
| 285 | + response.setStatusCode(HttpStatus.NOT_FOUND); |
| 286 | + response.getHeaders().setContentType(MediaType.APPLICATION_JSON); |
| 287 | + |
| 288 | + DataBufferFactory bufferFactory = response.bufferFactory(); |
| 289 | + |
| 290 | + Flux<DataBuffer> data = |
| 291 | + Flux.just( |
| 292 | + bufferFactory.wrap("{".getBytes(StandardCharsets.UTF_8)), |
| 293 | + bufferFactory.wrap(" \"error\": \"Not Found\",".getBytes(StandardCharsets.UTF_8)), |
| 294 | + bufferFactory.wrap(" \"message\": \"404 NOT_FOUND\",".getBytes(StandardCharsets.UTF_8)), |
| 295 | + bufferFactory.wrap(" \"path\": \"/spring-integration\",".getBytes(StandardCharsets.UTF_8)), |
| 296 | + bufferFactory.wrap(" \"status\": 404,".getBytes(StandardCharsets.UTF_8)), |
| 297 | + bufferFactory.wrap(" \"timestamp\": \"1970-01-01T00:00:00.000+00:00\",".getBytes(StandardCharsets.UTF_8)), |
| 298 | + bufferFactory.wrap(" \"trace\": \"some really\nlong\ntrace\",".getBytes(StandardCharsets.UTF_8)), |
| 299 | + bufferFactory.wrap("}".getBytes(StandardCharsets.UTF_8)) |
| 300 | + ); |
| 301 | + |
| 302 | + return response.writeWith(data) |
| 303 | + .then(Mono.defer(response::setComplete)); |
| 304 | + }); |
| 305 | + |
| 306 | + WebClient webClient = WebClient.builder() |
| 307 | + .clientConnector(httpConnector) |
| 308 | + .build(); |
| 309 | + |
| 310 | + String destinationUri = "https://www.springsource.org/spring-integration"; |
| 311 | + WebFluxRequestExecutingMessageHandler reactiveHandler = |
| 312 | + new WebFluxRequestExecutingMessageHandler(destinationUri, webClient); |
| 313 | + |
| 314 | + QueueChannel replyChannel = new QueueChannel(); |
| 315 | + QueueChannel errorChannel = new QueueChannel(); |
| 316 | + reactiveHandler.setOutputChannel(replyChannel); |
| 317 | + reactiveHandler.setBodyExtractor(new ClientHttpResponseBodyExtractor()); |
| 318 | + |
| 319 | + final Message<?> message = |
| 320 | + MessageBuilder.withPayload("hello, world") |
| 321 | + .setErrorChannel(errorChannel) |
| 322 | + .build(); |
| 323 | + reactiveHandler.handleMessage(message); |
| 324 | + |
| 325 | + Message<?> errorMessage = errorChannel.receive(10_000); |
| 326 | + |
| 327 | + assertThat(errorMessage).isNotNull(); |
| 328 | + assertThat(errorMessage).isInstanceOf(ErrorMessage.class); |
| 329 | + final Throwable throwable = (Throwable) errorMessage.getPayload(); |
| 330 | + assertThat(throwable).isInstanceOf(MessageHandlingException.class); |
| 331 | + assertThat(throwable.getCause()).isInstanceOf(WebClientResponseException.NotFound.class); |
| 332 | + assertThat(throwable.getMessage()).contains("404 Not Found"); |
| 333 | + } |
276 | 334 | } |
0 commit comments