@Test public void testPercentageEncodedUserInfo() { final Request req = get("http://hello:wor%20ld@foo.com").build(); assertEquals(req.getMethod(), "GET"); assertEquals(req.getUrl(), "http://hello:wor%20ld@foo.com"); }
@Test public void testParsesQueryParams() { Request request = get("http://foo.com/?param1=value1").addQueryParam("param2", "value2").build(); assertEquals(request.getUrl(), "http://foo.com/?param1=value1¶m2=value2"); List<Param> params = request.getQueryParams(); assertEquals(params.size(), 2); assertEquals(params.get(0), new Param("param1", "value1")); assertEquals(params.get(1), new Param("param2", "value2")); }
@Test public void testContentTypeCharsetToBodyEncoding() { final Request req = get("http://localhost").setHeader("Content-Type", "application/json; charset=utf-8").build(); assertEquals(req.getCharset(), UTF_8); final Request req2 = get("http://localhost").setHeader("Content-Type", "application/json; charset=\"utf-8\"").build(); assertEquals(req2.getCharset(), UTF_8); }
@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 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 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 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 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"); }
private void ntlmAuthTest(Realm.Builder realmBuilder) throws IOException, InterruptedException, ExecutionException { try (AsyncHttpClient client = asyncHttpClient(config().setRealm(realmBuilder))) { Future<Response> responseFuture = client.executeRequest(get(getTargetUrl())); int status = responseFuture.get().getStatusCode(); Assert.assertEquals(status, 200); } }
@Test public void testChaining() { Request request = get("http://foo.com").addQueryParam("x", "value").build(); Request request2 = new RequestBuilder(request).build(); assertEquals(request2.getUri(), request.getUri()); }
private ListenableFuture<Response> testMethodRequest(AsyncHttpClient client, int requests, String action, String id) { RequestBuilder r = get(getTargetUrl()) .addQueryParam(action, "1") .addQueryParam("maxRequests", "" + requests) .addQueryParam("id", id); return client.executeRequest(r); }
@Test public void nonPoolableConnectionReleaseSemaphoresTest() throws Throwable { RequestBuilder request = get(getTargetUrl()).setHeader("Connection", "close"); try (AsyncHttpClient client = asyncHttpClient(config().setMaxConnections(6).setMaxConnectionsPerHost(3))) { client.executeRequest(request).get(); Thread.sleep(1000); client.executeRequest(request).get(); Thread.sleep(1000); client.executeRequest(request).get(); Thread.sleep(1000); client.executeRequest(request).get(); } }
@Test public void getRootUrl() throws Throwable { withClient().run(client -> withServer(server).run(server -> { String url = server.getHttpUrl(); server.enqueueOk(); Response response = client.executeRequest(get(url), new AsyncCompletionHandlerAdapter()).get(TIMEOUT, SECONDS); assertEquals(response.getUri().toUrl(), url); })); }
@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 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 getUrlWithPathWithoutQuery() throws Throwable { withClient().run(client -> withServer(server).run(server -> { server.enqueueOk(); Response response = client.executeRequest(get(getTargetUrl()), new AsyncCompletionHandlerAdapter()).get(TIMEOUT, SECONDS); assertEquals(response.getUri().toUrl(), getTargetUrl()); })); }
@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 getRootUrlWithCustomRemoteAddress() throws Throwable { withClient().run(client -> withServer(server).run(server -> { String url = server.getHttpUrl(); server.enqueueOk(); RequestBuilder request = get(url).setAddress(SocketUtils.addressByName("localhost")); Response response = client.executeRequest(request, new AsyncCompletionHandlerAdapter()).get(TIMEOUT, SECONDS); 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 getUrlWithPathWithQueryParams() throws Throwable { withClient().run(client -> withServer(server).run(server -> { server.enqueueOk(); Response response = client.executeRequest(get(getTargetUrl()).addQueryParam("q", "a b"), new AsyncCompletionHandlerAdapter()).get(TIMEOUT, SECONDS); assertEquals(response.getUri().toUrl(), getTargetUrl() + "?q=a%20b"); })); }