1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
92
93
94
95
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
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
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 }