private HttpSession getSessionInstance() { if (!sessionWasRequested) { sessionWasRequested = true; String sessionId = getRequestedSessionId(); if (sessionId != null) { session = servletContext.getSession(sessionId); } } return session; }
@Test public void shouldReturnSessionForValidSID() throws IOException { HttpSessionImpl session = new HttpSessionImpl("123", System.currentTimeMillis()); when(sessionStorage.getSession("123")).thenReturn(session); HttpSessionImpl sessionRead = servletContext.getSession("123"); assertThat(sessionRead, is(not(nullValue()))); assertThat(sessionRead.getServletContext(), is((ServletContext) servletContext)); }
@Test public void shouldReturnTheSameSessionForConsecutiveCalls() { Map<String, Cookie> cookies = new HashMap<>(); Cookie sessionCookie = new Cookie(HttpSessionImpl.COOKIE_NAME, "sessionId"); cookies.put(HttpSessionImpl.COOKIE_NAME, sessionCookie); httpServletRequestImpl = builder.withCookies(cookies).withServletContext(servletContext).build(); when(servletContext.getSession("sessionId")).thenReturn(new HttpSessionImpl("sessionId", System.currentTimeMillis())); assertThat(httpServletRequestImpl.getSession(), is(instanceOf(HttpSessionImpl.class))); assertThat(httpServletRequestImpl.getSession().equals(httpServletRequestImpl.getSession()), is(true)); }
@Test public void shouldReturnSession() { Map<String, Cookie> cookies = new HashMap<>(); Cookie sessionCookie = new Cookie(HttpSessionImpl.COOKIE_NAME, "sessionId"); cookies.put(HttpSessionImpl.COOKIE_NAME, sessionCookie); httpServletRequestImpl = builder.withCookies(cookies).withServletContext(servletContext).build(); when(servletContext.getSession("sessionId")).thenReturn(new HttpSessionImpl("sessionId", System.currentTimeMillis())); assertThat(httpServletRequestImpl.getSession(), is(instanceOf(HttpSessionImpl.class))); }
@Test public void shouldRemoveExpiredSession() throws IOException { HttpSessionImpl session = new HttpSessionImpl("123", System.currentTimeMillis()); session.setLastAccessedTime(System.currentTimeMillis() - session.getMaxInactiveInterval() * 1000 - 1); when(sessionStorage.getSession("123")).thenReturn(session); HttpSessionImpl sessionRead = servletContext.getSession("123"); verify(sessionStorage, times(1)).removeSession(session); assertThat(sessionRead, is(nullValue())); }