Search in sources :

Example 91 with AsyncHttpClient

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

the class ChunkingTest method doTestWithFeedableBodyGenerator.

public void doTestWithFeedableBodyGenerator(InputStream is) throws Throwable {
    try (AsyncHttpClient c = asyncHttpClient(httpClientBuilder())) {
        final FeedableBodyGenerator feedableBodyGenerator = new UnboundedQueueFeedableBodyGenerator();
        Request r = post(getTargetUrl()).setBody(feedableBodyGenerator).build();
        ListenableFuture<Response> responseFuture = c.executeRequest(r);
        feed(feedableBodyGenerator, is);
        waitForAndAssertResponse(responseFuture);
    }
}
Also used : UnboundedQueueFeedableBodyGenerator(org.asynchttpclient.request.body.generator.UnboundedQueueFeedableBodyGenerator) Response(org.asynchttpclient.Response) FeedableBodyGenerator(org.asynchttpclient.request.body.generator.FeedableBodyGenerator) UnboundedQueueFeedableBodyGenerator(org.asynchttpclient.request.body.generator.UnboundedQueueFeedableBodyGenerator) Request(org.asynchttpclient.Request) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient)

Example 92 with AsyncHttpClient

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

the class EmptyBodyTest method testEmptyBody.

@Test(groups = "standalone")
public void testEmptyBody() throws IOException {
    try (AsyncHttpClient ahc = asyncHttpClient()) {
        final AtomicBoolean err = new AtomicBoolean(false);
        final LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<>();
        final AtomicBoolean status = new AtomicBoolean(false);
        final AtomicInteger headers = new AtomicInteger(0);
        final CountDownLatch latch = new CountDownLatch(1);
        ahc.executeRequest(ahc.prepareGet(getTargetUrl()).build(), new AsyncHandler<Object>() {

            public void onThrowable(Throwable t) {
                fail("Got throwable.", t);
                err.set(true);
            }

            public State onBodyPartReceived(HttpResponseBodyPart e) throws Exception {
                byte[] bytes = e.getBodyPartBytes();
                if (bytes.length != 0) {
                    String s = new String(bytes);
                    logger.info("got part: {}", s);
                    logger.warn("Sampling stacktrace.", new Throwable("trace that, we should not get called for empty body."));
                    queue.put(s);
                }
                return State.CONTINUE;
            }

            public State onStatusReceived(HttpResponseStatus e) throws Exception {
                status.set(true);
                return AsyncHandler.State.CONTINUE;
            }

            public State onHeadersReceived(HttpResponseHeaders e) throws Exception {
                if (headers.incrementAndGet() == 2) {
                    throw new Exception("Analyze this.");
                }
                return State.CONTINUE;
            }

            public Object onCompleted() throws Exception {
                latch.countDown();
                return null;
            }
        });
        try {
            assertTrue(latch.await(1, TimeUnit.SECONDS), "Latch failed.");
        } catch (InterruptedException e) {
            fail("Interrupted.", e);
        }
        assertFalse(err.get());
        assertEquals(queue.size(), 0);
        assertTrue(status.get());
        assertEquals(headers.get(), 1);
    }
}
Also used : HttpResponseHeaders(org.asynchttpclient.HttpResponseHeaders) HttpResponseStatus(org.asynchttpclient.HttpResponseStatus) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpResponseBodyPart(org.asynchttpclient.HttpResponseBodyPart) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 93 with AsyncHttpClient

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

the class FilePartLargeFileTest method testPutLargeTextFile.

@Test(groups = "standalone")
public void testPutLargeTextFile() throws Exception {
    File file = createTempFile(1024 * 1024);
    try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000))) {
        Response response = client.preparePut(getTargetUrl()).addBodyPart(new FilePart("test", file, "application/octet-stream", UTF_8)).execute().get();
        assertEquals(response.getStatusCode(), 200);
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) File(java.io.File) FilePart(org.asynchttpclient.request.body.multipart.FilePart) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 94 with AsyncHttpClient

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

the class FilePartLargeFileTest method testPutImageFile.

@Test(groups = "standalone")
public void testPutImageFile() throws Exception {
    try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000))) {
        Response response = client.preparePut(getTargetUrl()).addBodyPart(new FilePart("test", LARGE_IMAGE_FILE, "application/octet-stream", UTF_8)).execute().get();
        assertEquals(response.getStatusCode(), 200);
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) FilePart(org.asynchttpclient.request.body.multipart.FilePart) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 95 with AsyncHttpClient

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

the class TransferListenerTest method basicGetTest.

@Test(groups = "standalone")
public void basicGetTest() throws Exception {
    try (AsyncHttpClient c = asyncHttpClient()) {
        final AtomicReference<Throwable> throwable = new AtomicReference<>();
        final AtomicReference<HttpHeaders> hSent = new AtomicReference<>();
        final AtomicReference<HttpHeaders> hRead = new AtomicReference<>();
        final AtomicReference<byte[]> bb = new AtomicReference<>();
        final AtomicBoolean completed = new AtomicBoolean(false);
        TransferCompletionHandler tl = new TransferCompletionHandler();
        tl.addTransferListener(new TransferListener() {

            public void onRequestHeadersSent(HttpHeaders headers) {
                hSent.set(headers);
            }

            public void onResponseHeadersReceived(HttpHeaders headers) {
                hRead.set(headers);
            }

            public void onBytesReceived(byte[] b) {
                if (b.length != 0)
                    bb.set(b);
            }

            public void onBytesSent(long amount, long current, long total) {
            }

            public void onRequestResponseCompleted() {
                completed.set(true);
            }

            public void onThrowable(Throwable t) {
                throwable.set(t);
            }
        });
        Response response = c.prepareGet(getTargetUrl()).execute(tl).get();
        assertNotNull(response);
        assertEquals(response.getStatusCode(), 200);
        assertNotNull(hRead.get());
        assertNotNull(hSent.get());
        assertNull(bb.get());
        assertNull(throwable.get());
    }
}
Also used : TransferListener(org.asynchttpclient.handler.TransferListener) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) TransferCompletionHandler(org.asynchttpclient.handler.TransferCompletionHandler) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Aggregations

AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)146 Test (org.testng.annotations.Test)119 Response (org.asynchttpclient.Response)71 AbstractBasicTest (org.asynchttpclient.AbstractBasicTest)66 HttpServletResponse (javax.servlet.http.HttpServletResponse)40 CountDownLatch (java.util.concurrent.CountDownLatch)31 DefaultAsyncHttpClient (org.asynchttpclient.DefaultAsyncHttpClient)26 AtomicReference (java.util.concurrent.atomic.AtomicReference)23 RequestBuilder (org.asynchttpclient.RequestBuilder)16 IOException (java.io.IOException)14 RouteBuilder (org.apache.camel.builder.RouteBuilder)14 ExecutionException (java.util.concurrent.ExecutionException)13 Request (org.asynchttpclient.Request)13 WebSocket (org.asynchttpclient.ws.WebSocket)12 Test (org.junit.Test)11 AsyncHttpClientConfig (org.asynchttpclient.AsyncHttpClientConfig)10 File (java.io.File)9 WebSocketTextListener (org.asynchttpclient.ws.WebSocketTextListener)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7