Author: cziegeler Date: Fri Sep 1 11:47:52 2017 New Revision: 1806936 URL: http://svn.apache.org/viewvc?rev=1806936&view=rev Log: Add session timeout test
Modified: felix/trunk/http/itest/pom.xml felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/SessionHandlingTest.java Modified: felix/trunk/http/itest/pom.xml URL: http://svn.apache.org/viewvc/felix/trunk/http/itest/pom.xml?rev=1806936&r1=1806935&r2=1806936&view=diff ============================================================================== --- felix/trunk/http/itest/pom.xml (original) +++ felix/trunk/http/itest/pom.xml Fri Sep 1 11:47:52 2017 @@ -35,7 +35,7 @@ <pax.exam.version>4.4.0</pax.exam.version> <pax.url.aether.version>2.4.1</pax.url.aether.version> <http.servlet.api.version>1.1.2</http.servlet.api.version> - <http.jetty.version>3.4.3-SNAPSHOT</http.jetty.version> + <http.jetty.version>3.4.5-SNAPSHOT</http.jetty.version> </properties> <build> Modified: felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/SessionHandlingTest.java URL: http://svn.apache.org/viewvc/felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/SessionHandlingTest.java?rev=1806936&r1=1806935&r2=1806936&view=diff ============================================================================== --- felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/SessionHandlingTest.java (original) +++ felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/SessionHandlingTest.java Fri Sep 1 11:47:52 2017 @@ -69,7 +69,7 @@ import org.osgi.service.http.context.Ser @ExamReactorStrategy(PerMethod.class) public class SessionHandlingTest extends BaseIntegrationTest { - private List<ServiceRegistration<?>> registrations = new ArrayList<ServiceRegistration<?>>(); + private List<ServiceRegistration<?>> registrations = new ArrayList<>(); private CountDownLatch initLatch; private CountDownLatch destroyLatch; @@ -82,7 +82,7 @@ public class SessionHandlingTest extends private void setupServlet(final String name, String[] path, int rank, final String context) throws Exception { - Dictionary<String, Object> servletProps = new Hashtable<String, Object>(); + Dictionary<String, Object> servletProps = new Hashtable<>(); servletProps.put(HTTP_WHITEBOARD_SERVLET_NAME, name); servletProps.put(HTTP_WHITEBOARD_SERVLET_PATTERN, path); servletProps.put(SERVICE_RANKING, rank); @@ -109,6 +109,10 @@ public class SessionHandlingTest extends { req.getSession().invalidate(); } + if ( req.getParameter("timeout") != null ) + { + req.getSession().setMaxInactiveInterval(10); + } final HttpSession s = req.getSession(false); if ( s != null ) { @@ -125,7 +129,8 @@ public class SessionHandlingTest extends { pw.println(" \"session\" : true,"); pw.println(" \"sessionId\" : \"" + s.getId() + "\","); - pw.println(" \"value\" : \"" + s.getAttribute("value") + "\""); + pw.println(" \"value\" : \"" + s.getAttribute("value") + "\","); + pw.println(" \"timeout\" : \"" + s.getMaxInactiveInterval() + "\""); } pw.println("}"); } @@ -176,6 +181,7 @@ public class SessionHandlingTest extends } } + @Test public void testSessionAttributes() throws Exception { @@ -247,4 +253,52 @@ public class SessionHandlingTest extends assertEquals("test2", json.getString("value")); assertEquals(sessionId2, json.getString("sessionId")); } + + @Test + public void testSessionTimeout() throws Exception + { + setupContext("test1", "/"); + + setupLatches(1); + + setupServlet("foo", new String[] { "/foo" }, 1, "test1"); + + assertTrue(initLatch.await(5, TimeUnit.SECONDS)); + + RequestConfig globalConfig = RequestConfig.custom() + .setCookieSpec(CookieSpecs.BEST_MATCH) + .build(); + final CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(globalConfig) + .setDefaultCookieStore(new BasicCookieStore()) + .build(); + + JsonObject json; + + // session should not be available + // check for foo servlet + json = getJSONResponse(httpclient, "/foo"); + assertFalse(json.getBoolean("session")); + + // create session for context of servlet foo + // check session and timeout (should be 10 seconds) + json = getJSONResponse(httpclient, "/foo?create=true&timeout=true"); + assertTrue(json.getBoolean("session")); + assertEquals("10", json.getString("timeout")); + final String sessionId1 = json.getString("sessionId"); + assertNotNull(sessionId1); + + // after four seconds the session should still be there + Thread.sleep(4000); + + json = getJSONResponse(httpclient, "/foo"); + assertTrue(json.getBoolean("session")); + assertEquals("10", json.getString("timeout")); + assertEquals(sessionId1, json.getString("sessionId")); + + // wait 10 seconds, session should be gone + Thread.sleep(10000); + + json = getJSONResponse(httpclient, "/foo"); + assertFalse(json.getBoolean("session")); + } }