Search in sources :

Example 6 with Response

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

the class SimpleAsyncHttpClientTest method testMultiPartPost.

@Test(groups = "standalone")
public void testMultiPartPost() throws Exception {
    try (SimpleAsyncHttpClient client = new SimpleAsyncHttpClient.Builder().setUrl(getTargetUrl() + "/multipart").build()) {
        Response response = client.post(new ByteArrayPart("baPart", "testMultiPart".getBytes(UTF_8), "application/test", UTF_8, "fileName")).get();
        String body = response.getResponseBody();
        String contentType = response.getHeader("X-Content-Type");
        assertTrue(contentType.contains("multipart/form-data"));
        String boundary = contentType.substring(contentType.lastIndexOf("=") + 1);
        assertTrue(body.startsWith("--" + boundary));
        assertTrue(body.trim().endsWith("--" + boundary + "--"));
        assertTrue(body.contains("Content-Disposition:"));
        assertTrue(body.contains("Content-Type: application/test"));
        assertTrue(body.contains("name=\"baPart"));
        assertTrue(body.contains("filename=\"fileName"));
    }
}
Also used : Response(org.asynchttpclient.Response) ByteArrayPart(org.asynchttpclient.request.body.multipart.ByteArrayPart) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 7 with Response

use of org.asynchttpclient.Response 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 8 with Response

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

the class SimpleAsyncHttpClientTest method testPutZeroBytesFileTest.

/**
     * See https://issues.sonatype.org/browse/AHC-5
     */
@Test(groups = "standalone", enabled = true)
public void testPutZeroBytesFileTest() throws Exception {
    try (SimpleAsyncHttpClient client = //
    new SimpleAsyncHttpClient.Builder().setPooledConnectionIdleTimeout(//
    100).setMaxConnections(//
    50).setRequestTimeout(//
    5 * 1000).setUrl(//
    getTargetUrl() + "/testPutZeroBytesFileTest.txt").setHeader("Content-Type", "text/plain").build()) {
        File tmpfile = File.createTempFile("testPutZeroBytesFile", ".tmp");
        tmpfile.deleteOnExit();
        Future<Response> future = client.put(new FileBodyGenerator(tmpfile));
        System.out.println("waiting for response");
        Response response = future.get();
        tmpfile.delete();
        assertEquals(response.getStatusCode(), 200);
    }
}
Also used : Response(org.asynchttpclient.Response) FileBodyGenerator(org.asynchttpclient.request.body.generator.FileBodyGenerator) File(java.io.File) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 9 with Response

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

the class SimpleAsyncHttpClientTest method testCloseDerivedValidMaster.

@Test(groups = "standalone")
public void testCloseDerivedValidMaster() throws Exception {
    try (SimpleAsyncHttpClient client = new SimpleAsyncHttpClient.Builder().setUrl(getTargetUrl()).build()) {
        try (SimpleAsyncHttpClient derived = client.derive().build()) {
            derived.get().get();
        }
        Response response = client.get().get();
        assertEquals(response.getStatusCode(), 200);
    }
}
Also used : Response(org.asynchttpclient.Response) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 10 with Response

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

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