/** * Tests a HTTP Basic authentication. * * @throws Exception */ @Test public void testHttpBasic() throws Exception { TestServletRequest req = new TestServletRequest( new InputStream() { @Override public int read() throws IOException { return 0; } }); TestServletResponse resp = new TestServletResponse( new OutputStream() { @Override public void write(int b) throws IOException { System.out.println(b); } }); // Get Positive Authentication req.addHeader(PicketBoxConstants.HTTP_AUTHORIZATION_HEADER, "Basic " + getPositive()); req.setContextPath("/test"); req.setRequestURI(req.getContextPath() + "/index.html"); Principal principal = httpBasic.authenticate(req, resp); assertNotNull(principal); req.clearHeaders(); req.getSession().setAttribute(PicketBoxConstants.SUBJECT, null); // Get Negative Authentication req.addHeader(PicketBoxConstants.HTTP_AUTHORIZATION_HEADER, "Basic " + getNegative()); principal = httpBasic.authenticate(req, resp); assertNull(principal); String basicHeader = resp.getHeader(PicketBoxConstants.HTTP_WWW_AUTHENTICATE); assertTrue(basicHeader.startsWith("basic realm=")); }
@Before public void onSetup() throws Exception { super.initialize(); configuration.authentication().authManager(new PropertiesFileBasedAuthenticationManager()); PicketBoxHTTPManager picketBoxManager = new PicketBoxHTTPManager((PicketBoxHTTPConfiguration) configuration.build()); picketBoxManager.start(); httpBasic.setPicketBoxManager(picketBoxManager); }