use of org.asynchttpclient.AsyncHttpClient in project async-http-client by AsyncHttpClient.
the class ZeroCopyFileTest method zeroCopyFileTest.
@Test(groups = "standalone")
public void zeroCopyFileTest() throws IOException, ExecutionException, TimeoutException, InterruptedException, URISyntaxException {
File tmp = new File(System.getProperty("java.io.tmpdir") + File.separator + "zeroCopy.txt");
tmp.deleteOnExit();
try (AsyncHttpClient client = asyncHttpClient()) {
try (FileOutputStream stream = new FileOutputStream(tmp)) {
Response resp = client.preparePost("http://localhost:" + port1 + "/").setBody(SIMPLE_TEXT_FILE).execute(new AsyncHandler<Response>() {
public void onThrowable(Throwable t) {
}
public State onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
stream.write(bodyPart.getBodyPartBytes());
return State.CONTINUE;
}
public State onStatusReceived(HttpResponseStatus responseStatus) throws Exception {
return State.CONTINUE;
}
public State onHeadersReceived(HttpResponseHeaders headers) throws Exception {
return State.CONTINUE;
}
public Response onCompleted() throws Exception {
return null;
}
}).get();
assertNull(resp);
assertEquals(SIMPLE_TEXT_FILE.length(), tmp.length());
}
}
}
use of org.asynchttpclient.AsyncHttpClient in project async-http-client by AsyncHttpClient.
the class ZeroCopyFileTest method zeroCopyPutTest.
@Test(groups = "standalone")
public void zeroCopyPutTest() throws IOException, ExecutionException, TimeoutException, InterruptedException, URISyntaxException {
try (AsyncHttpClient client = asyncHttpClient()) {
Future<Response> f = client.preparePut("http://localhost:" + port1 + "/").setBody(SIMPLE_TEXT_FILE).execute();
Response resp = f.get();
assertNotNull(resp);
assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
assertEquals(resp.getResponseBody(), SIMPLE_TEXT_FILE_STRING);
}
}
use of org.asynchttpclient.AsyncHttpClient in project async-http-client by AsyncHttpClient.
the class ZeroCopyFileTest method zeroCopyFileWithBodyManipulationTest.
@Test(groups = "standalone")
public void zeroCopyFileWithBodyManipulationTest() throws IOException, ExecutionException, TimeoutException, InterruptedException, URISyntaxException {
File tmp = new File(System.getProperty("java.io.tmpdir") + File.separator + "zeroCopy.txt");
tmp.deleteOnExit();
try (AsyncHttpClient client = asyncHttpClient()) {
try (FileOutputStream stream = new FileOutputStream(tmp)) {
Response resp = client.preparePost("http://localhost:" + port1 + "/").setBody(SIMPLE_TEXT_FILE).execute(new AsyncHandler<Response>() {
public void onThrowable(Throwable t) {
}
public State onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
stream.write(bodyPart.getBodyPartBytes());
if (bodyPart.getBodyPartBytes().length == 0) {
return State.ABORT;
}
return State.CONTINUE;
}
public State onStatusReceived(HttpResponseStatus responseStatus) throws Exception {
return State.CONTINUE;
}
public State onHeadersReceived(HttpResponseHeaders headers) throws Exception {
return State.CONTINUE;
}
public Response onCompleted() throws Exception {
return null;
}
}).get();
assertNull(resp);
assertEquals(SIMPLE_TEXT_FILE.length(), tmp.length());
}
}
}
use of org.asynchttpclient.AsyncHttpClient in project async-http-client by AsyncHttpClient.
the class ReactiveStreamsTest method cancelStreamedResponseTest.
@Test(groups = "standalone")
public void cancelStreamedResponseTest() throws Throwable {
try (AsyncHttpClient c = asyncHttpClient()) {
// Cancel immediately
c.preparePost(getTargetUrl()).setBody(LARGE_IMAGE_BYTES).execute(new CancellingStreamedAsyncProvider(0)).get();
// Cancel after 1 element
c.preparePost(getTargetUrl()).setBody(LARGE_IMAGE_BYTES).execute(new CancellingStreamedAsyncProvider(1)).get();
// Cancel after 10 elements
c.preparePost(getTargetUrl()).setBody(LARGE_IMAGE_BYTES).execute(new CancellingStreamedAsyncProvider(10)).get();
// Make sure a regular request works
assertEquals(c.preparePost(getTargetUrl()).setBody("Hello").execute().get().getResponseBody(), "Hello");
}
}
use of org.asynchttpclient.AsyncHttpClient in project async-http-client by AsyncHttpClient.
the class ReactiveStreamsTest method testConnectionDoesNotGetClosed.
@Test(groups = "standalone")
public void testConnectionDoesNotGetClosed() throws Exception {
// test that we can stream the same request multiple times
try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000))) {
BoundRequestBuilder requestBuilder = client.preparePut(getTargetUrl()).setBody(LARGE_IMAGE_PUBLISHER);
Response response = requestBuilder.execute().get();
assertEquals(response.getStatusCode(), 200);
assertEquals(response.getResponseBodyAsBytes(), LARGE_IMAGE_BYTES);
response = requestBuilder.execute().get();
assertEquals(response.getStatusCode(), 200);
assertEquals(response.getResponseBodyAsBytes(), LARGE_IMAGE_BYTES);
}
}
Aggregations