@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 testRequestNonProxyHost() throws IOException, ExecutionException, TimeoutException, InterruptedException { ProxyServer proxy = proxyServer("localhost", port1 - 1).setNonProxyHost("localhost").build(); try (AsyncHttpClient client = asyncHttpClient()) { String target = "http://localhost:" + port1 + "/"; Future<Response> f = client.prepareGet(target).setProxyServer(proxy).execute(); Response resp = f.get(3, TimeUnit.SECONDS); assertNotNull(resp); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertEquals(resp.getHeader("target"), "/"); } }
@Test public void runSocksProxy() throws Exception { new Thread(() -> { try { new SocksProxy(60000); } catch (IOException e) { e.printStackTrace(); } }).start(); try (AsyncHttpClient client = asyncHttpClient()) { String target = "http://localhost:" + port1 + "/"; Future<Response> f = client.prepareGet(target).setProxyServer(new ProxyServer.Builder("localhost", 8000).setProxyType(ProxyType.SOCKS_V4)).execute(); assertEquals(200, f.get(60, TimeUnit.SECONDS).getStatusCode()); } }
@Test public void testBothProxies() throws IOException, ExecutionException, TimeoutException, InterruptedException { try (AsyncHttpClient client = asyncHttpClient(config().setProxyServer(proxyServer("localhost", port1 - 1)))) { 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 testNonProxyHostsRequestOverridesConfig() { ProxyServer configProxy = proxyServer("localhost", port1 - 1).build(); ProxyServer requestProxy = proxyServer("localhost", port1).setNonProxyHost("localhost").build(); try (AsyncHttpClient client = asyncHttpClient(config().setProxyServer(configProxy))) { String target = "http://localhost:1234/"; client.prepareGet(target).setProxyServer(requestProxy).execute().get(); assertFalse(true); } catch (Throwable e) { assertNotNull(e.getCause()); assertEquals(e.getCause().getClass(), ConnectException.class); } }