use of org.eclipse.jetty.client.api.Connection in project jetty.project by eclipse.
the class HttpConnectionLifecycleTest method test_BigRequestContent_ResponseWithConnectionCloseHeader_RemovesConnection.
@Test
public void test_BigRequestContent_ResponseWithConnectionCloseHeader_RemovesConnection() throws Exception {
try (StacklessLogging stackless = new StacklessLogging(HttpConnection.class)) {
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setHeader("Connection", "close");
baseRequest.setHandled(true);
// Don't read request content; this causes the server parser to be closed
}
});
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());
Log.getLogger(HttpConnection.class).info("Expecting java.lang.IllegalStateException: HttpParser{s=CLOSED,...");
final CountDownLatch latch = new CountDownLatch(1);
ByteBuffer buffer = ByteBuffer.allocate(16 * 1024 * 1024);
Arrays.fill(buffer.array(), (byte) 'x');
client.newRequest(host, port).scheme(scheme).content(new ByteBufferContentProvider(buffer)).send(new Response.Listener.Adapter() {
@Override
public void onComplete(Result result) {
Assert.assertEquals(1, latch.getCount());
Assert.assertEquals(0, idleConnections.size());
Assert.assertEquals(0, activeConnections.size());
latch.countDown();
}
});
Assert.assertTrue(latch.await(30, TimeUnit.SECONDS));
Assert.assertEquals(0, idleConnections.size());
Assert.assertEquals(0, activeConnections.size());
server.stop();
}
}
use of org.eclipse.jetty.client.api.Connection in project jetty.project by eclipse.
the class HttpConnectionLifecycleTest method test_IdleConnection_IsClosed_OnRemoteClose.
@Slow
@Test
public void test_IdleConnection_IsClosed_OnRemoteClose() 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());
ContentResponse response = client.newRequest(host, port).scheme(scheme).timeout(30, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response.getStatus());
connector.stop();
// Give the connection some time to process the remote close
TimeUnit.SECONDS.sleep(1);
Assert.assertEquals(0, idleConnections.size());
Assert.assertEquals(0, activeConnections.size());
}
use of org.eclipse.jetty.client.api.Connection in project jetty.project by eclipse.
the class HttpConnectionLifecycleTest method test_ResponseWithConnectionCloseHeader_RemovesConnection.
@Test
public void test_ResponseWithConnectionCloseHeader_RemovesConnection() throws Exception {
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setHeader("Connection", "close");
baseRequest.setHandled(true);
}
});
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 latch = new CountDownLatch(1);
client.newRequest(host, port).scheme(scheme).send(new Response.Listener.Adapter() {
@Override
public void onComplete(Result result) {
Assert.assertFalse(result.isFailed());
Assert.assertEquals(0, idleConnections.size());
Assert.assertEquals(0, activeConnections.size());
latch.countDown();
}
});
Assert.assertTrue(latch.await(30, TimeUnit.SECONDS));
Assert.assertEquals(0, idleConnections.size());
Assert.assertEquals(0, activeConnections.size());
}
use of org.eclipse.jetty.client.api.Connection in project jetty.project by eclipse.
the class HttpClientTransportOverFCGI method newConnection.
@Override
public org.eclipse.jetty.io.Connection newConnection(EndPoint endPoint, Map<String, Object> context) throws IOException {
HttpDestination destination = (HttpDestination) context.get(HTTP_DESTINATION_CONTEXT_KEY);
@SuppressWarnings("unchecked") Promise<Connection> promise = (Promise<Connection>) context.get(HTTP_CONNECTION_PROMISE_CONTEXT_KEY);
HttpConnectionOverFCGI connection = newHttpConnection(endPoint, destination, promise);
if (LOG.isDebugEnabled())
LOG.debug("Created {}", connection);
return customize(connection, context);
}
use of org.eclipse.jetty.client.api.Connection in project jetty.project by eclipse.
the class HttpSenderOverHTTPTest method test_Send_NoRequestContent.
@Test
public void test_Send_NoRequestContent() throws Exception {
ByteArrayEndPoint endPoint = new ByteArrayEndPoint();
HttpDestinationOverHTTP destination = new HttpDestinationOverHTTP(client, new Origin("http", "localhost", 8080));
destination.start();
HttpConnectionOverHTTP connection = new HttpConnectionOverHTTP(endPoint, destination, new Promise.Adapter<Connection>());
Request request = client.newRequest(URI.create("http://localhost/"));
final CountDownLatch headersLatch = new CountDownLatch(1);
final CountDownLatch successLatch = new CountDownLatch(1);
request.listener(new Request.Listener.Adapter() {
@Override
public void onHeaders(Request request) {
headersLatch.countDown();
}
@Override
public void onSuccess(Request request) {
successLatch.countDown();
}
});
connection.send(request, null);
String requestString = endPoint.takeOutputString();
Assert.assertTrue(requestString.startsWith("GET "));
Assert.assertTrue(requestString.endsWith("\r\n\r\n"));
Assert.assertTrue(headersLatch.await(5, TimeUnit.SECONDS));
Assert.assertTrue(successLatch.await(5, TimeUnit.SECONDS));
}
Aggregations