@Test(enabled = false) public void redirected302InvalidTest() throws Exception { isSet.getAndSet(false); Exception e = null; try (AsyncHttpClient c = asyncHttpClient(config().setFollowRedirect(true))) { c.prepareGet(getTargetUrl()).setHeader("X-redirect", String.format("http://localhost:%d/", port2)).execute().get(); } catch (ExecutionException ex) { e = ex; } assertNotNull(e); Throwable cause = e.getCause(); assertTrue(cause instanceof ConnectException); assertTrue(cause.getMessage().contains(":" + port2)); }
@Test(enabled = false) public void relativePathRedirectTest() throws Exception { isSet.getAndSet(false); try (AsyncHttpClient c = asyncHttpClient(config().setFollowRedirect(true))) { String redirectTarget = "bar/test1"; String destinationUrl = new URI(getTargetUrl()).resolve(redirectTarget).toString(); Response response = c.prepareGet(getTargetUrl()).setHeader("X-redirect", redirectTarget).execute().get(); assertNotNull(response); assertEquals(response.getStatusCode(), 200); assertEquals(response.getUri().toString(), destinationUrl); logger.debug("{} was redirected to {}", redirectTarget, destinationUrl); } }
@Test(enabled = false) public void absolutePathRedirectTest() throws Exception { isSet.getAndSet(false); try (AsyncHttpClient c = asyncHttpClient(config().setFollowRedirect(true))) { String redirectTarget = "/bar/test"; String destinationUrl = new URI(getTargetUrl()).resolve(redirectTarget).toString(); Response response = c.prepareGet(getTargetUrl()).setHeader("X-redirect", redirectTarget).execute().get(); assertNotNull(response); assertEquals(response.getStatusCode(), 200); assertEquals(response.getUri().toString(), destinationUrl); logger.debug("{} was redirected to {}", redirectTarget, destinationUrl); } }
@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()); } }
@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 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 redirectAndBasicAuthTest() throws Exception { try (AsyncHttpClient client = asyncHttpClient(config().setFollowRedirect(true).setMaxRedirects(10))) { Future<Response> f = client.prepareGet(getTargetUrl2()) .setRealm(basicAuthRealm(USER, ADMIN).build()) .execute(); Response resp = f.get(3, TimeUnit.SECONDS); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertNotNull(resp); assertNotNull(resp.getHeader("X-Auth")); } }
@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(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 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"); }
private DefaultAsyncHttpClientConfig.Builder httpClientBuilder() { return config() .setKeepAlive(true) .setMaxConnectionsPerHost(1) .setMaxConnections(1) .setConnectTimeout(1000) .setRequestTimeout(1000) .setFollowRedirect(true); }
@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(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 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 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); } }
@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 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); } }