@RunWith(DefaultServer.class)
public class ServletSessionPersistenceTestCase {
@Test
public void testSimpleSessionUsage() throws IOException, ServletException {
final PathHandler pathHandler = new PathHandler();
final ServletContainer container = ServletContainer.Factory.newInstance();
DeploymentInfo builder = new DeploymentInfo()
.setClassLoader(SimpleServletTestCase.class.getClassLoader())
.setContextPath("/servletContext")
.setClassIntrospecter(TestClassIntrospector.INSTANCE)
.setDeploymentName("servletContext.war")
.setSessionPersistenceManager(new InMemorySessionPersistence())
.setServletSessionConfig(new ServletSessionConfig().setPath("/servletContext/aa"))
.addServlets(new ServletInfo("servlet", SessionServlet.class)
.addMapping("/aa/b"));
DeploymentManager manager = container.addDeployment(builder);
manager.deploy();
try {
pathHandler.addPrefixPath(builder.getContextPath(), manager.start());
} catch (ServletException e) {
throw new RuntimeException(e);
}
DefaultServer.setRootHandler(pathHandler);
TestHttpClient client = new TestHttpClient();
try {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/aa/b");
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
String response = HttpClientUtils.readResponse(result);
Assert.assertEquals("1", response);
String cookieValue = result.getHeaders("Set-Cookie")[0].getValue();
Assert.assertTrue(cookieValue, cookieValue.contains("JSESSIONID"));
Assert.assertTrue(cookieValue, cookieValue.contains("/servletContext/aa"));
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
response = HttpClientUtils.readResponse(result);
Assert.assertEquals("2", response);
manager.stop();
manager.undeploy();
manager.deploy();
pathHandler.addPrefixPath(builder.getContextPath(), manager.start());
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
response = HttpClientUtils.readResponse(result);
Assert.assertEquals("3", response);