UrlSessionTracking extends BaseSessionTracking implements SessionTracking { @Override public String encodeUrl(RequestWithSession request, String url) { RepositoryBackedSession session = request.getRepositoryBackedSession(false); if (session == null || !session.isValid()) { return url; } String encodedSessionAlias; try { encodedSessionAlias = URLEncoder.encode(session.getId(), "UTF-8"); } catch (UnsupportedEncodingException e) { throw new IllegalStateException("This exception should never happen!", e); } int queryStart = url.indexOf('?'); if (queryStart < 0) { return url + sessionIdPathItem + encodedSessionAlias; } String path = url.substring(0, queryStart); String query = url.substring(queryStart + 1, url.length()); path += sessionIdPathItem + encodedSessionAlias; return path + '?' + query; }  @Override void configure(SessionConfiguration configuration); @Override IdAndSource retrieveId(RequestWithSession request); @Override void propagateSession(RequestWithSession request, Object response); @Override String encodeUrl(RequestWithSession request, String url); @Override boolean isCookieTracking();  }
@Test public void testEncodeUrl() { SessionConfiguration sc = new SessionConfiguration(); sc.setSessionIdName("asession"); urlSessionTracking.configure(sc); RequestWithSession request = mock(RequestWithSession.class, withSettings().extraInterfaces(HttpServletRequest.class)); RepositoryBackedSession session = mock(RepositoryBackedSession.class); when(request.getRepositoryBackedSession(false)).thenReturn(session); when(session.isValid()).thenReturn(Boolean.FALSE); String url = urlSessionTracking.encodeUrl(request, "http: assertEquals("Session is not valid, so URL should stay the same", "http: when(session.isValid()).thenReturn(Boolean.TRUE); when(session.getId()).thenReturn("1234"); url = urlSessionTracking.encodeUrl(request, "http: assertEquals("Session is valid, so URL should be encoded", "http: url = urlSessionTracking.encodeUrl(request, "http: assertEquals("Session is valid, so URL should be encoded", "http: url = urlSessionTracking.encodeUrl(request, "http: assertEquals("Session is valid, so URL should be encoded", "http: url = urlSessionTracking.encodeUrl(request, "http: assertEquals("Session is valid, so URL should be encoded", "http: }