|
22 | 22 | import org.junit.jupiter.api.DisplayName;
|
23 | 23 | import org.junit.jupiter.api.Test;
|
24 | 24 |
|
| 25 | +import java.io.InputStream; |
25 | 26 | import java.net.URI;
|
26 | 27 | import java.nio.ByteBuffer;
|
27 | 28 | import java.nio.charset.StandardCharsets;
|
|
31 | 32 | import java.util.List;
|
32 | 33 | import java.util.Set;
|
33 | 34 | import java.util.concurrent.CompletableFuture;
|
| 35 | +import java.util.concurrent.CompletionException; |
34 | 36 | import java.util.concurrent.ConcurrentHashMap;
|
35 | 37 | import java.util.concurrent.CountDownLatch;
|
36 | 38 | import java.util.concurrent.TimeUnit;
|
@@ -233,6 +235,37 @@ public void after(HttpRequest request, HttpResponse<?> response, Consumer<List<B
|
233 | 235 | }
|
234 | 236 | }
|
235 | 237 |
|
| 238 | + @Test |
| 239 | + @DisplayName("afterConnectionFailure, completionExceptions are passed through") |
| 240 | + public void afterConnectionFailureCompletionException() { |
| 241 | + // Given |
| 242 | + final CountDownLatch connectionFailureCallbackInvoked = new CountDownLatch(1); |
| 243 | + final HttpClient.Builder builder = getHttpClientFactory().newBuilder() |
| 244 | + .connectTimeout(1, TimeUnit.SECONDS) |
| 245 | + .addOrReplaceInterceptor("test", new Interceptor() { |
| 246 | + @Override |
| 247 | + public void afterConnectionFailure(HttpRequest request, Throwable failure) { |
| 248 | + connectionFailureCallbackInvoked.countDown(); |
| 249 | + } |
| 250 | + }); |
| 251 | + // When |
| 252 | + try (HttpClient client = builder.build()) { |
| 253 | + final CompletableFuture<HttpResponse<String>> response = client.sendAsync(client.newHttpRequestBuilder() |
| 254 | + .timeout(1, TimeUnit.SECONDS) |
| 255 | + .uri(server.url("/intercepted-url")) |
| 256 | + .method("POST", "application/json", new InputStream() { |
| 257 | + @Override |
| 258 | + public int read() { |
| 259 | + throw new CompletionException("boom time", null); // gets propagated by jetty but gets wrapped by OkHttp |
| 260 | + } |
| 261 | + }, 1L).build(), String.class); |
| 262 | + |
| 263 | + // Then |
| 264 | + assertThat(response).failsWithin(Duration.of(30, ChronoUnit.SECONDS)); |
| 265 | + assertThat(connectionFailureCallbackInvoked).extracting(CountDownLatch::getCount).isEqualTo(0L); |
| 266 | + } |
| 267 | + } |
| 268 | + |
236 | 269 | @Test
|
237 | 270 | @DisplayName("afterFailure (HTTP), replaces the HttpResponse produced by HttpClient.consumeBytes")
|
238 | 271 | public void afterHttpFailureReplacesResponseInConsumeBytes() throws Exception {
|
|
0 commit comments