View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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 static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.io.IOException;
25  import java.io.PrintWriter;
26  
27  import javax.servlet.ServletException;
28  import javax.servlet.http.HttpServlet;
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  import javax.servlet.http.HttpSession;
32  
33  import org.eclipse.jetty.client.HttpClient;
34  import org.eclipse.jetty.client.api.ContentResponse;
35  import org.eclipse.jetty.client.api.Request;
36  import org.junit.Test;
37  
38  
39  /**
40   * AbstractImmortalSessionTest
41   */
42  public abstract class AbstractImmortalSessionTest
43  {
44      public abstract AbstractTestServer createServer(int port, int maxInactiveMs, int scavengeMs);
45  
46      @Test
47      public void testImmortalSession() throws Exception
48      {
49          String contextPath = "";
50          String servletMapping = "/server";
51          int scavengePeriod = 2;
52          //turn off session expiry by setting maxInactiveInterval to -1
53          AbstractTestServer server = createServer(0, -1, scavengePeriod);
54          server.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
55  
56          try
57          {
58              server.start();
59              int port=server.getPort();
60              HttpClient client = new HttpClient();
61              client.start();
62              try
63              {
64                  int value = 42;
65                  ContentResponse response = client.GET("http://localhost:" + port + contextPath + servletMapping + "?action=set&value=" + value);
66                  assertEquals(HttpServletResponse.SC_OK,response.getStatus());
67                  String sessionCookie = response.getHeaders().get("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  
72                  String resp = response.getContentAsString();
73                  assertEquals(resp.trim(),String.valueOf(value));
74  
75                  // Let's wait for the scavenger to run, waiting 2.5 times the scavenger period
76                  Thread.sleep(scavengePeriod * 2500L);
77  
78                  // Be sure the session is still there
79                  Request request = client.newRequest("http://localhost:" + port + contextPath + servletMapping + "?action=get");
80                  request.header("Cookie", sessionCookie);
81                  response = request.send();
82                  assertEquals(HttpServletResponse.SC_OK,response.getStatus());
83                  resp = response.getContentAsString();
84                  assertEquals(String.valueOf(value),resp.trim());
85              }
86              finally
87              {
88                  client.stop();
89              }
90          }
91          finally
92          {
93              server.stop();
94          }
95      }
96  
97      public static class TestServlet extends HttpServlet
98      {
99          @Override
100         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
101         {
102             String result = null;
103             String action = request.getParameter("action");
104             if ("set".equals(action))
105             {
106                 String value = request.getParameter("value");
107                 HttpSession session = request.getSession(true);
108                 session.setAttribute("value", value);
109                 result = value;
110             }
111             else if ("get".equals(action))
112             {
113                 HttpSession session = request.getSession(false);
114                 if (session!=null)
115                     result = (String)session.getAttribute("value");
116             }
117             PrintWriter writer = response.getWriter();
118             writer.println(result);
119             writer.flush();
120         }
121     }
122 }