Fix ee11 session CreationTest to be same as ee10.

This commit is contained in:
Jan Bartel 2024-07-24 11:29:45 +10:00
parent 7ca1ef707e
commit 0ed14746f7
1 changed files with 142 additions and 62 deletions

View File

@ -297,6 +297,50 @@ public class CreationTest
} }
} }
@Test
public void testSessionCreateReForward() throws Exception
{
String contextPath = "";
String contextC = "/contextC";
String servletMapping = "/server";
int inactivePeriod = 20;
int scavengePeriod = 3;
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
cacheFactory.setFlushOnResponseCommit(true); //ensure session is saved before response comes back
SessionDataStoreFactory storeFactory = new TestSessionDataStoreFactory();
SessionTestSupport server1 = new SessionTestSupport(0, inactivePeriod, scavengePeriod, cacheFactory, storeFactory);
TestServlet servlet = new TestServlet();
ServletHolder holder = new ServletHolder(servlet);
ServletContextHandler contextHandler = server1.addContext(contextPath);
contextHandler.setCrossContextDispatchSupported(true);
contextHandler.addServlet(holder, servletMapping);
ServletContextHandler ctxC = server1.addContext(contextC);
ctxC.setCrossContextDispatchSupported(true);
ctxC.addServlet(TestServletC.class, servletMapping);
server1.start();
int port1 = server1.getPort();
try (StacklessLogging stackless = new StacklessLogging(CreationTest.class.getPackage()))
{
HttpClient client = new HttpClient();
client.start();
String url = "http://localhost:" + port1 + contextPath + servletMapping;
ContentResponse response = client.GET(url + "?action=forwardC");
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
//check that the sessions exist persisted
Awaitility.waitAtMost(5, TimeUnit.SECONDS).until(() -> contextHandler.getSessionHandler().getSessionCache().getSessionDataStore().exists(servlet._id));
Awaitility.waitAtMost(5, TimeUnit.SECONDS).until(() -> ctxC.getSessionHandler().getSessionCache().getSessionDataStore().exists(servlet._id));
}
finally
{
server1.stop();
}
}
/** /**
* Create a session in a context, forward to another context and create a * Create a session in a context, forward to another context and create a
* session in it too. Check that both sessions exist after the response * session in it too. Check that both sessions exist after the response
@ -426,64 +470,71 @@ public class CreationTest
protected void doGet(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException protected void doGet(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException
{ {
String action = request.getParameter("action"); String action = request.getParameter("action");
switch (action.toLowerCase())
if (action != null && action.startsWith("forward"))
{ {
HttpSession session = request.getSession(true); case "forward" ->
{
_id = session.getId(); HttpSession session = createAndSaveSessionId(request);
session.setAttribute("value", 1);
ServletContext contextB = getServletContext().getContext("/contextB"); ServletContext contextB = getServletContext().getContext("/contextB");
RequestDispatcher dispatcherB = contextB.getRequestDispatcher(request.getServletPath()); RequestDispatcher dispatcherB = contextB.getRequestDispatcher(request.getServletPath());
dispatcherB.forward(request, httpServletResponse); dispatcherB.forward(request, httpServletResponse);
if (action.endsWith("inv"))
{
session.invalidate();
}
else
{
session = request.getSession(false); session = request.getSession(false);
assertNotNull(session); assertNotNull(session);
assertEquals(_id, session.getId()); assertEquals(_id, session.getId());
assertNotNull(session.getAttribute("value")); assertNotNull(session.getAttribute("value"));
assertNull(session.getAttribute("B")); //check we don't see stuff from other context assertNull(session.getAttribute("B")); //check we don't see stuff from other context
} }
return; case "forwardc" ->
}
else if ("test".equals(action))
{ {
HttpSession session = createAndSaveSessionId(request);
//forward to contextC
ServletContext contextC = getServletContext().getContext("/contextC");
RequestDispatcher dispatcherC = contextC.getRequestDispatcher(request.getServletPath());
dispatcherC.forward(request, httpServletResponse);
}
case "forwardinv" ->
{
HttpSession session = createAndSaveSessionId(request);
assertNotNull(session);
ServletContext contextB = getServletContext().getContext("/contextB");
RequestDispatcher dispatcherB = contextB.getRequestDispatcher(request.getServletPath());
dispatcherB.forward(request, httpServletResponse);
session.invalidate();
}
case "test" ->
{
assertNotNull(_id);
HttpSession session = request.getSession(false); HttpSession session = request.getSession(false);
assertNotNull(session); assertNotNull(session);
return; assertNotNull(session.getAttribute("value")); //check we see our previous session
assertNull(session.getAttribute("B")); //check we don't see stuff from other contexts
assertNull(session.getAttribute("C"));
} }
else if (action != null && action.startsWith("create")) case "create", "createinv", "createinvcreate" ->
{ {
currentRequest.set(request); currentRequest.set(request);
HttpSession session = request.getSession(true); HttpSession session = createAndSaveSessionId(request);
_id = session.getId();
session.setAttribute("value", 1);
System.err.println("Created session " + _id);
String check = request.getParameter("check"); String check = request.getParameter("check");
if (!StringUtil.isBlank(check) && _store != null) if (!StringUtil.isBlank(check) && _store != null)
{ {
boolean exists; boolean exists = false;
try try
{ {
exists = _store.exists(_id); exists = _store.exists(_id);
System.err.println("Does session exist in store: " + exists);
} }
catch (Exception e) catch (Exception e)
{ {
throw new ServletException(e); throw new ServletException(e);
} }
if ("false".equalsIgnoreCase(check)) switch (check.toLowerCase())
assertFalse(exists); {
else case "true" -> assertTrue(exists);
assertTrue(exists); case "false" -> assertFalse(exists);
}
} }
if ("createinv".equals(action)) if ("createinv".equals(action))
@ -495,17 +546,25 @@ public class CreationTest
else if ("createinvcreate".equals(action)) else if ("createinvcreate".equals(action))
{ {
session.invalidate(); session.invalidate();
System.err.println("Session invalidated " + _id);
assertNull(request.getSession(false)); assertNull(request.getSession(false));
assertNotNull(session); assertNotNull(session);
session = request.getSession(true); session = request.getSession(true);
_id = session.getId(); _id = session.getId();
System.err.println("Created another session " + _id);
} }
} }
} }
} }
private HttpSession createAndSaveSessionId(HttpServletRequest request)
{
HttpSession session = request.getSession(true);
_id = session.getId();
session.setAttribute("value", 1);
return session;
}
}
public static class TestServletB extends HttpServlet public static class TestServletB extends HttpServlet
{ {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@ -525,4 +584,25 @@ public class CreationTest
session.setAttribute("B", "B"); session.setAttribute("B", "B");
} }
} }
public static class TestServletC extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException
{
HttpSession session = request.getSession(false);
assertNull(session);
session = request.getSession(true);
// Be sure nothing from contextA is present
Object objectA = session.getAttribute("value");
assertNull(objectA);
session.setAttribute("C", "C");
//forward back to A
ServletContext contextA = getServletContext().getContext("/");
RequestDispatcher dispatcherA = contextA.getRequestDispatcher(request.getServletPath() + "?action=test");
dispatcherA.forward(request, httpServletResponse);
}
}
} }