Search in sources :

Example 1 with BoundRequestBuilder

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

the class AsyncHttpSingleTest method testNewRequestForEachSubscription.

@Test(groups = "standalone")
public void testNewRequestForEachSubscription() throws Exception {
    final BoundRequestBuilder builder = mock(BoundRequestBuilder.class);
    final Single<?> underTest = AsyncHttpSingle.create(builder);
    underTest.subscribe(new TestSubscriber<>());
    underTest.subscribe(new TestSubscriber<>());
    verify(builder, times(2)).execute(any());
    verifyNoMoreInteractions(builder);
}
Also used : BoundRequestBuilder(org.asynchttpclient.BoundRequestBuilder) Test(org.testng.annotations.Test)

Example 2 with BoundRequestBuilder

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

the class BodyDeferringAsyncHandlerTest method deferredInputStreamTrickWithFailure.

@Test(groups = "standalone", expectedExceptions = RemotelyClosedException.class)
public void deferredInputStreamTrickWithFailure() throws Throwable {
    try (AsyncHttpClient client = asyncHttpClient(getAsyncHttpClientConfig())) {
        BoundRequestBuilder r = client.prepareGet("http://localhost:" + port1 + "/deferredInputStreamTrickWithFailure").addHeader("X-FAIL-TRANSFER", Boolean.TRUE.toString());
        PipedOutputStream pos = new PipedOutputStream();
        PipedInputStream pis = new PipedInputStream(pos);
        BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler(pos);
        Future<Response> f = r.execute(bdah);
        BodyDeferringInputStream is = new BodyDeferringInputStream(f, bdah, pis);
        Response resp = is.getAsapResponse();
        assertNotNull(resp);
        assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
        assertEquals(resp.getHeader(CONTENT_LENGTH), String.valueOf(CONTENT_LENGTH_VALUE));
        // "consume" the body, but our code needs input stream
        CountingOutputStream cos = new CountingOutputStream();
        try {
            try {
                copy(is, cos);
            } finally {
                is.close();
                cos.close();
            }
        } catch (IOException e) {
            throw e.getCause();
        }
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) BoundRequestBuilder(org.asynchttpclient.BoundRequestBuilder) PipedOutputStream(java.io.PipedOutputStream) BodyDeferringInputStream(org.asynchttpclient.handler.BodyDeferringAsyncHandler.BodyDeferringInputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 3 with BoundRequestBuilder

use of org.asynchttpclient.BoundRequestBuilder 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 4 with BoundRequestBuilder

use of org.asynchttpclient.BoundRequestBuilder 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 5 with BoundRequestBuilder

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

BoundRequestBuilder (org.asynchttpclient.BoundRequestBuilder)7 Test (org.testng.annotations.Test)7 AbstractBasicTest (org.asynchttpclient.AbstractBasicTest)6 AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)6 Response (org.asynchttpclient.Response)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 PipedInputStream (java.io.PipedInputStream)2 PipedOutputStream (java.io.PipedOutputStream)2 BodyDeferringInputStream (org.asynchttpclient.handler.BodyDeferringAsyncHandler.BodyDeferringInputStream)2 IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1