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"));
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
}
Aggregations