use of io.netty.handler.codec.http.HttpHeaders 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 io.netty.handler.codec.http.HttpHeaders 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");
}
}
use of io.netty.handler.codec.http.HttpHeaders in project async-http-client by AsyncHttpClient.
the class ResumableAsyncHandlerTest method testOnHeadersReceivedContentLengthMinus.
@Test
public void testOnHeadersReceivedContentLengthMinus() throws Exception {
ResumableAsyncHandler handler = new ResumableAsyncHandler();
HttpHeaders responseHeaders = new DefaultHttpHeaders();
responseHeaders.add(CONTENT_LENGTH, -1);
HttpResponseHeaders headers = new HttpResponseHeaders(responseHeaders);
State status = handler.onHeadersReceived(headers);
assertEquals(status, AsyncHandler.State.ABORT, "State should be ABORT for content length -1");
}
use of io.netty.handler.codec.http.HttpHeaders in project async-http-client by AsyncHttpClient.
the class ResumableAsyncHandlerTest method testOnHeadersReceivedWithDecoratedAsyncHandler.
@Test
public void testOnHeadersReceivedWithDecoratedAsyncHandler() throws Exception {
HttpHeaders responseHeaders = new DefaultHttpHeaders();
HttpResponseHeaders headers = new HttpResponseHeaders(responseHeaders);
@SuppressWarnings("unchecked") AsyncHandler<Response> decoratedAsyncHandler = mock(AsyncHandler.class);
State mockState = mock(State.class);
when(decoratedAsyncHandler.onHeadersReceived(headers)).thenReturn(mockState);
ResumableAsyncHandler handler = new ResumableAsyncHandler(decoratedAsyncHandler);
State status = handler.onHeadersReceived(headers);
assertEquals(status, mockState, "State should be equal to the state returned from decoratedAsyncHandler");
}
use of io.netty.handler.codec.http.HttpHeaders in project async-http-client by AsyncHttpClient.
the class RetryNonBlockingIssue method testRetryNonBlocking.
/**
* Tests that a head request can be made
*
* @throws IOException
* @throws ExecutionException
* @throws InterruptedException
*/
@Test(groups = "standalone")
public void testRetryNonBlocking() throws IOException, InterruptedException, ExecutionException {
AsyncHttpClientConfig config = //
config().setKeepAlive(//
true).setMaxConnections(//
100).setConnectTimeout(//
60000).setRequestTimeout(//
30000).build();
try (AsyncHttpClient client = asyncHttpClient(config)) {
List<ListenableFuture<Response>> res = new ArrayList<>();
for (int i = 0; i < 32; i++) {
res.add(testMethodRequest(client, 3, "servlet", UUID.randomUUID().toString()));
}
StringBuilder b = new StringBuilder();
for (ListenableFuture<Response> r : res) {
Response theres = r.get();
assertEquals(200, theres.getStatusCode());
b.append("==============\r\n");
b.append("Response Headers\r\n");
HttpHeaders heads = theres.getHeaders();
b.append(heads + "\r\n");
b.append("==============\r\n");
}
System.out.println(b.toString());
System.out.flush();
}
}
Aggregations