### Question:
JiraFailedAuthenticationRequestHandler implements FailedAuthenticationRequestHandler { @Override public void handle(HttpServletRequest httpRequest, HttpServletResponse httpResponse, Throwable e) { try { String requestIdentifier = getRequestIdentifier(httpRequest); if(acceptsHtml(httpRequest) && shouldSendCookieCleanupRedirect(httpRequest, requestIdentifier)) { httpResponse.sendRedirect(String.format("%s?%s=%s",httpRequest.getRequestURI(), CF_PLUGIN_REQUEST_IDENTIFIER_PARAM, requestIdentifier)); }else { rememberMeService.removeRememberMeCookie(httpRequest, httpResponse); sendErrorResponse(httpRequest, httpResponse, e); } }catch (Exception e2) { log.error("Unable to send 401 error with message " + e.getMessage(), e2); throw new RuntimeException(e2); } } @Inject  JiraFailedAuthenticationRequestHandler(RememberMeHelperService rememberMeService); @Override void handle(HttpServletRequest httpRequest, HttpServletResponse httpResponse, Throwable e);  }### Answer:
@Test public void testThatSends401WhenNoCookieIsAvailable() throws IOException { JiraFailedAuthenticationRequestHandler handler = new JiraFailedAuthenticationRequestHandler(rememberMeService); HttpServletRequest httpRequest = mock(HttpServletRequest.class); HttpServletResponse httpResponse = mock(HttpServletResponse.class); when(httpRequest.getRequestURI()).thenReturn("/secure/website"); handler.handle(httpRequest, httpResponse, new Exception("testing exception")); verify(httpResponse, never()).sendRedirect(anyString()); verify(httpResponse, times(1)).sendError(401, "testing exception"); verify(httpResponse, times(1)).addHeader(eq("WWW-Authenticate"), anyString()); }
@Test public void testThatSendsRedirectOnTheFirstFailure() throws IOException { JiraFailedAuthenticationRequestHandler handler = new JiraFailedAuthenticationRequestHandler(rememberMeService); HttpServletRequest httpRequest = mock(HttpServletRequest.class); HttpServletResponse httpResponse = mock(HttpServletResponse.class); when(httpRequest.getHeader("Accept")).thenReturn("text/html"); when(httpRequest.getRequestURI()).thenReturn("/secure/website"); when(httpRequest.getCookies()).thenReturn(new Cookie[] {new Cookie("JSESSIONID","somehashvalue")}); handler.handle(httpRequest, httpResponse, new Exception("testing exception")); verify(httpResponse, times(1)).sendRedirect(httpRequest.getRequestURI() + "?" + JiraFailedAuthenticationRequestHandler.CF_PLUGIN_REQUEST_IDENTIFIER_PARAM + "=somehashvalue" ); verify(httpResponse, never()).sendError(401); }
@Test public void testThatRedirectsToErrorPageOnTheSubsequentFailure() throws IOException { JiraFailedAuthenticationRequestHandler handler = new JiraFailedAuthenticationRequestHandler(rememberMeService); HttpServletRequest httpRequest = mock(HttpServletRequest.class); HttpServletResponse httpResponse = mock(HttpServletResponse.class); when(httpRequest.getHeader("Accept")).thenReturn("text/html"); when(httpRequest.getRequestURI()).thenReturn("/secure/website"); when(httpRequest.getCookies()).thenReturn(new Cookie[] {new Cookie("JSESSIONID","somehashvalue2")}); handler.handle(httpRequest, httpResponse, new Exception("testing exception")); ArgumentCaptor<String> redirectUrlCaptor = ArgumentCaptor.forClass(String.class); verify(httpResponse, times(1)).sendRedirect(redirectUrlCaptor.capture()); verify(httpResponse, never()).sendError(401); httpResponse = mock(HttpServletResponse.class); Exception secondException = new Exception("another testing exception"); when(httpRequest.getRequestURI()).thenReturn(redirectUrlCaptor.getValue()); handler.handle(httpRequest, httpResponse, secondException); verify(httpResponse, times(1)).sendRedirect(redirectUrlCaptor.capture()); assertTrue(redirectUrlCaptor.getValue().contains(AuthenticationErrorServlet.PATH)); }