Search in sources :

Example 61 with AsyncHttpClient

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

the class BodyDeferringAsyncHandlerTest method testConnectionRefused.

@Test(groups = "standalone", expectedExceptions = IOException.class)
public void testConnectionRefused() throws IOException, ExecutionException, TimeoutException, InterruptedException {
    int newPortWithoutAnyoneListening = findFreePort();
    try (AsyncHttpClient client = asyncHttpClient(getAsyncHttpClientConfig())) {
        BoundRequestBuilder r = client.prepareGet("http://localhost:" + newPortWithoutAnyoneListening + "/testConnectionRefused");
        CountingOutputStream cos = new CountingOutputStream();
        BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler(cos);
        r.execute(bdah);
        bdah.getResponse();
    }
}
Also used : BoundRequestBuilder(org.asynchttpclient.BoundRequestBuilder) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 62 with AsyncHttpClient

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

the class BodyDeferringAsyncHandlerTest method deferredSimple.

@Test(groups = "standalone")
public void deferredSimple() throws IOException, ExecutionException, TimeoutException, InterruptedException {
    try (AsyncHttpClient client = asyncHttpClient(getAsyncHttpClientConfig())) {
        BoundRequestBuilder r = client.prepareGet("http://localhost:" + port1 + "/deferredSimple");
        CountingOutputStream cos = new CountingOutputStream();
        BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler(cos);
        Future<Response> f = r.execute(bdah);
        Response resp = bdah.getResponse();
        assertNotNull(resp);
        assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
        assertEquals(resp.getHeader(CONTENT_LENGTH), String.valueOf(CONTENT_LENGTH_VALUE));
        // we got headers only, it's probably not all yet here (we have BIG file
        // downloading)
        assertTrue(cos.getByteCount() <= CONTENT_LENGTH_VALUE);
        // now be polite and wait for body arrival too (otherwise we would be
        // dropping the "line" on server)
        f.get();
        // it all should be here now
        assertEquals(cos.getByteCount(), CONTENT_LENGTH_VALUE);
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) BoundRequestBuilder(org.asynchttpclient.BoundRequestBuilder) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 63 with AsyncHttpClient

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

the class InputStreamTest method testInvalidInputStream.

@Test(groups = "standalone")
public void testInvalidInputStream() throws IOException, ExecutionException, TimeoutException, InterruptedException {
    try (AsyncHttpClient c = asyncHttpClient()) {
        HttpHeaders h = new DefaultHttpHeaders().add(CONTENT_TYPE, HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED);
        InputStream is = new InputStream() {

            public int readAllowed;

            @Override
            public int available() {
                // Fake
                return 1;
            }

            @Override
            public int read() throws IOException {
                int fakeCount = readAllowed++;
                if (fakeCount == 0) {
                    return (int) 'a';
                } else if (fakeCount == 1) {
                    return (int) 'b';
                } else if (fakeCount == 2) {
                    return (int) 'c';
                } else {
                    return -1;
                }
            }
        };
        Response resp = c.preparePost(getTargetUrl()).setHeaders(h).setBody(is).execute().get();
        assertNotNull(resp);
        assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
        assertEquals(resp.getHeader("X-Param"), "abc");
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) InputStream(java.io.InputStream) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 64 with AsyncHttpClient

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

the class PutLargeFileTest method testPutSmallFile.

@Test(groups = "standalone")
public void testPutSmallFile() throws Exception {
    File file = createTempFile(1024);
    try (AsyncHttpClient client = asyncHttpClient()) {
        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 65 with AsyncHttpClient

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

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