/** * Creates a new session. * * @return */ public HttpSessionImpl createNewSession() { HttpSessionImpl session = new HttpSessionImpl(StringUtilities.generateRandom(), System.currentTimeMillis()); session.setServletContext(this); LOGGER.log(Level.FINE, "Created a new session {0}", new Object[]{session.getId()}); return session; }
@Test(expected = IOException.class) public void shouldThrowExceptionWhenUnableToCreateFile() throws IOException { String nonExistentDirectory = "/tmp/nonexistent-" + Math.random() + "/"; assertThat(new File(nonExistentDirectory).exists(), is(false)); SessionStorage sessionStorage = new FileSessionStorage(nonExistentDirectory); sessionStorage.persistSession(new HttpSessionImpl(VALID_SESSION_ID, System.currentTimeMillis())); }
@Test(expected = IllegalArgumentException.class) public void shouldValidateSessionNameLength() throws IOException { HttpSessionImpl sessionWrapper = new HttpSessionImpl("abcX8", System.currentTimeMillis()); fileSessionStorage.persistSession(sessionWrapper); }
@Test public void shouldHandleSessionOnTerminateIfSessionExists() throws IOException { when(request.getSession(false)).thenReturn(new HttpSessionImpl("1", System.currentTimeMillis())); servletResourceProvider.load("/", request, response); verify(servletContext, times(1)).handleSession(any(HttpSessionImpl.class), any(HttpServletResponseImpl.class)); }
@Test(expected = IllegalArgumentException.class) public void shouldValidateSessionNameNull() throws IOException { HttpSessionImpl sessionWrapper = new HttpSessionImpl(null, System.currentTimeMillis()); fileSessionStorage.persistSession(sessionWrapper); }
@Test(expected = IllegalArgumentException.class) public void shouldValidateSessionNameIllegalCharacters() throws IOException { HttpSessionImpl sessionWrapper = new HttpSessionImpl(ILLEGAL_SESSION_ID, System.currentTimeMillis()); fileSessionStorage.persistSession(sessionWrapper); }
@Before public void setUp() { session = new HttpSessionImpl("123", System.currentTimeMillis()); session.setAttribute("attribute", "value"); }
@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 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())); }
@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 shouldPersistRestoreAndRemoveSession() throws IOException { HttpSessionImpl sessionWrapper = new HttpSessionImpl(VALID_SESSION_ID, System.currentTimeMillis()); sessionWrapper.setAttribute("attributeName", "SomeValue"); fileSessionStorage.persistSession(sessionWrapper); sessionWrapper = fileSessionStorage.getSession(VALID_SESSION_ID); assertThat(sessionWrapper, is(not(nullValue()))); assertThat((String) sessionWrapper.getAttribute("attributeName"), is("SomeValue")); fileSessionStorage.removeSession(sessionWrapper); sessionWrapper = fileSessionStorage.getSession(VALID_SESSION_ID); assertThat(sessionWrapper, is(nullValue())); }
@Test(expected = IOException.class) public void shouldThrowExceptionWhenUnableToCreateSessionDirectory() throws IOException { String nonWritableDirectory = FileUtils.createTempDirectory(); new File(nonWritableDirectory).setWritable(false); FileSessionStorage fileSessionStorage = new FileSessionStorage(nonWritableDirectory); HttpSessionImpl sessionWrapper = new HttpSessionImpl(VALID_SESSION_ID, System.currentTimeMillis()); sessionWrapper.setAttribute("attributeName", "SomeValue"); fileSessionStorage.persistSession(sessionWrapper); }
@Test public void shouldSetCookieAndPersistForValidSession() throws IOException { HttpSessionImpl session = new HttpSessionImpl("123", System.currentTimeMillis()); servletContext.handleSession(session, response); verify(sessionStorage, times(1)).persistSession(session); assertThat(response.getCookies().size(), is(greaterThan(0))); for (Cookie cookie : response.getCookies()) { if (cookie.getName().equals(HttpSessionImpl.COOKIE_NAME)) { assertThat(cookie.getValue(), is(not(nullValue()))); return; } } fail("Session cookie was not set."); }
@Test public void shouldPersistSessionAndOverWriteFile() throws IOException { HttpSessionImpl sessionWrapper = new HttpSessionImpl(VALID_SESSION_ID, System.currentTimeMillis()); sessionWrapper.setAttribute("attributeName", "SomeValue"); fileSessionStorage.persistSession(sessionWrapper); sessionWrapper = fileSessionStorage.getSession(VALID_SESSION_ID); assertThat(sessionWrapper, is(not(nullValue()))); assertThat((String) sessionWrapper.getAttribute("attributeName"), is("SomeValue")); HttpSessionImpl session2Wrapper = new HttpSessionImpl(VALID_SESSION_ID, System.currentTimeMillis()); session2Wrapper.setAttribute("otherName", "OtherValue"); fileSessionStorage.persistSession(session2Wrapper); sessionWrapper = fileSessionStorage.getSession(VALID_SESSION_ID); assertThat(sessionWrapper, is(not(nullValue()))); assertThat(sessionWrapper.getAttribute("attributeName"), is(nullValue())); assertThat((String) sessionWrapper.getAttribute("otherName"), is("OtherValue")); fileSessionStorage.removeSession(sessionWrapper); sessionWrapper = fileSessionStorage.getSession(VALID_SESSION_ID); assertThat(sessionWrapper, is(nullValue())); }
@Test public void shouldEraseCookieAndRemoveForInvalidatedSession() throws IOException { HttpSessionImpl session = new HttpSessionImpl("123", System.currentTimeMillis()); session.invalidate(); servletContext.handleSession(session, response); verify(sessionStorage, times(1)).removeSession(session); assertThat(response.getCookies().size(), is(greaterThan(0))); for (Cookie cookie : response.getCookies()) { if (cookie.getName().equals(HttpSessionImpl.COOKIE_NAME)) { assertThat(cookie.getMaxAge(), lessThan(-1)); return; } } fail("Session DELETE cookie was not set."); }