### Question:
BodyDeferringAsyncHandler implements AsyncHandler<Response> { public Response getResponse() throws InterruptedException, IOException { headersArrived.await(); try { semaphore.acquire(); if (throwable != null) { throw new IOException(throwable.getMessage(), throwable); } else { return response; } } finally { semaphore.release(); } } BodyDeferringAsyncHandler(final OutputStream os); @Override void onThrowable(Throwable t); @Override State onStatusReceived(HttpResponseStatus responseStatus); @Override State onHeadersReceived(HttpHeaders headers); @Override State onTrailingHeadersReceived(HttpHeaders headers); @Override void onRetry(); @Override State onBodyPartReceived(HttpResponseBodyPart bodyPart); @Override Response onCompleted(); Response getResponse();  }### Answer:
@Test public void deferredSimple() throws IOException, ExecutionException, InterruptedException { try (AsyncHttpClient client = asyncHttpClient(getAsyncHttpClientConfig())) { BoundRequestBuilder r = client.prepareGet(getTargetUrl()); CountingOutputStream cos = new CountingOutputStream(); BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler(cos); Future<Response> f = r.execute(bdah); Response resp = bdah.getResponse(); assertNotNull(resp); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertEquals(resp.getHeader(CONTENT_LENGTH), String.valueOf(CONTENT_LENGTH_VALUE)); assertTrue(cos.getByteCount() <= CONTENT_LENGTH_VALUE); f.get(); assertEquals(cos.getByteCount(), CONTENT_LENGTH_VALUE); } }
@Test(expectedExceptions = RemotelyClosedException.class, enabled = false) public void deferredSimpleWithFailure() throws Throwable { try (AsyncHttpClient client = asyncHttpClient(getAsyncHttpClientConfig())) { BoundRequestBuilder r = client.prepareGet(getTargetUrl()).addHeader("X-FAIL-TRANSFER", Boolean.TRUE.toString()); CountingOutputStream cos = new CountingOutputStream(); BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler(cos); Future<Response> f = r.execute(bdah); Response resp = bdah.getResponse(); assertNotNull(resp); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertEquals(resp.getHeader(CONTENT_LENGTH), String.valueOf(CONTENT_LENGTH_VALUE)); assertTrue(cos.getByteCount() <= CONTENT_LENGTH_VALUE); try { f.get(); } catch (ExecutionException e) { assertNotEquals(cos.getByteCount(), CONTENT_LENGTH_VALUE); throw e.getCause(); } } }
@Test(expectedExceptions = IOException.class) public void testConnectionRefused() throws IOException, InterruptedException { int newPortWithoutAnyoneListening = findFreePort(); try (AsyncHttpClient client = asyncHttpClient(getAsyncHttpClientConfig())) { BoundRequestBuilder r = client.prepareGet("http: CountingOutputStream cos = new CountingOutputStream(); BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler(cos); r.execute(bdah); bdah.getResponse(); } }
@Test public void testPipedStreams() throws Exception { try (AsyncHttpClient client = asyncHttpClient(getAsyncHttpClientConfig())) { PipedOutputStream pout = new PipedOutputStream(); try (PipedInputStream pin = new PipedInputStream(pout)) { BodyDeferringAsyncHandler handler = new BodyDeferringAsyncHandler(pout); ListenableFuture<Response> respFut = client.prepareGet(getTargetUrl()).execute(handler); Response resp = handler.getResponse(); if (resp.getStatusCode() == 200) { try (BodyDeferringInputStream is = new BodyDeferringInputStream(respFut, handler, pin)) { String body = IOUtils.toString(is, StandardCharsets.UTF_8); assertTrue(body.contains("ABCDEF")); } } else { throw new IOException("HTTP error " + resp.getStatusCode()); } } } }