Search in sources :

Example 1 with TestServer

use of org.eclipse.jetty.server.session.TestServer in project jetty.project by eclipse.

the class AttributeNameTest method testAttributeNamesWithDots.

@Test
public void testAttributeNamesWithDots() throws Exception {
    String contextPath = "";
    String servletMapping = "/server";
    int maxInactivePeriod = 10000;
    int scavengePeriod = 20000;
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    MongoSessionDataStoreFactory storeFactory = MongoTestHelper.newSessionDataStoreFactory();
    storeFactory.setGracePeriodSec(scavengePeriod);
    TestServer server1 = new TestServer(0, maxInactivePeriod, scavengePeriod, cacheFactory, storeFactory);
    server1.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
    server1.start();
    int port1 = server1.getPort();
    TestServer server2 = new TestServer(0, maxInactivePeriod, scavengePeriod, cacheFactory, storeFactory);
    server2.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
    server2.start();
    int port2 = server2.getPort();
    try {
        HttpClient client = new HttpClient();
        client.start();
        try {
            // Perform one request to server1 to create a session with attribute with dotted name
            ContentResponse response = client.GET("http://localhost:" + port1 + contextPath + servletMapping + "?action=init");
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            String resp = response.getContentAsString();
            String[] sessionTestResponse = resp.split("/");
            assertEquals("a.b.c", sessionTestResponse[0]);
            String sessionCookie = response.getHeaders().get(HttpHeader.SET_COOKIE);
            assertTrue(sessionCookie != null);
            //Mangle the cookie, replacing Path with $Path, etc.
            sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
            //Make a request to the 2nd server which will do a refresh, use TestServlet to ensure that the
            //session attribute with dotted name is not removed
            Request request2 = client.newRequest("http://localhost:" + port2 + contextPath + servletMapping + "?action=get");
            request2.header("Cookie", sessionCookie);
            ContentResponse response2 = request2.send();
            assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
        } finally {
            client.stop();
        }
    } finally {
        server1.stop();
        server2.stop();
    }
}
Also used : DefaultSessionCacheFactory(org.eclipse.jetty.server.session.DefaultSessionCacheFactory) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpClient(org.eclipse.jetty.client.HttpClient) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) TestServer(org.eclipse.jetty.server.session.TestServer) Test(org.junit.Test)

Example 2 with TestServer

use of org.eclipse.jetty.server.session.TestServer 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();
    }
}
Also used : DBCollection(com.mongodb.DBCollection) DefaultSessionCacheFactory(org.eclipse.jetty.server.session.DefaultSessionCacheFactory) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpClient(org.eclipse.jetty.client.HttpClient) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) TestServer(org.eclipse.jetty.server.session.TestServer) Test(org.junit.Test) AbstractSessionExpiryTest(org.eclipse.jetty.server.session.AbstractSessionExpiryTest)

Example 3 with TestServer

use of org.eclipse.jetty.server.session.TestServer 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();
    }
}
Also used : DefaultSessionCacheFactory(org.eclipse.jetty.server.session.DefaultSessionCacheFactory) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) TestServer(org.eclipse.jetty.server.session.TestServer) DBCollection(com.mongodb.DBCollection) HttpClient(org.eclipse.jetty.client.HttpClient) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test) AbstractSessionExpiryTest(org.eclipse.jetty.server.session.AbstractSessionExpiryTest)

Example 4 with TestServer

use of org.eclipse.jetty.server.session.TestServer 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();
    }
}
Also used : DefaultSessionCacheFactory(org.eclipse.jetty.server.session.DefaultSessionCacheFactory) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) TestServer(org.eclipse.jetty.server.session.TestServer) DBCollection(com.mongodb.DBCollection) HttpClient(org.eclipse.jetty.client.HttpClient) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test) AbstractSessionExpiryTest(org.eclipse.jetty.server.session.AbstractSessionExpiryTest)

Example 5 with TestServer

use of org.eclipse.jetty.server.session.TestServer in project jetty.project by eclipse.

the class CachingSessionDataStoreTest method testSessionCRUD.

@Test
public void testSessionCRUD() throws Exception {
    String servletMapping = "/server";
    int scavengePeriod = -1;
    int maxInactivePeriod = -1;
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    SessionDataStoreFactory storeFactory = MemcachedTestHelper.newSessionDataStoreFactory();
    //Make sure sessions are evicted on request exit so they will need to be reloaded via cache/persistent store
    TestServer server = new TestServer(0, maxInactivePeriod, scavengePeriod, cacheFactory, storeFactory);
    ServletContextHandler context = server.addContext("/");
    context.addServlet(TestServlet.class, servletMapping);
    String contextPath = "";
    try {
        server.start();
        int port = server.getPort();
        HttpClient client = new HttpClient();
        client.start();
        try {
            //
            //Create a session
            //
            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=");
            String id = TestServer.extractSessionId(sessionCookie);
            //check that the memcache contains the session, and the session data store contains the session
            CachingSessionDataStore ds = (CachingSessionDataStore) context.getSessionHandler().getSessionCache().getSessionDataStore();
            assertNotNull(ds);
            SessionDataStore persistentStore = ds.getSessionStore();
            SessionDataMap dataMap = ds.getSessionDataMap();
            //the backing persistent store contains the session
            assertNotNull(persistentStore.load(id));
            //the memcache cache contains the session
            assertNotNull(dataMap.load(id));
            //
            //Update a session and check that is is NOT loaded via the persistent store
            //
            ((MockDataStore) persistentStore).zeroLoadCount();
            Request request = client.newRequest("http://localhost:" + port + contextPath + servletMapping + "?action=update");
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            assertEquals(0, ((MockDataStore) persistentStore).getLoadCount());
            //check it was updated in the persistent store
            SessionData sd = persistentStore.load(id);
            assertNotNull(sd);
            assertEquals("bar", sd.getAttribute("foo"));
            //check it was updated in the cache
            sd = dataMap.load(id);
            assertNotNull(sd);
            assertEquals("bar", sd.getAttribute("foo"));
        } finally {
            client.stop();
        }
    } finally {
        server.stop();
    }
}
Also used : DefaultSessionCacheFactory(org.eclipse.jetty.server.session.DefaultSessionCacheFactory) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) CachingSessionDataStore(org.eclipse.jetty.server.session.CachingSessionDataStore) SessionDataStoreFactory(org.eclipse.jetty.server.session.SessionDataStoreFactory) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) SessionData(org.eclipse.jetty.server.session.SessionData) TestServer(org.eclipse.jetty.server.session.TestServer) CachingSessionDataStore(org.eclipse.jetty.server.session.CachingSessionDataStore) SessionDataStore(org.eclipse.jetty.server.session.SessionDataStore) SessionDataMap(org.eclipse.jetty.server.session.SessionDataMap) HttpClient(org.eclipse.jetty.client.HttpClient) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) MockDataStore(org.eclipse.jetty.memcached.sessions.MemcachedTestHelper.MockDataStore) Test(org.junit.Test)

Aggregations

HttpClient (org.eclipse.jetty.client.HttpClient)5 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)5 DefaultSessionCacheFactory (org.eclipse.jetty.server.session.DefaultSessionCacheFactory)5 TestServer (org.eclipse.jetty.server.session.TestServer)5 Test (org.junit.Test)5 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)4 DBCollection (com.mongodb.DBCollection)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 Request (org.eclipse.jetty.client.api.Request)3 AbstractSessionExpiryTest (org.eclipse.jetty.server.session.AbstractSessionExpiryTest)3 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)3 MockDataStore (org.eclipse.jetty.memcached.sessions.MemcachedTestHelper.MockDataStore)1 CachingSessionDataStore (org.eclipse.jetty.server.session.CachingSessionDataStore)1 SessionData (org.eclipse.jetty.server.session.SessionData)1 SessionDataMap (org.eclipse.jetty.server.session.SessionDataMap)1 SessionDataStore (org.eclipse.jetty.server.session.SessionDataStore)1 SessionDataStoreFactory (org.eclipse.jetty.server.session.SessionDataStoreFactory)1