@Test public void testHEAD302() throws IOException, InterruptedException, ExecutionException, TimeoutException { AsyncHttpClientConfig clientConfig = new DefaultAsyncHttpClientConfig.Builder().setFollowRedirect(true).build(); try (AsyncHttpClient client = asyncHttpClient(clientConfig)) { final CountDownLatch l = new CountDownLatch(1); Request request = head("http://localhost:" + port1 + "/Test").build(); Response response = client.executeRequest(request, new AsyncCompletionHandlerBase() { @Override public Response onCompleted(Response response) throws Exception { l.countDown(); return super.onCompleted(response); } }).get(3, TimeUnit.SECONDS); if (l.await(TIMEOUT, TimeUnit.SECONDS)) { assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK); assertTrue(response.getUri().getPath().endsWith("_moved")); } else { fail("Timeout out"); } } }
@Test public void negativeContentTypeTest() throws Exception { AsyncHttpClientConfig config = config() .setConnectTimeout(100) .setMaxConnections(50) .setRequestTimeout(5 * 60 * 1000) // 5 minutes .build(); try (AsyncHttpClient client = asyncHttpClient(config)) { RequestBuilder requestBuilder = post(getTargetUrl()) .setHeader("Content-Type", "message/rfc822") .setBody(new InputStreamBodyGenerator(new ByteArrayInputStream(MY_MESSAGE.getBytes()))); Future<Response> future = client.executeRequest(requestBuilder.build()); System.out.println("waiting for response"); Response response = future.get(); assertEquals(response.getStatusCode(), 200); assertEquals(response.getResponseBody(), MY_MESSAGE); } } }
@Test public void testDefaultFollowRedirect() { Request request = Dsl.get("http://stackoverflow.com/questions/1057564").setVirtualHost("example.com").build(); DefaultAsyncHttpClientConfig config = new DefaultAsyncHttpClientConfig.Builder().build(); boolean followRedirect = HttpUtils.followRedirect(config, request); assertFalse(followRedirect, "Default value of redirect should be false"); }
@Test(groups = "online", invocationCount = 4) public void multipleSslRequestsWithDelayAndKeepAlive() throws Exception { AsyncHttpClientConfig config = config() .setFollowRedirect(true) .setKeepAlive(true) .setConnectTimeout(10000) .setPooledConnectionIdleTimeout(60000) .setRequestTimeout(10000) .setMaxConnectionsPerHost(-1) .setMaxConnections(-1) .build(); try (AsyncHttpClient client = asyncHttpClient(config)) { final BoundRequestBuilder builder = client.prepareGet(GOOGLE_HTTPS_URL); final Response response1 = builder.execute().get(); Thread.sleep(4000); final Response response2 = builder.execute().get(); assertNotNull(response1); assertNotNull(response2); } } }
/** * Tests that after a redirect the final url in the response reflect the redirect */ @Test public void testGetRedirectFinalUrl() throws Exception { AsyncHttpClientConfig config = config() .setKeepAlive(true) .setMaxConnectionsPerHost(1) .setMaxConnections(1) .setConnectTimeout(1000) .setRequestTimeout(1000) .setFollowRedirect(true) .build(); try (AsyncHttpClient c = asyncHttpClient(config)) { ListenableFuture<Response> response = c.executeRequest(get(servletEndpointRedirectUrl)); Response res = response.get(); assertNotNull(res.getResponseBody()); assertEquals(res.getUri().toString(), BASE_URL + "/overthere"); } }
@Test(enabled = false) public void httpToHttpsProperConfig() throws Exception { redirectDone.getAndSet(false); AsyncHttpClientConfig cg = config() .setMaxRedirects(5) .setFollowRedirect(true) .setUseInsecureTrustManager(true) .build(); try (AsyncHttpClient c = asyncHttpClient(cg)) { Response response = c.prepareGet(getTargetUrl()).setHeader("X-redirect", getTargetUrl2() + "/test2").execute().get(); assertNotNull(response); assertEquals(response.getStatusCode(), 200); assertEquals(response.getHeader("X-httpToHttps"), "PASS"); // Test if the internal channel is downgraded to clean http. response = c.prepareGet(getTargetUrl()).setHeader("X-redirect", getTargetUrl2() + "/foo2").execute().get(); assertNotNull(response); assertEquals(response.getStatusCode(), 200); assertEquals(response.getHeader("X-httpToHttps"), "PASS"); } }
public SimpleAsyncHttpClient build() { if (realmBuilder != null) { configBuilder.setRealm(realmBuilder.build()); } if (proxyHost != null) { Realm realm = null; if (proxyPrincipal != null) { AuthScheme proxyAuthScheme = withDefault(this.proxyAuthScheme, AuthScheme.BASIC); realm = realm(proxyAuthScheme, proxyPrincipal, proxyPassword).build(); } configBuilder.setProxyServer(proxyServer(proxyHost, proxyPort).setRealm(realm).build()); } configBuilder.addIOExceptionFilter(new ResumableIOExceptionFilter()); SimpleAsyncHttpClient sc = new SimpleAsyncHttpClient(configBuilder.build(), requestBuilder, defaultThrowableHandler, errorDocumentBehaviour, enableResumableDownload, ahc, listener); return sc; } }
@Test public void testDecompressBodyWithProxy() throws Exception { AsyncHttpClientConfig config = config() .setFollowRedirect(true) .setProxyServer(proxyServer("localhost", port1).build()) .setUseInsecureTrustManager(true) .build(); try (AsyncHttpClient asyncHttpClient = asyncHttpClient(config)) { String body = "hello world"; Response r = asyncHttpClient.executeRequest(post(getTargetUrl2()) .setHeader("X-COMPRESS", "true") .setBody(body)).get(); assertEquals(r.getStatusCode(), 200); assertEquals(r.getResponseBody(), body); } }
@Test(enabled = false) public void httpToHttpsRedirect() throws Exception { redirectDone.getAndSet(false); AsyncHttpClientConfig cg = config() .setMaxRedirects(5) .setFollowRedirect(true) .setUseInsecureTrustManager(true) .build(); try (AsyncHttpClient c = asyncHttpClient(cg)) { Response response = c.prepareGet(getTargetUrl()).setHeader("X-redirect", getTargetUrl2()).execute().get(); assertNotNull(response); assertEquals(response.getStatusCode(), 200); assertEquals(response.getHeader("X-httpToHttps"), "PASS"); } }
@Test(enabled = false) public void relativeLocationUrl() throws Exception { redirectDone.getAndSet(false); AsyncHttpClientConfig cg = config() .setMaxRedirects(5) .setFollowRedirect(true) .setUseInsecureTrustManager(true) .build(); try (AsyncHttpClient c = asyncHttpClient(cg)) { Response response = c.prepareGet(getTargetUrl()).setHeader("X-redirect", "/foo/test").execute().get(); assertNotNull(response); assertEquals(response.getStatusCode(), 200); assertEquals(response.getUri().toString(), getTargetUrl()); } }
protected ClientTestBody withClient(DefaultAsyncHttpClientConfig.Builder builder) { return withClient(builder.build()); }
public static AsyncHttpClient asyncHttpClient(DefaultAsyncHttpClientConfig.Builder configBuilder) { return new DefaultAsyncHttpClient(configBuilder.build()); }
private AsyncHttpClientConfig getAsyncHttpClientConfig() { // for this test brevity's sake, we are limiting to 1 retries return config().setMaxRequestRetry(0).setRequestTimeout(10000).build(); }
@Test public void testConfigProxy() throws Exception { AsyncHttpClientConfig config = config() .setFollowRedirect(true) .setProxyServer(proxyServer("localhost", port1).build()) .setUseInsecureTrustManager(true) .build(); try (AsyncHttpClient asyncHttpClient = asyncHttpClient(config)) { Response r = asyncHttpClient.executeRequest(get(getTargetUrl2())).get(); assertEquals(r.getStatusCode(), 200); } }
@Test public void testGetFollowRedirectPriorityGivenToRequest() { Request request = Dsl.get("http://stackoverflow.com/questions/1057564").setFollowRedirect(false).build(); DefaultAsyncHttpClientConfig config = new DefaultAsyncHttpClientConfig.Builder().setFollowRedirect(true).build(); boolean followRedirect = HttpUtils.followRedirect(config, request); assertFalse(followRedirect, "Follow redirect value set in request should be given priority"); }
@Test public void testGetFollowRedirectInConfig() { Request request = Dsl.get("http://stackoverflow.com/questions/1057564").build(); DefaultAsyncHttpClientConfig config = new DefaultAsyncHttpClientConfig.Builder().setFollowRedirect(true).build(); boolean followRedirect = HttpUtils.followRedirect(config, request); assertTrue(followRedirect, "Follow redirect should be equal to value specified in config when not specified in request"); }
@Test public void testGetFollowRedirectInRequest() { Request request = Dsl.get("http://stackoverflow.com/questions/1057564").setFollowRedirect(true).build(); DefaultAsyncHttpClientConfig config = new DefaultAsyncHttpClientConfig.Builder().build(); boolean followRedirect = HttpUtils.followRedirect(config, request); assertTrue(followRedirect, "Follow redirect must be true as set in the request"); }
@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); } }
/** * Create a new HTTP Asynchronous Client using the default * {@link DefaultAsyncHttpClientConfig} configuration. The default * {@link AsyncHttpClient} that will be used will be based on the classpath * configuration. * <p> * If none of those providers are found, then the engine will throw an * IllegalStateException. */ public DefaultAsyncHttpClient() { this(new DefaultAsyncHttpClientConfig.Builder().build()); }
static AsyncHttpClient createAsyncHttpClient( String nameFormat, String timerThreadNameFormat, @Nullable SSLContext sslContext ) { final DefaultAsyncHttpClientConfig.Builder builder = new DefaultAsyncHttpClientConfig.Builder() .setThreadFactory(Execs.makeThreadFactory(nameFormat)) .setNettyTimer(new HashedWheelTimer(Execs.makeThreadFactory(timerThreadNameFormat))); if (sslContext != null) { builder.setSslContext(new JdkSslContext(sslContext, true, ClientAuth.NONE)); } return new DefaultAsyncHttpClient(builder.build()); }