use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.
the class SessionExpiryTest method testChangeNewSessionTimeout.
@Test
public void testChangeNewSessionTimeout() throws Exception {
String contextPath = "";
String servletMapping = "/server";
int inactivePeriod = 10;
int scavengePeriod = 1;
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
MongoSessionDataStoreFactory storeFactory = MongoTestHelper.newSessionDataStoreFactory();
storeFactory.setGracePeriodSec(scavengePeriod);
TestServer server1 = new TestServer(0, inactivePeriod, scavengePeriod, cacheFactory, storeFactory);
ImmediateChangeTimeoutServlet servlet = new ImmediateChangeTimeoutServlet();
ServletHolder holder = new ServletHolder(servlet);
ServletContextHandler context = server1.addContext(contextPath);
context.addServlet(holder, servletMapping);
TestHttpSessionListener listener = new TestHttpSessionListener();
context.getSessionHandler().addEventListener(listener);
server1.start();
int port1 = server1.getPort();
try {
HttpClient client = new HttpClient();
client.start();
String url = "http://localhost:" + port1 + contextPath + servletMapping;
//change from the sessionmanager configured default
inactivePeriod = 5;
//make a request to set up a session on the server and change its inactive setting straight away
ContentResponse response1 = client.GET(url + "?action=init&val=" + inactivePeriod);
assertEquals(HttpServletResponse.SC_OK, response1.getStatus());
String sessionCookie = response1.getHeaders().get("Set-Cookie");
assertTrue(sessionCookie != null);
// Mangle the cookie, replacing Path with $Path, etc.
sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
String sessionId = TestServer.extractSessionId(sessionCookie);
DBCollection sessions = MongoTestHelper.getCollection();
verifySessionCreated(listener, sessionId);
//verify that the session timeout is the new value and not the default
verifySessionTimeout(sessions, sessionId, inactivePeriod);
} finally {
server1.stop();
}
}
use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.
the class SessionExpiryTest method testBigSessionExpiry.
@Test
public void testBigSessionExpiry() throws Exception {
String contextPath = "";
String servletMapping = "/server";
//integer overflow
int inactivePeriod = Integer.MAX_VALUE * 60;
int scavengePeriod = 10;
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
MongoSessionDataStoreFactory storeFactory = MongoTestHelper.newSessionDataStoreFactory();
storeFactory.setGracePeriodSec(scavengePeriod);
TestServer server1 = new TestServer(0, inactivePeriod, scavengePeriod, cacheFactory, storeFactory);
ChangeTimeoutServlet servlet = new ChangeTimeoutServlet();
ServletHolder holder = new ServletHolder(servlet);
ServletContextHandler context = server1.addContext(contextPath);
context.addServlet(holder, servletMapping);
TestHttpSessionListener listener = new TestHttpSessionListener();
context.getSessionHandler().addEventListener(listener);
server1.start();
int port1 = server1.getPort();
try {
HttpClient client = new HttpClient();
client.start();
String url = "http://localhost:" + port1 + contextPath + servletMapping;
//make a request to set up a session on the server
ContentResponse response1 = client.GET(url + "?action=init");
assertEquals(HttpServletResponse.SC_OK, response1.getStatus());
String sessionCookie = response1.getHeaders().get("Set-Cookie");
assertTrue(sessionCookie != null);
// Mangle the cookie, replacing Path with $Path, etc.
sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
String sessionId = TestServer.extractSessionId(sessionCookie);
DBCollection sessions = MongoTestHelper.getCollection();
verifySessionCreated(listener, sessionId);
//verify that the session timeout is set in mongo
//SessionManager sets -1 if maxInactive < 0
verifySessionTimeout(sessions, sessionId, -1);
//get the session expiry time from mongo
long expiry = getSessionExpiry(sessions, sessionId);
assertEquals(0, expiry);
} finally {
server1.stop();
}
}
use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.
the class SessionExpiryTest method changeSessionTimeout.
@Test
public void changeSessionTimeout() throws Exception {
String contextPath = "";
String servletMapping = "/server";
int inactivePeriod = 10;
int scavengePeriod = 1;
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
MongoSessionDataStoreFactory storeFactory = MongoTestHelper.newSessionDataStoreFactory();
storeFactory.setGracePeriodSec(scavengePeriod);
TestServer server1 = new TestServer(0, inactivePeriod, scavengePeriod, cacheFactory, storeFactory);
ChangeTimeoutServlet servlet = new ChangeTimeoutServlet();
ServletHolder holder = new ServletHolder(servlet);
ServletContextHandler context = server1.addContext(contextPath);
context.addServlet(holder, servletMapping);
TestHttpSessionListener listener = new TestHttpSessionListener();
context.getSessionHandler().addEventListener(listener);
server1.start();
int port1 = server1.getPort();
try {
HttpClient client = new HttpClient();
client.start();
String url = "http://localhost:" + port1 + contextPath + servletMapping;
//make a request to set up a session on the server
ContentResponse response1 = client.GET(url + "?action=init");
assertEquals(HttpServletResponse.SC_OK, response1.getStatus());
String sessionCookie = response1.getHeaders().get("Set-Cookie");
assertTrue(sessionCookie != null);
// Mangle the cookie, replacing Path with $Path, etc.
sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
String sessionId = TestServer.extractSessionId(sessionCookie);
DBCollection sessions = MongoTestHelper.getCollection();
verifySessionCreated(listener, sessionId);
//verify that the session timeout is set in mongo
verifySessionTimeout(sessions, sessionId, inactivePeriod);
//get the session expiry time from mongo
long expiry = getSessionExpiry(sessions, sessionId);
//make another request to change the session timeout to a smaller value
inactivePeriod = 5;
Request request = client.newRequest(url + "?action=change&val=" + inactivePeriod);
request.getHeaders().add("Cookie", sessionCookie);
ContentResponse response2 = request.send();
assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
//check the timeout in mongo
verifySessionTimeout(sessions, sessionId, inactivePeriod);
//check the session expiry time has decreased from previous value
assertTrue(getSessionExpiry(sessions, sessionId) < expiry);
expiry = getSessionExpiry(sessions, sessionId);
//increase the session timeout
inactivePeriod = 20;
request = client.newRequest(url + "?action=change&val=" + inactivePeriod);
request.getHeaders().add("Cookie", sessionCookie);
response2 = request.send();
assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
//verify that the session timeout is set in mongo
verifySessionTimeout(sessions, sessionId, inactivePeriod);
long latestExpiry = getSessionExpiry(sessions, sessionId);
assertTrue(latestExpiry > expiry);
assertTrue(getSessionAccessed(sessions, sessionId) + (1000L * inactivePeriod) <= getSessionExpiry(sessions, sessionId));
//old inactive expired in 5, new inactive expired in 20
assertTrue(latestExpiry >= 15);
} finally {
server1.stop();
}
}
use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.
the class AbstractClusteredInvalidationSessionTest method testInvalidation.
@Test
public void testInvalidation() throws Exception {
String contextPath = "";
String servletMapping = "/server";
int maxInactiveInterval = 30;
int scavengeInterval = 1;
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(SessionCache.EVICT_ON_SESSION_EXIT);
SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(scavengeInterval);
TestServer server1 = new TestServer(0, maxInactiveInterval, scavengeInterval, cacheFactory, storeFactory);
server1.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
try {
server1.start();
int port1 = server1.getPort();
TestServer server2 = new TestServer(0, maxInactiveInterval, scavengeInterval, cacheFactory, storeFactory);
server2.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
try {
server2.start();
int port2 = server2.getPort();
HttpClient client = new HttpClient();
QueuedThreadPool executor = new QueuedThreadPool();
client.setExecutor(executor);
client.start();
try {
String[] urls = new String[2];
urls[0] = "http://localhost:" + port1 + contextPath + servletMapping;
urls[1] = "http://localhost:" + port2 + contextPath + servletMapping;
// Create the session on node1
ContentResponse response1 = client.GET(urls[0] + "?action=init");
assertEquals(HttpServletResponse.SC_OK, response1.getStatus());
String sessionCookie = response1.getHeaders().get("Set-Cookie");
assertTrue(sessionCookie != null);
// Mangle the cookie, replacing Path with $Path, etc.
sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
// Be sure the session is also present in node2
Request request2 = client.newRequest(urls[1] + "?action=increment");
request2.header("Cookie", sessionCookie);
ContentResponse response2 = request2.send();
assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
// Invalidate on node1
Request request1 = client.newRequest(urls[0] + "?action=invalidate");
request1.header("Cookie", sessionCookie);
response1 = request1.send();
assertEquals(HttpServletResponse.SC_OK, response1.getStatus());
// Be sure on node2 we don't see the session anymore
request2 = client.newRequest(urls[1] + "?action=test");
request2.header("Cookie", sessionCookie);
response2 = request2.send();
assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
} finally {
client.stop();
}
} finally {
server2.stop();
}
} finally {
server1.stop();
}
}
use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.
the class AbstractClusteredOrphanedSessionTest method testOrphanedSession.
/**
* @throws Exception on test failure
*/
@Test
public void testOrphanedSession() throws Exception {
// Disable scavenging for the first server, so that we simulate its "crash".
String contextPath = "";
String servletMapping = "/server";
int inactivePeriod = 5;
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
TestServer server1 = new TestServer(0, inactivePeriod, -1, cacheFactory, storeFactory);
server1.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
try {
server1.start();
int port1 = server1.getPort();
int scavengePeriod = 2;
DefaultSessionCacheFactory evictCacheFactory = new DefaultSessionCacheFactory();
//evict after idle for 2 sec
cacheFactory.setEvictionPolicy(2);
TestServer server2 = new TestServer(0, inactivePeriod, scavengePeriod, evictCacheFactory, storeFactory);
server2.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
try {
server2.start();
int port2 = server2.getPort();
HttpClient client = new HttpClient();
client.start();
try {
// Connect to server1 to create a session and get its session cookie
ContentResponse response1 = client.GET("http://localhost:" + port1 + contextPath + servletMapping + "?action=init");
assertEquals(HttpServletResponse.SC_OK, response1.getStatus());
String sessionCookie = response1.getHeaders().get("Set-Cookie");
assertTrue(sessionCookie != null);
// Mangle the cookie, replacing Path with $Path, etc.
sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
// Wait for the session to expire.
// The first node does not do any scavenging, but the session
// must be removed by scavenging done in the other node.
Thread.sleep(TimeUnit.SECONDS.toMillis(inactivePeriod + 2L * scavengePeriod));
// Perform one request to server2 to be sure that the session has been expired
Request request = client.newRequest("http://localhost:" + port2 + contextPath + servletMapping + "?action=check");
request.header("Cookie", sessionCookie);
ContentResponse response2 = request.send();
assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
} finally {
client.stop();
}
} finally {
server2.stop();
}
} finally {
server1.stop();
}
}
Aggregations