public static AsyncHttpClient getAsyncHttpClient(AsyncHttpClientConfig config) { if (attemptInstantiation()) { try { Constructor<AsyncHttpClient> constructor = asyncHttpClientImplClass.getConstructor(AsyncHttpClientConfig.class); return constructor.newInstance(config); } catch (Exception e) { throw new AsyncHttpClientImplException("Unable to find the instantiate the class specified by system property : " + AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY + "(AsyncHttpProvider) due to : " + e.getMessage(), e); } } return asyncHttpClient(config); }
@Test public void testUrlRequestParametersEncoding() throws IOException, ExecutionException, InterruptedException { String URL = getTargetUrl() + "?q="; String REQUEST_PARAM = "github github \ngithub"; try (AsyncHttpClient client = asyncHttpClient()) { String requestUrl2 = URL + URLEncoder.encode(REQUEST_PARAM, UTF_8.name()); logger.info("Executing request [{}] ...", requestUrl2); Response response = client.prepareGet(requestUrl2).execute().get(); String s = URLDecoder.decode(response.getHeader("q"), UTF_8.name()); assertEquals(s, REQUEST_PARAM); } }
@Test(timeOut = 60000) public void sendPingMessage() throws Exception { try (AsyncHttpClient c = asyncHttpClient()) { getWebSocket(c).sendPingFrame("PING".getBytes()).get(10, TimeUnit.SECONDS); } }
@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(expectedExceptions = ExecutionException.class) public void streamTextExpectFailure() throws Exception { try (AsyncHttpClient c = asyncHttpClient()) { CountDownLatch closeLatch = new CountDownLatch(1); WebSocket websocket = getWebSocket(c, closeLatch); websocket.sendCloseFrame(); closeLatch.await(1, TimeUnit.SECONDS); websocket.sendTextFrame("STREAM", true, 0).get(1, TimeUnit.SECONDS); } }
@Test public void testListenableFutureAfterCompletion() throws Exception { final CountDownLatch latch = new CountDownLatch(1); try (AsyncHttpClient ahc = asyncHttpClient()) { final ListenableFuture<Response> future = ahc.prepareGet(getTargetUrl()).execute(); future.get(); future.addListener(latch::countDown, Runnable::run); } latch.await(10, TimeUnit.SECONDS); }
@Test public void digestAuthNegativeTest() throws IOException, ExecutionException, TimeoutException, InterruptedException { try (AsyncHttpClient client = asyncHttpClient()) { Future<Response> f = client.prepareGet("http://localhost:" + port1 + "/") .setRealm(digestAuthRealm("fake", ADMIN).build()) .execute(); Response resp = f.get(20, TimeUnit.SECONDS); assertNotNull(resp); assertEquals(resp.getStatusCode(), 401); } }
@Test public void testGlobalProxy() throws IOException, ExecutionException, TimeoutException, InterruptedException { try (AsyncHttpClient client = asyncHttpClient(config().setProxyServer(proxyServer("localhost", port1)))) { String target = "http://localhost:1234/"; Future<Response> f = client.prepareGet(target).execute(); Response resp = f.get(3, TimeUnit.SECONDS); assertNotNull(resp); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertEquals(resp.getHeader("target"), "/"); } }
@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 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); } }
@Test public void digestAuthTestWithoutScheme() throws IOException, ExecutionException, TimeoutException, InterruptedException { try (AsyncHttpClient client = asyncHttpClient()) { Future<Response> f = client.prepareGet("http://localhost:" + port1 + "/") .setRealm(digestAuthRealm(USER, ADMIN).setRealmName("MyRealm").build()) .execute(); Response resp = f.get(60, TimeUnit.SECONDS); assertNotNull(resp); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertNotNull(resp.getHeader("X-Auth")); } }
@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 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 testPutLargeTextFileUnknownSize() throws Exception { File file = createTempFile(1024 * 1024); InputStream inputStream = new BufferedInputStream(new FileInputStream(file)); try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000))) { Response response = client.preparePut(getTargetUrl()) .addBodyPart(new InputStreamPart("test", inputStream, file.getName(), -1, "application/octet-stream", UTF_8)).execute().get(); assertEquals(response.getStatusCode(), 200); } } }
@Test public void mkcolWebDavTest1() 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); } }
@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 testPutImageFile() throws Exception { try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000))) { Response response = client.preparePut(getTargetUrl()).addBodyPart(new FilePart("test", LARGE_IMAGE_FILE, "application/octet-stream", UTF_8)).execute().get(); assertEquals(response.getStatusCode(), 200); } }
@Test public void testPutLargeTextFile() throws Exception { File file = createTempFile(1024 * 1024); try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000))) { Response response = client.preparePut(getTargetUrl()).addBodyPart(new FilePart("test", file, "application/octet-stream", UTF_8)).execute().get(); assertEquals(response.getStatusCode(), 200); } } }
@Test public void testPooledConnectionsWithProxy() throws Exception { try (AsyncHttpClient asyncHttpClient = asyncHttpClient(config().setFollowRedirect(true).setUseInsecureTrustManager(true).setKeepAlive(true))) { RequestBuilder rb = get(getTargetUrl2()).setProxyServer(proxyServer("localhost", port1)); Response r1 = asyncHttpClient.executeRequest(rb.build()).get(); assertEquals(r1.getStatusCode(), 200); Response r2 = asyncHttpClient.executeRequest(rb.build()).get(); assertEquals(r2.getStatusCode(), 200); } } }
@Test public void testNoDirectRequestBodyWithProxy() throws Exception { AsyncHttpClientConfig config = config() .setFollowRedirect(true) .setProxyServer(proxyServer("localhost", port1).build()) .setUseInsecureTrustManager(true) .build(); try (AsyncHttpClient asyncHttpClient = asyncHttpClient(config)) { Response r = asyncHttpClient.executeRequest(post(getTargetUrl2()).setBody(new ByteArrayBodyGenerator(LARGE_IMAGE_BYTES))).get(); assertEquals(r.getStatusCode(), 200); } }