Package org.asynchttpclient

Examples of org.asynchttpclient.AsyncHttpClient


    }

    @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();
        AsyncHttpClient client = getAsyncHttpClient(cfg);
        try {
            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"), "/");
        } finally {
            client.close();
        }
    }
View Full Code Here


    }

    @Test(groups = { "standalone", "default_provider" })
    public void testBothProxies() throws IOException, ExecutionException, TimeoutException, InterruptedException {
        AsyncHttpClientConfig cfg = new AsyncHttpClientConfig.Builder().setProxyServer(new ProxyServer("127.0.0.1", port1 - 1)).build();
        AsyncHttpClient client = getAsyncHttpClient(cfg);
        try {
            String target = "http://127.0.0.1:1234/";
            Future<Response> f = client.prepareGet(target).setProxyServer(new ProxyServer("127.0.0.1", port1)).execute();
            Response resp = f.get(3, TimeUnit.SECONDS);
            assertNotNull(resp);
            assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
            assertEquals(resp.getHeader("target"), "/");
        } finally {
            client.close();
        }
    }
View Full Code Here

    }

    @Test(groups = { "standalone", "default_provider" })
    public void testNonProxyHosts() throws IOException, ExecutionException, TimeoutException, InterruptedException {
        AsyncHttpClientConfig cfg = new AsyncHttpClientConfig.Builder().setProxyServer(new ProxyServer("127.0.0.1", port1 - 1)).build();
        AsyncHttpClient client = getAsyncHttpClient(cfg);
        try {

            String target = "http://127.0.0.1:1234/";
            client.prepareGet(target).setProxyServer(new ProxyServer("127.0.0.1", port1).addNonProxyHost("127.0.0.1")).execute().get();
            assertFalse(true);
        } catch (Throwable e) {
            assertNotNull(e.getCause());
            assertEquals(e.getCause().getClass(), ConnectException.class);
        } finally {
            client.close();
        }
    }
View Full Code Here

        }
    }

    @Test(groups = { "standalone", "default_provider" })
    public void testNonProxyHostIssue202() throws IOException, ExecutionException, TimeoutException, InterruptedException {
        AsyncHttpClient client = getAsyncHttpClient(null);
        try {
            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"), "/");
        } finally {
            client.close();
        }
    }
View Full Code Here

            System.setProperty("http.proxyHost", "127.0.0.1");
            System.setProperty("http.proxyPort", String.valueOf(port1));
            System.setProperty("http.nonProxyHosts", "localhost");

            AsyncHttpClientConfig cfg = new AsyncHttpClientConfig.Builder().setUseProxyProperties(true).build();
            AsyncHttpClient client = getAsyncHttpClient(cfg);
            try {
                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"), "/");

                target = "http://localhost:1234/";
                f = client.prepareGet(target).execute();
                try {
                    resp = f.get(3, TimeUnit.SECONDS);
                    fail("should not be able to connect");
                } catch (ExecutionException e) {
                    // ok, no proxy used
                }
            } finally {
                client.close();
            }
        } finally {
            System.setProperties(originalProps);
        }
    }
View Full Code Here

            System.setProperty("http.proxyHost", "127.0.0.1");
            System.setProperty("http.proxyPort", String.valueOf(port1));
            System.setProperty("http.nonProxyHosts", "localhost");

            AsyncHttpClient client = getAsyncHttpClient(null);
            try {
                String target = "http://127.0.0.1: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 {
                client.close();
            }
        } finally {
            System.setProperties(originalProps);
        }
    }
View Full Code Here

            System.setProperty("http.proxyHost", "127.0.0.1");
            System.setProperty("http.proxyPort", String.valueOf(port1));
            System.setProperty("http.nonProxyHosts", "localhost");
            System.setProperty("org.asynchttpclient.AsyncHttpClientConfig.useProxyProperties", "true");

            AsyncHttpClient client = getAsyncHttpClient(null);
            try {
                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"), "/");

                target = "http://localhost:1234/";
                f = client.prepareGet(target).execute();
                try {
                    resp = f.get(3, TimeUnit.SECONDS);
                    fail("should not be able to connect");
                } catch (ExecutionException e) {
                    // ok, no proxy used
                }
            } finally {
                client.close();
            }
        } finally {
            System.setProperties(originalProps);
        }
    }
View Full Code Here

            System.setProperty("http.proxyHost", "127.0.0.1");
            System.setProperty("http.proxyPort", String.valueOf(port1));
            System.setProperty("http.nonProxyHosts", "127.*");

            AsyncHttpClientConfig cfg = new AsyncHttpClientConfig.Builder().setUseProxyProperties(true).build();
            AsyncHttpClient client = getAsyncHttpClient(cfg);
            try {
                String target = "http://127.0.0.1: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 {
                client.close();
            }
        } finally {
            System.setProperties(originalProps);
        }
    }
View Full Code Here

                public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
                }
            });

            AsyncHttpClientConfig cfg = new AsyncHttpClientConfig.Builder().setUseProxySelector(true).build();
            AsyncHttpClient client = getAsyncHttpClient(cfg);
            try {
                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"), "/");

                target = "http://localhost:1234/";
                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 {
                client.close();
            }
        } finally {
            // FIXME not threadsafe
            ProxySelector.setDefault(originalProxySelector);
        }
View Full Code Here

    // TODO Netty only.

    @Test(groups = { "standalone", "default_provider" })
    public void testStream() throws IOException {
        AsyncHttpClient ahc = getAsyncHttpClient(null);
        try {
            final AtomicBoolean err = new AtomicBoolean(false);
            final LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<String>();
            final AtomicBoolean status = new AtomicBoolean(false);
            final AtomicInteger headers = new AtomicInteger(0);
            final CountDownLatch latch = new CountDownLatch(1);
            ahc.executeRequest(ahc.prepareGet(getTargetUrl()).build(), new AsyncHandler<Object>() {
                public void onThrowable(Throwable t) {
                    fail("Got throwable.", t);
                    err.set(true);
                }

                public STATE onBodyPartReceived(HttpResponseBodyPart e) throws Exception {
                    if (e.length() != 0) {
                        String s = new String(e.getBodyPartBytes());
                        logger.info("got part: {}", s);
                        queue.put(s);
                    }
                    return STATE.CONTINUE;
                }

                public STATE onStatusReceived(HttpResponseStatus e) throws Exception {
                    status.set(true);
                    return STATE.CONTINUE;
                }

                public STATE onHeadersReceived(HttpResponseHeaders e) throws Exception {
                    if (headers.incrementAndGet() == 2) {
                        throw new Exception("Analyze this.");
                    }
                    return STATE.CONTINUE;
                }

                public Object onCompleted() throws Exception {
                    latch.countDown();
                    return null;
                }
            });
            try {
                assertTrue(latch.await(1, TimeUnit.SECONDS), "Latch failed.");
            } catch (InterruptedException e) {
                fail("Interrupted.", e);
            }
            assertFalse(err.get());
            assertEquals(queue.size(), 2);
            assertTrue(queue.contains("part1"));
            assertTrue(queue.contains("part2"));
            assertTrue(status.get());
            assertEquals(headers.get(), 1);
        } finally {
            ahc.close();
        }
    }
View Full Code Here

TOP

Related Classes of org.asynchttpclient.AsyncHttpClient

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.