/** * Close the underlying AsyncHttpClient for this instance. * <br> * If this instance is derived from another instance, this method does * nothing as the client instance is managed by the original * SimpleAsyncHttpClient. * * @see #derive() * @see AsyncHttpClient#close() */ public void close() throws IOException { if (!derived && asyncHttpClient != null) { asyncHttpClient.close(); } }
@Test public void ntlmProxyTest() throws IOException, InterruptedException, ExecutionException { try (AsyncHttpClient client = asyncHttpClient()) { Request request = get("http://localhost").setProxyServer(ntlmProxy()).build(); Future<Response> responseFuture = client.executeRequest(request); int status = responseFuture.get().getStatusCode(); Assert.assertEquals(status, 200); } }
@Test public void testRequestLevelProxy() throws IOException, ExecutionException, TimeoutException, InterruptedException { try (AsyncHttpClient client = asyncHttpClient()) { String target = "http://localhost:1234/"; Future<Response> f = client.prepareGet(target).setProxyServer(proxyServer("localhost", port1)).execute(); Response resp = f.get(3, TimeUnit.SECONDS); assertNotNull(resp); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertEquals(resp.getHeader("target"), "/"); } }
@Test public void testMaxRetry() { try (AsyncHttpClient ahc = asyncHttpClient(config().setMaxRequestRetry(0))) { ahc.executeRequest(ahc.prepareGet(getTargetUrl()).build()).get(); fail(); } catch (Exception t) { assertEquals(t.getCause(), RemotelyClosedException.INSTANCE); } }
@Test public void testEolTerminatedResponse() throws Exception { try (AsyncHttpClient ahc = asyncHttpClient(config().setMaxRequestRetry(0))) { ahc.executeRequest(ahc.prepareGet(getTargetUrl()).setHeader(ACCEPT_ENCODING, HttpHeaderValues.GZIP_DEFLATE).setHeader(CONNECTION, HttpHeaderValues.CLOSE).build()) .get(); } }
@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 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(groups = "online", enabled = false) public void redirected302Test() throws Exception { isSet.getAndSet(false); try (AsyncHttpClient c = asyncHttpClient(config().setFollowRedirect(true))) { Response response = c.prepareGet(getTargetUrl()).setHeader("X-redirect", "http://www.google.com/").execute().get(); assertNotNull(response); assertEquals(response.getStatusCode(), 200); String baseUrl = getBaseUrl(response.getUri()); assertTrue(baseUrl.startsWith("http://www.google."), "response does not show redirection to a google subdomain, got " + baseUrl); } }
@Test public void testPutImageFile() throws Exception { try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000))) { InputStream inputStream = new BufferedInputStream(new FileInputStream(LARGE_IMAGE_FILE)); Response response = client.preparePut(getTargetUrl()).addBodyPart(new InputStreamPart("test", inputStream, LARGE_IMAGE_FILE.getName(), LARGE_IMAGE_FILE.length(), "application/octet-stream", UTF_8)).execute().get(); assertEquals(response.getStatusCode(), 200); } }
@Test public void postWithQueryString() throws IOException, ExecutionException, TimeoutException, InterruptedException { try (AsyncHttpClient client = asyncHttpClient()) { Future<Response> f = client.preparePost("http://localhost:" + port1 + "/?a=b").setBody("abc".getBytes()).execute(); Response resp = f.get(3, TimeUnit.SECONDS); assertNotNull(resp); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); } }
@Test public void getUrlWithPathWithQuery() throws Throwable { withClient().run(client -> withServer(server).run(server -> { String targetUrl = getTargetUrl() + "?q=+%20x"; Request request = get(targetUrl).build(); assertEquals(request.getUrl(), targetUrl); server.enqueueOk(); Response response = client.executeRequest(request, new AsyncCompletionHandlerAdapter()).get(TIMEOUT, SECONDS); assertEquals(response.getUri().toUrl(), targetUrl); })); }
@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 testParameters() throws IOException, ExecutionException, TimeoutException, InterruptedException { String value = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKQLMNOPQRSTUVWXYZ1234567809`~!@#$%^&*()_+-=,.<>/?;:'\"[]{}\\| "; try (AsyncHttpClient client = asyncHttpClient()) { Future<Response> f = client.preparePost("http://localhost:" + port1).addFormParam("test", value).execute(); Response resp = f.get(10, TimeUnit.SECONDS); assertNotNull(resp); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertEquals(resp.getHeader("X-Param"), value.trim()); } }
@Test(enabled = false) public void relativeLocationUrl() throws Exception { isSet.getAndSet(false); try (AsyncHttpClient c = asyncHttpClient()) { Response response = c.preparePost(getTargetUrl()).setFollowRedirect(true).setHeader("X-redirect", "/foo/test").execute().get(); assertNotNull(response); assertEquals(response.getStatusCode(), 200); assertEquals(response.getUri().toString(), getTargetUrl()); } }
@Test(groups = "online", enabled = false) public void notRedirected302Test() throws Exception { isSet.getAndSet(false); try (AsyncHttpClient c = asyncHttpClient(config().setFollowRedirect(true))) { Response response = c.prepareGet(getTargetUrl()).setFollowRedirect(false).setHeader("X-redirect", "http://www.microsoft.com/").execute().get(); assertNotNull(response); assertEquals(response.getStatusCode(), 302); } }
@Test public void testStreamingPutImage() throws Exception { try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000))) { Response response = client.preparePut(getTargetUrl()).setBody(createAsyncPublisher(LARGE_IMAGE_BYTES, 2342)) .execute().get(); assertEquals(response.getStatusCode(), 200); assertEquals(response.getResponseBodyAsBytes(), LARGE_IMAGE_BYTES); } }
@Test public void urlWithColonTest() throws Exception { try (AsyncHttpClient c = asyncHttpClient()) { String query = "test:colon:"; Response response = c.prepareGet(String.format("http://localhost:%d/foo/test/colon?q=%s", port1, query)).setHeader("Content-Type", "text/html").execute().get(TIMEOUT, TimeUnit.SECONDS); assertEquals(response.getHeader("q"), query); } }
@Test public void maxConnectionsText() throws Exception { try (AsyncHttpClient c = asyncHttpClient(config().addRequestFilter(new ThrottleRequestFilter(0, 1000)))) { c.preparePost(getTargetUrl()).execute().get(); fail("Should have timed out"); } catch (ExecutionException ex) { assertTrue(ex.getCause() instanceof FilterException); } }
@Test public void emptyResponseBodyBytesAreEmpty() throws Throwable { withClient().run(client -> withServer(server).run(server -> { server.enqueueEcho(); Response response = client.prepareGet(getTargetUrl()).execute().get(); assertEquals(response.getStatusCode(), 200); assertEquals(response.getResponseBodyAsBytes(), new byte[]{}); })); }
@Test public void testConnectionDoesNotGetClosed() throws Exception { try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000))) { BoundRequestBuilder requestBuilder = client.preparePut(getTargetUrl()) .setBody(createPublisher(LARGE_IMAGE_BYTES, 1000)) .setHeader("X-" + CONTENT_LENGTH, LARGE_IMAGE_BYTES.length) .setHeader("X-" + CONTENT_MD5, LARGE_IMAGE_BYTES_MD5); Response response = requestBuilder.execute().get(); assertEquals(response.getStatusCode(), 200, "HTTP response was invalid on first request."); byte[] responseBody = response.getResponseBodyAsBytes(); assertEquals(Integer.valueOf(response.getHeader("X-" + CONTENT_LENGTH)).intValue(), LARGE_IMAGE_BYTES.length, "Server side payload length invalid"); assertEquals(responseBody.length, LARGE_IMAGE_BYTES.length, "Client side payload length invalid"); assertEquals(response.getHeader(CONTENT_MD5), LARGE_IMAGE_BYTES_MD5, "Server side payload MD5 invalid"); assertEquals(TestUtils.md5(responseBody), LARGE_IMAGE_BYTES_MD5, "Client side payload MD5 invalid");