BasicAuthAuthenticator implements WebappAuthenticator { @Override public boolean canHandle(Request request) { if (!isAuthenticationSupported(request)) { return false; } if (request.getCoyoteRequest() == null || request.getCoyoteRequest().getMimeHeaders() == null) { return false; } MessageBytes authorization = request.getCoyoteRequest().getMimeHeaders().getValue(Constants.HTTPHeaders.HEADER_HTTP_AUTHORIZATION); if (authorization != null) { authorization.toBytes(); ByteChunk authBC = authorization.getByteChunk(); if (authBC.startsWithIgnoreCase(AUTH_HEADER, 0)) { return true; } } return false; }  @Override void init(); @Override boolean canHandle(Request request); @Override AuthenticationInfo authenticate(Request request, Response response); @Override String getName(); @Override void setProperties(Properties properties); @Override Properties getProperties(); @Override String getProperty(String name);  }
@Test(description = "This method tests the behaviour of canHandle method when different wrong values given for a " + "request") public void testCanHandleWithoutRequireParameters() throws NoSuchFieldException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException { request = new Request(); context = new StandardContext(); request.setContext(context); Assert.assertFalse(basicAuthAuthenticator.canHandle(request), "Without proper headers and parameters, the request can be handled by BasicAuthAuthenticator."); context.addParameter("basicAuth", "true"); request.setContext(context); Assert.assertFalse(basicAuthAuthenticator.canHandle(request), "Without proper Authentication headers request can be handled by BasicAuthAuthenticator."); coyoteRequest = new org.apache.coyote.Request(); mimeHeaders = new MimeHeaders(); bytes = mimeHeaders.addValue(BaseWebAppAuthenticatorFrameworkTest.AUTHORIZATION_HEADER); bytes.setString("test"); headersField.set(coyoteRequest, mimeHeaders); request.setCoyoteRequest(coyoteRequest); Assert.assertFalse(basicAuthAuthenticator.canHandle(request), "With a different authorization header Basic Authenticator can handle the request"); }
@Test(description = "This method tests the canHandle method when all the required parameters are given with the " + "request", dependsOnMethods = {"testCanHandleWithoutRequireParameters"}) public void testCanHandleWithRequireParameters() throws IllegalAccessException { request = new Request(); context = new StandardContext(); context.addParameter("basicAuth", "true"); request.setContext(context); mimeHeaders = new MimeHeaders(); bytes = mimeHeaders.addValue(BaseWebAppAuthenticatorFrameworkTest.AUTHORIZATION_HEADER); bytes.setString(BASIC_HEADER); headersField.set(coyoteRequest, mimeHeaders); request.setCoyoteRequest(coyoteRequest); Assert.assertTrue(basicAuthAuthenticator.canHandle(request), "Basic Authenticator cannot handle a request with all the required headers and parameters."); }