View Javadoc

1   package org.eclipse.jetty.server.session;
2   
3   import static org.junit.Assert.*;
4   
5   import java.io.IOException;
6   import java.util.Random;
7   
8   import javax.servlet.ServletException;
9   import javax.servlet.http.HttpServlet;
10  import javax.servlet.http.HttpServletRequest;
11  import javax.servlet.http.HttpServletResponse;
12  import javax.servlet.http.HttpSession;
13  
14  import org.eclipse.jetty.client.ContentExchange;
15  import org.eclipse.jetty.client.HttpClient;
16  import org.eclipse.jetty.http.HttpMethods;
17  import org.eclipse.jetty.servlet.ServletContextHandler;
18  import org.junit.Test;
19  
20  public abstract class AbstractRemoveSessionTest
21  {
22      public abstract AbstractTestServer createServer(int port, int max, int scavenge);
23      
24      
25      @Test
26      public void testRemoveSession() throws Exception
27      {
28          String contextPath = "";
29          String servletMapping = "/server";
30          int scavengePeriod = 3;
31          AbstractTestServer server = createServer(0, 1, scavengePeriod);
32          ServletContextHandler context = server.addContext(contextPath);
33          context.addServlet(TestServlet.class, servletMapping);
34          server.start();
35          int port = server.getPort();
36          try
37          {
38              HttpClient client = new HttpClient();
39              client.setConnectorType(HttpClient.CONNECTOR_SOCKET);
40              client.start();
41              try
42              {
43                  ContentExchange exchange = new ContentExchange(true);
44                  exchange.setMethod(HttpMethods.GET);
45                  exchange.setURL("http://localhost:" + port + contextPath + servletMapping + "?action=create");
46                  client.send(exchange);
47                  exchange.waitForDone();
48                  assertEquals(HttpServletResponse.SC_OK,exchange.getResponseStatus());
49                  String sessionCookie = exchange.getResponseFields().getStringField("Set-Cookie");
50                  assertTrue(sessionCookie != null);
51                  // Mangle the cookie, replacing Path with $Path, etc.
52                  sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
53  
54                  //now delete the session
55                  exchange = new ContentExchange(true);
56                  exchange.setMethod(HttpMethods.GET);
57                  exchange.setURL("http://localhost:" + port + contextPath + servletMapping + "?action=delete");
58                  exchange.getRequestFields().add("Cookie", sessionCookie);
59                  client.send(exchange);
60                  exchange.waitForDone();
61                  assertEquals(HttpServletResponse.SC_OK,exchange.getResponseStatus());
62                  
63                  
64                  // The session is not there anymore, but we present an old cookie
65                  // The server creates a new session, we must ensure we released all locks
66                  exchange = new ContentExchange(true);
67                  exchange.setMethod(HttpMethods.GET);
68                  exchange.setURL("http://localhost:" + port + contextPath + servletMapping + "?action=check");
69                  exchange.getRequestFields().add("Cookie", sessionCookie);
70                  client.send(exchange);
71                  exchange.waitForDone();
72                  assertEquals(HttpServletResponse.SC_OK,exchange.getResponseStatus());
73              }
74              finally
75              {
76                  client.stop();
77              }
78          }
79          finally
80          {
81              server.stop();
82          }
83  
84      }
85      public static class TestServlet extends HttpServlet
86      {
87          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
88          {
89              String action = request.getParameter("action");
90              if ("create".equals(action))
91              {
92                  request.getSession(true);
93              }
94              else if ("delete".equals(action))
95              {
96                  HttpSession s = request.getSession(false);
97                  assertNotNull(s);
98                  s.invalidate();
99                  s = request.getSession(false);
100                 assertNull(s);
101             }
102             else
103             {
104                HttpSession s = request.getSession(false);
105                assertNull(s);
106             }
107         }
108     }
109     
110 }