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  import javax.servlet.http.HttpSession;
24  
25  import org.eclipse.jetty.client.ContentExchange;
26  import org.eclipse.jetty.client.HttpClient;
27  import org.eclipse.jetty.http.HttpMethods;
28  import org.eclipse.jetty.servlet.ServletContextHandler;
29  import org.junit.Test;
30  import static org.junit.Assert.assertEquals;
31  import static org.junit.Assert.assertTrue;
32  
33  /**
34   * AbstractNewSessionTest
35   */
36  public abstract class AbstractNewSessionTest
37  {
38      public abstract AbstractTestServer createServer(int port, int max, int scavenge);
39  
40      public void pause(int scavenge)
41      {
42          try
43          {
44              Thread.sleep(scavenge * 2500L);
45          }
46          catch (InterruptedException e)
47          {
48              e.printStackTrace();
49          }
50      }
51      
52      @Test
53      public void testNewSession() throws Exception
54      {
55          String contextPath = "";
56          String servletMapping = "/server";
57          int scavengePeriod = 3;
58          AbstractTestServer server = createServer(0, 1, scavengePeriod);
59          ServletContextHandler context = server.addContext(contextPath);
60          context.addServlet(TestServlet.class, servletMapping);
61          server.start();
62          int port=server.getPort();
63          try
64          {
65              HttpClient client = new HttpClient();
66              client.setConnectorType(HttpClient.CONNECTOR_SOCKET);
67              client.start();
68              try
69              {
70                  ContentExchange exchange = new ContentExchange(true);
71                  exchange.setMethod(HttpMethods.GET);
72                  exchange.setURL("http://localhost:" + port + contextPath + servletMapping + "?action=create");
73                  client.send(exchange);
74                  exchange.waitForDone();
75                  assertEquals(HttpServletResponse.SC_OK,exchange.getResponseStatus());
76                  String sessionCookie = exchange.getResponseFields().getStringField("Set-Cookie");
77                  assertTrue(sessionCookie != null);
78                  // Mangle the cookie, replacing Path with $Path, etc.
79                  sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
80  
81                  // Let's wait for the scavenger to run, waiting 2.5 times the scavenger period
82                  pause(scavengePeriod);
83  
84                  // The session is not there anymore, but we present an old cookie
85                  // The server creates a new session, we must ensure we released all locks
86                  exchange = new ContentExchange(true);
87                  exchange.setMethod(HttpMethods.GET);
88                  exchange.setURL("http://localhost:" + port + contextPath + servletMapping + "?action=old-create");
89                  exchange.getRequestFields().add("Cookie", sessionCookie);
90                  client.send(exchange);
91                  exchange.waitForDone();
92                  assertEquals(HttpServletResponse.SC_OK,exchange.getResponseStatus());
93              }
94              finally
95              {
96                  client.stop();
97              }
98          }
99          finally
100         {
101             server.stop();
102         }
103 
104     }
105     public static class TestServlet extends HttpServlet
106     {
107         @Override
108         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
109         {
110             String action = request.getParameter("action");
111             if ("create".equals(action))
112             {
113                 HttpSession session = request.getSession(true);
114                 assertTrue(session.isNew());
115             }
116             else if ("old-create".equals(action))
117             {
118                 request.getSession(true);
119             }
120             else
121             {
122                 assertTrue(false);
123             }
124         }
125     }
126 }