View Javadoc

1   // ========================================================================
2   // Copyright 2004-2010 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses.
12  // ========================================================================
13  
14  package org.eclipse.jetty.server.session;
15  
16  import java.io.IOException;
17  import java.util.Random;
18  
19  import javax.servlet.ServletException;
20  import javax.servlet.http.HttpServlet;
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletResponse;
23  
24  import org.eclipse.jetty.client.ContentExchange;
25  import org.eclipse.jetty.client.HttpClient;
26  import org.eclipse.jetty.http.HttpMethods;
27  import org.eclipse.jetty.servlet.ServletContextHandler;
28  import org.junit.Test;
29  import static org.junit.Assert.assertEquals;
30  import static org.junit.Assert.assertTrue;
31  
32  /**
33   * AbstractNewSessionTest
34   */
35  public abstract class AbstractNewSessionTest
36  {
37      public abstract AbstractTestServer createServer(int port, int max, int scavenge);
38  
39      public void pause(int scavenge)
40      {
41          try
42          {
43              Thread.sleep(scavenge * 2500L);
44          }
45          catch (InterruptedException e)
46          {
47              e.printStackTrace();
48          }
49      }
50      
51      @Test
52      public void testNewSession() throws Exception
53      {
54          String contextPath = "";
55          String servletMapping = "/server";
56          int scavengePeriod = 3;
57          AbstractTestServer server = createServer(0, 1, scavengePeriod);
58          ServletContextHandler context = server.addContext(contextPath);
59          context.addServlet(TestServlet.class, servletMapping);
60          server.start();
61          int port=server.getPort();
62          try
63          {
64              HttpClient client = new HttpClient();
65              client.setConnectorType(HttpClient.CONNECTOR_SOCKET);
66              client.start();
67              try
68              {
69                  ContentExchange exchange = new ContentExchange(true);
70                  exchange.setMethod(HttpMethods.GET);
71                  exchange.setURL("http://localhost:" + port + contextPath + servletMapping + "?action=create");
72                  client.send(exchange);
73                  exchange.waitForDone();
74                  assertEquals(HttpServletResponse.SC_OK,exchange.getResponseStatus());
75                  String sessionCookie = exchange.getResponseFields().getStringField("Set-Cookie");
76                  assertTrue(sessionCookie != null);
77                  // Mangle the cookie, replacing Path with $Path, etc.
78                  sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
79  
80                  // Let's wait for the scavenger to run, waiting 2.5 times the scavenger period
81                  pause(scavengePeriod);
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                  exchange = new ContentExchange(true);
86                  exchange.setMethod(HttpMethods.GET);
87                  exchange.setURL("http://localhost:" + port + contextPath + servletMapping + "?action=old-create");
88                  exchange.getRequestFields().add("Cookie", sessionCookie);
89                  client.send(exchange);
90                  exchange.waitForDone();
91                  assertEquals(HttpServletResponse.SC_OK,exchange.getResponseStatus());
92              }
93              finally
94              {
95                  client.stop();
96              }
97          }
98          finally
99          {
100             server.stop();
101         }
102 
103     }
104     public static class TestServlet extends HttpServlet
105     {
106         @Override
107         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
108         {
109             String action = request.getParameter("action");
110             if ("create".equals(action))
111             {
112                 request.getSession(true);
113             }
114             else if ("old-create".equals(action))
115             {
116                 request.getSession(true);
117             }
118             else
119             {
120                 assertTrue(false);
121             }
122         }
123     }
124 }