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