Search in sources :

Example 6 with BoundRequestBuilder

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

the class BodyDeferringAsyncHandlerTest method deferredInputStreamTrick.

@Test(groups = "standalone")
public void deferredInputStreamTrick() throws IOException, ExecutionException, TimeoutException, InterruptedException {
    try (AsyncHttpClient client = asyncHttpClient(getAsyncHttpClientConfig())) {
        BoundRequestBuilder r = client.prepareGet("http://localhost:" + port1 + "/deferredInputStreamTrick");
        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 {
            copy(is, cos);
        } finally {
            is.close();
            cos.close();
        }
        // now we don't need to be polite, since consuming and closing
        // BodyDeferringInputStream does all.
        // 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) PipedOutputStream(java.io.PipedOutputStream) BodyDeferringInputStream(org.asynchttpclient.handler.BodyDeferringAsyncHandler.BodyDeferringInputStream) PipedInputStream(java.io.PipedInputStream) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 7 with BoundRequestBuilder

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

the class BodyDeferringAsyncHandlerTest method deferredSimpleWithFailure.

@Test(groups = "standalone", expectedExceptions = RemotelyClosedException.class)
public void deferredSimpleWithFailure() throws Throwable {
    try (AsyncHttpClient client = asyncHttpClient(getAsyncHttpClientConfig())) {
        BoundRequestBuilder r = client.prepareGet("http://localhost:" + port1 + "/deferredSimpleWithFailure").addHeader("X-FAIL-TRANSFER", Boolean.TRUE.toString());
        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);
        // dropping the "line" on server)
        try {
            f.get();
        } catch (ExecutionException e) {
            // good
            // it's incomplete, there was an error
            assertNotEquals(cos.getByteCount(), CONTENT_LENGTH_VALUE);
            throw e.getCause();
        }
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) BoundRequestBuilder(org.asynchttpclient.BoundRequestBuilder) ExecutionException(java.util.concurrent.ExecutionException) 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