HttpRequestWrapper extends HttpServletRequestWrapper implements RequestWithSession { @Override public RepositoryBackedHttpSession getSession() { return getSession(true); } HttpRequestWrapper(HttpServletRequest req, ServletContext servletContext); @Override RepositoryBackedHttpSession getSession(); @Override RepositoryBackedHttpSession getSession(boolean create); SessionManager getManager(); HttpResponseWrapper getResponse(); void setResponse(HttpResponseWrapper response); void commit(); @Override RepositoryBackedHttpSession getRepositoryBackedSession(boolean create); @Override ServletContext getServletContext(); HttpRequestWrapper getEmbeddedRequest(); boolean isPropagateOnCreate(); void setPropagateOnCreate(boolean propagate); @Override boolean isIdRetrieved(); @Override void setRequestedSessionId(String id, boolean isFromCookie); @Override String getRequestedSessionId(); @Override boolean isRequestedSessionIdFromCookie(); @Override boolean isRequestedSessionIdFromURL(); @Override boolean isRequestedSessionIdFromUrl(); @Override boolean isRequestedSessionIdValid(); @Override boolean isRepositoryChecked(); @Override void repositoryChecked(); String encodeURL(String url); @Override String changeSessionId();  }
@Test public void testGetSession() { HttpServletRequest wrappedSimple = mock(HttpServletRequest.class); RepositoryBackedHttpSession session = mock(RepositoryBackedHttpSession.class); when(session.getId()).thenReturn(SESSION_ID); HttpRequestWrapper wrappedHttpRequestWrapper = spy(new HttpRequestWrapper(wrappedSimple, servletContext)); wrappedHttpRequestWrapper.session = session; HttpServletRequest wrappedComplex = new HttpServletRequestWrapper(wrappedHttpRequestWrapper); HttpRequestWrapper req = new HttpRequestWrapper(wrappedComplex, servletContext); req.getSession(); verify(wrappedHttpRequestWrapper).getSession(true); verify(wrappedHttpRequestWrapper).getSession(true); }
@Test public void testNewSessionIdCreatedIfSessionWasInvalidated() { HttpServletRequest wrappedSimple = mock(HttpServletRequest.class); RepositoryBackedHttpSession invalidSession = mock(RepositoryBackedHttpSession.class); RepositoryBackedHttpSession newSession = mock(RepositoryBackedHttpSession.class); when(newSession.getId()).thenReturn(NEW_SESSION_ID); when(invalidSession.getId()).thenReturn(SESSION_ID); when(invalidSession.isValid()).thenReturn(false); HttpRequestWrapper wrappedHttpRequestWrapper = spy(new HttpRequestWrapper(wrappedSimple, servletContext)); wrappedHttpRequestWrapper.session = invalidSession; HttpServletRequest wrappedComplex = new HttpServletRequestWrapper(wrappedHttpRequestWrapper); HttpRequestWrapper req = new HttpRequestWrapper(wrappedComplex, servletContext); when(sessionManager.getSession(req, true, new SessionTracking.IdAndSource(SESSION_ID, false))).thenReturn(invalidSession); when(sessionManager.getSession(req, true, null)).thenReturn(newSession); RepositoryBackedHttpSession session2 = req.getSession(); Assert.assertNotNull(session2); assertEquals(NEW_SESSION_ID, session2.getId()); }