Search in sources :

Example 71 with AsyncHttpClient

use of org.asynchttpclient.AsyncHttpClient 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 72 with AsyncHttpClient

use of org.asynchttpclient.AsyncHttpClient 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 73 with AsyncHttpClient

use of org.asynchttpclient.AsyncHttpClient 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 74 with AsyncHttpClient

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

Example 75 with AsyncHttpClient

use of org.asynchttpclient.AsyncHttpClient in project camel by apache.

the class WebsocketComponentRouteExampleTest method testWSHttpCall.

@Test
public void testWSHttpCall() throws Exception {
    AsyncHttpClient c = new DefaultAsyncHttpClient();
    WebSocket websocket = c.prepareGet("ws://localhost:" + port + "/echo").execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketTextListener() {

        @Override
        public void onMessage(String message) {
            received.add(message);
            log.info("received --> " + message);
            latch.countDown();
        }

        @Override
        public void onOpen(WebSocket websocket) {
        }

        @Override
        public void onClose(WebSocket websocket) {
        }

        @Override
        public void onError(Throwable t) {
            t.printStackTrace();
        }
    }).build()).get();
    websocket.sendMessage("Beer");
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertEquals(1, received.size());
    assertEquals("BeerBeer", received.get(0));
    websocket.close();
    c.close();
}
Also used : WebSocketTextListener(org.asynchttpclient.ws.WebSocketTextListener) RouteBuilder(org.apache.camel.builder.RouteBuilder) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) WebSocket(org.asynchttpclient.ws.WebSocket) Test(org.junit.Test)

Aggregations

AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)146 Test (org.testng.annotations.Test)119 Response (org.asynchttpclient.Response)71 AbstractBasicTest (org.asynchttpclient.AbstractBasicTest)66 HttpServletResponse (javax.servlet.http.HttpServletResponse)40 CountDownLatch (java.util.concurrent.CountDownLatch)31 DefaultAsyncHttpClient (org.asynchttpclient.DefaultAsyncHttpClient)26 AtomicReference (java.util.concurrent.atomic.AtomicReference)23 RequestBuilder (org.asynchttpclient.RequestBuilder)16 IOException (java.io.IOException)14 RouteBuilder (org.apache.camel.builder.RouteBuilder)14 ExecutionException (java.util.concurrent.ExecutionException)13 Request (org.asynchttpclient.Request)13 WebSocket (org.asynchttpclient.ws.WebSocket)12 Test (org.junit.Test)11 AsyncHttpClientConfig (org.asynchttpclient.AsyncHttpClientConfig)10 File (java.io.File)9 WebSocketTextListener (org.asynchttpclient.ws.WebSocketTextListener)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7