Search in sources :

Example 56 with Response

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

the class SimpleAsyncHttpClientTest method testMultiPartPost.

@Test
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 57 with Response

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

the class SimpleAsyncHttpClientTest method testDeriveOverrideURL.

@Test
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 58 with Response

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

the class SimpleAsyncHttpClientTest method testCloseDerivedValidMaster.

@Test
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 59 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
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 60 with Response

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

the class SimpleAsyncHttpClientTest method inputStreamBodyConsumerTest.

@Test
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)

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