Search in sources :

Example 1 with InputStreamBodyGenerator

use of org.asynchttpclient.request.body.generator.InputStreamBodyGenerator in project async-http-client by AsyncHttpClient.

the class SimpleAsyncHttpClientTest method inputStreamBodyConsumerTest.

@Test(groups = "standalone")
public void inputStreamBodyConsumerTest() throws Exception {
    try (SimpleAsyncHttpClient client = //
    new SimpleAsyncHttpClient.Builder().setPooledConnectionIdleTimeout(//
    100).setMaxConnections(//
    50).setRequestTimeout(//
    5 * 60 * 1000).setUrl(//
    getTargetUrl()).setHeader("Content-Type", "text/html").build()) {
        Future<Response> future = client.post(new InputStreamBodyGenerator(new ByteArrayInputStream(MY_MESSAGE.getBytes())));
        Response response = future.get();
        assertEquals(response.getStatusCode(), 200);
        assertEquals(response.getResponseBody(), MY_MESSAGE);
    }
}
Also used : Response(org.asynchttpclient.Response) InputStreamBodyGenerator(org.asynchttpclient.request.body.generator.InputStreamBodyGenerator) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 2 with InputStreamBodyGenerator

use of org.asynchttpclient.request.body.generator.InputStreamBodyGenerator in project async-http-client by AsyncHttpClient.

the class SimpleAsyncHttpClientTest method testSimpleTransferListener.

@Test(groups = "standalone")
public void testSimpleTransferListener() throws Exception {
    final List<Error> errors = Collections.synchronizedList(new ArrayList<>());
    SimpleAHCTransferListener listener = new SimpleAHCTransferListener() {

        public void onStatus(Uri uri, int statusCode, String statusText) {
            try {
                assertEquals(statusCode, 200);
                assertEquals(uri.toUrl(), getTargetUrl());
            } catch (Error e) {
                errors.add(e);
                throw e;
            }
        }

        public void onHeaders(Uri uri, HttpHeaders headers) {
            try {
                assertEquals(uri.toUrl(), getTargetUrl());
                assertNotNull(headers);
                assertTrue(!headers.isEmpty());
                assertEquals(headers.get("X-Custom"), "custom");
            } catch (Error e) {
                errors.add(e);
                throw e;
            }
        }

        public void onCompleted(Uri uri, int statusCode, String statusText) {
            try {
                assertEquals(statusCode, 200);
                assertEquals(uri.toUrl(), getTargetUrl());
            } catch (Error e) {
                errors.add(e);
                throw e;
            }
        }

        public void onBytesSent(Uri uri, long amount, long current, long total) {
            try {
                assertEquals(uri.toUrl(), getTargetUrl());
            // FIXME Netty bug, see
            // https://github.com/netty/netty/issues/1855
            // assertEquals(total, MY_MESSAGE.getBytes().length);
            } catch (Error e) {
                errors.add(e);
                throw e;
            }
        }

        public void onBytesReceived(Uri uri, long amount, long current, long total) {
            try {
                assertEquals(uri.toUrl(), getTargetUrl());
                assertEquals(total, -1);
            } catch (Error e) {
                errors.add(e);
                throw e;
            }
        }
    };
    try (SimpleAsyncHttpClient client = //
    new SimpleAsyncHttpClient.Builder().setUrl(//
    getTargetUrl()).setHeader("Custom", //
    "custom").setListener(listener).build()) {
        ByteArrayOutputStream o = new ByteArrayOutputStream(10);
        InputStreamBodyGenerator generator = new InputStreamBodyGenerator(new ByteArrayInputStream(MY_MESSAGE.getBytes()));
        OutputStreamBodyConsumer consumer = new OutputStreamBodyConsumer(o);
        Future<Response> future = client.post(generator, consumer);
        Response response = future.get();
        if (!errors.isEmpty()) {
            for (Error e : errors) {
                e.printStackTrace();
            }
            throw errors.get(0);
        }
        assertEquals(response.getStatusCode(), 200);
        assertEquals(o.toString(), MY_MESSAGE);
    }
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) InputStreamBodyGenerator(org.asynchttpclient.request.body.generator.InputStreamBodyGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Uri(org.asynchttpclient.uri.Uri) Response(org.asynchttpclient.Response) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 3 with InputStreamBodyGenerator

use of org.asynchttpclient.request.body.generator.InputStreamBodyGenerator in project async-http-client by AsyncHttpClient.

the class SimpleAsyncHttpClientTest method testDeriveOverrideURL.

@Test(groups = "standalone")
public void testDeriveOverrideURL() throws Exception {
    try (SimpleAsyncHttpClient client = new SimpleAsyncHttpClient.Builder().setUrl("http://invalid.url").build()) {
        ByteArrayOutputStream o = new ByteArrayOutputStream(10);
        InputStreamBodyGenerator generator = new InputStreamBodyGenerator(new ByteArrayInputStream(MY_MESSAGE.getBytes()));
        OutputStreamBodyConsumer consumer = new OutputStreamBodyConsumer(o);
        try (SimpleAsyncHttpClient derived = client.derive().setUrl(getTargetUrl()).build()) {
            Future<Response> future = derived.post(generator, consumer);
            Response response = future.get();
            assertEquals(response.getStatusCode(), 200);
            assertEquals(o.toString(), MY_MESSAGE);
        }
    }
}
Also used : Response(org.asynchttpclient.Response) InputStreamBodyGenerator(org.asynchttpclient.request.body.generator.InputStreamBodyGenerator) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 4 with InputStreamBodyGenerator

use of org.asynchttpclient.request.body.generator.InputStreamBodyGenerator 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 5 with InputStreamBodyGenerator

use of org.asynchttpclient.request.body.generator.InputStreamBodyGenerator 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)

Aggregations

InputStreamBodyGenerator (org.asynchttpclient.request.body.generator.InputStreamBodyGenerator)11 ByteArrayInputStream (java.io.ByteArrayInputStream)9 Response (org.asynchttpclient.Response)8 Test (org.testng.annotations.Test)8 AbstractBasicTest (org.asynchttpclient.AbstractBasicTest)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)2 RequestBuilder (org.asynchttpclient.RequestBuilder)2 FileBodyGenerator (org.asynchttpclient.request.body.generator.FileBodyGenerator)2 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)1 File (java.io.File)1 Serializable (java.io.Serializable)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Charset (java.nio.charset.Charset)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1