Search in sources :

Example 66 with AsyncHttpClient

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

the class ZeroCopyFileTest method zeroCopyFileTest.

@Test(groups = "standalone")
public void zeroCopyFileTest() throws IOException, ExecutionException, TimeoutException, InterruptedException, URISyntaxException {
    File tmp = new File(System.getProperty("java.io.tmpdir") + File.separator + "zeroCopy.txt");
    tmp.deleteOnExit();
    try (AsyncHttpClient client = asyncHttpClient()) {
        try (FileOutputStream stream = new FileOutputStream(tmp)) {
            Response resp = client.preparePost("http://localhost:" + port1 + "/").setBody(SIMPLE_TEXT_FILE).execute(new AsyncHandler<Response>() {

                public void onThrowable(Throwable t) {
                }

                public State onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
                    stream.write(bodyPart.getBodyPartBytes());
                    return State.CONTINUE;
                }

                public State onStatusReceived(HttpResponseStatus responseStatus) throws Exception {
                    return State.CONTINUE;
                }

                public State onHeadersReceived(HttpResponseHeaders headers) throws Exception {
                    return State.CONTINUE;
                }

                public Response onCompleted() throws Exception {
                    return null;
                }
            }).get();
            assertNull(resp);
            assertEquals(SIMPLE_TEXT_FILE.length(), tmp.length());
        }
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) AsyncHandler(org.asynchttpclient.AsyncHandler) HttpResponseHeaders(org.asynchttpclient.HttpResponseHeaders) HttpResponseStatus(org.asynchttpclient.HttpResponseStatus) FileOutputStream(java.io.FileOutputStream) File(java.io.File) HttpResponseBodyPart(org.asynchttpclient.HttpResponseBodyPart) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) BasicHttpsTest(org.asynchttpclient.BasicHttpsTest) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 67 with AsyncHttpClient

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

the class ZeroCopyFileTest method zeroCopyPutTest.

@Test(groups = "standalone")
public void zeroCopyPutTest() throws IOException, ExecutionException, TimeoutException, InterruptedException, URISyntaxException {
    try (AsyncHttpClient client = asyncHttpClient()) {
        Future<Response> f = client.preparePut("http://localhost:" + port1 + "/").setBody(SIMPLE_TEXT_FILE).execute();
        Response resp = f.get();
        assertNotNull(resp);
        assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
        assertEquals(resp.getResponseBody(), SIMPLE_TEXT_FILE_STRING);
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) BasicHttpsTest(org.asynchttpclient.BasicHttpsTest) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 68 with AsyncHttpClient

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

the class ZeroCopyFileTest method zeroCopyFileWithBodyManipulationTest.

@Test(groups = "standalone")
public void zeroCopyFileWithBodyManipulationTest() throws IOException, ExecutionException, TimeoutException, InterruptedException, URISyntaxException {
    File tmp = new File(System.getProperty("java.io.tmpdir") + File.separator + "zeroCopy.txt");
    tmp.deleteOnExit();
    try (AsyncHttpClient client = asyncHttpClient()) {
        try (FileOutputStream stream = new FileOutputStream(tmp)) {
            Response resp = client.preparePost("http://localhost:" + port1 + "/").setBody(SIMPLE_TEXT_FILE).execute(new AsyncHandler<Response>() {

                public void onThrowable(Throwable t) {
                }

                public State onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
                    stream.write(bodyPart.getBodyPartBytes());
                    if (bodyPart.getBodyPartBytes().length == 0) {
                        return State.ABORT;
                    }
                    return State.CONTINUE;
                }

                public State onStatusReceived(HttpResponseStatus responseStatus) throws Exception {
                    return State.CONTINUE;
                }

                public State onHeadersReceived(HttpResponseHeaders headers) throws Exception {
                    return State.CONTINUE;
                }

                public Response onCompleted() throws Exception {
                    return null;
                }
            }).get();
            assertNull(resp);
            assertEquals(SIMPLE_TEXT_FILE.length(), tmp.length());
        }
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) AsyncHandler(org.asynchttpclient.AsyncHandler) HttpResponseHeaders(org.asynchttpclient.HttpResponseHeaders) HttpResponseStatus(org.asynchttpclient.HttpResponseStatus) FileOutputStream(java.io.FileOutputStream) File(java.io.File) HttpResponseBodyPart(org.asynchttpclient.HttpResponseBodyPart) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) BasicHttpsTest(org.asynchttpclient.BasicHttpsTest) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 69 with AsyncHttpClient

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

the class ReactiveStreamsTest method cancelStreamedResponseTest.

@Test(groups = "standalone")
public void cancelStreamedResponseTest() throws Throwable {
    try (AsyncHttpClient c = asyncHttpClient()) {
        // Cancel immediately
        c.preparePost(getTargetUrl()).setBody(LARGE_IMAGE_BYTES).execute(new CancellingStreamedAsyncProvider(0)).get();
        // Cancel after 1 element
        c.preparePost(getTargetUrl()).setBody(LARGE_IMAGE_BYTES).execute(new CancellingStreamedAsyncProvider(1)).get();
        // Cancel after 10 elements
        c.preparePost(getTargetUrl()).setBody(LARGE_IMAGE_BYTES).execute(new CancellingStreamedAsyncProvider(10)).get();
        // Make sure a regular request works
        assertEquals(c.preparePost(getTargetUrl()).setBody("Hello").execute().get().getResponseBody(), "Hello");
    }
}
Also used : AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 70 with AsyncHttpClient

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

the class ReactiveStreamsTest method testConnectionDoesNotGetClosed.

@Test(groups = "standalone")
public void testConnectionDoesNotGetClosed() throws Exception {
    // test that we can stream the same request multiple times
    try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000))) {
        BoundRequestBuilder requestBuilder = client.preparePut(getTargetUrl()).setBody(LARGE_IMAGE_PUBLISHER);
        Response response = requestBuilder.execute().get();
        assertEquals(response.getStatusCode(), 200);
        assertEquals(response.getResponseBodyAsBytes(), LARGE_IMAGE_BYTES);
        response = requestBuilder.execute().get();
        assertEquals(response.getStatusCode(), 200);
        assertEquals(response.getResponseBodyAsBytes(), LARGE_IMAGE_BYTES);
    }
}
Also used : Response(org.asynchttpclient.Response) BoundRequestBuilder(org.asynchttpclient.BoundRequestBuilder) 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