SSOLogoutServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { final String token = req.getParameter("authToken"); if (token == null) { resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Token is not set"); return; } HttpSession session = sessionStore.removeSessionByToken(token); if (session != null) { session.removeAttribute("principal"); session.invalidate(); LOG.debug( "SSO logout [token: {}, session: {}, context {}]", token, session.getId(), session.getServletContext().getServletContextName()); } else { LOG.warn("Not found session associated to {}", token); } }    }
@Test public void shouldFailIfTokenIsNotSet() throws ServletException, IOException { HttpServletRequest request = new MockHttpServletRequest("http: servlet.doPost(request, response); verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), eq("Token is not set")); }
@Test public void shouldRemoveToken() throws ServletException, IOException { MockHttpServletRequest request = new MockHttpServletRequest("http: request.setParameter("authToken", "t-12312344"); servlet.doPost(request, response); verify(sessionStore).removeSessionByToken("t-12312344"); verifyZeroInteractions(response); }
@Test public void shouldCleanupHttpSession() throws ServletException, IOException { MockHttpServletRequest request = new MockHttpServletRequest("http: request.setParameter("authToken", "t-12312344"); when(sessionStore.removeSessionByToken(eq("t-12312344"))).thenReturn(session); servlet.doPost(request, response); verify(session).invalidate(); verify(session).removeAttribute(eq("principal")); }