use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpClientChunkedContentTest method test_Server_ContentTerminal_Client_ContentDelay.
@Test
public void test_Server_ContentTerminal_Client_ContentDelay() throws Exception {
startClient();
try (ServerSocket server = new ServerSocket()) {
server.bind(new InetSocketAddress("localhost", 0));
final AtomicReference<Callback> callbackRef = new AtomicReference<>();
final CountDownLatch firstContentLatch = new CountDownLatch(1);
final AtomicReference<Result> resultRef = new AtomicReference<>();
final CountDownLatch completeLatch = new CountDownLatch(1);
client.newRequest("localhost", server.getLocalPort()).onResponseContentAsync(new Response.AsyncContentListener() {
@Override
public void onContent(Response response, ByteBuffer content, Callback callback) {
if (callbackRef.compareAndSet(null, callback))
firstContentLatch.countDown();
else
callback.succeeded();
}
}).timeout(5, TimeUnit.SECONDS).send(new Response.CompleteListener() {
@Override
public void onComplete(Result result) {
resultRef.set(result);
completeLatch.countDown();
}
});
try (Socket socket = server.accept()) {
consumeRequestHeaders(socket);
OutputStream output = socket.getOutputStream();
String response = "" + "HTTP/1.1 200 OK\r\n" + "Transfer-Encoding: chunked\r\n" + "\r\n" + "8\r\n" + "01234567\r\n" + "0\r\n" + "\r\n";
output.write(response.getBytes(StandardCharsets.UTF_8));
output.flush();
// Simulate a delay in consuming the content.
assertTrue(firstContentLatch.await(5, TimeUnit.SECONDS));
Thread.sleep(1000);
callbackRef.get().succeeded();
// Wait for the client to read 0 and become idle.
Thread.sleep(1000);
assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
Result result = resultRef.get();
assertTrue(result.isSucceeded());
Assert.assertEquals(200, result.getResponse().getStatus());
// Issue another request to be sure the connection is sane.
Request request = client.newRequest("localhost", server.getLocalPort()).timeout(5, TimeUnit.SECONDS);
FutureResponseListener listener = new FutureResponseListener(request);
request.send(listener);
consumeRequestHeaders(socket);
output.write(response.getBytes(StandardCharsets.UTF_8));
output.flush();
Assert.assertEquals(200, listener.get(5, TimeUnit.SECONDS).getStatus());
}
}
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpConnectionLifecycleTest method testConnectionForHTTP10ResponseIsRemoved.
@Test
public void testConnectionForHTTP10ResponseIsRemoved() throws Exception {
start(new EmptyServerHandler());
String host = "localhost";
int port = connector.getLocalPort();
HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, host, port);
DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
final Collection<Connection> idleConnections = connectionPool.getIdleConnections();
Assert.assertEquals(0, idleConnections.size());
final Collection<Connection> activeConnections = connectionPool.getActiveConnections();
Assert.assertEquals(0, activeConnections.size());
client.setStrictEventOrdering(false);
ContentResponse response = client.newRequest(host, port).scheme(scheme).onResponseBegin(response1 -> {
((HttpResponse) response1).version(HttpVersion.HTTP_1_0);
}).send();
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals(0, idleConnections.size());
Assert.assertEquals(0, activeConnections.size());
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpConnectionLifecycleTest method test_SuccessfulRequest_ReturnsConnection.
@Test
public void test_SuccessfulRequest_ReturnsConnection() throws Exception {
start(new EmptyServerHandler());
String host = "localhost";
int port = connector.getLocalPort();
HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, host, port);
DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
final Collection<Connection> idleConnections = connectionPool.getIdleConnections();
Assert.assertEquals(0, idleConnections.size());
final Collection<Connection> activeConnections = connectionPool.getActiveConnections();
Assert.assertEquals(0, activeConnections.size());
final CountDownLatch headersLatch = new CountDownLatch(1);
final CountDownLatch successLatch = new CountDownLatch(3);
client.newRequest(host, port).scheme(scheme).onRequestSuccess(request -> successLatch.countDown()).onResponseHeaders(response -> {
Assert.assertEquals(0, idleConnections.size());
Assert.assertEquals(1, activeConnections.size());
headersLatch.countDown();
}).send(new Response.Listener.Adapter() {
@Override
public void onSuccess(Response response) {
successLatch.countDown();
}
@Override
public void onComplete(Result result) {
Assert.assertFalse(result.isFailed());
successLatch.countDown();
}
});
Assert.assertTrue(headersLatch.await(30, TimeUnit.SECONDS));
Assert.assertTrue(successLatch.await(30, TimeUnit.SECONDS));
Assert.assertEquals(1, idleConnections.size());
Assert.assertEquals(0, activeConnections.size());
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpConnectionLifecycleTest method test_BadRequest_RemovesConnection.
@Test
public void test_BadRequest_RemovesConnection() throws Exception {
start(new EmptyServerHandler());
String host = "localhost";
int port = connector.getLocalPort();
HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, host, port);
DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
final Queue<Connection> idleConnections = connectionPool.getIdleConnections();
Assert.assertEquals(0, idleConnections.size());
final Collection<Connection> activeConnections = connectionPool.getActiveConnections();
Assert.assertEquals(0, activeConnections.size());
final CountDownLatch successLatch = new CountDownLatch(3);
client.newRequest(host, port).scheme(scheme).listener(new Request.Listener.Adapter() {
@Override
public void onBegin(Request request) {
// Remove the host header, this will make the request invalid
request.header(HttpHeader.HOST, null);
}
@Override
public void onSuccess(Request request) {
successLatch.countDown();
}
}).send(new Response.Listener.Adapter() {
@Override
public void onSuccess(Response response) {
Assert.assertEquals(400, response.getStatus());
// 400 response also come with a Connection: close,
// so the connection is closed and removed
successLatch.countDown();
}
@Override
public void onComplete(Result result) {
Assert.assertFalse(result.isFailed());
successLatch.countDown();
}
});
Assert.assertTrue(successLatch.await(30, TimeUnit.SECONDS));
Assert.assertEquals(0, idleConnections.size());
Assert.assertEquals(0, activeConnections.size());
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpClientTest method testAllHeadersDiscarded.
@Test
public void testAllHeadersDiscarded() throws Exception {
start(new EmptyServerHandler());
int count = 10;
final CountDownLatch latch = new CountDownLatch(count);
for (int i = 0; i < count; ++i) {
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).send(new Response.Listener.Adapter() {
@Override
public boolean onHeader(Response response, HttpField field) {
return false;
}
@Override
public void onComplete(Result result) {
if (result.isSucceeded())
latch.countDown();
}
});
}
Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
}
Aggregations