use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class AbstractModifyMaxInactiveIntervalTest method testSetMaxInactiveIntervalWithNonImmortalSessionAndEviction.
@Test
public void testSetMaxInactiveIntervalWithNonImmortalSessionAndEviction() throws Exception {
int oldMaxInactive = 10;
int newMaxInactive = 2;
int evict = 4;
int sleep = evict;
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(evict);
SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(TestServer.DEFAULT_SCAVENGE_SEC);
TestServer server = new TestServer(0, oldMaxInactive, 1, cacheFactory, storeFactory);
ServletContextHandler ctxA = server.addContext("/mod");
ctxA.addServlet(TestModServlet.class, "/test");
server.start();
int port = server.getPort();
try {
HttpClient client = new HttpClient();
client.start();
try {
// Perform a request to create a session
ContentResponse response = client.GET("http://localhost:" + port + "/mod/test?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=");
//do another request to reduce the maxinactive interval
Request request = client.newRequest("http://localhost:" + port + "/mod/test?action=change&val=" + newMaxInactive + "&wait=" + sleep);
request.header("Cookie", sessionCookie);
response = request.send();
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
//do another request using the cookie to ensure the session is still there
request = client.newRequest("http://localhost:" + port + "/mod/test?action=test&val=" + newMaxInactive);
request.header("Cookie", sessionCookie);
response = request.send();
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
} finally {
client.stop();
}
} finally {
server.stop();
}
}
use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class AbstractModifyMaxInactiveIntervalTest method testSetMaxInactiveIntervalWithImmortalSessionAndEviction.
@Test
public void testSetMaxInactiveIntervalWithImmortalSessionAndEviction() throws Exception {
int oldMaxInactive = -1;
//2min
int newMaxInactive = 120;
int evict = 2;
int sleep = evict;
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(evict);
SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(TestServer.DEFAULT_SCAVENGE_SEC);
TestServer server = new TestServer(0, oldMaxInactive, 1, cacheFactory, storeFactory);
ServletContextHandler ctxA = server.addContext("/mod");
ctxA.addServlet(TestModServlet.class, "/test");
server.start();
int port = server.getPort();
try {
HttpClient client = new HttpClient();
client.start();
try {
// Perform a request to create a session
ContentResponse response = client.GET("http://localhost:" + port + "/mod/test?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=");
//do another request to reduce the maxinactive interval
Request request = client.newRequest("http://localhost:" + port + "/mod/test?action=change&val=" + newMaxInactive + "&wait=" + sleep);
request.header("Cookie", sessionCookie);
response = request.send();
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
//do another request using the cookie to ensure the session is still there
request = client.newRequest("http://localhost:" + port + "/mod/test?action=test&val=" + newMaxInactive);
request.header("Cookie", sessionCookie);
response = request.send();
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
} finally {
client.stop();
}
} finally {
server.stop();
}
}
use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class AbstractProxySerializationTest method testProxySerialization.
@Test
public void testProxySerialization() throws Exception {
String contextPath = "";
String servletMapping = "/server";
int scavengePeriod = 10;
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(scavengePeriod);
TestServer server = new TestServer(0, 20, scavengePeriod, cacheFactory, storeFactory);
ServletContextHandler context = server.addContext(contextPath);
InputStream is = this.getClass().getClassLoader().getResourceAsStream("proxy-serialization.jar");
File testDir = MavenTestingUtils.getTargetTestingDir("proxy-serialization");
testDir.mkdirs();
File extractedJar = new File(testDir, "proxy-serialization.jar");
extractedJar.createNewFile();
IO.copy(is, new FileOutputStream(extractedJar));
URLClassLoader loader = new URLClassLoader(new URL[] { extractedJar.toURI().toURL() }, Thread.currentThread().getContextClassLoader());
context.setClassLoader(loader);
context.addServlet("TestServlet", servletMapping);
customizeContext(context);
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=");
//stop the context to be sure the sesssion will be passivated
context.stop();
//restart the context
context.start();
// Make another request using the session id from before
Request request = client.newRequest("http://localhost:" + port + contextPath + servletMapping + "?action=test");
request.header("Cookie", sessionCookie);
response = request.send();
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
} finally {
client.stop();
}
} finally {
server.stop();
}
}
use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class AbstractSessionExpiryTest method testRequestForSessionWithChangedTimeout.
@Test
public void testRequestForSessionWithChangedTimeout() throws Exception {
String contextPath = "";
String servletMapping = "/server";
int inactivePeriod = 5;
int scavengePeriod = 1;
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
((AbstractSessionDataStoreFactory) 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 with the session manager's inactive timeout
ContentResponse response = client.GET(url + "?action=init");
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=");
//make another request to change the session timeout to a larger value
int newInactivePeriod = 100;
Request request = client.newRequest(url + "?action=change&val=" + newInactivePeriod);
request.getHeaders().add("Cookie", sessionCookie);
response = request.send();
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
//stop and restart the session manager to ensure it needs to reload the session
context.stop();
context.start();
//wait until the session manager timeout has passed and re-request the session
//which should still be valid
pause(inactivePeriod);
request = client.newRequest(url + "?action=check");
request.getHeaders().add("Cookie", sessionCookie);
response = request.send();
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
String sessionCookie2 = response.getHeaders().get("Set-Cookie");
assertNull(sessionCookie2);
} finally {
server1.stop();
}
}
use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class AbstractSessionExpiryTest method testSessionNotExpired.
/**
* Check session is preserved over stop/start
* @throws Exception
*/
@Test
public void testSessionNotExpired() throws Exception {
String contextPath = "";
String servletMapping = "/server";
int inactivePeriod = 20;
int scavengePeriod = 10;
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(scavengePeriod);
TestServer server1 = new TestServer(0, inactivePeriod, scavengePeriod, cacheFactory, storeFactory);
TestServlet servlet = new TestServlet();
ServletHolder holder = new ServletHolder(servlet);
server1.addContext(contextPath).addServlet(holder, servletMapping);
HttpClient client = new HttpClient();
try {
server1.start();
int port1 = server1.getPort();
client.start();
String url = "http://localhost:" + port1 + contextPath + servletMapping;
//make a request to set up a session on the server
ContentResponse response = client.GET(url + "?action=init");
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=");
//now stop the server
server1.stop();
//start the server again, before the session times out
server1.start();
port1 = server1.getPort();
url = "http://localhost:" + port1 + contextPath + servletMapping;
//make another request, the session should not have expired
Request request = client.newRequest(url + "?action=notexpired");
request.getHeaders().add("Cookie", sessionCookie);
ContentResponse response2 = request.send();
assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
} finally {
client.stop();
server1.stop();
}
}
Aggregations