Search in sources :

Example 1 with InMemorySessionPersistence

use of io.undertow.servlet.util.InMemorySessionPersistence in project undertow by undertow-io.

the class ServletSessionPersistenceTestCase method testSimpleSessionUsage.

@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);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : DeploymentManager(io.undertow.servlet.api.DeploymentManager) HttpGet(org.apache.http.client.methods.HttpGet) PathHandler(io.undertow.server.handlers.PathHandler) HttpResponse(org.apache.http.HttpResponse) ServletSessionConfig(io.undertow.servlet.api.ServletSessionConfig) SimpleServletTestCase(io.undertow.servlet.test.SimpleServletTestCase) TestHttpClient(io.undertow.testutils.TestHttpClient) ServletInfo(io.undertow.servlet.api.ServletInfo) ServletException(javax.servlet.ServletException) ServletContainer(io.undertow.servlet.api.ServletContainer) InMemorySessionPersistence(io.undertow.servlet.util.InMemorySessionPersistence) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) Test(org.junit.Test)

Aggregations

PathHandler (io.undertow.server.handlers.PathHandler)1 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)1 DeploymentManager (io.undertow.servlet.api.DeploymentManager)1 ServletContainer (io.undertow.servlet.api.ServletContainer)1 ServletInfo (io.undertow.servlet.api.ServletInfo)1 ServletSessionConfig (io.undertow.servlet.api.ServletSessionConfig)1 SimpleServletTestCase (io.undertow.servlet.test.SimpleServletTestCase)1 InMemorySessionPersistence (io.undertow.servlet.util.InMemorySessionPersistence)1 TestHttpClient (io.undertow.testutils.TestHttpClient)1 ServletException (javax.servlet.ServletException)1 HttpResponse (org.apache.http.HttpResponse)1 HttpGet (org.apache.http.client.methods.HttpGet)1 Test (org.junit.Test)1