Search in sources :

Example 36 with Response

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

the class PutLargeFileTest method testPutLargeFile.

@Test(groups = "standalone")
public void testPutLargeFile() throws Exception {
    File file = createTempFile(1024 * 1024);
    int timeout = (int) file.length() / 1000;
    try (AsyncHttpClient client = asyncHttpClient(config().setConnectTimeout(timeout))) {
        Response response = client.preparePut(getTargetUrl()).setBody(file).execute().get();
        assertEquals(response.getStatusCode(), 200);
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) 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 37 with Response

use of org.asynchttpclient.Response 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 38 with Response

use of org.asynchttpclient.Response 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 39 with Response

use of org.asynchttpclient.Response 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 40 with Response

use of org.asynchttpclient.Response 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

Response (org.asynchttpclient.Response)90 Test (org.testng.annotations.Test)78 AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)71 AbstractBasicTest (org.asynchttpclient.AbstractBasicTest)69 HttpServletResponse (javax.servlet.http.HttpServletResponse)42 Request (org.asynchttpclient.Request)14 RequestBuilder (org.asynchttpclient.RequestBuilder)14 IOException (java.io.IOException)12 ExecutionException (java.util.concurrent.ExecutionException)12 File (java.io.File)10 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 InputStreamBodyGenerator (org.asynchttpclient.request.body.generator.InputStreamBodyGenerator)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 AsyncHttpClientConfig (org.asynchttpclient.AsyncHttpClientConfig)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 TestSubscriber (rx.observers.TestSubscriber)6 ArrayList (java.util.ArrayList)5