use of org.asynchttpclient.handler.TransferListener 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());
}
}
use of org.asynchttpclient.handler.TransferListener in project async-http-client by AsyncHttpClient.
the class TransferListenerTest method basicPutFileTest.
@Test(groups = "standalone")
public void basicPutFileTest() throws Exception {
final AtomicReference<Throwable> throwable = new AtomicReference<>();
final AtomicReference<HttpHeaders> hSent = new AtomicReference<>();
final AtomicReference<HttpHeaders> hRead = new AtomicReference<>();
final AtomicInteger bbReceivedLenght = new AtomicInteger(0);
final AtomicLong bbSentLenght = new AtomicLong(0L);
final AtomicBoolean completed = new AtomicBoolean(false);
File file = createTempFile(1024 * 100 * 10);
int timeout = (int) (file.length() / 1000);
try (AsyncHttpClient client = asyncHttpClient(config().setConnectTimeout(timeout))) {
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) {
bbReceivedLenght.addAndGet(b.length);
}
public void onBytesSent(long amount, long current, long total) {
bbSentLenght.addAndGet(amount);
}
public void onRequestResponseCompleted() {
completed.set(true);
}
public void onThrowable(Throwable t) {
throwable.set(t);
}
});
Response response = client.preparePut(getTargetUrl()).setBody(file).execute(tl).get();
assertNotNull(response);
assertEquals(response.getStatusCode(), 200);
assertNotNull(hRead.get());
assertNotNull(hSent.get());
assertEquals(bbReceivedLenght.get(), file.length(), "Number of received bytes incorrect");
assertEquals(bbSentLenght.get(), file.length(), "Number of sent bytes incorrect");
}
}
use of org.asynchttpclient.handler.TransferListener in project async-http-client by AsyncHttpClient.
the class TransferListenerTest method basicPutFileBodyGeneratorTest.
@Test(groups = "standalone")
public void basicPutFileBodyGeneratorTest() throws Exception {
try (AsyncHttpClient client = asyncHttpClient()) {
final AtomicReference<Throwable> throwable = new AtomicReference<>();
final AtomicReference<HttpHeaders> hSent = new AtomicReference<>();
final AtomicReference<HttpHeaders> hRead = new AtomicReference<>();
final AtomicInteger bbReceivedLenght = new AtomicInteger(0);
final AtomicLong bbSentLenght = new AtomicLong(0L);
final AtomicBoolean completed = new AtomicBoolean(false);
File file = createTempFile(1024 * 100 * 10);
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) {
bbReceivedLenght.addAndGet(b.length);
}
public void onBytesSent(long amount, long current, long total) {
bbSentLenght.addAndGet(amount);
}
public void onRequestResponseCompleted() {
completed.set(true);
}
public void onThrowable(Throwable t) {
throwable.set(t);
}
});
Response response = client.preparePut(getTargetUrl()).setBody(new FileBodyGenerator(file)).execute(tl).get();
assertNotNull(response);
assertEquals(response.getStatusCode(), 200);
assertNotNull(hRead.get());
assertNotNull(hSent.get());
assertEquals(bbReceivedLenght.get(), file.length(), "Number of received bytes incorrect");
assertEquals(bbSentLenght.get(), file.length(), "Number of sent bytes incorrect");
}
}
Aggregations