@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 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(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); } }
private void sendEmptyFileInputStream(boolean disableZeroCopy) throws Exception { File file = getClasspathFile("empty.txt"); try (AsyncHttpClient c = asyncHttpClient(config().setDisableZeroCopy(disableZeroCopy))) { InputStream inputStream = new BufferedInputStream(new FileInputStream(file)); Request r = post("http://localhost" + ":" + port1 + "/upload") .addBodyPart(new InputStreamPart("file", inputStream, file.getName(), file.length(), "text/plain", UTF_8)).build(); Response res = c.executeRequest(r).get(); assertEquals(res.getStatusCode(), 200); } }
@Test public void idleStateTest() throws Exception { try (AsyncHttpClient c = asyncHttpClient(config().setPooledConnectionIdleTimeout(10 * 1000))) { c.prepareGet(getTargetUrl()).execute().get(); } catch (ExecutionException e) { fail("Should allow to finish processing request.", e); } }
@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 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 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); } } }
private DefaultAsyncHttpClientConfig.Builder httpClientBuilder() { return config() .setKeepAlive(true) .setMaxConnectionsPerHost(1) .setMaxConnections(1) .setConnectTimeout(1000) .setRequestTimeout(1000) .setFollowRedirect(true); }
@Test public void testRequestProxy() throws Exception { try (AsyncHttpClient asyncHttpClient = asyncHttpClient(config().setFollowRedirect(true).setUseInsecureTrustManager(true))) { RequestBuilder rb = get(getTargetUrl2()).setProxyServer(proxyServer("localhost", port1)); Response r = asyncHttpClient.executeRequest(rb.build()).get(); assertEquals(r.getStatusCode(), 200); } }
private void put(int fileSize) throws Exception { File file = createTempFile(fileSize); try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(2000))) { Response response = client.preparePut(getTargetUrl()).setBody(file).execute().get(); assertEquals(response.getStatusCode(), 200); } }
@Test(expectedExceptions = ExecutionException.class) public void testFailingStream() throws Exception { try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000))) { Publisher<ByteBuf> failingPublisher = Flowable.error(new FailedStream()); client.preparePut(getTargetUrl()).setBody(failingPublisher).execute().get(); } }
@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); } } }
private void sendEmptyFile0(boolean disableZeroCopy) throws Exception { File file = getClasspathFile("empty.txt"); try (AsyncHttpClient c = asyncHttpClient(config().setDisableZeroCopy(disableZeroCopy))) { Request r = post("http://localhost" + ":" + port1 + "/upload") .addBodyPart(new FilePart("file", file, "text/plain", UTF_8)).build(); Response res = c.executeRequest(r).get(); assertEquals(res.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); } }