Search in sources :

Example 66 with Response

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

the class InputStreamPartLargeFileTest method testPutImageFileUnknownSize.

@Test
public void testPutImageFileUnknownSize() throws Exception {
    try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000))) {
        InputStream inputStream = new BufferedInputStream(new FileInputStream(LARGE_IMAGE_FILE));
        Response response = client.preparePut(getTargetUrl()).addBodyPart(new InputStreamPart("test", inputStream, LARGE_IMAGE_FILE.getName(), -1, "application/octet-stream", UTF_8)).execute().get();
        assertEquals(response.getStatusCode(), 200);
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) ServletInputStream(javax.servlet.ServletInputStream) InputStreamPart(org.asynchttpclient.request.body.multipart.InputStreamPart) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 67 with Response

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

the class InputStreamPartLargeFileTest method testPutLargeTextFile.

@Test
public void testPutLargeTextFile() throws Exception {
    File file = createTempFile(1024 * 1024);
    InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
    try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000))) {
        Response response = client.preparePut(getTargetUrl()).addBodyPart(new InputStreamPart("test", inputStream, file.getName(), file.length(), "application/octet-stream", UTF_8)).execute().get();
        assertEquals(response.getStatusCode(), 200);
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) ServletInputStream(javax.servlet.ServletInputStream) InputStreamPart(org.asynchttpclient.request.body.multipart.InputStreamPart) TestUtils.createTempFile(org.asynchttpclient.test.TestUtils.createTempFile) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 68 with Response

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

the class AsyncHttpTest method testPromiseAdapter.

public void testPromiseAdapter() throws IOException {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicInteger successCount = new AtomicInteger();
    final AtomicInteger progressCount = new AtomicInteger();
    try (AsyncHttpClient client = asyncHttpClient()) {
        Promise<Response, Throwable, HttpProgress> p1 = AsyncHttpDeferredObject.promise(client.prepareGet("http://gatling.io"));
        p1.done(new DoneCallback<Response>() {

            @Override
            public void onDone(Response response) {
                try {
                    assertEquals(response.getStatusCode(), 200);
                    successCount.incrementAndGet();
                } finally {
                    latch.countDown();
                }
            }
        }).progress(new ProgressCallback<HttpProgress>() {

            @Override
            public void onProgress(HttpProgress progress) {
                progressCount.incrementAndGet();
            }
        });
        latch.await();
        assertTrue(progressCount.get() > 0);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
}
Also used : Response(org.asynchttpclient.Response) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DoneCallback(org.jdeferred.DoneCallback) HttpProgress(org.asynchttpclient.extras.jdeferred.HttpProgress) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient)

Example 69 with Response

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

the class AsyncHttpTest method testMultiplePromiseAdapter.

public void testMultiplePromiseAdapter() throws IOException {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicInteger successCount = new AtomicInteger();
    try (AsyncHttpClient client = asyncHttpClient()) {
        Promise<Response, Throwable, HttpProgress> p1 = AsyncHttpDeferredObject.promise(client.prepareGet("http://gatling.io"));
        Promise<Response, Throwable, HttpProgress> p2 = AsyncHttpDeferredObject.promise(client.prepareGet("http://www.google.com"));
        AsyncHttpDeferredObject deferredRequest = new AsyncHttpDeferredObject(client.prepareGet("http://jdeferred.org"));
        deferredManager.when(p1, p2, deferredRequest).then(new DoneCallback<MultipleResults>() {

            @Override
            public void onDone(MultipleResults result) {
                try {
                    assertEquals(result.size(), 3);
                    assertEquals(Response.class.cast(result.get(0).getResult()).getStatusCode(), 200);
                    assertEquals(Response.class.cast(result.get(1).getResult()).getStatusCode(), 200);
                    assertEquals(Response.class.cast(result.get(2).getResult()).getStatusCode(), 200);
                    successCount.incrementAndGet();
                } finally {
                    latch.countDown();
                }
            }
        });
        latch.await();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
}
Also used : Response(org.asynchttpclient.Response) AsyncHttpDeferredObject(org.asynchttpclient.extras.jdeferred.AsyncHttpDeferredObject) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpProgress(org.asynchttpclient.extras.jdeferred.HttpProgress) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) MultipleResults(org.jdeferred.multiple.MultipleResults)

Example 70 with Response

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

the class InputStreamTest method testInvalidInputStream.

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

            int readAllowed;

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

            @Override
            public int read() {
                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)

Aggregations

Response (org.asynchttpclient.Response)142 Test (org.testng.annotations.Test)85 AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)79 AbstractBasicTest (org.asynchttpclient.AbstractBasicTest)55 HttpServletResponse (javax.servlet.http.HttpServletResponse)42 BoundRequestBuilder (org.asynchttpclient.BoundRequestBuilder)32 Request (org.asynchttpclient.Request)29 IOException (java.io.IOException)21 Test (org.junit.Test)20 ExecutionException (java.util.concurrent.ExecutionException)16 PubSubMessage (com.yahoo.bullet.pubsub.PubSubMessage)14 RESTPubSubTest.getOkResponse (com.yahoo.bullet.pubsub.rest.RESTPubSubTest.getOkResponse)11 TimeoutException (java.util.concurrent.TimeoutException)11 RequestBuilder (org.asynchttpclient.RequestBuilder)11 UnitEvent (org.apache.druid.java.util.emitter.service.UnitEvent)10 RESTPubSubTest.getNotOkResponse (com.yahoo.bullet.pubsub.rest.RESTPubSubTest.getNotOkResponse)9 UnsupportedEncodingException (java.io.UnsupportedEncodingException)9 Map (java.util.Map)9 AuthorizationException (org.apache.shiro.authz.AuthorizationException)9 TestUtils.createTempFile (org.asynchttpclient.test.TestUtils.createTempFile)9