public String getHeader(String name) { return response.getHeader(name); }
/** * Get the given HTTP header of the response */ public String getHeader(String key) { return ahcResponse.getHeader(key); }
/** * Get the given HTTP header of the response */ public String getHeader(String key) { return ahcResponse.getHeader(key); }
@Override public URI onCompleted(Response response) throws Exception { String location = response.getHeader("Location"); if (location != null) { return URI.create(location); } else { throw new TrelloHttpException("Location header not set"); } }
@Override public URI onCompleted(Response response) throws Exception { String location = response.getHeader("Location"); if (location != null) { return URI.create(location); } else { throw new TrelloHttpException("Location header not set"); } }
private InputStream streamResponse(final Response response) throws IOException { final InputStream input = response.getResponseBodyAsStream(); final boolean compressed = "gzip".equals(response.getHeader("Content-Encoding")); return compressed ? new GZIPInputStream(input) : input; }
private byte[] getResponseBodyAsBytes(final Response response) { try { final boolean gzipEncoded = Optional.ofNullable(response.getHeader(HttpHeaders.CONTENT_ENCODING)) .map(v -> v.equalsIgnoreCase("gzip")) .orElse(false); return gzipEncoded ? unzip(response) : response.getResponseBodyAsBytes(); } catch (IOException e) { throw new HttpException(e); } }
private byte[] getResponseBodyAsBytes(final Response response) { try { final boolean gzipEncoded = Optional.ofNullable(response.getHeader(HttpHeaders.CONTENT_ENCODING)) .map(v -> v.equalsIgnoreCase("gzip")) .orElse(false); return gzipEncoded ? unzip(response) : response.getResponseBodyAsBytes(); } catch (IOException e) { throw new HttpException(e); } }
@Override public Response onCompleted(Response response) throws Exception { try { assertEquals(response.getStatusCode(), 200); assertEquals(response.getHeader("Transfer-Encoding"), "chunked"); } finally { l.countDown(); } return response; } }).get();
@Override public Response onCompleted(Response response) throws Exception { try { assertEquals(response.getStatusCode(), 200); assertEquals(response.getHeader("Upgrade"), "h2,h2c"); } finally { l.countDown(); } return response; } }).get();
@Test(groups = { "standalone", "default_provider" }) public void urlWithColonTest() throws Throwable { try (AsyncHttpClient client = getAsyncHttpClient(null)) { String query = "test:colon:"; Response response = client.prepareGet(String.format("http://127.0.0.1:%d/foo/test/colon?q=%s", port1, query)).setHeader("Content-Type", "text/html").execute().get(TIMEOUT, TimeUnit.SECONDS); assertEquals(response.getHeader("q"), query); } } }
@Test(groups = { "standalone", "default_provider" }) public void testNonProxyHostIssue202() throws IOException, ExecutionException, TimeoutException, InterruptedException { try (AsyncHttpClient client = getAsyncHttpClient(null)) { String target = "http://127.0.0.1:" + port1 + "/"; Future<Response> f = client.prepareGet(target).setProxyServer(new ProxyServer("127.0.0.1", port1 - 1).addNonProxyHost("127.0.0.1")).execute(); Response resp = f.get(3, TimeUnit.SECONDS); assertNotNull(resp); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertEquals(resp.getHeader("target"), "/"); } }
@Test(groups = { "standalone", "default_provider" }) public void testGlobalProxy() throws IOException, ExecutionException, TimeoutException, InterruptedException { AsyncHttpClientConfig cfg = new AsyncHttpClientConfig.Builder().setProxyServer(new ProxyServer("127.0.0.1", port1)).build(); try (AsyncHttpClient client = getAsyncHttpClient(cfg)) { String target = "http://127.0.0.1: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(groups = { "standalone", "default_provider" }) public void digestAuthTestWithoutScheme() throws IOException, ExecutionException, TimeoutException, InterruptedException { try (AsyncHttpClient client = getAsyncHttpClient(null)) { AsyncHttpClient.BoundRequestBuilder r = client.prepareGet("http://127.0.0.1:" + port1 + "/").setRealm((new Realm.RealmBuilder()).setPrincipal(user).setPassword(admin).setRealmName("MyRealm").build()); Future<Response> f = r.execute(); Response resp = f.get(60, TimeUnit.SECONDS); assertNotNull(resp); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertNotNull(resp.getHeader("X-Auth")); } }
@Test(groups = { "standalone", "default_provider" }) public void basicAuthTest() throws IOException, ExecutionException, TimeoutException, InterruptedException { try (AsyncHttpClient client = getAsyncHttpClient(null)) { AsyncHttpClient.BoundRequestBuilder r = client.prepareGet(getTargetUrl()).setRealm((new Realm.RealmBuilder()).setPrincipal(user).setPassword(admin).build()); Future<Response> f = r.execute(); Response resp = f.get(3, TimeUnit.SECONDS); assertNotNull(resp); assertNotNull(resp.getHeader("X-Auth")); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); } }
@Test(groups = { "standalone", "default_provider" }) public void digestAuthTest() throws IOException, ExecutionException, TimeoutException, InterruptedException { try (AsyncHttpClient client = getAsyncHttpClient(null)) { AsyncHttpClient.BoundRequestBuilder r = client.prepareGet("http://127.0.0.1:" + port1 + "/").setRealm((new Realm.RealmBuilder()).setPrincipal(user).setPassword(admin).setRealmName("MyRealm").setScheme(Realm.AuthScheme.DIGEST).build()); Future<Response> f = r.execute(); Response resp = f.get(60, TimeUnit.SECONDS); assertNotNull(resp); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertNotNull(resp.getHeader("X-Auth")); } }
@Test(groups = { "default_provider", "async" }) public void optionsTest() throws Throwable { try (AsyncHttpClient client = getAsyncHttpClient(new Builder().build())) { Response r = client.prepareOptions(getTargetUrl()).execute().get(); assertEquals(r.getStatusCode(), 200); assertEquals(r.getHeader("Allow"), "GET,HEAD,POST,OPTIONS,TRACE"); } }
private void doTest(boolean usePreemptiveAuth) throws UnknownHostException, InterruptedException, ExecutionException { try (AsyncHttpClient client = getAsyncHttpClient(new AsyncHttpClientConfig.Builder().build())) { Request request = new RequestBuilder("GET").setProxyServer(basicProxy()).setUrl(getTargetUrl()).setRealm( new Realm.RealmBuilder().setPrincipal("user").setPassword("passwd").setScheme(BASIC).setUsePreemptiveAuth(usePreemptiveAuth).build()).build(); Future<Response> responseFuture = client.executeRequest(request); Response response = responseFuture.get(); Assert.assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK); Assert.assertTrue(getTargetUrl().endsWith(response.getHeader("target"))); } }