Search in sources :

Example 16 with AsyncCompletionHandlerAdapter

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

the class BasicHttpTest method getWithCookies.

@Test
public void getWithCookies() throws Throwable {
    withClient().run(client -> {
        withServer(server).run(server -> {
            final Cookie coo = new DefaultCookie("foo", "value");
            coo.setDomain("/");
            coo.setPath("/");
            server.enqueueEcho();
            client.prepareGet(getTargetUrl()).addCookie(coo).execute(new AsyncCompletionHandlerAdapter() {

                @Override
                public Response onCompleted(Response response) throws Exception {
                    assertEquals(response.getStatusCode(), 200);
                    List<Cookie> cookies = response.getCookies();
                    assertEquals(cookies.size(), 1);
                    assertEquals(cookies.get(0).toString(), "foo=value");
                    return response;
                }
            }).get(TIMEOUT, SECONDS);
        });
    });
}
Also used : Cookie(io.netty.handler.codec.http.cookie.Cookie) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) HttpServletResponse(javax.servlet.http.HttpServletResponse) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) AsyncCompletionHandlerAdapter(org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter) Test(org.testng.annotations.Test) HttpTest(org.asynchttpclient.testserver.HttpTest)

Example 17 with AsyncCompletionHandlerAdapter

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

the class BasicHttpTest method connectionIsReusedForSequentialRequests.

@Test
public void connectionIsReusedForSequentialRequests() throws Throwable {
    withClient().run(client -> {
        withServer(server).run(server -> {
            final CountDownLatch l = new CountDownLatch(2);
            AsyncCompletionHandler<Response> handler = new AsyncCompletionHandlerAdapter() {

                volatile String clientPort;

                @Override
                public Response onCompleted(Response response) throws Exception {
                    try {
                        assertEquals(response.getStatusCode(), 200);
                        if (clientPort == null) {
                            clientPort = response.getHeader("X-ClientPort");
                        } else {
                            assertEquals(response.getHeader("X-ClientPort"), clientPort);
                        }
                    } finally {
                        l.countDown();
                    }
                    return response;
                }
            };
            server.enqueueEcho();
            client.prepareGet(getTargetUrl()).execute(handler).get(TIMEOUT, SECONDS);
            server.enqueueEcho();
            client.prepareGet(getTargetUrl()).execute(handler);
            if (!l.await(TIMEOUT, SECONDS)) {
                fail("Timed out");
            }
        });
    });
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncCompletionHandlerAdapter(org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test) HttpTest(org.asynchttpclient.testserver.HttpTest)

Example 18 with AsyncCompletionHandlerAdapter

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

the class BasicHttpTest method cancellingFutureNotifiesOnThrowableWithCancellationException.

@Test
public void cancellingFutureNotifiesOnThrowableWithCancellationException() throws Throwable {
    withClient().run(client -> {
        withServer(server).run(server -> {
            HttpHeaders h = new DefaultHttpHeaders();
            h.add(CONTENT_TYPE, HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED);
            h.add("X-Delay", 2_000);
            CountDownLatch latch = new CountDownLatch(1);
            Future<Response> future = client.preparePost(getTargetUrl()).setHeaders(h).setBody("Body").execute(new AsyncCompletionHandlerAdapter() {

                @Override
                public void onThrowable(Throwable t) {
                    if (t instanceof CancellationException) {
                        latch.countDown();
                    }
                }
            });
            future.cancel(true);
            if (!latch.await(TIMEOUT, SECONDS)) {
                fail("Timed out");
            }
        });
    });
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) AsyncCompletionHandlerAdapter(org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) CancellationException(java.util.concurrent.CancellationException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test) HttpTest(org.asynchttpclient.testserver.HttpTest)

Example 19 with AsyncCompletionHandlerAdapter

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

the class BasicHttpTest method configTimeoutNotifiesOnThrowableAndFuture.

@Test(expectedExceptions = TimeoutException.class)
public void configTimeoutNotifiesOnThrowableAndFuture() throws Throwable {
    withClient(config().setRequestTimeout(1_000)).run(client -> {
        withServer(server).run(server -> {
            HttpHeaders headers = new DefaultHttpHeaders();
            headers.add("X-Delay", 5_000);
            final AtomicBoolean onCompletedWasNotified = new AtomicBoolean();
            final AtomicBoolean onThrowableWasNotifiedWithTimeoutException = new AtomicBoolean();
            final CountDownLatch latch = new CountDownLatch(1);
            server.enqueueEcho();
            Future<Response> whenResponse = client.prepareGet(getTargetUrl()).setHeaders(headers).execute(new AsyncCompletionHandlerAdapter() {

                @Override
                public Response onCompleted(Response response) throws Exception {
                    onCompletedWasNotified.set(true);
                    latch.countDown();
                    return response;
                }

                @Override
                public void onThrowable(Throwable t) {
                    onThrowableWasNotifiedWithTimeoutException.set(t instanceof TimeoutException);
                    latch.countDown();
                }
            });
            if (!latch.await(TIMEOUT, SECONDS)) {
                fail("Timed out");
            }
            assertFalse(onCompletedWasNotified.get());
            assertTrue(onThrowableWasNotifiedWithTimeoutException.get());
            try {
                whenResponse.get(TIMEOUT, SECONDS);
            } catch (ExecutionException e) {
                throw e.getCause();
            }
        });
    });
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AsyncCompletionHandlerAdapter(org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionException(java.util.concurrent.ExecutionException) ServletException(javax.servlet.ServletException) TimeoutException(java.util.concurrent.TimeoutException) ConnectException(java.net.ConnectException) CancellationException(java.util.concurrent.CancellationException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException) SSLException(javax.net.ssl.SSLException) MaxRedirectException(org.asynchttpclient.handler.MaxRedirectException) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test) HttpTest(org.asynchttpclient.testserver.HttpTest)

Example 20 with AsyncCompletionHandlerAdapter

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

the class BasicHttpTest method exceptionInOnCompletedGetNotifiedToFuture.

@Test(expectedExceptions = IllegalStateException.class)
public void exceptionInOnCompletedGetNotifiedToFuture() throws Throwable {
    withClient().run(client -> {
        withServer(server).run(server -> {
            server.enqueueOk();
            Future<Response> whenResponse = client.prepareGet(getTargetUrl()).execute(new AsyncCompletionHandlerAdapter() {

                @Override
                public Response onCompleted(Response response) throws Exception {
                    throw unknownStackTrace(new IllegalStateException("FOO"), BasicHttpTest.class, "exceptionInOnCompletedGetNotifiedToFuture");
                }

                @Override
                public void onThrowable(Throwable t) {
                }
            });
            try {
                whenResponse.get(TIMEOUT, SECONDS);
            } catch (ExecutionException e) {
                throw e.getCause();
            }
        });
    });
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncCompletionHandlerAdapter(org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter) ExecutionException(java.util.concurrent.ExecutionException) ServletException(javax.servlet.ServletException) TimeoutException(java.util.concurrent.TimeoutException) ConnectException(java.net.ConnectException) CancellationException(java.util.concurrent.CancellationException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException) SSLException(javax.net.ssl.SSLException) MaxRedirectException(org.asynchttpclient.handler.MaxRedirectException) Test(org.testng.annotations.Test) HttpTest(org.asynchttpclient.testserver.HttpTest)

Aggregations

AsyncCompletionHandlerAdapter (org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter)26 HttpTest (org.asynchttpclient.testserver.HttpTest)26 Test (org.testng.annotations.Test)26 HttpServletResponse (javax.servlet.http.HttpServletResponse)25 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)11 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)11 CountDownLatch (java.util.concurrent.CountDownLatch)6 IOException (java.io.IOException)5 ServletException (javax.servlet.ServletException)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ConnectException (java.net.ConnectException)4 CancellationException (java.util.concurrent.CancellationException)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 UnknownHostException (java.net.UnknownHostException)3 ExecutionException (java.util.concurrent.ExecutionException)3 TimeoutException (java.util.concurrent.TimeoutException)3 SSLException (javax.net.ssl.SSLException)3 MaxRedirectException (org.asynchttpclient.handler.MaxRedirectException)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 EchoHandler (org.asynchttpclient.testserver.HttpServer.EchoHandler)2