From 804f8eef3eaabf65c3841142349e8b6365b00db5 Mon Sep 17 00:00:00 2001 From: Jan Bartel Date: Tue, 29 Mar 2011 23:59:06 +0000 Subject: [PATCH] Add extra test for removing a http session and verifying it is gone git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@2926 7e9141cc-0065-0410-87d8-b60c137991c4 --- VERSION.txt | 1 + .../server/session/RemoveSessionTest.java | 19 +++ .../session/AbstractRemoveSessionTest.java | 112 ++++++++++++++++++ 3 files changed, 132 insertions(+) create mode 100644 tests/test-sessions/test-hash-sessions/src/test/java/org/eclipse/jetty/server/session/RemoveSessionTest.java create mode 100644 tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractRemoveSessionTest.java diff --git a/VERSION.txt b/VERSION.txt index 6652b981a0e..02e585d1b1b 100644 --- a/VERSION.txt +++ b/VERSION.txt @@ -17,6 +17,7 @@ jetty-7.3.2-SNAPSHOT + JETTY-1245 Pooled Buffers implementation + 341206 Stop order is wrong in HandlerWrapper + 341255 org.eclipse.http usage in AJP/SessionId linkage + + Added extra session removal test jetty-7.3.1.v20110307 7 March 2011 + 316382 Support a more strict SSL option with certificates diff --git a/tests/test-sessions/test-hash-sessions/src/test/java/org/eclipse/jetty/server/session/RemoveSessionTest.java b/tests/test-sessions/test-hash-sessions/src/test/java/org/eclipse/jetty/server/session/RemoveSessionTest.java new file mode 100644 index 00000000000..6663343ef16 --- /dev/null +++ b/tests/test-sessions/test-hash-sessions/src/test/java/org/eclipse/jetty/server/session/RemoveSessionTest.java @@ -0,0 +1,19 @@ +package org.eclipse.jetty.server.session; + +import org.junit.Test; + +public class RemoveSessionTest extends AbstractRemoveSessionTest +{ + + public AbstractTestServer createServer(int port, int max, int scavenge) + { + return new HashTestServer(port,max,scavenge); + } + + @Test + public void testRemoveSession() throws Exception + { + super.testRemoveSession(); + } + +} diff --git a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractRemoveSessionTest.java b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractRemoveSessionTest.java new file mode 100644 index 00000000000..5d0de13133d --- /dev/null +++ b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractRemoveSessionTest.java @@ -0,0 +1,112 @@ +package org.eclipse.jetty.server.session; + +import static org.junit.Assert.*; + +import java.io.IOException; +import java.util.Random; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +import org.eclipse.jetty.client.ContentExchange; +import org.eclipse.jetty.client.HttpClient; +import org.eclipse.jetty.http.HttpMethods; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.junit.Test; + +public abstract class AbstractRemoveSessionTest +{ + public abstract AbstractTestServer createServer(int port, int max, int scavenge); + + + @Test + public void testRemoveSession() throws Exception + { + Random random = new Random(System.nanoTime()); + + String contextPath = ""; + String servletMapping = "/server"; + int port = random.nextInt(50000) + 10000; + int scavengePeriod = 3; + AbstractTestServer server = createServer(port, 1, scavengePeriod); + ServletContextHandler context = server.addContext(contextPath); + context.addServlet(TestServlet.class, servletMapping); + server.start(); + try + { + HttpClient client = new HttpClient(); + client.setConnectorType(HttpClient.CONNECTOR_SOCKET); + client.start(); + try + { + ContentExchange exchange = new ContentExchange(true); + exchange.setMethod(HttpMethods.GET); + exchange.setURL("http://localhost:" + port + contextPath + servletMapping + "?action=create"); + client.send(exchange); + exchange.waitForDone(); + assertEquals(HttpServletResponse.SC_OK,exchange.getResponseStatus()); + String sessionCookie = exchange.getResponseFields().getStringField("Set-Cookie"); + assertTrue(sessionCookie != null); + // Mangle the cookie, replacing Path with $Path, etc. + sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path="); + + //now delete the session + exchange = new ContentExchange(true); + exchange.setMethod(HttpMethods.GET); + exchange.setURL("http://localhost:" + port + contextPath + servletMapping + "?action=delete"); + exchange.getRequestFields().add("Cookie", sessionCookie); + client.send(exchange); + exchange.waitForDone(); + assertEquals(HttpServletResponse.SC_OK,exchange.getResponseStatus()); + + + // The session is not there anymore, but we present an old cookie + // The server creates a new session, we must ensure we released all locks + exchange = new ContentExchange(true); + exchange.setMethod(HttpMethods.GET); + exchange.setURL("http://localhost:" + port + contextPath + servletMapping + "?action=check"); + exchange.getRequestFields().add("Cookie", sessionCookie); + client.send(exchange); + exchange.waitForDone(); + assertEquals(HttpServletResponse.SC_OK,exchange.getResponseStatus()); + } + finally + { + client.stop(); + } + } + finally + { + server.stop(); + } + + } + public static class TestServlet extends HttpServlet + { + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException + { + String action = request.getParameter("action"); + if ("create".equals(action)) + { + request.getSession(true); + } + else if ("delete".equals(action)) + { + HttpSession s = request.getSession(false); + assertNotNull(s); + s.invalidate(); + s = request.getSession(false); + assertNull(s); + } + else + { + HttpSession s = request.getSession(false); + assertNull(s); + } + } + } + +}