Search in sources :

Example 1 with ByteArrayPart

use of org.asynchttpclient.request.body.multipart.ByteArrayPart 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 2 with ByteArrayPart

use of org.asynchttpclient.request.body.multipart.ByteArrayPart in project async-http-client by AsyncHttpClient.

the class SimpleAsyncHttpClientTest method testMultiPartPut.

@Test(groups = "standalone")
public void testMultiPartPut() throws Exception {
    try (SimpleAsyncHttpClient client = new SimpleAsyncHttpClient.Builder().setUrl(getTargetUrl() + "/multipart").build()) {
        Response response = client.put(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)

Aggregations

AbstractBasicTest (org.asynchttpclient.AbstractBasicTest)2 Response (org.asynchttpclient.Response)2 ByteArrayPart (org.asynchttpclient.request.body.multipart.ByteArrayPart)2 Test (org.testng.annotations.Test)2