use of org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter in project async-http-client by AsyncHttpClient.
the class BasicHttpTest method futureTimeOutThrowsTimeoutException.
@Test(expectedExceptions = TimeoutException.class)
public void futureTimeOutThrowsTimeoutException() throws Throwable {
withClient().run(client -> {
withServer(server).run(server -> {
HttpHeaders headers = new DefaultHttpHeaders();
headers.add("X-Delay", 5_000);
server.enqueueEcho();
Future<Response> future = client.prepareGet(getTargetUrl()).setHeaders(headers).execute(new AsyncCompletionHandlerAdapter() {
@Override
public void onThrowable(Throwable t) {
}
});
future.get(2, SECONDS);
});
});
}
use of org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter in project async-http-client by AsyncHttpClient.
the class BasicHttpTest method putFormParametersAsBodyStream.
@Test
public void putFormParametersAsBodyStream() throws Throwable {
withClient().run(client -> {
withServer(server).run(server -> {
HttpHeaders h = new DefaultHttpHeaders();
h.add(CONTENT_TYPE, HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 5; i++) {
sb.append("param_").append(i).append("=value_").append(i).append("&");
}
sb.setLength(sb.length() - 1);
ByteArrayInputStream is = new ByteArrayInputStream(sb.toString().getBytes());
server.enqueueEcho();
client.preparePut(getTargetUrl()).setHeaders(h).setBody(is).execute(new AsyncCompletionHandlerAdapter() {
@Override
public Response onCompleted(Response response) throws Exception {
assertEquals(response.getStatusCode(), 200);
for (int i = 1; i < 5; i++) {
assertEquals(response.getHeader("X-param_" + i), "value_" + i);
}
return response;
}
}).get(TIMEOUT, SECONDS);
});
});
}
use of org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter in project async-http-client by AsyncHttpClient.
the class BasicHttpTest method cancelledFutureThrowsCancellationException.
@Test(expectedExceptions = CancellationException.class)
public void cancelledFutureThrowsCancellationException() throws Throwable {
withClient().run(client -> {
withServer(server).run(server -> {
HttpHeaders headers = new DefaultHttpHeaders();
headers.add("X-Delay", 5_000);
server.enqueueEcho();
Future<Response> future = client.prepareGet(getTargetUrl()).setHeaders(headers).execute(new AsyncCompletionHandlerAdapter() {
@Override
public void onThrowable(Throwable t) {
}
});
future.cancel(true);
future.get(TIMEOUT, SECONDS);
});
});
}
use of org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter in project async-http-client by AsyncHttpClient.
the class BasicHttpTest method nonBlockingNestedRequetsFromIoThreadAreFine.
@Test
public void nonBlockingNestedRequetsFromIoThreadAreFine() throws Throwable {
withClient().run(client -> {
withServer(server).run(server -> {
final int maxNested = 5;
final CountDownLatch latch = new CountDownLatch(2);
final AsyncCompletionHandlerAdapter handler = new AsyncCompletionHandlerAdapter() {
private AtomicInteger nestedCount = new AtomicInteger(0);
@Override
public Response onCompleted(Response response) throws Exception {
try {
if (nestedCount.getAndIncrement() < maxNested) {
client.prepareGet(getTargetUrl()).execute(this);
}
} finally {
latch.countDown();
}
return response;
}
};
for (int i = 0; i < maxNested + 1; i++) {
server.enqueueOk();
}
client.prepareGet(getTargetUrl()).execute(handler);
if (!latch.await(TIMEOUT, SECONDS)) {
fail("Timed out");
}
});
});
}
use of org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter in project async-http-client by AsyncHttpClient.
the class BasicHttpTest method postFormParametersAsBodyString.
@Test
public void postFormParametersAsBodyString() throws Throwable {
withClient().run(client -> {
withServer(server).run(server -> {
HttpHeaders h = new DefaultHttpHeaders();
h.add(CONTENT_TYPE, HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 5; i++) {
sb.append("param_").append(i).append("=value_").append(i).append("&");
}
sb.setLength(sb.length() - 1);
server.enqueueEcho();
client.preparePost(getTargetUrl()).setHeaders(h).setBody(sb.toString()).execute(new AsyncCompletionHandlerAdapter() {
@Override
public Response onCompleted(Response response) throws Exception {
assertEquals(response.getStatusCode(), 200);
for (int i = 1; i < 5; i++) {
assertEquals(response.getHeader("X-param_" + i), "value_" + i);
}
return response;
}
}).get(TIMEOUT, SECONDS);
});
});
}
Aggregations