use of org.eclipse.jetty.client.api.Request in project jetty.project by eclipse.
the class HttpClientURITest method testPath.
@Test
public void testPath() throws Exception {
final String path = "/path";
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
Assert.assertEquals(path, request.getRequestURI());
}
});
Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).timeout(5, TimeUnit.SECONDS).path(path);
Assert.assertEquals(path, request.getPath());
Assert.assertNull(request.getQuery());
Fields params = request.getParams();
Assert.assertEquals(0, params.getSize());
Assert.assertTrue(request.getURI().toString().endsWith(path));
ContentResponse response = request.send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
use of org.eclipse.jetty.client.api.Request in project jetty.project by eclipse.
the class HttpClientURITest method testAsteriskFormTarget.
@Test
public void testAsteriskFormTarget() throws Exception {
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
Assert.assertEquals("*", target);
Assert.assertEquals("*", request.getPathInfo());
}
});
Request request = client.newRequest("localhost", connector.getLocalPort()).method(HttpMethod.OPTIONS).scheme(scheme).path("*").timeout(5, TimeUnit.SECONDS);
Assert.assertEquals("*", request.getPath());
Assert.assertNull(request.getQuery());
Fields params = request.getParams();
Assert.assertEquals(0, params.getSize());
Assert.assertNull(request.getURI());
ContentResponse response = request.send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
use of org.eclipse.jetty.client.api.Request in project jetty.project by eclipse.
the class HttpConnectionLifecycleTest method test_BadRequest_WithSlowRequest_RemovesConnection.
@Slow
@Test
public void test_BadRequest_WithSlowRequest_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 Collection<Connection> idleConnections = connectionPool.getIdleConnections();
Assert.assertEquals(0, idleConnections.size());
final Collection<Connection> activeConnections = connectionPool.getActiveConnections();
Assert.assertEquals(0, activeConnections.size());
final long delay = 1000;
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 onHeaders(Request request) {
try {
TimeUnit.MILLISECONDS.sleep(delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@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(delay * 30, TimeUnit.MILLISECONDS));
Assert.assertEquals(0, idleConnections.size());
Assert.assertEquals(0, activeConnections.size());
}
use of org.eclipse.jetty.client.api.Request in project jetty.project by eclipse.
the class HttpConnectionLifecycleTest method test_FailedRequest_RemovesConnection.
@Test
public void test_FailedRequest_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 Collection<Connection> idleConnections = connectionPool.getIdleConnections();
Assert.assertEquals(0, idleConnections.size());
final Collection<Connection> activeConnections = connectionPool.getActiveConnections();
Assert.assertEquals(0, activeConnections.size());
final CountDownLatch beginLatch = new CountDownLatch(1);
final CountDownLatch failureLatch = new CountDownLatch(2);
client.newRequest(host, port).scheme(scheme).listener(new Request.Listener.Adapter() {
@Override
public void onBegin(Request request) {
activeConnections.iterator().next().close();
beginLatch.countDown();
}
@Override
public void onFailure(Request request, Throwable failure) {
failureLatch.countDown();
}
}).send(new Response.Listener.Adapter() {
@Override
public void onComplete(Result result) {
Assert.assertTrue(result.isFailed());
Assert.assertEquals(0, idleConnections.size());
Assert.assertEquals(0, activeConnections.size());
failureLatch.countDown();
}
});
Assert.assertTrue(beginLatch.await(30, TimeUnit.SECONDS));
Assert.assertTrue(failureLatch.await(30, TimeUnit.SECONDS));
Assert.assertEquals(0, idleConnections.size());
Assert.assertEquals(0, activeConnections.size());
}
use of org.eclipse.jetty.client.api.Request in project jetty.project by eclipse.
the class RemoveSessionTest method testRemoveSession.
@Test
public void testRemoveSession() throws Exception {
String contextPath = "";
String servletMapping = "/server";
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
SessionDataStoreFactory storeFactory = new TestSessionDataStoreFactory();
TestServer server = new TestServer(0, -1, -1, cacheFactory, storeFactory);
ServletContextHandler context = server.addContext(contextPath);
context.addServlet(TestServlet.class, servletMapping);
TestEventListener testListener = new TestEventListener();
context.getSessionHandler().addEventListener(testListener);
SessionHandler m = context.getSessionHandler();
try {
server.start();
int port = server.getPort();
HttpClient client = new HttpClient();
client.start();
try {
ContentResponse response = client.GET("http://localhost:" + port + contextPath + servletMapping + "?action=create");
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
String sessionCookie = response.getHeaders().get("Set-Cookie");
assertTrue(sessionCookie != null);
// Mangle the cookie, replacing Path with $Path, etc.
sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
//ensure sessionCreated listener is called
assertTrue(testListener.isCreated());
assertEquals(1, m.getSessionsCreated());
assertEquals(1, ((DefaultSessionCache) m.getSessionCache()).getSessionsMax());
assertEquals(1, ((DefaultSessionCache) m.getSessionCache()).getSessionsTotal());
//now delete the session
Request request = client.newRequest("http://localhost:" + port + contextPath + servletMapping + "?action=delete");
request.header("Cookie", sessionCookie);
response = request.send();
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
//ensure sessionDestroyed listener is called
assertTrue(testListener.isDestroyed());
assertEquals(0, ((DefaultSessionCache) m.getSessionCache()).getSessionsCurrent());
assertEquals(1, ((DefaultSessionCache) m.getSessionCache()).getSessionsMax());
assertEquals(1, ((DefaultSessionCache) m.getSessionCache()).getSessionsTotal());
//check the session is not persisted any more
assertFalse(m.getSessionCache().getSessionDataStore().exists(TestServer.extractSessionId(sessionCookie)));
// The session is not there anymore, even if we present an old cookie
request = client.newRequest("http://localhost:" + port + contextPath + servletMapping + "?action=check");
request.header("Cookie", sessionCookie);
response = request.send();
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
assertEquals(0, ((DefaultSessionCache) m.getSessionCache()).getSessionsCurrent());
assertEquals(1, ((DefaultSessionCache) m.getSessionCache()).getSessionsMax());
assertEquals(1, ((DefaultSessionCache) m.getSessionCache()).getSessionsTotal());
} finally {
client.stop();
}
} finally {
server.stop();
}
}
Aggregations