View Javadoc

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