@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"); } }
@Test public void asyncStreamReusePOSTTest() throws Throwable { withClient() .run( client -> { withServer(server) .run( server -> { server.enqueueEcho(); final AtomicReference<HttpHeaders> responseHeaders = new AtomicReference<>(); BoundRequestBuilder rb = client .preparePost(getTargetUrl()) // .setHeader( CONTENT_TYPE, HttpHeaderValues.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"); assertContentTypesEquals( h.get(CONTENT_TYPE), TEXT_HTML_CONTENT_TYPE_WITH_UTF_8_CHARSET); assertNotNull(r, "No response body"); assertEquals(r.trim(), RESPONSE, "Unexpected response body"); responseHeaders.set(null); server.enqueueEcho(); // 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"); assertContentTypesEquals( h.get(CONTENT_TYPE), TEXT_HTML_CONTENT_TYPE_WITH_UTF_8_CHARSET); assertNotNull(r, "No response body"); assertEquals(r.trim(), RESPONSE, "Unexpected response body"); }); }); }