Search in sources :

Example 46 with AsyncHttpClient

use of org.asynchttpclient.AsyncHttpClient in project async-http-client by AsyncHttpClient.

the class ProxyTest method testBothProxies.

@Test(groups = "standalone")
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"), "/");
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 47 with AsyncHttpClient

use of org.asynchttpclient.AsyncHttpClient in project async-http-client by AsyncHttpClient.

the class ProxyTest method testProxyActivationProperty.

@Test(groups = "standalone", enabled = false)
public void testProxyActivationProperty() throws IOException, ExecutionException, TimeoutException, InterruptedException {
    // FIXME not threadsafe!
    Properties originalProps = new Properties();
    originalProps.putAll(System.getProperties());
    System.setProperty(ProxyUtils.PROXY_HOST, "127.0.0.1");
    System.setProperty(ProxyUtils.PROXY_PORT, String.valueOf(port1));
    System.setProperty(ProxyUtils.PROXY_NONPROXYHOSTS, "localhost");
    System.setProperty(AsyncHttpClientConfigDefaults.ASYNC_CLIENT_CONFIG_ROOT + "useProxyProperties", "true");
    AsyncHttpClientConfigHelper.reloadProperties();
    try (AsyncHttpClient client = asyncHttpClient()) {
        String proxifiedTarget = "http://127.0.0.1:1234/";
        Future<Response> f = client.prepareGet(proxifiedTarget).execute();
        Response resp = f.get(3, TimeUnit.SECONDS);
        assertNotNull(resp);
        assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
        assertEquals(resp.getHeader("target"), "/");
        String nonProxifiedTarget = "http://localhost:1234/";
        f = client.prepareGet(nonProxifiedTarget).execute();
        try {
            resp = f.get(3, TimeUnit.SECONDS);
            fail("should not be able to connect");
        } catch (ExecutionException e) {
        // ok, no proxy used
        }
    } finally {
        System.setProperties(originalProps);
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) Properties(java.util.Properties) ExecutionException(java.util.concurrent.ExecutionException) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 48 with AsyncHttpClient

use of org.asynchttpclient.AsyncHttpClient in project async-http-client by AsyncHttpClient.

the class ProxyTest method testNonProxyHostsRequestOverridesConfig.

@Test(groups = "standalone")
public void testNonProxyHostsRequestOverridesConfig() throws IOException, ExecutionException, TimeoutException, InterruptedException {
    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);
    }
}
Also used : AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) ConnectException(java.net.ConnectException) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 49 with AsyncHttpClient

use of org.asynchttpclient.AsyncHttpClient in project async-http-client by AsyncHttpClient.

the class ProxyTest method testIgnoreProxyPropertiesByDefault.

// @Test(groups = "standalone")
public void testIgnoreProxyPropertiesByDefault() throws IOException, ExecutionException, TimeoutException, InterruptedException {
    // FIXME not threadsafe!
    Properties originalProps = new Properties();
    originalProps.putAll(System.getProperties());
    System.setProperty(ProxyUtils.PROXY_HOST, "localhost");
    System.setProperty(ProxyUtils.PROXY_PORT, String.valueOf(port1));
    System.setProperty(ProxyUtils.PROXY_NONPROXYHOSTS, "localhost");
    AsyncHttpClientConfigHelper.reloadProperties();
    try (AsyncHttpClient client = asyncHttpClient()) {
        String target = "http://localhost:1234/";
        Future<Response> f = client.prepareGet(target).execute();
        try {
            f.get(3, TimeUnit.SECONDS);
            fail("should not be able to connect");
        } catch (ExecutionException e) {
        // ok, no proxy used
        }
    } finally {
        System.setProperties(originalProps);
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) Properties(java.util.Properties) ExecutionException(java.util.concurrent.ExecutionException) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient)

Example 50 with AsyncHttpClient

use of org.asynchttpclient.AsyncHttpClient in project async-http-client by AsyncHttpClient.

the class EventPipelineTest method asyncPipelineTest.

@Test(groups = "standalone")
public void asyncPipelineTest() throws Exception {
    AsyncHttpClientConfig.AdditionalChannelInitializer httpAdditionalPipelineInitializer = new AsyncHttpClientConfig.AdditionalChannelInitializer() {

        public void initChannel(Channel channel) throws Exception {
            channel.pipeline().addBefore("inflater", "copyEncodingHeader", new CopyEncodingHandler());
        }
    };
    try (AsyncHttpClient p = asyncHttpClient(config().setHttpAdditionalChannelInitializer(httpAdditionalPipelineInitializer))) {
        final CountDownLatch l = new CountDownLatch(1);
        p.executeRequest(get(getTargetUrl()), new AsyncCompletionHandlerAdapter() {

            @Override
            public Response onCompleted(Response response) throws Exception {
                try {
                    assertEquals(response.getStatusCode(), 200);
                    assertEquals(response.getHeader("X-Original-Content-Encoding"), "<original encoding>");
                } finally {
                    l.countDown();
                }
                return response;
            }
        }).get();
        if (!l.await(TIMEOUT, TimeUnit.SECONDS)) {
            fail("Timeout out");
        }
    }
}
Also used : Response(org.asynchttpclient.Response) Channel(io.netty.channel.Channel) AsyncHttpClientConfig(org.asynchttpclient.AsyncHttpClientConfig) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Aggregations

AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)146 Test (org.testng.annotations.Test)119 Response (org.asynchttpclient.Response)71 AbstractBasicTest (org.asynchttpclient.AbstractBasicTest)66 HttpServletResponse (javax.servlet.http.HttpServletResponse)40 CountDownLatch (java.util.concurrent.CountDownLatch)31 DefaultAsyncHttpClient (org.asynchttpclient.DefaultAsyncHttpClient)26 AtomicReference (java.util.concurrent.atomic.AtomicReference)23 RequestBuilder (org.asynchttpclient.RequestBuilder)16 IOException (java.io.IOException)14 RouteBuilder (org.apache.camel.builder.RouteBuilder)14 ExecutionException (java.util.concurrent.ExecutionException)13 Request (org.asynchttpclient.Request)13 WebSocket (org.asynchttpclient.ws.WebSocket)12 Test (org.junit.Test)11 AsyncHttpClientConfig (org.asynchttpclient.AsyncHttpClientConfig)10 File (java.io.File)9 WebSocketTextListener (org.asynchttpclient.ws.WebSocketTextListener)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7