MultiSchemeAuthenticationHandler implements
    CompositeAuthenticationHandler { @Override public AuthenticationToken authenticate(HttpServletRequest request, HttpServletResponse response) throws IOException, AuthenticationException { String authorization = request.getHeader(HttpConstants.AUTHORIZATION_HEADER); if (authorization != null) { for (Map.Entry<String, AuthenticationHandler> entry : schemeToAuthHandlerMapping.entrySet()) { if (AuthenticationHandlerUtil.matchAuthScheme( entry.getKey(), authorization)) { AuthenticationToken token = entry.getValue().authenticate(request, response); logger.trace("Token generated with type {}", token.getType()); return token; } } } response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); for (String scheme : schemeToAuthHandlerMapping.keySet()) { response.addHeader(HttpConstants.WWW_AUTHENTICATE_HEADER, scheme); } return null; } MultiSchemeAuthenticationHandler();  MultiSchemeAuthenticationHandler(String authType); @Override String getType(); @Override Collection<String> getTokenTypes(); @Override void init(Properties config); @Override void destroy(); @Override boolean managementOperation(AuthenticationToken token,
      HttpServletRequest request, HttpServletResponse response); @Override AuthenticationToken authenticate(HttpServletRequest request,
      HttpServletResponse response); static final String SCHEMES_PROPERTY; static final String AUTH_HANDLER_PROPERTY; static final String TYPE; }
@Test(timeout = 60000) public void testRequestWithoutAuthorization() throws Exception { HttpServletRequest request = Mockito.mock(HttpServletRequest.class); HttpServletResponse response = Mockito.mock(HttpServletResponse.class); Assert.assertNull(handler.authenticate(request, response)); Mockito.verify(response).addHeader(WWW_AUTHENTICATE_HEADER, BASIC); Mockito.verify(response).addHeader(WWW_AUTHENTICATE_HEADER, NEGOTIATE); Mockito.verify(response).setStatus(HttpServletResponse.SC_UNAUTHORIZED); }
@Test(timeout = 60000) public void testRequestWithInvalidAuthorization() throws Exception { HttpServletRequest request = Mockito.mock(HttpServletRequest.class); HttpServletResponse response = Mockito.mock(HttpServletResponse.class); final Base64 base64 = new Base64(0); String credentials = "bjones:invalidpassword"; Mockito.when(request.getHeader(AUTHORIZATION_HEADER)) .thenReturn(base64.encodeToString(credentials.getBytes())); Assert.assertNull(handler.authenticate(request, response)); Mockito.verify(response).addHeader(WWW_AUTHENTICATE_HEADER, BASIC); Mockito.verify(response).addHeader(WWW_AUTHENTICATE_HEADER, NEGOTIATE); Mockito.verify(response).setStatus(HttpServletResponse.SC_UNAUTHORIZED); }
@Test(timeout = 60000) public void testRequestWithInvalidKerberosAuthorization() throws Exception { String token = new Base64(0).encodeToString(new byte[]{0, 1, 2}); HttpServletRequest request = Mockito.mock(HttpServletRequest.class); HttpServletResponse response = Mockito.mock(HttpServletResponse.class); Mockito.when(request.getHeader(AUTHORIZATION_HEADER)).thenReturn( NEGOTIATE + token); try { handler.authenticate(request, response); Assert.fail(); } catch (AuthenticationException ex) { } catch (Exception ex) { Assert.fail("Wrong exception :"+ex); } }