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