Search in sources :

Example 41 with DefaultHttpHeaders

use of io.netty.handler.codec.http.DefaultHttpHeaders in project async-http-client by AsyncHttpClient.

the class BasicHttpTest method postWithHeadersAndFormParams.

@Test
public void postWithHeadersAndFormParams() throws Throwable {
    withClient().run(client -> {
        withServer(server).run(server -> {
            HttpHeaders h = new DefaultHttpHeaders();
            h.add(CONTENT_TYPE, HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED);
            Map<String, List<String>> m = new HashMap<>();
            for (int i = 0; i < 5; i++) {
                m.put("param_" + i, Arrays.asList("value_" + i));
            }
            Request request = post(getTargetUrl()).setHeaders(h).setFormParams(m).build();
            server.enqueueEcho();
            client.executeRequest(request, 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) HashMap(java.util.HashMap) HttpServletRequest(javax.servlet.http.HttpServletRequest) List(java.util.List) Test(org.testng.annotations.Test) HttpTest(org.asynchttpclient.testserver.HttpTest)

Example 42 with DefaultHttpHeaders

use of io.netty.handler.codec.http.DefaultHttpHeaders 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 43 with DefaultHttpHeaders

use of io.netty.handler.codec.http.DefaultHttpHeaders 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 44 with DefaultHttpHeaders

use of io.netty.handler.codec.http.DefaultHttpHeaders 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 45 with DefaultHttpHeaders

use of io.netty.handler.codec.http.DefaultHttpHeaders 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)

Aggregations

DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)49 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)36 Test (org.testng.annotations.Test)20 Test (org.junit.Test)18 Cookie (io.netty.handler.codec.http.cookie.Cookie)14 DefaultCookie (io.netty.handler.codec.http.cookie.DefaultCookie)12 HttpServletResponse (javax.servlet.http.HttpServletResponse)12 HttpTest (org.asynchttpclient.testserver.HttpTest)12 AsyncCompletionHandlerAdapter (org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter)11 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)9 Charset (java.nio.charset.Charset)8 HttpResponseHeaders (org.asynchttpclient.HttpResponseHeaders)6 HttpRequest (io.netty.handler.codec.http.HttpRequest)5 RequestInfo (com.nike.riposte.server.http.RequestInfo)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Map (java.util.Map)4 ByteBuf (io.netty.buffer.ByteBuf)3 Channel (io.netty.channel.Channel)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3