@Test(groups = "standalone") public void asyncStreamFutureTest() throws Exception { final AtomicReference<HttpHeaders> responseHeaders = new AtomicReference<>(); final AtomicReference<Throwable> throwable = new AtomicReference<>(); try (AsyncHttpClient c = asyncHttpClient()) { Future<String> f = c.preparePost(getTargetUrl()) .addFormParam("param_1", "value_1") .execute( new AsyncHandlerAdapter() { private StringBuilder builder = new StringBuilder(); @Override public State onHeadersReceived(HttpResponseHeaders content) throws Exception { responseHeaders.set(content.getHeaders()); return State.CONTINUE; } @Override public State onBodyPartReceived(HttpResponseBodyPart content) throws Exception { builder.append(new String(content.getBodyPartBytes())); return State.CONTINUE; } @Override public String onCompleted() throws Exception { return builder.toString().trim(); } @Override public void onThrowable(Throwable t) { throwable.set(t); } }); String responseBody = f.get(5, TimeUnit.SECONDS); HttpHeaders h = responseHeaders.get(); assertNotNull(h, "Should receive non null headers"); assertEquals( h.get(HttpHeaders.Names.CONTENT_TYPE).toLowerCase(Locale.ENGLISH), TEXT_HTML_CONTENT_TYPE_WITH_UTF_8_CHARSET.toLowerCase(Locale.ENGLISH), "Unexpected content-type"); assertNotNull(responseBody, "No response body"); assertEquals(responseBody.trim(), RESPONSE, "Unexpected response body"); assertNull(throwable.get(), "Unexpected exception"); } }
@Test(groups = "standalone") public void asyncStreamInterruptTest() throws Exception { final CountDownLatch l = new CountDownLatch(1); final AtomicReference<HttpHeaders> responseHeaders = new AtomicReference<>(); final AtomicBoolean bodyReceived = new AtomicBoolean(false); final AtomicReference<Throwable> throwable = new AtomicReference<>(); try (AsyncHttpClient c = asyncHttpClient()) { c.preparePost(getTargetUrl()) // .setHeader("Content-Type", "application/x-www-form-urlencoded") // .addFormParam("param_1", "value_1") // .execute( new AsyncHandlerAdapter() { @Override public State onHeadersReceived(HttpResponseHeaders content) throws Exception { responseHeaders.set(content.getHeaders()); return State.ABORT; } @Override public State onBodyPartReceived(final HttpResponseBodyPart content) throws Exception { bodyReceived.set(true); return State.ABORT; } @Override public void onThrowable(Throwable t) { throwable.set(t); l.countDown(); } }); l.await(5, TimeUnit.SECONDS); assertTrue(!bodyReceived.get(), "Interrupted not working"); HttpHeaders h = responseHeaders.get(); assertNotNull(h, "Should receive non null headers"); assertEquals( h.get(HttpHeaders.Names.CONTENT_TYPE).toLowerCase(Locale.ENGLISH), TEXT_HTML_CONTENT_TYPE_WITH_UTF_8_CHARSET.toLowerCase(Locale.ENGLISH), "Unexpected content-type"); assertNull(throwable.get(), "Should get an exception"); } }
@Test(groups = "standalone") public void asyncStreamReusePOSTTest() throws Exception { final AtomicReference<HttpHeaders> responseHeaders = new AtomicReference<>(); try (AsyncHttpClient c = asyncHttpClient()) { BoundRequestBuilder rb = c.preparePost(getTargetUrl()) // .setHeader("Content-Type", "application/x-www-form-urlencoded") .addFormParam("param_1", "value_1"); Future<String> f = rb.execute( new AsyncHandlerAdapter() { private StringBuilder builder = new StringBuilder(); @Override public State onHeadersReceived(HttpResponseHeaders content) throws Exception { responseHeaders.set(content.getHeaders()); return State.CONTINUE; } @Override public State onBodyPartReceived(HttpResponseBodyPart content) throws Exception { builder.append(new String(content.getBodyPartBytes())); return State.CONTINUE; } @Override public String onCompleted() throws Exception { return builder.toString(); } }); String r = f.get(5, TimeUnit.SECONDS); HttpHeaders h = responseHeaders.get(); assertNotNull(h, "Should receive non null headers"); assertEquals( h.get(HttpHeaders.Names.CONTENT_TYPE).toLowerCase(Locale.ENGLISH), TEXT_HTML_CONTENT_TYPE_WITH_UTF_8_CHARSET.toLowerCase(Locale.ENGLISH), "Unexpected content-type"); assertNotNull(r, "No response body"); assertEquals(r.trim(), RESPONSE, "Unexpected response body"); responseHeaders.set(null); // Let do the same again f = rb.execute( new AsyncHandlerAdapter() { private StringBuilder builder = new StringBuilder(); @Override public State onHeadersReceived(HttpResponseHeaders content) throws Exception { responseHeaders.set(content.getHeaders()); return State.CONTINUE; } @Override public State onBodyPartReceived(HttpResponseBodyPart content) throws Exception { builder.append(new String(content.getBodyPartBytes())); return State.CONTINUE; } @Override public String onCompleted() throws Exception { return builder.toString(); } }); f.get(5, TimeUnit.SECONDS); h = responseHeaders.get(); assertNotNull(h, "Should receive non null headers"); assertEquals( h.get(HttpHeaders.Names.CONTENT_TYPE).toLowerCase(Locale.ENGLISH), TEXT_HTML_CONTENT_TYPE_WITH_UTF_8_CHARSET.toLowerCase(Locale.ENGLISH), "Unexpected content-type"); assertNotNull(r, "No response body"); assertEquals(r.trim(), RESPONSE, "Unexpected response body"); } }