Search in sources :

Example 11 with AsyncCompletionHandlerAdapter

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

the class BasicHttpTest method postFormParametersAsBodyStream.

@Test
public void postFormParametersAsBodyStream() 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(new ByteArrayInputStream(sb.toString().getBytes(StandardCharsets.UTF_8))).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 12 with AsyncCompletionHandlerAdapter

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

the class BasicHttpTest method postSingleStringPart.

@Test
public void postSingleStringPart() throws Throwable {
    withClient().run(client -> {
        withServer(server).run(server -> {
            server.enqueueEcho();
            client.preparePost(getTargetUrl()).addBodyPart(new StringPart("foo", "bar")).execute(new AsyncCompletionHandlerAdapter() {

                @Override
                public Response onCompleted(Response response) throws Exception {
                    String requestContentType = response.getHeader("X-" + CONTENT_TYPE);
                    String boundary = requestContentType.substring((requestContentType.indexOf("boundary") + "boundary".length() + 1));
                    assertTrue(response.getResponseBody().regionMatches(false, "--".length(), boundary, 0, boundary.length()));
                    return response;
                }
            }).get(TIMEOUT, SECONDS);
        });
    });
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncCompletionHandlerAdapter(org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter) StringPart(org.asynchttpclient.request.body.multipart.StringPart) Test(org.testng.annotations.Test) HttpTest(org.asynchttpclient.testserver.HttpTest)

Example 13 with AsyncCompletionHandlerAdapter

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

the class BasicHttpTest method getUrlWithPathWithQuery.

@Test
public void getUrlWithPathWithQuery() throws Throwable {
    withClient().run(client -> {
        withServer(server).run(server -> {
            String targetUrl = getTargetUrl() + "?q=+%20x";
            Request request = get(targetUrl).build();
            assertEquals(request.getUrl(), targetUrl);
            server.enqueueOk();
            Response response = client.executeRequest(request, new AsyncCompletionHandlerAdapter()).get(TIMEOUT, SECONDS);
            assertEquals(response.getUri().toUrl(), targetUrl);
        });
    });
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncCompletionHandlerAdapter(org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter) HttpServletRequest(javax.servlet.http.HttpServletRequest) Test(org.testng.annotations.Test) HttpTest(org.asynchttpclient.testserver.HttpTest)

Example 14 with AsyncCompletionHandlerAdapter

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

the class BasicHttpTest method postInputStreamWithContentLengthAsBodyGenerator.

@Test
public void postInputStreamWithContentLengthAsBodyGenerator() 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 {
                    assertNull(request.getHeader(TRANSFER_ENCODING.toString()));
                    assertEquals(request.getHeader(CONTENT_LENGTH.toString()), Integer.toString("{}".getBytes(StandardCharsets.ISO_8859_1).length));
                    chain.handle(target, request, httpServletRequest, httpServletResponse);
                }
            });
            byte[] bodyBytes = "{}".getBytes(StandardCharsets.ISO_8859_1);
            InputStream bodyStream = new ByteArrayInputStream(bodyBytes);
            client.preparePost(getTargetUrl()).setHeaders(h).setBody(new InputStreamBodyGenerator(bodyStream, bodyBytes.length)).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) InputStreamBodyGenerator(org.asynchttpclient.request.body.generator.InputStreamBodyGenerator) AsyncCompletionHandlerAdapter(org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) 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 15 with AsyncCompletionHandlerAdapter

use of org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter 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)

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