use of org.eclipse.jetty.client.http.HttpConnectionOverHTTP in project jetty.project by eclipse.
the class ClientConnectionCloseTest method test_ClientConnectionClose_ServerConnectionClose_ClientClosesAfterExchange.
@Test
public void test_ClientConnectionClose_ServerConnectionClose_ClientClosesAfterExchange() throws Exception {
byte[] data = new byte[128 * 1024];
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
ServletInputStream input = request.getInputStream();
while (true) {
int read = input.read();
if (read < 0)
break;
}
response.setContentLength(data.length);
response.getOutputStream().write(data);
try {
// Delay the server from sending the TCP FIN.
Thread.sleep(1000);
} catch (InterruptedException x) {
throw new InterruptedIOException();
}
}
});
String host = "localhost";
int port = connector.getLocalPort();
HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, host, port);
DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
ContentResponse response = client.newRequest(host, port).scheme(scheme).header(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.asString()).content(new StringContentProvider("0")).onRequestSuccess(request -> {
HttpConnectionOverHTTP connection = (HttpConnectionOverHTTP) connectionPool.getActiveConnections().iterator().next();
Assert.assertFalse(connection.getEndPoint().isOutputShutdown());
}).send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
Assert.assertArrayEquals(data, response.getContent());
Assert.assertEquals(0, connectionPool.getConnectionCount());
}
use of org.eclipse.jetty.client.http.HttpConnectionOverHTTP in project jetty.project by eclipse.
the class ClientConnectionCloseTest method test_ClientConnectionClose_ServerNoConnectionClose_ClientCloses.
@Test
public void test_ClientConnectionClose_ServerNoConnectionClose_ClientCloses() throws Exception {
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
response.setContentLength(0);
response.flushBuffer();
try {
// Delay the server from sending the TCP FIN.
Thread.sleep(1000);
} catch (InterruptedException x) {
throw new InterruptedIOException();
}
}
});
String host = "localhost";
int port = connector.getLocalPort();
HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, host, port);
DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
ContentResponse response = client.newRequest(host, port).scheme(scheme).header(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.asString()).onRequestSuccess(request -> {
HttpConnectionOverHTTP connection = (HttpConnectionOverHTTP) connectionPool.getActiveConnections().iterator().next();
Assert.assertFalse(connection.getEndPoint().isOutputShutdown());
}).onResponseHeaders(r -> r.getHeaders().remove(HttpHeader.CONNECTION)).send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
Assert.assertEquals(0, connectionPool.getConnectionCount());
}
use of org.eclipse.jetty.client.http.HttpConnectionOverHTTP in project jetty.project by eclipse.
the class HttpClientExplicitConnectionTest method testExplicitConnectionIsClosedOnRemoteClose.
@Test
public void testExplicitConnectionIsClosedOnRemoteClose() throws Exception {
start(new EmptyServerHandler());
Destination destination = client.getDestination(scheme, "localhost", connector.getLocalPort());
FuturePromise<Connection> futureConnection = new FuturePromise<>();
destination.newConnection(futureConnection);
Connection connection = futureConnection.get(5, TimeUnit.SECONDS);
Request request = client.newRequest(destination.getHost(), destination.getPort()).scheme(scheme);
FutureResponseListener listener = new FutureResponseListener(request);
connection.send(request, listener);
ContentResponse response = listener.get(5, TimeUnit.SECONDS);
Assert.assertEquals(200, response.getStatus());
// Wait some time to have the client is an idle state.
TimeUnit.SECONDS.sleep(1);
connector.stop();
// Give the connection some time to process the remote close.
TimeUnit.SECONDS.sleep(1);
HttpConnectionOverHTTP httpConnection = (HttpConnectionOverHTTP) connection;
Assert.assertFalse(httpConnection.getEndPoint().isOpen());
HttpDestinationOverHTTP httpDestination = (HttpDestinationOverHTTP) destination;
DuplexConnectionPool connectionPool = (DuplexConnectionPool) httpDestination.getConnectionPool();
Assert.assertTrue(connectionPool.getActiveConnections().isEmpty());
Assert.assertTrue(connectionPool.getIdleConnections().isEmpty());
}
use of org.eclipse.jetty.client.http.HttpConnectionOverHTTP in project jetty.project by eclipse.
the class HttpClientFailureTest method testFailureBeforeRequestCommit.
@Test
public void testFailureBeforeRequestCommit() throws Exception {
startServer(new EmptyServerHandler());
final AtomicReference<HttpConnectionOverHTTP> connectionRef = new AtomicReference<>();
client = new HttpClient(new HttpClientTransportOverHTTP() {
@Override
protected HttpConnectionOverHTTP newHttpConnection(EndPoint endPoint, HttpDestination destination, Promise<Connection> promise) {
HttpConnectionOverHTTP connection = super.newHttpConnection(endPoint, destination, promise);
connectionRef.set(connection);
return connection;
}
}, null);
client.start();
try {
client.newRequest("localhost", connector.getLocalPort()).onRequestHeaders(request -> connectionRef.get().getEndPoint().close()).timeout(5, TimeUnit.SECONDS).send();
Assert.fail();
} catch (ExecutionException x) {
// Expected.
}
DuplexConnectionPool connectionPool = (DuplexConnectionPool) connectionRef.get().getHttpDestination().getConnectionPool();
Assert.assertEquals(0, connectionPool.getConnectionCount());
Assert.assertEquals(0, connectionPool.getActiveConnections().size());
Assert.assertEquals(0, connectionPool.getIdleConnections().size());
}
use of org.eclipse.jetty.client.http.HttpConnectionOverHTTP 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());
}
Aggregations