public String getResponseBody() { return response.getResponseBody(); }
@Test public void testPutEmptyBody() throws Exception { try (AsyncHttpClient ahc = asyncHttpClient()) { Response response = ahc.preparePut(getTargetUrl()).setBody("String").execute().get(); assertNotNull(response); assertEquals(response.getStatusCode(), 204); assertEquals(response.getResponseBody(), ""); assertNotNull(response.getResponseBodyAsStream()); assertEquals(response.getResponseBodyAsStream().read(), -1); } }
public String getResponseBody(Charset charset) { return response.getResponseBody(charset); }
@Test public void zeroCopyPutTest() throws IOException, ExecutionException, InterruptedException { try (AsyncHttpClient client = asyncHttpClient()) { Future<Response> f = client.preparePut("http://localhost:" + port1 + "/").setBody(SIMPLE_TEXT_FILE).execute(); Response resp = f.get(); assertNotNull(resp); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertEquals(resp.getResponseBody(), SIMPLE_TEXT_FILE_STRING); } }
@Test(groups = "standalone") public void testQueryParameters() throws Exception { try (AsyncHttpClient client = asyncHttpClient()) { Future<Response> f = client.prepareGet("http://localhost:" + port1 + "/foo").addHeader("Accepts", "*/*").execute(); Response resp = f.get(3, TimeUnit.SECONDS); assertNotNull(resp); assertEquals(resp.getStatusCode(), 400); assertEquals(resp.getResponseBody(), BAD_REQUEST_STR); } }
@Test public void Expect100Continue() throws Exception { try (AsyncHttpClient client = asyncHttpClient()) { Future<Response> f = client.preparePut("http://localhost:" + port1 + "/") .setHeader(EXPECT, HttpHeaderValues.CONTINUE) .setBody(SIMPLE_TEXT_FILE) .execute(); Response resp = f.get(); assertNotNull(resp); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertEquals(resp.getResponseBody(), SIMPLE_TEXT_FILE_STRING); } }
@Test public void urlWithoutSlashTest() throws Exception { try (AsyncHttpClient c = asyncHttpClient()) { String body = "hello there"; Response response = c.preparePost(String.format("http://localhost:%d/foo/test", port1)).setBody(body).setHeader("Content-Type", "text/html").execute().get(TIMEOUT, TimeUnit.SECONDS); assertEquals(response.getResponseBody(), body); } } }
@Test public void basicAuthInputStreamTest() throws IOException, ExecutionException, TimeoutException, InterruptedException { try (AsyncHttpClient client = asyncHttpClient()) { Future<Response> f = client.preparePost(getTargetUrl()) .setBody(new ByteArrayInputStream("test".getBytes())) .setRealm(basicAuthRealm(USER, ADMIN).build()) .execute(); Response resp = f.get(30, TimeUnit.SECONDS); assertNotNull(resp); assertNotNull(resp.getHeader("X-Auth")); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertEquals(resp.getResponseBody(), "test"); } }
protected void execute(String body) throws IOException, InterruptedException, ExecutionException { try (AsyncHttpClient client = asyncHttpClient()) { BoundRequestBuilder r = client.preparePost(getTargetUrl()).setBody(body).setCharset(UTF_8); Future<Response> f = r.execute(); Response resp = f.get(); assertEquals(resp.getStatusCode(), 200); assertEquals(body, resp.getResponseBody(UTF_8)); } } }
@Test public void basicAuthAsyncConfigTest() throws Exception { try (AsyncHttpClient client = asyncHttpClient(config().setRealm(basicAuthRealm(USER, ADMIN)))) { Future<Response> f = client.preparePost(getTargetUrl()) .setBody(SIMPLE_TEXT_FILE_STRING) .execute(); Response resp = f.get(3, TimeUnit.SECONDS); assertNotNull(resp); assertNotNull(resp.getHeader("X-Auth")); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertEquals(resp.getResponseBody(), SIMPLE_TEXT_FILE_STRING); } }
@Test public void basicAuthFileTest() throws Exception { try (AsyncHttpClient client = asyncHttpClient()) { Future<Response> f = client.preparePost(getTargetUrl()) .setBody(SIMPLE_TEXT_FILE) .setRealm(basicAuthRealm(USER, ADMIN).build()) .execute(); Response resp = f.get(3, TimeUnit.SECONDS); assertNotNull(resp); assertNotNull(resp.getHeader("X-Auth")); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertEquals(resp.getResponseBody(), SIMPLE_TEXT_FILE_STRING); } }
@Test public void basicAuthFileNoKeepAliveTest() throws Exception { try (AsyncHttpClient client = asyncHttpClient(config().setKeepAlive(false))) { Future<Response> f = client.preparePost(getTargetUrl()) .setBody(SIMPLE_TEXT_FILE) .setRealm(basicAuthRealm(USER, ADMIN).build()) .execute(); Response resp = f.get(3, TimeUnit.SECONDS); assertNotNull(resp); assertNotNull(resp.getHeader("X-Auth")); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertEquals(resp.getResponseBody(), SIMPLE_TEXT_FILE_STRING); } }
@Test public void multipleRequestsTest() throws Exception { try (AsyncHttpClient c = asyncHttpClient()) { String body = "hello there"; // once Response response = c.preparePost(getTargetUrl()).setBody(body).setHeader("Content-Type", "text/html").execute().get(TIMEOUT, TimeUnit.SECONDS); assertEquals(response.getResponseBody(), body); // twice response = c.preparePost(getTargetUrl()).setBody(body).setHeader("Content-Type", "text/html").execute().get(TIMEOUT, TimeUnit.SECONDS); assertEquals(response.getResponseBody(), body); } }
@Test public void getEmptyBody() throws Throwable { withClient().run(client -> withServer(server).run(server -> { server.enqueueOk(); Response response = client.prepareGet(getTargetUrl()).execute(new AsyncCompletionHandlerAdapter()) .get(TIMEOUT, SECONDS); assertTrue(response.getResponseBody().isEmpty()); })); }
@Test public void regular302LosesBody() throws Exception { try (AsyncHttpClient c = asyncHttpClient(config().setFollowRedirect(true))) { String body = "hello there"; String contentType = "text/plain; charset=UTF-8"; Response response = c.preparePost(getTargetUrl()).setHeader(CONTENT_TYPE, contentType).setBody(body).setHeader("X-REDIRECT", "302").execute().get(TIMEOUT, TimeUnit.SECONDS); assertEquals(response.getResponseBody(), ""); assertNull(receivedContentType); } }
@Test public void regular301LosesBody() throws Exception { try (AsyncHttpClient c = asyncHttpClient(config().setFollowRedirect(true))) { String body = "hello there"; String contentType = "text/plain; charset=UTF-8"; Response response = c.preparePost(getTargetUrl()).setHeader(CONTENT_TYPE, contentType).setBody(body).setHeader("X-REDIRECT", "301").execute().get(TIMEOUT, TimeUnit.SECONDS); assertEquals(response.getResponseBody(), ""); assertNull(receivedContentType); } }
@Test public void regular307KeepsBody() throws Exception { try (AsyncHttpClient c = asyncHttpClient(config().setFollowRedirect(true))) { String body = "hello there"; String contentType = "text/plain; charset=UTF-8"; Response response = c.preparePost(getTargetUrl()).setHeader(CONTENT_TYPE, contentType).setBody(body).setHeader("X-REDIRECT", "307").execute().get(TIMEOUT, TimeUnit.SECONDS); assertEquals(response.getResponseBody(), body); assertEquals(receivedContentType, contentType); } } }
@Test public void postFileOverHttps() throws Throwable { logger.debug(">>> postBodyOverHttps"); withClient(config().setSslEngineFactory(createSslEngineFactory())).run(client -> withServer(server).run(server -> { server.enqueueEcho(); Response resp = client.preparePost(getTargetUrl()).setBody(SIMPLE_TEXT_FILE).setHeader(CONTENT_TYPE, "text/html").execute().get(); assertNotNull(resp); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertEquals(resp.getResponseBody(), SIMPLE_TEXT_FILE_STRING); })); logger.debug("<<< postBodyOverHttps"); }
@Test public void regular302StrictKeepsBody() throws Exception { try (AsyncHttpClient c = asyncHttpClient(config().setFollowRedirect(true).setStrict302Handling(true))) { String body = "hello there"; String contentType = "text/plain; charset=UTF-8"; Response response = c.preparePost(getTargetUrl()).setHeader(CONTENT_TYPE, contentType).setBody(body).setHeader("X-REDIRECT", "302").execute().get(TIMEOUT, TimeUnit.SECONDS); assertEquals(response.getResponseBody(), body); assertEquals(receivedContentType, contentType); } }
@Test public void propFindWebDavTest() throws InterruptedException, IOException, ExecutionException { try (AsyncHttpClient c = asyncHttpClient()) { Request mkcolRequest = new RequestBuilder("MKCOL").setUrl(getTargetUrl()).build(); Response response = c.executeRequest(mkcolRequest).get(); assertEquals(response.getStatusCode(), 201); Request putRequest = put(getTargetUrl() + "/Test.txt").setBody("this is a test").build(); response = c.executeRequest(putRequest).get(); assertEquals(response.getStatusCode(), 201); Request propFindRequest = new RequestBuilder("PROPFIND").setUrl(getTargetUrl() + "/Test.txt").build(); response = c.executeRequest(propFindRequest).get(); assertEquals(response.getStatusCode(), 207); assertTrue(response.getResponseBody().contains("HTTP/1.1 200 OK"), "Got " + response.getResponseBody()); } }