Search in sources :

Example 96 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)

Example 97 with AsyncHttpClient

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

the class TransferListenerTest method basicPutFileTest.

@Test(groups = "standalone")
public void basicPutFileTest() throws Exception {
    final AtomicReference<Throwable> throwable = new AtomicReference<>();
    final AtomicReference<HttpHeaders> hSent = new AtomicReference<>();
    final AtomicReference<HttpHeaders> hRead = new AtomicReference<>();
    final AtomicInteger bbReceivedLenght = new AtomicInteger(0);
    final AtomicLong bbSentLenght = new AtomicLong(0L);
    final AtomicBoolean completed = new AtomicBoolean(false);
    File file = createTempFile(1024 * 100 * 10);
    int timeout = (int) (file.length() / 1000);
    try (AsyncHttpClient client = asyncHttpClient(config().setConnectTimeout(timeout))) {
        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) {
                bbReceivedLenght.addAndGet(b.length);
            }

            public void onBytesSent(long amount, long current, long total) {
                bbSentLenght.addAndGet(amount);
            }

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

            public void onThrowable(Throwable t) {
                throwable.set(t);
            }
        });
        Response response = client.preparePut(getTargetUrl()).setBody(file).execute(tl).get();
        assertNotNull(response);
        assertEquals(response.getStatusCode(), 200);
        assertNotNull(hRead.get());
        assertNotNull(hSent.get());
        assertEquals(bbReceivedLenght.get(), file.length(), "Number of received bytes incorrect");
        assertEquals(bbSentLenght.get(), file.length(), "Number of sent bytes incorrect");
    }
}
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) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) File(java.io.File) TestUtils.createTempFile(org.asynchttpclient.test.TestUtils.createTempFile) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 98 with AsyncHttpClient

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

the class TransferListenerTest method basicPutFileBodyGeneratorTest.

@Test(groups = "standalone")
public void basicPutFileBodyGeneratorTest() throws Exception {
    try (AsyncHttpClient client = asyncHttpClient()) {
        final AtomicReference<Throwable> throwable = new AtomicReference<>();
        final AtomicReference<HttpHeaders> hSent = new AtomicReference<>();
        final AtomicReference<HttpHeaders> hRead = new AtomicReference<>();
        final AtomicInteger bbReceivedLenght = new AtomicInteger(0);
        final AtomicLong bbSentLenght = new AtomicLong(0L);
        final AtomicBoolean completed = new AtomicBoolean(false);
        File file = createTempFile(1024 * 100 * 10);
        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) {
                bbReceivedLenght.addAndGet(b.length);
            }

            public void onBytesSent(long amount, long current, long total) {
                bbSentLenght.addAndGet(amount);
            }

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

            public void onThrowable(Throwable t) {
                throwable.set(t);
            }
        });
        Response response = client.preparePut(getTargetUrl()).setBody(new FileBodyGenerator(file)).execute(tl).get();
        assertNotNull(response);
        assertEquals(response.getStatusCode(), 200);
        assertNotNull(hRead.get());
        assertNotNull(hSent.get());
        assertEquals(bbReceivedLenght.get(), file.length(), "Number of received bytes incorrect");
        assertEquals(bbSentLenght.get(), file.length(), "Number of sent bytes incorrect");
    }
}
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) FileBodyGenerator(org.asynchttpclient.request.body.generator.FileBodyGenerator) HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) File(java.io.File) TestUtils.createTempFile(org.asynchttpclient.test.TestUtils.createTempFile) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 99 with AsyncHttpClient

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

the class ZeroCopyFileTest method zeroCopyPostTest.

@Test(groups = "standalone")
public void zeroCopyPostTest() throws IOException, ExecutionException, TimeoutException, InterruptedException, URISyntaxException {
    try (AsyncHttpClient client = asyncHttpClient()) {
        final AtomicBoolean headerSent = new AtomicBoolean(false);
        final AtomicBoolean operationCompleted = new AtomicBoolean(false);
        Response resp = client.preparePost("http://localhost:" + port1 + "/").setBody(SIMPLE_TEXT_FILE).execute(new AsyncCompletionHandler<Response>() {

            public State onHeadersWritten() {
                headerSent.set(true);
                return State.CONTINUE;
            }

            public State onContentWritten() {
                operationCompleted.set(true);
                return State.CONTINUE;
            }

            @Override
            public Response onCompleted(Response response) throws Exception {
                return response;
            }
        }).get();
        assertNotNull(resp);
        assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
        assertEquals(resp.getResponseBody(), SIMPLE_TEXT_FILE_STRING);
        assertTrue(operationCompleted.get());
        assertTrue(headerSent.get());
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AsyncCompletionHandler(org.asynchttpclient.AsyncCompletionHandler) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) BasicHttpsTest(org.asynchttpclient.BasicHttpsTest) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 100 with AsyncHttpClient

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

the class WebDavBasicTest method mkcolWebDavTest2.

@Test(groups = "standalone")
public void mkcolWebDavTest2() throws InterruptedException, IOException, ExecutionException {
    try (AsyncHttpClient c = asyncHttpClient()) {
        Request mkcolRequest = new RequestBuilder("MKCOL").setUrl(getTargetUrl() + "/folder2").build();
        Response response = c.executeRequest(mkcolRequest).get();
        assertEquals(response.getStatusCode(), 409);
    }
}
Also used : Response(org.asynchttpclient.Response) RequestBuilder(org.asynchttpclient.RequestBuilder) Request(org.asynchttpclient.Request) 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