BasicAuthenticationHttpContext extends DefaultHttpContext { public boolean handleSecurity(HttpServletRequest request, HttpServletResponse response) throws IOException { if (!authenticator.authenticate(request)) { response.setHeader("WWW-Authenticate",HttpServletRequest.BASIC_AUTH + " realm=\""+realm+"\""); response.sendError(HttpServletResponse.SC_UNAUTHORIZED); return false; } else { return true; } } BasicAuthenticationHttpContext(String pRealm, Authenticator pAuthenticator); boolean handleSecurity(HttpServletRequest request, HttpServletResponse response);  }
@Test public void correctAuth() throws IOException { expect(request.getHeader("Authorization")).andReturn("Basic cm9sYW5kOnMhY3IhdA=="); request.setAttribute(HttpContext.AUTHENTICATION_TYPE,HttpServletRequest.BASIC_AUTH); request.setAttribute(HttpContext.REMOTE_USER, "roland"); replay(request,response); assertTrue(context.handleSecurity(request,response)); }
@Test public void correctAlternateAuth() throws IOException { expect(request.getHeader("Authorization")).andReturn(null); expect(request.getHeader(AuthorizationHeaderParser.JOLOKIA_ALTERNATE_AUTHORIZATION_HEADER)).andReturn("Basic cm9sYW5kOnMhY3IhdA=="); request.setAttribute(HttpContext.AUTHENTICATION_TYPE,HttpServletRequest.BASIC_AUTH); request.setAttribute(HttpContext.REMOTE_USER, "roland"); replay(request,response); assertTrue(context.handleSecurity(request,response)); }
@Test public void noAuth() throws IOException { expect(request.getHeader("Authorization")).andReturn(null); expect(request.getHeader(AuthorizationHeaderParser.JOLOKIA_ALTERNATE_AUTHORIZATION_HEADER)).andReturn(null); response.setHeader(eq("WWW-Authenticate"), EasyMock.<String>anyObject()); response.sendError(HttpServletResponse.SC_UNAUTHORIZED); replay(request, response); assertFalse(context.handleSecurity(request, response)); }
@Test public void wrongAuth() throws IOException { for (String val : new String[] { "cm9sYW5kOmJsdWI=", "Blub"}) { expect(request.getHeader("Authorization")).andReturn("Basic " + val); response.setHeader(eq("WWW-Authenticate"), EasyMock.<String>anyObject()); response.sendError(401); replay(request, response); assertFalse(context.handleSecurity(request, response)); reset(request,response); } }
@Test(expectedExceptions = IllegalArgumentException.class,expectedExceptionsMessageRegExp = ".*BasicAuthentication.*") public void invalidMethod() throws IOException { expect(request.getHeader("Authorization")).andReturn("unknown Blub"); replay(request, response); context.handleSecurity(request,response); }