View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.server.session;
20  
21  import java.io.IOException;
22  import javax.servlet.ServletException;
23  import javax.servlet.http.HttpServlet;
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  import javax.servlet.http.HttpSession;
27  import javax.servlet.http.HttpSessionEvent;
28  import javax.servlet.http.HttpSessionListener;
29  
30  import org.eclipse.jetty.client.HttpClient;
31  import org.eclipse.jetty.client.api.ContentResponse;
32  import org.eclipse.jetty.client.api.Request;
33  import org.eclipse.jetty.servlet.ServletContextHandler;
34  import org.junit.Test;
35  
36  import static org.junit.Assert.assertEquals;
37  import static org.junit.Assert.assertNotNull;
38  import static org.junit.Assert.assertNull;
39  import static org.junit.Assert.assertTrue;
40  
41  public abstract class AbstractRemoveSessionTest
42  {
43      public abstract AbstractTestServer createServer(int port, int max, int scavenge);
44  
45  
46      @Test
47      public void testRemoveSession() throws Exception
48      {
49          String contextPath = "";
50          String servletMapping = "/server";
51          int scavengePeriod = 3;
52          AbstractTestServer server = createServer(0, 1, scavengePeriod);
53          ServletContextHandler context = server.addContext(contextPath);
54          context.addServlet(TestServlet.class, servletMapping);
55          TestEventListener testListener = new TestEventListener();
56          context.getSessionHandler().addEventListener(testListener);
57          server.start();
58          int port = server.getPort();
59          try
60          {
61              HttpClient client = new HttpClient();
62              client.start();
63              try
64              {
65                  ContentResponse response = client.GET("http://localhost:" + port + contextPath + servletMapping + "?action=create");
66                  assertEquals(HttpServletResponse.SC_OK,response.getStatus());
67                  String sessionCookie = response.getHeaders().getStringField("Set-Cookie");
68                  assertTrue(sessionCookie != null);
69                  // Mangle the cookie, replacing Path with $Path, etc.
70                  sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
71                  //ensure sessionCreated listener is called
72                  assertTrue (testListener.isCreated());
73  
74                  //now delete the session
75                  Request request = client.newRequest("http://localhost:" + port + contextPath + servletMapping + "?action=delete");
76                  request.header("Cookie", sessionCookie);
77                  response = request.send();
78                  assertEquals(HttpServletResponse.SC_OK,response.getStatus());
79                  //ensure sessionDestroyed listener is called
80                  assertTrue(testListener.isDestroyed());
81  
82  
83                  // The session is not there anymore, but we present an old cookie
84                  // The server creates a new session, we must ensure we released all locks
85                  request = client.newRequest("http://localhost:" + port + contextPath + servletMapping + "?action=check");
86                  request.header("Cookie", sessionCookie);
87                  response = request.send();
88                  assertEquals(HttpServletResponse.SC_OK,response.getStatus());
89              }
90              finally
91              {
92                  client.stop();
93              }
94          }
95          finally
96          {
97              server.stop();
98          }
99  
100     }
101     public static class TestServlet extends HttpServlet
102     {
103         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
104         {
105             String action = request.getParameter("action");
106             if ("create".equals(action))
107             {
108                 request.getSession(true);
109             }
110             else if ("delete".equals(action))
111             {
112                 HttpSession s = request.getSession(false);
113                 assertNotNull(s);
114                 s.invalidate();
115                 s = request.getSession(false);
116                 assertNull(s);
117             }
118             else
119             {
120                HttpSession s = request.getSession(false);
121                assertNull(s);
122             }
123         }
124     }
125 
126     public static class TestEventListener implements HttpSessionListener
127     {
128         boolean wasCreated;
129         boolean wasDestroyed;
130 
131         public void sessionCreated(HttpSessionEvent se)
132         {
133             wasCreated = true;
134         }
135 
136         public void sessionDestroyed(HttpSessionEvent se)
137         {
138            wasDestroyed = true;
139         }
140 
141         public boolean isDestroyed()
142         {
143             return wasDestroyed;
144         }
145 
146 
147         public boolean isCreated()
148         {
149             return wasCreated;
150         }
151 
152     }
153 
154 }