@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 nonPreemptiveProxyAuthWithHttpsTarget() throws IOException, InterruptedException, ExecutionException { try (AsyncHttpClient client = asyncHttpClient(config().setUseInsecureTrustManager(true))) { String targetUrl = "https://localhost:" + httpPort + "/foo/bar"; Request request = get(targetUrl) .setProxyServer(proxyServer("127.0.0.1", proxyPort).setRealm(realm(AuthScheme.BASIC, "johndoe", "pass"))) // .setRealm(realm(AuthScheme.BASIC, "user", "passwd")) .build(); Future<Response> responseFuture = client.executeRequest(request); Response response = responseFuture.get(); Assert.assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK); Assert.assertEquals("/foo/bar", response.getHeader("X-pathInfo")); } } }
@Test public void basicAuthNegativeTest() throws IOException, ExecutionException, TimeoutException, InterruptedException { try (AsyncHttpClient client = asyncHttpClient()) { Future<Response> f = client.prepareGet(getTargetUrl()) .setRealm(basicAuthRealm("fake", ADMIN).build()) .execute(); Response resp = f.get(3, TimeUnit.SECONDS); assertNotNull(resp); assertEquals(resp.getStatusCode(), 401); } }
realm = basicAuthRealm(principal, password).build(); ProxyServer.Builder proxyServer = proxyServer(host, port).setRealm(realm);
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 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 basicAuthAsyncConfigTest() throws Exception { try (AsyncHttpClient client = asyncHttpClient(config().setRealm(basicAuthRealm(USER, ADMIN)))) { Future<Response> f = client.preparePost(getTargetUrl()) .setBody(SIMPLE_TEXT_FILE_STRING) .execute(); Response resp = f.get(3, TimeUnit.SECONDS); assertNotNull(resp); assertNotNull(resp.getHeader("X-Auth")); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertEquals(resp.getResponseBody(), SIMPLE_TEXT_FILE_STRING); } }
@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); } }
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); } }
public boolean connect() { try { if (proxyServerInfo != null) { ProxyServer.Builder proxyServer = proxyServer(proxyServerInfo.getHost(), proxyServerInfo.getPort()); if (proxyServerInfo.getPrincipal() != null && proxyServerInfo.getPassword() != null) { proxyServer = proxyServer.setRealm(basicAuthRealm(proxyServerInfo.getPrincipal(), proxyServerInfo.getPassword())); asyncHttpClient = asyncHttpClient(config().setProxyServer(proxyServer)); } else { asyncHttpClient = asyncHttpClient();
@Test public void testUrlRequestParametersEncoding() throws IOException, ExecutionException, InterruptedException { String URL = getTargetUrl() + "?q="; String REQUEST_PARAM = "github github \ngithub"; try (AsyncHttpClient client = asyncHttpClient()) { String requestUrl2 = URL + URLEncoder.encode(REQUEST_PARAM, UTF_8.name()); logger.info("Executing request [{}] ...", requestUrl2); Response response = client.prepareGet(requestUrl2).execute().get(); String s = URLDecoder.decode(response.getHeader("q"), UTF_8.name()); assertEquals(s, REQUEST_PARAM); } }
@Test public void testRequestLevelProxy() throws IOException, ExecutionException, TimeoutException, InterruptedException { try (AsyncHttpClient client = asyncHttpClient()) { String target = "http://localhost:1234/"; Future<Response> f = client.prepareGet(target).setProxyServer(proxyServer("localhost", port1)).execute(); Response resp = f.get(3, TimeUnit.SECONDS); assertNotNull(resp); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertEquals(resp.getHeader("target"), "/"); } }
@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); } }
@AfterMethod(alwaysRun = true) public void clean() throws Exception { try (AsyncHttpClient c = asyncHttpClient()) { c.executeRequest(delete(getTargetUrl())).get(); } }
@Test public void digestAuthNegativeTest() throws IOException, ExecutionException, TimeoutException, InterruptedException { try (AsyncHttpClient client = asyncHttpClient()) { Future<Response> f = client.prepareGet("http://localhost:" + port1 + "/") .setRealm(digestAuthRealm("fake", ADMIN).build()) .execute(); Response resp = f.get(20, TimeUnit.SECONDS); assertNotNull(resp); assertEquals(resp.getStatusCode(), 401); } }
@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 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"); }
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; } }
public static Realm.Builder ntlmAuthRealm(String principal, String password) { return realm(AuthScheme.NTLM, principal, password); } }