Search in sources :

Example 46 with Response

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

the class BodyDeferringAsyncHandlerTest method testPipedStreams.

@Test
public void testPipedStreams() throws Exception {
    try (AsyncHttpClient client = asyncHttpClient(getAsyncHttpClientConfig())) {
        PipedOutputStream pout = new PipedOutputStream();
        try (PipedInputStream pin = new PipedInputStream(pout)) {
            BodyDeferringAsyncHandler handler = new BodyDeferringAsyncHandler(pout);
            ListenableFuture<Response> respFut = client.prepareGet(getTargetUrl()).execute(handler);
            Response resp = handler.getResponse();
            if (resp.getStatusCode() == 200) {
                try (BodyDeferringInputStream is = new BodyDeferringInputStream(respFut, handler, pin)) {
                    String body = IOUtils.toString(is, StandardCharsets.UTF_8);
                    assertTrue(body.contains("ABCDEF"));
                }
            } else {
                throw new IOException("HTTP error " + resp.getStatusCode());
            }
        }
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) PipedOutputStream(java.io.PipedOutputStream) BodyDeferringInputStream(org.asynchttpclient.handler.BodyDeferringAsyncHandler.BodyDeferringInputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 47 with Response

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

the class ProxyTest method testGlobalProxy.

// @Test
// public void asyncDoPostProxyTest() throws Throwable {
// try (AsyncHttpClient client = asyncHttpClient(config().setProxyServer(proxyServer("localhost", port2).build()))) {
// HttpHeaders h = new DefaultHttpHeaders();
// h.add(CONTENT_TYPE, APPLICATION_X_WWW_FORM_URLENCODED);
// StringBuilder sb = new StringBuilder();
// for (int i = 0; i < 5; i++) {
// sb.append("param_").append(i).append("=value_").append(i).append("&");
// }
// sb.setLength(sb.length() - 1);
// 
// Response response = client.preparePost(getTargetUrl()).setHeaders(h).setBody(sb.toString()).execute(new AsyncCompletionHandler<Response>() {
// @Override
// public Response onCompleted(Response response) throws Throwable {
// return response;
// }
// 
// @Override
// public void onThrowable(Throwable t) {
// }
// }).get();
// 
// assertEquals(response.getStatusCode(), 200);
// assertEquals(response.getHeader("X-" + CONTENT_TYPE), APPLICATION_X_WWW_FORM_URLENCODED);
// }
// }
@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"), "/");
    }
}
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 48 with Response

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

the class ProxyTest method testProxyActivationProperty.

@Test(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 {
            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 49 with Response

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

the class ProxyTest method testRequestNonProxyHost.

@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"), "/");
    }
}
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 50 with Response

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

the class ProxyTest method testIgnoreProxyPropertiesByDefault.

@Test(enabled = false)
public void testIgnoreProxyPropertiesByDefault() throws IOException, 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) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Aggregations

Response (org.asynchttpclient.Response)142 Test (org.testng.annotations.Test)85 AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)79 AbstractBasicTest (org.asynchttpclient.AbstractBasicTest)55 HttpServletResponse (javax.servlet.http.HttpServletResponse)42 BoundRequestBuilder (org.asynchttpclient.BoundRequestBuilder)32 Request (org.asynchttpclient.Request)29 IOException (java.io.IOException)21 Test (org.junit.Test)20 ExecutionException (java.util.concurrent.ExecutionException)16 PubSubMessage (com.yahoo.bullet.pubsub.PubSubMessage)14 RESTPubSubTest.getOkResponse (com.yahoo.bullet.pubsub.rest.RESTPubSubTest.getOkResponse)11 TimeoutException (java.util.concurrent.TimeoutException)11 RequestBuilder (org.asynchttpclient.RequestBuilder)11 UnitEvent (org.apache.druid.java.util.emitter.service.UnitEvent)10 RESTPubSubTest.getNotOkResponse (com.yahoo.bullet.pubsub.rest.RESTPubSubTest.getNotOkResponse)9 UnsupportedEncodingException (java.io.UnsupportedEncodingException)9 Map (java.util.Map)9 AuthorizationException (org.apache.shiro.authz.AuthorizationException)9 TestUtils.createTempFile (org.asynchttpclient.test.TestUtils.createTempFile)9