use of org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter in project async-http-client by AsyncHttpClient.
the class BasicHttpTest method getEmptyBody.
@Test
public void getEmptyBody() throws Throwable {
withClient().run(client -> {
withServer(server).run(server -> {
server.enqueueOk();
Response response = client.prepareGet(getTargetUrl()).execute(new AsyncCompletionHandlerAdapter()).get(TIMEOUT, SECONDS);
assertTrue(response.getResponseBody().isEmpty());
});
});
}
use of org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter in project async-http-client by AsyncHttpClient.
the class BasicHttpTest method getEmptyBodyNotifiesHandler.
@Test
public void getEmptyBodyNotifiesHandler() throws Throwable {
withClient().run(client -> {
withServer(server).run(server -> {
final AtomicBoolean handlerWasNotified = new AtomicBoolean();
server.enqueueOk();
client.prepareGet(getTargetUrl()).execute(new AsyncCompletionHandlerAdapter() {
@Override
public Response onCompleted(Response response) throws Exception {
assertEquals(response.getStatusCode(), 200);
handlerWasNotified.set(true);
return response;
}
}).get(TIMEOUT, SECONDS);
assertTrue(handlerWasNotified.get());
});
});
}
use of org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter in project async-http-client by AsyncHttpClient.
the class BasicHttpTest method exceptionInOnCompletedGetNotifiedToOnThrowable.
@Test
public void exceptionInOnCompletedGetNotifiedToOnThrowable() throws Throwable {
withClient().run(client -> {
withServer(server).run(server -> {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<String> message = new AtomicReference<>();
server.enqueueOk();
client.prepareGet(getTargetUrl()).execute(new AsyncCompletionHandlerAdapter() {
@Override
public Response onCompleted(Response response) throws Exception {
throw unknownStackTrace(new IllegalStateException("FOO"), BasicHttpTest.class, "exceptionInOnCompletedGetNotifiedToOnThrowable");
}
@Override
public void onThrowable(Throwable t) {
message.set(t.getMessage());
latch.countDown();
}
});
if (!latch.await(TIMEOUT, SECONDS)) {
fail("Timed out");
}
assertEquals(message.get(), "FOO");
});
});
}
use of org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter in project async-http-client by AsyncHttpClient.
the class BasicHttpTest method postUnboundedInputStreamAsBodyStream.
@Test
public void postUnboundedInputStreamAsBodyStream() throws Throwable {
withClient().run(client -> {
withServer(server).run(server -> {
HttpHeaders h = new DefaultHttpHeaders();
h.add(CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON);
server.enqueue(new AbstractHandler() {
EchoHandler chain = new EchoHandler();
@Override
public void handle(String target, org.eclipse.jetty.server.Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
assertEquals(request.getHeader(TRANSFER_ENCODING.toString()), HttpHeaderValues.CHUNKED.toString());
assertNull(request.getHeader(CONTENT_LENGTH.toString()));
chain.handle(target, request, httpServletRequest, httpServletResponse);
}
});
server.enqueueEcho();
client.preparePost(getTargetUrl()).setHeaders(h).setBody(new ByteArrayInputStream("{}".getBytes(StandardCharsets.ISO_8859_1))).execute(new AsyncCompletionHandlerAdapter() {
@Override
public Response onCompleted(Response response) throws Exception {
assertEquals(response.getStatusCode(), 200);
assertEquals(response.getResponseBody(), "{}");
return response;
}
}).get(TIMEOUT, SECONDS);
});
});
}
use of org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter in project async-http-client by AsyncHttpClient.
the class BasicHttpTest method connectFailureNotifiesHandlerWithConnectException.
@Test
public void connectFailureNotifiesHandlerWithConnectException() throws Throwable {
withClient().run(client -> {
withServer(server).run(server -> {
final CountDownLatch l = new CountDownLatch(1);
int port = findFreePort();
client.prepareGet(String.format("http://localhost:%d/", port)).execute(new AsyncCompletionHandlerAdapter() {
@Override
public void onThrowable(Throwable t) {
try {
assertTrue(t instanceof ConnectException);
} finally {
l.countDown();
}
}
});
if (!l.await(TIMEOUT, SECONDS)) {
fail("Timed out");
}
});
});
}
Aggregations