use of org.asynchttpclient.AsyncHttpClient in project async-http-client by AsyncHttpClient.
the class ChunkingTest method doTestWithFeedableBodyGenerator.
public void doTestWithFeedableBodyGenerator(InputStream is) throws Throwable {
try (AsyncHttpClient c = asyncHttpClient(httpClientBuilder())) {
final FeedableBodyGenerator feedableBodyGenerator = new UnboundedQueueFeedableBodyGenerator();
Request r = post(getTargetUrl()).setBody(feedableBodyGenerator).build();
ListenableFuture<Response> responseFuture = c.executeRequest(r);
feed(feedableBodyGenerator, is);
waitForAndAssertResponse(responseFuture);
}
}
use of org.asynchttpclient.AsyncHttpClient in project async-http-client by AsyncHttpClient.
the class EmptyBodyTest method testEmptyBody.
@Test(groups = "standalone")
public void testEmptyBody() throws IOException {
try (AsyncHttpClient ahc = asyncHttpClient()) {
final AtomicBoolean err = new AtomicBoolean(false);
final LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<>();
final AtomicBoolean status = new AtomicBoolean(false);
final AtomicInteger headers = new AtomicInteger(0);
final CountDownLatch latch = new CountDownLatch(1);
ahc.executeRequest(ahc.prepareGet(getTargetUrl()).build(), new AsyncHandler<Object>() {
public void onThrowable(Throwable t) {
fail("Got throwable.", t);
err.set(true);
}
public State onBodyPartReceived(HttpResponseBodyPart e) throws Exception {
byte[] bytes = e.getBodyPartBytes();
if (bytes.length != 0) {
String s = new String(bytes);
logger.info("got part: {}", s);
logger.warn("Sampling stacktrace.", new Throwable("trace that, we should not get called for empty body."));
queue.put(s);
}
return State.CONTINUE;
}
public State onStatusReceived(HttpResponseStatus e) throws Exception {
status.set(true);
return AsyncHandler.State.CONTINUE;
}
public State onHeadersReceived(HttpResponseHeaders e) throws Exception {
if (headers.incrementAndGet() == 2) {
throw new Exception("Analyze this.");
}
return State.CONTINUE;
}
public Object onCompleted() throws Exception {
latch.countDown();
return null;
}
});
try {
assertTrue(latch.await(1, TimeUnit.SECONDS), "Latch failed.");
} catch (InterruptedException e) {
fail("Interrupted.", e);
}
assertFalse(err.get());
assertEquals(queue.size(), 0);
assertTrue(status.get());
assertEquals(headers.get(), 1);
}
}
use of org.asynchttpclient.AsyncHttpClient in project async-http-client by AsyncHttpClient.
the class FilePartLargeFileTest method testPutLargeTextFile.
@Test(groups = "standalone")
public void testPutLargeTextFile() throws Exception {
File file = createTempFile(1024 * 1024);
try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000))) {
Response response = client.preparePut(getTargetUrl()).addBodyPart(new FilePart("test", file, "application/octet-stream", UTF_8)).execute().get();
assertEquals(response.getStatusCode(), 200);
}
}
use of org.asynchttpclient.AsyncHttpClient in project async-http-client by AsyncHttpClient.
the class FilePartLargeFileTest method testPutImageFile.
@Test(groups = "standalone")
public void testPutImageFile() throws Exception {
try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000))) {
Response response = client.preparePut(getTargetUrl()).addBodyPart(new FilePart("test", LARGE_IMAGE_FILE, "application/octet-stream", UTF_8)).execute().get();
assertEquals(response.getStatusCode(), 200);
}
}
use of org.asynchttpclient.AsyncHttpClient in project async-http-client by AsyncHttpClient.
the class TransferListenerTest method basicGetTest.
@Test(groups = "standalone")
public void basicGetTest() throws Exception {
try (AsyncHttpClient c = asyncHttpClient()) {
final AtomicReference<Throwable> throwable = new AtomicReference<>();
final AtomicReference<HttpHeaders> hSent = new AtomicReference<>();
final AtomicReference<HttpHeaders> hRead = new AtomicReference<>();
final AtomicReference<byte[]> bb = new AtomicReference<>();
final AtomicBoolean completed = new AtomicBoolean(false);
TransferCompletionHandler tl = new TransferCompletionHandler();
tl.addTransferListener(new TransferListener() {
public void onRequestHeadersSent(HttpHeaders headers) {
hSent.set(headers);
}
public void onResponseHeadersReceived(HttpHeaders headers) {
hRead.set(headers);
}
public void onBytesReceived(byte[] b) {
if (b.length != 0)
bb.set(b);
}
public void onBytesSent(long amount, long current, long total) {
}
public void onRequestResponseCompleted() {
completed.set(true);
}
public void onThrowable(Throwable t) {
throwable.set(t);
}
});
Response response = c.prepareGet(getTargetUrl()).execute(tl).get();
assertNotNull(response);
assertEquals(response.getStatusCode(), 200);
assertNotNull(hRead.get());
assertNotNull(hSent.get());
assertNull(bb.get());
assertNull(throwable.get());
}
}
Aggregations