Search in sources :

Example 1 with AsyncCompletionHandlerAdapter

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

the class BasicHttpTest method getEmptyBody.

@Test
public void getEmptyBody() throws Throwable {
    withClient().run(client -> {
        withServer(server).run(server -> {
            server.enqueueOk();
            Response response = client.prepareGet(getTargetUrl()).execute(new AsyncCompletionHandlerAdapter()).get(TIMEOUT, SECONDS);
            assertTrue(response.getResponseBody().isEmpty());
        });
    });
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncCompletionHandlerAdapter(org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter) Test(org.testng.annotations.Test) HttpTest(org.asynchttpclient.testserver.HttpTest)

Example 2 with AsyncCompletionHandlerAdapter

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

the class BasicHttpTest method getEmptyBodyNotifiesHandler.

@Test
public void getEmptyBodyNotifiesHandler() throws Throwable {
    withClient().run(client -> {
        withServer(server).run(server -> {
            final AtomicBoolean handlerWasNotified = new AtomicBoolean();
            server.enqueueOk();
            client.prepareGet(getTargetUrl()).execute(new AsyncCompletionHandlerAdapter() {

                @Override
                public Response onCompleted(Response response) throws Exception {
                    assertEquals(response.getStatusCode(), 200);
                    handlerWasNotified.set(true);
                    return response;
                }
            }).get(TIMEOUT, SECONDS);
            assertTrue(handlerWasNotified.get());
        });
    });
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AsyncCompletionHandlerAdapter(org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter) Test(org.testng.annotations.Test) HttpTest(org.asynchttpclient.testserver.HttpTest)

Example 3 with AsyncCompletionHandlerAdapter

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

the class BasicHttpTest method exceptionInOnCompletedGetNotifiedToOnThrowable.

@Test
public void exceptionInOnCompletedGetNotifiedToOnThrowable() throws Throwable {
    withClient().run(client -> {
        withServer(server).run(server -> {
            final CountDownLatch latch = new CountDownLatch(1);
            final AtomicReference<String> message = new AtomicReference<>();
            server.enqueueOk();
            client.prepareGet(getTargetUrl()).execute(new AsyncCompletionHandlerAdapter() {

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

                @Override
                public void onThrowable(Throwable t) {
                    message.set(t.getMessage());
                    latch.countDown();
                }
            });
            if (!latch.await(TIMEOUT, SECONDS)) {
                fail("Timed out");
            }
            assertEquals(message.get(), "FOO");
        });
    });
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncCompletionHandlerAdapter(org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) 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)

Example 4 with AsyncCompletionHandlerAdapter

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

the class BasicHttpTest method postUnboundedInputStreamAsBodyStream.

@Test
public void postUnboundedInputStreamAsBodyStream() throws Throwable {
    withClient().run(client -> {
        withServer(server).run(server -> {
            HttpHeaders h = new DefaultHttpHeaders();
            h.add(CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON);
            server.enqueue(new AbstractHandler() {

                EchoHandler chain = new EchoHandler();

                @Override
                public void handle(String target, org.eclipse.jetty.server.Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
                    assertEquals(request.getHeader(TRANSFER_ENCODING.toString()), HttpHeaderValues.CHUNKED.toString());
                    assertNull(request.getHeader(CONTENT_LENGTH.toString()));
                    chain.handle(target, request, httpServletRequest, httpServletResponse);
                }
            });
            server.enqueueEcho();
            client.preparePost(getTargetUrl()).setHeaders(h).setBody(new ByteArrayInputStream("{}".getBytes(StandardCharsets.ISO_8859_1))).execute(new AsyncCompletionHandlerAdapter() {

                @Override
                public Response onCompleted(Response response) throws Exception {
                    assertEquals(response.getStatusCode(), 200);
                    assertEquals(response.getResponseBody(), "{}");
                    return response;
                }
            }).get(TIMEOUT, SECONDS);
        });
    });
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) AsyncCompletionHandlerAdapter(org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpServletResponse(javax.servlet.http.HttpServletResponse) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) ByteArrayInputStream(java.io.ByteArrayInputStream) EchoHandler(org.asynchttpclient.testserver.HttpServer.EchoHandler) Test(org.testng.annotations.Test) HttpTest(org.asynchttpclient.testserver.HttpTest)

Example 5 with AsyncCompletionHandlerAdapter

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

the class BasicHttpTest method connectFailureNotifiesHandlerWithConnectException.

@Test
public void connectFailureNotifiesHandlerWithConnectException() throws Throwable {
    withClient().run(client -> {
        withServer(server).run(server -> {
            final CountDownLatch l = new CountDownLatch(1);
            int port = findFreePort();
            client.prepareGet(String.format("http://localhost:%d/", port)).execute(new AsyncCompletionHandlerAdapter() {

                @Override
                public void onThrowable(Throwable t) {
                    try {
                        assertTrue(t instanceof ConnectException);
                    } finally {
                        l.countDown();
                    }
                }
            });
            if (!l.await(TIMEOUT, SECONDS)) {
                fail("Timed out");
            }
        });
    });
}
Also used : AsyncCompletionHandlerAdapter(org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectException(java.net.ConnectException) 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