|
27 | 27 | package org.apache.hc.core5.jackson2.http; |
28 | 28 |
|
29 | 29 | import java.io.InputStream; |
| 30 | +import java.net.URI; |
30 | 31 | import java.net.URL; |
31 | 32 | import java.nio.ByteBuffer; |
32 | 33 | import java.nio.charset.StandardCharsets; |
33 | 34 | import java.util.Collections; |
34 | 35 | import java.util.LinkedList; |
35 | 36 | import java.util.List; |
| 37 | +import java.util.concurrent.atomic.AtomicBoolean; |
36 | 38 | import java.util.concurrent.atomic.AtomicReference; |
37 | 39 |
|
38 | 40 | import com.fasterxml.jackson.core.JsonFactory; |
|
47 | 49 | import org.apache.hc.core5.http.UnsupportedMediaTypeException; |
48 | 50 | import org.apache.hc.core5.http.impl.BasicEntityDetails; |
49 | 51 | import org.apache.hc.core5.http.message.BasicHttpResponse; |
| 52 | +import org.apache.hc.core5.http.nio.AsyncClientExchangeHandler; |
50 | 53 | import org.apache.hc.core5.http.nio.AsyncResponseConsumer; |
51 | 54 | import org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer; |
52 | 55 | import org.apache.hc.core5.http.protocol.HttpCoreContext; |
@@ -362,4 +365,53 @@ void testResponseJsonTokenContentCorrectlyProcessed() throws Exception { |
362 | 365 | Mockito.verifyNoMoreInteractions(mockJsonTokenConsumer); |
363 | 366 | } |
364 | 367 |
|
| 368 | + @Test |
| 369 | + void testAsyncPipelineErrorResponseInvokesErrorCallbackAndSignalsResultCallback() throws Exception { |
| 370 | + final String errorBody = "Unexpected internal failure"; |
| 371 | + final AtomicReference<String> errorRef = new AtomicReference<>(); |
| 372 | + final AtomicBoolean completed = new AtomicBoolean(); |
| 373 | + final AtomicBoolean failed = new AtomicBoolean(); |
| 374 | + final AtomicBoolean cancelled = new AtomicBoolean(); |
| 375 | + final AsyncClientExchangeHandler exchangeHandler = AsyncJsonClientPipeline.assemble(objectMapper) |
| 376 | + .request() |
| 377 | + .get(URI.create("http://localhost/test")) |
| 378 | + .response() |
| 379 | + .asSequence( |
| 380 | + RequestData.class, |
| 381 | + response -> { |
| 382 | + }, |
| 383 | + error -> errorRef.set(error != null ? error.asText() : null), |
| 384 | + requestData -> { |
| 385 | + }) |
| 386 | + .result(new FutureCallback<Long>() { |
| 387 | + @Override |
| 388 | + public void completed(final Long result) { |
| 389 | + completed.set(true); |
| 390 | + } |
| 391 | + |
| 392 | + @Override |
| 393 | + public void failed(final Exception ex) { |
| 394 | + failed.set(true); |
| 395 | + } |
| 396 | + |
| 397 | + @Override |
| 398 | + public void cancelled() { |
| 399 | + cancelled.set(true); |
| 400 | + } |
| 401 | + }) |
| 402 | + .create(); |
| 403 | + |
| 404 | + exchangeHandler.consumeResponse( |
| 405 | + BasicResponseBuilder.create(500).build(), |
| 406 | + new BasicEntityDetails(errorBody.length(), ContentType.TEXT_PLAIN), |
| 407 | + HttpCoreContext.create()); |
| 408 | + exchangeHandler.consume(ByteBuffer.wrap(errorBody.getBytes(StandardCharsets.UTF_8))); |
| 409 | + exchangeHandler.streamEnd(Collections.emptyList()); |
| 410 | + |
| 411 | + Assertions.assertThat(errorRef.get()).isEqualTo(errorBody); |
| 412 | + Assertions.assertThat(completed.get()).isTrue(); |
| 413 | + Assertions.assertThat(failed.get()).isFalse(); |
| 414 | + Assertions.assertThat(cancelled.get()).isFalse(); |
| 415 | + } |
| 416 | + |
365 | 417 | } |
0 commit comments