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