Search in sources :

Example 41 with Response

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

the class BodyChunkTest method negativeContentTypeTest.

@Test(groups = "standalone")
public void negativeContentTypeTest() throws Exception {
    AsyncHttpClientConfig config = //
    config().setConnectTimeout(//
    100).setMaxConnections(//
    50).setRequestTimeout(// 5 minutes
    5 * 60 * 1000).build();
    try (AsyncHttpClient client = asyncHttpClient(config)) {
        RequestBuilder requestBuilder = //
        post(getTargetUrl()).setHeader("Content-Type", //
        "message/rfc822").setBody(new InputStreamBodyGenerator(new ByteArrayInputStream(MY_MESSAGE.getBytes())));
        Future<Response> future = client.executeRequest(requestBuilder.build());
        System.out.println("waiting for response");
        Response response = future.get();
        assertEquals(response.getStatusCode(), 200);
        assertEquals(response.getResponseBody(), MY_MESSAGE);
    }
}
Also used : Response(org.asynchttpclient.Response) RequestBuilder(org.asynchttpclient.RequestBuilder) InputStreamBodyGenerator(org.asynchttpclient.request.body.generator.InputStreamBodyGenerator) ByteArrayInputStream(java.io.ByteArrayInputStream) AsyncHttpClientConfig(org.asynchttpclient.AsyncHttpClientConfig) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest) Test(org.testng.annotations.Test)

Example 42 with Response

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

the class ChunkingTest method doTestWithInputStreamBodyGenerator.

public void doTestWithInputStreamBodyGenerator(InputStream is) throws Throwable {
    try (AsyncHttpClient c = asyncHttpClient(httpClientBuilder())) {
        RequestBuilder builder = post(getTargetUrl()).setBody(new InputStreamBodyGenerator(is));
        Request r = builder.build();
        final ListenableFuture<Response> responseFuture = c.executeRequest(r);
        waitForAndAssertResponse(responseFuture);
    }
}
Also used : Response(org.asynchttpclient.Response) RequestBuilder(org.asynchttpclient.RequestBuilder) InputStreamBodyGenerator(org.asynchttpclient.request.body.generator.InputStreamBodyGenerator) Request(org.asynchttpclient.Request) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient)

Example 43 with Response

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

the class ChunkingTest method waitForAndAssertResponse.

private void waitForAndAssertResponse(ListenableFuture<Response> responseFuture) throws InterruptedException, java.util.concurrent.ExecutionException, IOException {
    Response response = responseFuture.get();
    if (500 == response.getStatusCode()) {
        StringBuilder sb = new StringBuilder();
        sb.append("==============\n");
        sb.append("500 response from call\n");
        sb.append("Headers:" + response.getHeaders() + "\n");
        sb.append("==============\n");
        logger.debug(sb.toString());
        assertEquals(response.getStatusCode(), 500, "Should have 500 status code");
        assertTrue(response.getHeader("X-Exception").contains("invalid.chunk.length"), "Should have failed due to chunking");
        fail("HARD Failing the test due to provided InputStreamBodyGenerator, chunking incorrectly:" + response.getHeader("X-Exception"));
    } else {
        assertEquals(response.getResponseBodyAsBytes(), LARGE_IMAGE_BYTES);
    }
}
Also used : Response(org.asynchttpclient.Response)

Example 44 with Response

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

the class EmptyBodyTest method testPutEmptyBody.

@Test(groups = "standalone")
public void testPutEmptyBody() throws Exception {
    try (AsyncHttpClient ahc = asyncHttpClient()) {
        Response response = ahc.preparePut(getTargetUrl()).setBody("String").execute().get();
        assertNotNull(response);
        assertEquals(response.getStatusCode(), 204);
        assertEquals(response.getResponseBody(), "");
        assertTrue(response.getResponseBodyAsStream() instanceof InputStream);
        assertEquals(response.getResponseBodyAsStream().read(), -1);
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) InputStream(java.io.InputStream) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 45 with Response

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

the class FastUnauthorizedUploadTest method testUnauthorizedWhileUploading.

@Test(groups = "standalone")
public void testUnauthorizedWhileUploading() throws Exception {
    File file = createTempFile(1024 * 1024);
    try (AsyncHttpClient client = asyncHttpClient()) {
        Response response = client.preparePut(getTargetUrl()).addBodyPart(new FilePart("test", file, "application/octet-stream", UTF_8)).execute().get();
        assertEquals(response.getStatusCode(), 401);
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) File(java.io.File) TestUtils.createTempFile(org.asynchttpclient.test.TestUtils.createTempFile) FilePart(org.asynchttpclient.request.body.multipart.FilePart) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Aggregations

Response (org.asynchttpclient.Response)90 Test (org.testng.annotations.Test)78 AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)71 AbstractBasicTest (org.asynchttpclient.AbstractBasicTest)69 HttpServletResponse (javax.servlet.http.HttpServletResponse)42 Request (org.asynchttpclient.Request)14 RequestBuilder (org.asynchttpclient.RequestBuilder)14 IOException (java.io.IOException)12 ExecutionException (java.util.concurrent.ExecutionException)12 File (java.io.File)10 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 InputStreamBodyGenerator (org.asynchttpclient.request.body.generator.InputStreamBodyGenerator)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 AsyncHttpClientConfig (org.asynchttpclient.AsyncHttpClientConfig)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 TestSubscriber (rx.observers.TestSubscriber)6 ArrayList (java.util.ArrayList)5