1 package org.eclipse.jetty.server.session;
2
3 import static org.junit.Assert.*;
4
5 import java.io.IOException;
6 import java.util.EventListener;
7
8 import javax.servlet.ServletException;
9 import javax.servlet.http.HttpServlet;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12 import javax.servlet.http.HttpSession;
13 import javax.servlet.http.HttpSessionActivationListener;
14 import javax.servlet.http.HttpSessionEvent;
15 import javax.servlet.http.HttpSessionListener;
16
17 import org.eclipse.jetty.client.ContentExchange;
18 import org.eclipse.jetty.client.HttpClient;
19 import org.eclipse.jetty.http.HttpMethods;
20 import org.eclipse.jetty.servlet.ServletContextHandler;
21 import org.junit.Test;
22
23 public abstract class AbstractRemoveSessionTest
24 {
25 public abstract AbstractTestServer createServer(int port, int max, int scavenge);
26
27
28 @Test
29 public void testRemoveSession() throws Exception
30 {
31 String contextPath = "";
32 String servletMapping = "/server";
33 int scavengePeriod = 3;
34 AbstractTestServer server = createServer(0, 1, scavengePeriod);
35 ServletContextHandler context = server.addContext(contextPath);
36 context.addServlet(TestServlet.class, servletMapping);
37 TestEventListener testListener = new TestEventListener();
38 context.getSessionHandler().addEventListener(testListener);
39 server.start();
40 int port = server.getPort();
41 try
42 {
43 HttpClient client = new HttpClient();
44 client.setConnectorType(HttpClient.CONNECTOR_SOCKET);
45 client.start();
46 try
47 {
48 ContentExchange exchange = new ContentExchange(true);
49 exchange.setMethod(HttpMethods.GET);
50 exchange.setURL("http://localhost:" + port + contextPath + servletMapping + "?action=create");
51 client.send(exchange);
52 exchange.waitForDone();
53 assertEquals(HttpServletResponse.SC_OK,exchange.getResponseStatus());
54 String sessionCookie = exchange.getResponseFields().getStringField("Set-Cookie");
55 assertTrue(sessionCookie != null);
56
57 sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
58
59 assertTrue (testListener.isCreated());
60
61
62 exchange = new ContentExchange(true);
63 exchange.setMethod(HttpMethods.GET);
64 exchange.setURL("http://localhost:" + port + contextPath + servletMapping + "?action=delete");
65 exchange.getRequestFields().add("Cookie", sessionCookie);
66 client.send(exchange);
67 exchange.waitForDone();
68 assertEquals(HttpServletResponse.SC_OK,exchange.getResponseStatus());
69
70 assertTrue(testListener.isDestroyed());
71
72
73
74
75 exchange = new ContentExchange(true);
76 exchange.setMethod(HttpMethods.GET);
77 exchange.setURL("http://localhost:" + port + contextPath + servletMapping + "?action=check");
78 exchange.getRequestFields().add("Cookie", sessionCookie);
79 client.send(exchange);
80 exchange.waitForDone();
81 assertEquals(HttpServletResponse.SC_OK,exchange.getResponseStatus());
82 }
83 finally
84 {
85 client.stop();
86 }
87 }
88 finally
89 {
90 server.stop();
91 }
92
93 }
94 public static class TestServlet extends HttpServlet
95 {
96 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
97 {
98 String action = request.getParameter("action");
99 if ("create".equals(action))
100 {
101 request.getSession(true);
102 }
103 else if ("delete".equals(action))
104 {
105 HttpSession s = request.getSession(false);
106 assertNotNull(s);
107 s.invalidate();
108 s = request.getSession(false);
109 assertNull(s);
110 }
111 else
112 {
113 HttpSession s = request.getSession(false);
114 assertNull(s);
115 }
116 }
117 }
118
119 public static class TestEventListener implements HttpSessionListener
120 {
121 boolean wasCreated;
122 boolean wasDestroyed;
123
124 public void sessionCreated(HttpSessionEvent se)
125 {
126 wasCreated = true;
127 }
128
129 public void sessionDestroyed(HttpSessionEvent se)
130 {
131 wasDestroyed = true;
132 }
133
134 public boolean isDestroyed()
135 {
136 return wasDestroyed;
137 }
138
139
140 public boolean isCreated()
141 {
142 return wasCreated;
143 }
144
145 }
146
147 }