Search in sources :

Example 21 with Response

use of org.asynchttpclient.Response 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 22 with Response

use of org.asynchttpclient.Response 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 23 with Response

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

the class ResumableAsyncHandlerTest method testOnStatusReceivedOkStatusWithDecoratedAsyncHandler.

@Test
public void testOnStatusReceivedOkStatusWithDecoratedAsyncHandler() throws Exception {
    HttpResponseStatus mockResponseStatus = mock(HttpResponseStatus.class);
    when(mockResponseStatus.getStatusCode()).thenReturn(200);
    when(mockResponseStatus.getUri()).thenReturn(mock(Uri.class));
    @SuppressWarnings("unchecked") AsyncHandler<Response> decoratedAsyncHandler = mock(AsyncHandler.class);
    State mockState = mock(State.class);
    when(decoratedAsyncHandler.onStatusReceived(mockResponseStatus)).thenReturn(mockState);
    ResumableAsyncHandler handler = new ResumableAsyncHandler(decoratedAsyncHandler);
    State state = handler.onStatusReceived(mockResponseStatus);
    verify(decoratedAsyncHandler).onStatusReceived(mockResponseStatus);
    assertEquals(state, mockState, "State returned should be equal to the one returned from decoratedAsyncHandler");
}
Also used : Response(org.asynchttpclient.Response) HttpResponseStatus(org.asynchttpclient.HttpResponseStatus) State(org.asynchttpclient.AsyncHandler.State) Uri(org.asynchttpclient.uri.Uri) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 24 with Response

use of org.asynchttpclient.Response 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)

Example 25 with Response

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

the class ConnectionPoolTest method testMaxTotalConnectionsException.

@Test(groups = "standalone", expectedExceptions = TooManyConnectionsException.class)
public void testMaxTotalConnectionsException() throws Throwable {
    try (AsyncHttpClient client = asyncHttpClient(config().setKeepAlive(true).setMaxConnections(1))) {
        String url = getTargetUrl();
        List<ListenableFuture<Response>> futures = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            logger.info("{} requesting url [{}]...", i, url);
            futures.add(client.prepareGet(url).execute());
        }
        Exception exception = null;
        for (ListenableFuture<Response> future : futures) {
            try {
                future.get();
            } catch (Exception ex) {
                exception = ex;
                break;
            }
        }
        assertNotNull(exception);
        throw exception.getCause();
    }
}
Also used : Response(org.asynchttpclient.Response) ArrayList(java.util.ArrayList) ListenableFuture(org.asynchttpclient.ListenableFuture) TooManyConnectionsException(org.asynchttpclient.exception.TooManyConnectionsException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Aggregations

Response (org.asynchttpclient.Response)90 Test (org.testng.annotations.Test)78 AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)71 AbstractBasicTest (org.asynchttpclient.AbstractBasicTest)69 HttpServletResponse (javax.servlet.http.HttpServletResponse)42 Request (org.asynchttpclient.Request)14 RequestBuilder (org.asynchttpclient.RequestBuilder)14 IOException (java.io.IOException)12 ExecutionException (java.util.concurrent.ExecutionException)12 File (java.io.File)10 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 InputStreamBodyGenerator (org.asynchttpclient.request.body.generator.InputStreamBodyGenerator)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 AsyncHttpClientConfig (org.asynchttpclient.AsyncHttpClientConfig)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 TestSubscriber (rx.observers.TestSubscriber)6 ArrayList (java.util.ArrayList)5