@Test
  public void testConnectionBasedAuthOnlyIfChallenged() throws Exception {
    HttpRequest request = new BasicHttpRequest("GET", "/");
    HttpContext context = new BasicHttpContext();

    AuthState authstate = new AuthState();

    BasicScheme authscheme =
        new BasicScheme() {

          @Override
          public boolean isConnectionBased() {
            return true;
          }
        };

    BasicHeader challenge = new BasicHeader(AUTH.WWW_AUTH, "BASIC realm=auth-realm");
    authscheme.processChallenge(challenge);

    Credentials creds = new UsernamePasswordCredentials("user", "secret");

    authstate.setState(AuthProtocolState.SUCCESS);
    authstate.update(authscheme, creds);

    context.setAttribute(ClientContext.TARGET_AUTH_STATE, authstate);

    HttpRequestInterceptor interceptor = new RequestTargetAuthentication();
    interceptor.process(request, context);
    Header header = request.getFirstHeader(AUTH.WWW_AUTH_RESP);
    Assert.assertNull(header);
  }
  @Test
  public void testAuthStateNotSet() throws Exception {
    HttpRequest request = new BasicHttpRequest("GET", "/");
    HttpContext context = new BasicHttpContext();

    context.setAttribute(ClientContext.TARGET_AUTH_STATE, null);

    HttpRequestInterceptor interceptor = new RequestTargetAuthentication();
    interceptor.process(request, context);
    Header header = request.getFirstHeader(AUTH.WWW_AUTH_RESP);
    Assert.assertNull(header);
  }
  @Test
  public void testNoTargetAuthForConnectRequests() throws Exception {
    HttpRequest request = new BasicHttpRequest("CONNECT", "www.somedomain.com");
    HttpContext context = new BasicHttpContext();

    BasicScheme authscheme = new BasicScheme();
    Credentials creds = new UsernamePasswordCredentials("user", "secret");
    BasicHeader challenge = new BasicHeader(AUTH.WWW_AUTH, "BASIC realm=auth-realm");
    authscheme.processChallenge(challenge);

    AuthState authstate = new AuthState();
    authstate.update(authscheme, creds);

    context.setAttribute(ClientContext.TARGET_AUTH_STATE, authstate);

    HttpRequestInterceptor interceptor = new RequestTargetAuthentication();
    interceptor.process(request, context);
    Header header = request.getFirstHeader(AUTH.WWW_AUTH_RESP);
    Assert.assertNull(header);
  }
  @Test
  public void testTargetAuth() throws Exception {
    HttpRequest request = new BasicHttpRequest("GET", "/");
    HttpContext context = new BasicHttpContext();

    BasicScheme authscheme = new BasicScheme();
    Credentials creds = new UsernamePasswordCredentials("user", "secret");
    BasicHeader challenge = new BasicHeader(AUTH.WWW_AUTH, "BASIC realm=auth-realm");
    authscheme.processChallenge(challenge);

    AuthState authstate = new AuthState();
    authstate.update(authscheme, creds);

    context.setAttribute(ClientContext.TARGET_AUTH_STATE, authstate);

    HttpRequestInterceptor interceptor = new RequestTargetAuthentication();
    interceptor.process(request, context);
    Header header = request.getFirstHeader(AUTH.WWW_AUTH_RESP);
    Assert.assertNotNull(header);
    Assert.assertEquals("Basic dXNlcjpzZWNyZXQ=", header.getValue());
  }
 public void process(HttpRequest httpRequest, HttpContext httpContext)
     throws IOException, HttpException {
   for (HttpRequestInterceptor process : this.requestInterceptors) {
     process.process(httpRequest, httpContext);
   }
 }
 @Test(expected = IllegalArgumentException.class)
 public void testContextParameterCheck() throws Exception {
   HttpRequest request = new BasicHttpRequest("GET", "/");
   HttpRequestInterceptor interceptor = new RequestTargetAuthentication();
   interceptor.process(request, null);
 }
 @Test(expected = IllegalArgumentException.class)
 public void testRequestParameterCheck() throws Exception {
   HttpContext context = new BasicHttpContext();
   HttpRequestInterceptor interceptor = new RequestTargetAuthentication();
   interceptor.process(null, context);
 }