CookieSessionTracking extends BaseSessionTracking implements SessionTracking { @Override public IdAndSource retrieveId(RequestWithSession request) { Cookie[] cookies = ((HttpServletRequest)request).getCookies(); if (cookies != null) { for (Cookie cookie : cookies) { if (idName.equals(cookie.getName())) { String id = clean(cookie.getValue()); if (id != null) { return new IdAndSource(id, isCookieTracking()); } else { return null; } } } } if (this.nextSessionTracking != null) { return this.nextSessionTracking.retrieveId(request); } return null; }  @Override IdAndSource retrieveId(RequestWithSession request); @Override void propagateSession(RequestWithSession request, Object response); @Override void configure(SessionConfiguration conf); @Override boolean isCookieTracking();  }
@Test public void testRetrieveId() { SessionConfiguration sc = new SessionConfiguration(); sc.setSessionIdName("somesession"); sc.setAttribute("com.amadeus.session.id", "uuid"); cookieSessionTracking.configure(sc); RequestWithSession request = mock(RequestWithSession.class, withSettings().extraInterfaces(HttpServletRequest.class)); HttpServletRequest hsr = (HttpServletRequest)request; UUID uuid = UUID.randomUUID(); UUID uuidUrl = UUID.randomUUID(); when(hsr.getRequestURI()).thenReturn("/url;somesession=" + uuidUrl); when(hsr.getCookies()).thenReturn(new Cookie[] { new Cookie("somesession", uuid.toString()) }); assertEquals(uuid.toString(), cookieSessionTracking.retrieveId(request).id); when(hsr.getCookies()).thenReturn(new Cookie[] { new Cookie("notused", uuid.toString()) }); assertNull(cookieSessionTracking.retrieveId(request)); when(hsr.getCookies()).thenReturn(new Cookie[] { new Cookie("othercookie", "ABC"), new Cookie("somesession", uuid.toString()) }); assertEquals(uuid.toString(), cookieSessionTracking.retrieveId(request).id); String sessionIdWithTimestamp = uuid.toString() + BaseSessionTracking.SESSION_ID_TIMESTAMP_SEPARATOR + System.currentTimeMillis(); String invalidSessionIdWithTimestamp = uuid.toString() + "-abcdefgh" + BaseSessionTracking.SESSION_ID_TIMESTAMP_SEPARATOR + System.currentTimeMillis(); String sessionIdWithoutTimestamp = uuid.toString(); when(hsr.getCookies()).thenReturn(new Cookie[] { new Cookie("somesession", sessionIdWithTimestamp)}); assertNull(cookieSessionTracking.retrieveId(request)); when(hsr.getCookies()).thenReturn(new Cookie[] { new Cookie("somesession", sessionIdWithoutTimestamp)}); assertEquals(sessionIdWithoutTimestamp, cookieSessionTracking.retrieveId(request).id); sc.setTimestampSufix(true); cookieSessionTracking.configure(sc); when(hsr.getCookies()).thenReturn(new Cookie[] { new Cookie("somesession", sessionIdWithTimestamp)}); assertEquals(sessionIdWithTimestamp, cookieSessionTracking.retrieveId(request).id); when(hsr.getCookies()).thenReturn(new Cookie[] { new Cookie("somesession", sessionIdWithoutTimestamp)}); assertEquals(sessionIdWithoutTimestamp, cookieSessionTracking.retrieveId(request).id); when(hsr.getCookies()).thenReturn(new Cookie[] { new Cookie("somesession", invalidSessionIdWithTimestamp)}); assertNull(cookieSessionTracking.retrieveId(request)); }