Search in sources :

Example 61 with Response

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

the class AsyncHttpClientCallTest method responseWithBody.

private Response responseWithBody(String contentType, String content) {
    Response response = aResponse();
    when(response.hasResponseBody()).thenReturn(true);
    when(response.getContentType()).thenReturn(contentType);
    when(response.getResponseBodyAsBytes()).thenReturn(content.getBytes(StandardCharsets.UTF_8));
    return response;
}
Also used : Response(org.asynchttpclient.Response)

Example 62 with Response

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

the class AsyncHttpObservableTest method testObserveMultiple.

@Test
public void testObserveMultiple() {
    final TestSubscriber<Response> tester = new TestSubscriber<>();
    try (AsyncHttpClient client = asyncHttpClient()) {
        Observable<Response> o1 = AsyncHttpObservable.observe(() -> client.prepareGet("https://gatling.io"));
        Observable<Response> o2 = AsyncHttpObservable.observe(() -> client.prepareGet("http://www.wisc.edu").setFollowRedirect(true));
        Observable<Response> o3 = AsyncHttpObservable.observe(() -> client.prepareGet("http://www.umn.edu").setFollowRedirect(true));
        Observable<Response> all = Observable.merge(o1, o2, o3);
        all.subscribe(tester);
        tester.awaitTerminalEvent();
        tester.assertTerminalEvent();
        tester.assertNoErrors();
        tester.assertCompleted();
        List<Response> responses = tester.getOnNextEvents();
        assertNotNull(responses);
        assertEquals(responses.size(), 3);
        for (Response response : responses) {
            assertEquals(response.getStatusCode(), 200);
        }
    } catch (Exception e) {
        Thread.currentThread().interrupt();
    }
}
Also used : Response(org.asynchttpclient.Response) TestSubscriber(rx.observers.TestSubscriber) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test)

Example 63 with Response

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

the class MultipartUploadTest method sendFileInputStream.

private void sendFileInputStream(boolean useContentLength, boolean disableZeroCopy) throws Exception {
    File file = getClasspathFile("textfile.txt");
    try (AsyncHttpClient c = asyncHttpClient(config().setDisableZeroCopy(disableZeroCopy))) {
        InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
        InputStreamPart part;
        if (useContentLength) {
            part = new InputStreamPart("file", inputStream, file.getName(), file.length());
        } else {
            part = new InputStreamPart("file", inputStream, file.getName());
        }
        Request r = post("http://localhost" + ":" + port1 + "/upload").addBodyPart(part).build();
        Response res = c.executeRequest(r).get();
        assertEquals(res.getStatusCode(), 200);
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) GZIPInputStream(java.util.zip.GZIPInputStream) Request(org.asynchttpclient.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) TestUtils.getClasspathFile(org.asynchttpclient.test.TestUtils.getClasspathFile) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient)

Example 64 with Response

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

the class MultipartUploadTest method sendEmptyFile0.

private void sendEmptyFile0(boolean disableZeroCopy) throws Exception {
    File file = getClasspathFile("empty.txt");
    try (AsyncHttpClient c = asyncHttpClient(config().setDisableZeroCopy(disableZeroCopy))) {
        Request r = post("http://localhost" + ":" + port1 + "/upload").addBodyPart(new FilePart("file", file, "text/plain", UTF_8)).build();
        Response res = c.executeRequest(r).get();
        assertEquals(res.getStatusCode(), 200);
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) Request(org.asynchttpclient.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) TestUtils.getClasspathFile(org.asynchttpclient.test.TestUtils.getClasspathFile) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient)

Example 65 with Response

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

the class InputStreamPartLargeFileTest method testPutLargeTextFileUnknownSize.

@Test
public void testPutLargeTextFileUnknownSize() 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(), -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) TestUtils.createTempFile(org.asynchttpclient.test.TestUtils.createTempFile) 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