use of org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter in project async-http-client by AsyncHttpClient.
the class BasicHttpTest method getWithCookies.
@Test
public void getWithCookies() throws Throwable {
withClient().run(client -> {
withServer(server).run(server -> {
final Cookie coo = new DefaultCookie("foo", "value");
coo.setDomain("/");
coo.setPath("/");
server.enqueueEcho();
client.prepareGet(getTargetUrl()).addCookie(coo).execute(new AsyncCompletionHandlerAdapter() {
@Override
public Response onCompleted(Response response) throws Exception {
assertEquals(response.getStatusCode(), 200);
List<Cookie> cookies = response.getCookies();
assertEquals(cookies.size(), 1);
assertEquals(cookies.get(0).toString(), "foo=value");
return response;
}
}).get(TIMEOUT, SECONDS);
});
});
}
use of org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter in project async-http-client by AsyncHttpClient.
the class BasicHttpTest method connectionIsReusedForSequentialRequests.
@Test
public void connectionIsReusedForSequentialRequests() throws Throwable {
withClient().run(client -> {
withServer(server).run(server -> {
final CountDownLatch l = new CountDownLatch(2);
AsyncCompletionHandler<Response> handler = new AsyncCompletionHandlerAdapter() {
volatile String clientPort;
@Override
public Response onCompleted(Response response) throws Exception {
try {
assertEquals(response.getStatusCode(), 200);
if (clientPort == null) {
clientPort = response.getHeader("X-ClientPort");
} else {
assertEquals(response.getHeader("X-ClientPort"), clientPort);
}
} finally {
l.countDown();
}
return response;
}
};
server.enqueueEcho();
client.prepareGet(getTargetUrl()).execute(handler).get(TIMEOUT, SECONDS);
server.enqueueEcho();
client.prepareGet(getTargetUrl()).execute(handler);
if (!l.await(TIMEOUT, SECONDS)) {
fail("Timed out");
}
});
});
}
use of org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter in project async-http-client by AsyncHttpClient.
the class BasicHttpTest method cancellingFutureNotifiesOnThrowableWithCancellationException.
@Test
public void cancellingFutureNotifiesOnThrowableWithCancellationException() throws Throwable {
withClient().run(client -> {
withServer(server).run(server -> {
HttpHeaders h = new DefaultHttpHeaders();
h.add(CONTENT_TYPE, HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED);
h.add("X-Delay", 2_000);
CountDownLatch latch = new CountDownLatch(1);
Future<Response> future = client.preparePost(getTargetUrl()).setHeaders(h).setBody("Body").execute(new AsyncCompletionHandlerAdapter() {
@Override
public void onThrowable(Throwable t) {
if (t instanceof CancellationException) {
latch.countDown();
}
}
});
future.cancel(true);
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 configTimeoutNotifiesOnThrowableAndFuture.
@Test(expectedExceptions = TimeoutException.class)
public void configTimeoutNotifiesOnThrowableAndFuture() throws Throwable {
withClient(config().setRequestTimeout(1_000)).run(client -> {
withServer(server).run(server -> {
HttpHeaders headers = new DefaultHttpHeaders();
headers.add("X-Delay", 5_000);
final AtomicBoolean onCompletedWasNotified = new AtomicBoolean();
final AtomicBoolean onThrowableWasNotifiedWithTimeoutException = new AtomicBoolean();
final CountDownLatch latch = new CountDownLatch(1);
server.enqueueEcho();
Future<Response> whenResponse = client.prepareGet(getTargetUrl()).setHeaders(headers).execute(new AsyncCompletionHandlerAdapter() {
@Override
public Response onCompleted(Response response) throws Exception {
onCompletedWasNotified.set(true);
latch.countDown();
return response;
}
@Override
public void onThrowable(Throwable t) {
onThrowableWasNotifiedWithTimeoutException.set(t instanceof TimeoutException);
latch.countDown();
}
});
if (!latch.await(TIMEOUT, SECONDS)) {
fail("Timed out");
}
assertFalse(onCompletedWasNotified.get());
assertTrue(onThrowableWasNotifiedWithTimeoutException.get());
try {
whenResponse.get(TIMEOUT, SECONDS);
} catch (ExecutionException e) {
throw e.getCause();
}
});
});
}
use of org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter in project async-http-client by AsyncHttpClient.
the class BasicHttpTest method exceptionInOnCompletedGetNotifiedToFuture.
@Test(expectedExceptions = IllegalStateException.class)
public void exceptionInOnCompletedGetNotifiedToFuture() throws Throwable {
withClient().run(client -> {
withServer(server).run(server -> {
server.enqueueOk();
Future<Response> whenResponse = client.prepareGet(getTargetUrl()).execute(new AsyncCompletionHandlerAdapter() {
@Override
public Response onCompleted(Response response) throws Exception {
throw unknownStackTrace(new IllegalStateException("FOO"), BasicHttpTest.class, "exceptionInOnCompletedGetNotifiedToFuture");
}
@Override
public void onThrowable(Throwable t) {
}
});
try {
whenResponse.get(TIMEOUT, SECONDS);
} catch (ExecutionException e) {
throw e.getCause();
}
});
});
}
Aggregations