Search in sources :

Example 21 with AsyncCompletionHandlerAdapter

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

the class BasicHttpTest method futureTimeOutThrowsTimeoutException.

@Test(expectedExceptions = TimeoutException.class)
public void futureTimeOutThrowsTimeoutException() throws Throwable {
    withClient().run(client -> {
        withServer(server).run(server -> {
            HttpHeaders headers = new DefaultHttpHeaders();
            headers.add("X-Delay", 5_000);
            server.enqueueEcho();
            Future<Response> future = client.prepareGet(getTargetUrl()).setHeaders(headers).execute(new AsyncCompletionHandlerAdapter() {

                @Override
                public void onThrowable(Throwable t) {
                }
            });
            future.get(2, SECONDS);
        });
    });
}
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) Test(org.testng.annotations.Test) HttpTest(org.asynchttpclient.testserver.HttpTest)

Example 22 with AsyncCompletionHandlerAdapter

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

the class BasicHttpTest method putFormParametersAsBodyStream.

@Test
public void putFormParametersAsBodyStream() throws Throwable {
    withClient().run(client -> {
        withServer(server).run(server -> {
            HttpHeaders h = new DefaultHttpHeaders();
            h.add(CONTENT_TYPE, HttpHeaderValues.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);
            ByteArrayInputStream is = new ByteArrayInputStream(sb.toString().getBytes());
            server.enqueueEcho();
            client.preparePut(getTargetUrl()).setHeaders(h).setBody(is).execute(new AsyncCompletionHandlerAdapter() {

                @Override
                public Response onCompleted(Response response) throws Exception {
                    assertEquals(response.getStatusCode(), 200);
                    for (int i = 1; i < 5; i++) {
                        assertEquals(response.getHeader("X-param_" + i), "value_" + i);
                    }
                    return response;
                }
            }).get(TIMEOUT, SECONDS);
        });
    });
}
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) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.testng.annotations.Test) HttpTest(org.asynchttpclient.testserver.HttpTest)

Example 23 with AsyncCompletionHandlerAdapter

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

the class BasicHttpTest method cancelledFutureThrowsCancellationException.

@Test(expectedExceptions = CancellationException.class)
public void cancelledFutureThrowsCancellationException() throws Throwable {
    withClient().run(client -> {
        withServer(server).run(server -> {
            HttpHeaders headers = new DefaultHttpHeaders();
            headers.add("X-Delay", 5_000);
            server.enqueueEcho();
            Future<Response> future = client.prepareGet(getTargetUrl()).setHeaders(headers).execute(new AsyncCompletionHandlerAdapter() {

                @Override
                public void onThrowable(Throwable t) {
                }
            });
            future.cancel(true);
            future.get(TIMEOUT, SECONDS);
        });
    });
}
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) Test(org.testng.annotations.Test) HttpTest(org.asynchttpclient.testserver.HttpTest)

Example 24 with AsyncCompletionHandlerAdapter

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

the class BasicHttpTest method nonBlockingNestedRequetsFromIoThreadAreFine.

@Test
public void nonBlockingNestedRequetsFromIoThreadAreFine() throws Throwable {
    withClient().run(client -> {
        withServer(server).run(server -> {
            final int maxNested = 5;
            final CountDownLatch latch = new CountDownLatch(2);
            final AsyncCompletionHandlerAdapter handler = new AsyncCompletionHandlerAdapter() {

                private AtomicInteger nestedCount = new AtomicInteger(0);

                @Override
                public Response onCompleted(Response response) throws Exception {
                    try {
                        if (nestedCount.getAndIncrement() < maxNested) {
                            client.prepareGet(getTargetUrl()).execute(this);
                        }
                    } finally {
                        latch.countDown();
                    }
                    return response;
                }
            };
            for (int i = 0; i < maxNested + 1; i++) {
                server.enqueueOk();
            }
            client.prepareGet(getTargetUrl()).execute(handler);
            if (!latch.await(TIMEOUT, SECONDS)) {
                fail("Timed out");
            }
        });
    });
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncCompletionHandlerAdapter(org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test) HttpTest(org.asynchttpclient.testserver.HttpTest)

Example 25 with AsyncCompletionHandlerAdapter

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

the class BasicHttpTest method postFormParametersAsBodyString.

@Test
public void postFormParametersAsBodyString() throws Throwable {
    withClient().run(client -> {
        withServer(server).run(server -> {
            HttpHeaders h = new DefaultHttpHeaders();
            h.add(CONTENT_TYPE, HttpHeaderValues.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);
            server.enqueueEcho();
            client.preparePost(getTargetUrl()).setHeaders(h).setBody(sb.toString()).execute(new AsyncCompletionHandlerAdapter() {

                @Override
                public Response onCompleted(Response response) throws Exception {
                    assertEquals(response.getStatusCode(), 200);
                    for (int i = 1; i < 5; i++) {
                        assertEquals(response.getHeader("X-param_" + i), "value_" + i);
                    }
                    return response;
                }
            }).get(TIMEOUT, SECONDS);
        });
    });
}
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) 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