use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpResponseAbortTest method testAbortOnHeader.
@Test
public void testAbortOnHeader() throws Exception {
start(new EmptyServerHandler());
final CountDownLatch latch = new CountDownLatch(1);
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onResponseHeader(new Response.HeaderListener() {
@Override
public boolean onHeader(Response response, HttpField field) {
response.abort(new Exception());
return true;
}
}).send(new Response.CompleteListener() {
@Override
public void onComplete(Result result) {
Assert.assertTrue(result.isFailed());
latch.countDown();
}
});
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpClientTest method testStoppingClosesConnections.
@Test
public void testStoppingClosesConnections() throws Exception {
start(new EmptyServerHandler());
String host = "localhost";
int port = connector.getLocalPort();
String path = "/";
Response response = client.GET(scheme + "://" + host + ":" + port + path);
Assert.assertEquals(200, response.getStatus());
HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, host, port);
DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
long start = System.nanoTime();
HttpConnectionOverHTTP connection = null;
while (connection == null && TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - start) < 5) {
connection = (HttpConnectionOverHTTP) connectionPool.getIdleConnections().peek();
TimeUnit.MILLISECONDS.sleep(10);
}
Assert.assertNotNull(connection);
String uri = destination.getScheme() + "://" + destination.getHost() + ":" + destination.getPort();
client.getCookieStore().add(URI.create(uri), new HttpCookie("foo", "bar"));
client.stop();
Assert.assertEquals(0, client.getDestinations().size());
Assert.assertEquals(0, connectionPool.getIdleConnectionCount());
Assert.assertEquals(0, connectionPool.getActiveConnectionCount());
Assert.assertFalse(connection.getEndPoint().isOpen());
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpClientTest method testResponseListenerForMultipleEventsIsInvokedOncePerEvent.
@Test
public void testResponseListenerForMultipleEventsIsInvokedOncePerEvent() throws Exception {
start(new EmptyServerHandler());
final AtomicInteger counter = new AtomicInteger();
final CountDownLatch latch = new CountDownLatch(1);
Response.Listener listener = new Response.Listener() {
@Override
public void onBegin(Response response) {
counter.incrementAndGet();
}
@Override
public boolean onHeader(Response response, HttpField field) {
// Number of header may vary, so don't count
return true;
}
@Override
public void onHeaders(Response response) {
counter.incrementAndGet();
}
@Override
public void onContent(Response response, ByteBuffer content) {
// Should not be invoked
counter.incrementAndGet();
}
@Override
public void onContent(Response response, ByteBuffer content, Callback callback) {
// Should not be invoked
counter.incrementAndGet();
}
@Override
public void onSuccess(Response response) {
counter.incrementAndGet();
}
@Override
public void onFailure(Response response, Throwable failure) {
// Should not be invoked
counter.incrementAndGet();
}
@Override
public void onComplete(Result result) {
Assert.assertEquals(200, result.getResponse().getStatus());
counter.incrementAndGet();
latch.countDown();
}
};
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onResponseBegin(listener).onResponseHeader(listener).onResponseHeaders(listener).onResponseContent(listener).onResponseContentAsync(listener).onResponseSuccess(listener).onResponseFailure(listener).send(listener);
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
int expectedEventsTriggeredByOnResponseXXXListeners = 3;
int expectedEventsTriggeredByCompletionListener = 4;
int expected = expectedEventsTriggeredByOnResponseXXXListeners + expectedEventsTriggeredByCompletionListener;
Assert.assertEquals(expected, counter.get());
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpReceiverOverHTTPTest method test_Receive_ResponseContent.
@Test
public void test_Receive_ResponseContent() throws Exception {
String content = "0123456789ABCDEF";
endPoint.addInput("" + "HTTP/1.1 200 OK\r\n" + "Content-length: " + content.length() + "\r\n" + "\r\n" + content);
HttpExchange exchange = newExchange();
FutureResponseListener listener = (FutureResponseListener) exchange.getResponseListeners().get(0);
connection.getHttpChannel().receive();
Response response = listener.get(5, TimeUnit.SECONDS);
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals("OK", response.getReason());
Assert.assertSame(HttpVersion.HTTP_1_1, response.getVersion());
HttpFields headers = response.getHeaders();
Assert.assertNotNull(headers);
Assert.assertEquals(1, headers.size());
Assert.assertEquals(String.valueOf(content.length()), headers.get(HttpHeader.CONTENT_LENGTH));
String received = listener.getContentAsString(StandardCharsets.UTF_8);
Assert.assertEquals(content, received);
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpClientTimeoutTest method testTimeoutIsCancelledOnSuccessWithExplicitConnection.
@Slow
@Test
public void testTimeoutIsCancelledOnSuccessWithExplicitConnection() throws Exception {
long timeout = 1000;
start(new TimeoutHandler(timeout));
final CountDownLatch latch = new CountDownLatch(1);
Destination destination = client.getDestination(scheme, "localhost", connector.getLocalPort());
FuturePromise<Connection> futureConnection = new FuturePromise<>();
destination.newConnection(futureConnection);
try (Connection connection = futureConnection.get(5, TimeUnit.SECONDS)) {
Request request = client.newRequest(destination.getHost(), destination.getPort()).scheme(scheme).timeout(2 * timeout, TimeUnit.MILLISECONDS);
connection.send(request, new Response.CompleteListener() {
@Override
public void onComplete(Result result) {
Response response = result.getResponse();
Assert.assertEquals(200, response.getStatus());
Assert.assertFalse(result.isFailed());
latch.countDown();
}
});
Assert.assertTrue(latch.await(3 * timeout, TimeUnit.MILLISECONDS));
TimeUnit.MILLISECONDS.sleep(2 * timeout);
Assert.assertNull(request.getAbortCause());
}
}
Aggregations