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  
26  import javax.servlet.ServletException;
27  import javax.servlet.http.HttpServlet;
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  import javax.servlet.http.HttpSession;
31  
32  import junit.framework.Assert;
33  
34  import org.eclipse.jetty.client.HttpClient;
35  import org.eclipse.jetty.client.api.ContentResponse;
36  import org.eclipse.jetty.client.api.Request;
37  import org.eclipse.jetty.servlet.ServletContextHandler;
38  import org.junit.Ignore;
39  import org.junit.Test;
40  
41  /**
42   * AbstractSessionCookieTest
43   */
44  public abstract class AbstractSessionCookieTest
45  {
46      public abstract AbstractTestServer createServer(int port, int max, int scavenge);
47  
48      public void pause(int scavenge)
49      {
50          try
51          {
52              Thread.sleep(scavenge * 2500L);
53          }
54          catch (InterruptedException e)
55          {
56              e.printStackTrace();
57          }
58      }
59  
60      @Test
61      @Ignore("failing because an http cookie with null value is coming over as \"null\"")
62      public void testSessionCookie() throws Exception
63      {
64          String contextPath = "";
65          String servletMapping = "/server";
66          int scavengePeriod = 3;
67          AbstractTestServer server = createServer(0, 1, scavengePeriod);
68          ServletContextHandler context = server.addContext(contextPath);
69          context.addServlet(TestServlet.class, servletMapping);
70  
71          try
72          {
73              server.start();
74              int port=server.getPort();
75              
76              HttpClient client = new HttpClient();
77              client.start();
78              try
79              {
80  
81                  ContentResponse response = client.GET("http://localhost:" + port + contextPath + servletMapping + "?action=create");
82                  assertEquals(HttpServletResponse.SC_OK,response.getStatus());
83  
84                  String sessionCookie = response.getHeaders().get("Set-Cookie");
85                  assertTrue(sessionCookie != null);
86                  // Mangle the cookie, replacing Path with $Path, etc.
87                  //sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
88  
89                  // Let's wait for the scavenger to run, waiting 2.5 times the scavenger period
90                  //pause(scavengePeriod);
91                  Request request = client.newRequest("http://localhost:" + port + contextPath + servletMapping + "?action=check-cookie");
92                  request.header("Cookie", sessionCookie);
93                  response = request.send();
94  
95                  assertEquals(HttpServletResponse.SC_OK,response.getStatus());
96  
97                  request = client.newRequest("http://localhost:" + port + contextPath + servletMapping + "?action=null-cookie");
98                  request.header("Cookie", sessionCookie);
99                  response = request.send();
100                 assertEquals(HttpServletResponse.SC_OK,response.getStatus());
101             }
102             finally
103             {
104                 client.stop();
105             }
106         }
107         finally
108         {
109             server.stop();
110         }
111 
112     }
113     public static class TestServlet extends HttpServlet
114     {
115         @Override
116         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
117         {
118             String action = request.getParameter("action");
119             if ("create".equals(action))
120             {
121                 HttpSession session = request.getSession(true);
122                 assertTrue(session.isNew());
123             }
124             else if ("check-cookie".equals(action))
125             {
126                 HttpSession session = request.getSession(false);
127 
128                 assertTrue(session != null);
129 
130                 //request.getSession(true);
131             }
132             else if ("null-cookie".equals(action))
133             {
134                 HttpSession session = request.getSession(false);
135 
136                 assertEquals(1, request.getCookies().length);
137 
138                 Assert.assertFalse("null".equals(request.getCookies()[0].getValue()));
139 
140                 assertTrue(session == null);
141 
142             }
143             else
144             {
145                 assertTrue(false);
146             }
147         }
148     }
149 }