use of org.eclipse.jetty.client.http.HttpClientTransportOverHTTP 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.HttpClientTransportOverHTTP in project jetty.project by eclipse.
the class TLSServerConnectionCloseTest method startClient.
private void startClient() throws Exception {
SslContextFactory sslContextFactory = new SslContextFactory();
sslContextFactory.setEndpointIdentificationAlgorithm("");
sslContextFactory.setKeyStorePath("src/test/resources/keystore.jks");
sslContextFactory.setKeyStorePassword("storepwd");
sslContextFactory.setTrustStorePath("src/test/resources/truststore.jks");
sslContextFactory.setTrustStorePassword("storepwd");
QueuedThreadPool clientThreads = new QueuedThreadPool();
clientThreads.setName("client");
client = new HttpClient(new HttpClientTransportOverHTTP(1), sslContextFactory);
client.setExecutor(clientThreads);
client.start();
}
use of org.eclipse.jetty.client.http.HttpClientTransportOverHTTP in project jetty.project by eclipse.
the class HttpClientTimeoutTest method testIdleTimeout.
@Test
public void testIdleTimeout() throws Throwable {
long timeout = 1000;
start(new TimeoutHandler(2 * timeout));
client.stop();
final AtomicBoolean sslIdle = new AtomicBoolean();
client = new HttpClient(new HttpClientTransportOverHTTP() {
@Override
public HttpDestination newHttpDestination(Origin origin) {
return new HttpDestinationOverHTTP(getHttpClient(), origin) {
@Override
protected ClientConnectionFactory newSslClientConnectionFactory(ClientConnectionFactory connectionFactory) {
HttpClient client = getHttpClient();
return new SslClientConnectionFactory(client.getSslContextFactory(), client.getByteBufferPool(), client.getExecutor(), connectionFactory) {
@Override
protected SslConnection newSslConnection(ByteBufferPool byteBufferPool, Executor executor, EndPoint endPoint, SSLEngine engine) {
return new SslConnection(byteBufferPool, executor, endPoint, engine) {
@Override
protected boolean onReadTimeout() {
sslIdle.set(true);
return super.onReadTimeout();
}
};
}
};
}
};
}
}, sslContextFactory);
client.setIdleTimeout(timeout);
client.start();
try {
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).send();
Assert.fail();
} catch (Exception x) {
Assert.assertFalse(sslIdle.get());
Assert.assertThat(x.getCause(), Matchers.instanceOf(TimeoutException.class));
}
}
use of org.eclipse.jetty.client.http.HttpClientTransportOverHTTP in project jetty.project by eclipse.
the class HttpClientTest method testRequestSentOnlyAfterConnectionOpen.
@Test
public void testRequestSentOnlyAfterConnectionOpen() throws Exception {
startServer(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
}
});
final AtomicBoolean open = new AtomicBoolean();
client = new HttpClient(new HttpClientTransportOverHTTP() {
@Override
protected HttpConnectionOverHTTP newHttpConnection(EndPoint endPoint, HttpDestination destination, Promise<Connection> promise) {
return new HttpConnectionOverHTTP(endPoint, destination, promise) {
@Override
public void onOpen() {
open.set(true);
super.onOpen();
}
};
}
}, sslContextFactory);
client.start();
final CountDownLatch latch = new CountDownLatch(2);
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onRequestBegin(request -> {
Assert.assertTrue(open.get());
latch.countDown();
}).send(result -> {
if (result.isSucceeded())
latch.countDown();
});
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.http.HttpClientTransportOverHTTP in project jetty.project by eclipse.
the class HttpClientFailureTest method testFailureAfterRequestCommit.
@Test
public void testFailureAfterRequestCommit() 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();
final CountDownLatch commitLatch = new CountDownLatch(1);
final CountDownLatch completeLatch = new CountDownLatch(1);
DeferredContentProvider content = new DeferredContentProvider();
client.newRequest("localhost", connector.getLocalPort()).onRequestCommit(request -> {
connectionRef.get().getEndPoint().close();
commitLatch.countDown();
}).content(content).idleTimeout(2, TimeUnit.SECONDS).send(result -> {
if (result.isFailed())
completeLatch.countDown();
});
Assert.assertTrue(commitLatch.await(5, TimeUnit.SECONDS));
final CountDownLatch contentLatch = new CountDownLatch(1);
content.offer(ByteBuffer.allocate(1024), new Callback() {
@Override
public void failed(Throwable x) {
contentLatch.countDown();
}
});
Assert.assertTrue(commitLatch.await(5, TimeUnit.SECONDS));
Assert.assertTrue(contentLatch.await(5, TimeUnit.SECONDS));
Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
DuplexConnectionPool connectionPool = (DuplexConnectionPool) connectionRef.get().getHttpDestination().getConnectionPool();
Assert.assertEquals(0, connectionPool.getConnectionCount());
Assert.assertEquals(0, connectionPool.getActiveConnections().size());
Assert.assertEquals(0, connectionPool.getIdleConnections().size());
}
Aggregations