ResponseAuthCache implements HttpResponseInterceptor { public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException { if (response == null) { throw new IllegalArgumentException("HTTP request may not be null"); } if (context == null) { throw new IllegalArgumentException("HTTP context may not be null"); } AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE); HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); AuthState targetState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); if (target != null && targetState != null) { if (isCachable(targetState)) { if (authCache == null) { authCache = new BasicAuthCache(); context.setAttribute(ClientContext.AUTH_CACHE, authCache); } cache(authCache, target, targetState); } } HttpHost proxy = (HttpHost) context.getAttribute(ExecutionContext.HTTP_PROXY_HOST); AuthState proxyState = (AuthState) context.getAttribute(ClientContext.PROXY_AUTH_STATE); if (proxy != null && proxyState != null) { if (isCachable(proxyState)) { if (authCache == null) { authCache = new BasicAuthCache(); context.setAttribute(ClientContext.AUTH_CACHE, authCache); } cache(authCache, proxy, proxyState); } } } ResponseAuthCache(); void process(final HttpResponse response, final HttpContext context);  }
@Test(expected=IllegalArgumentException.class) public void testResponseParameterCheck() throws Exception { HttpContext context = new BasicHttpContext(); HttpResponseInterceptor interceptor = new ResponseAuthCache(); interceptor.process(null, context); }
@Test(expected=IllegalArgumentException.class) public void testContextParameterCheck() throws Exception { HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); HttpResponseInterceptor interceptor = new ResponseAuthCache(); interceptor.process(response, null); }
@Test public void testTargetAndProxyAuthCaching() throws Exception { HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); this.authscheme1.processChallenge( new BasicHeader(AUTH.WWW_AUTH, "BASIC realm=auth-realm")); this.authscheme2.processChallenge( new BasicHeader(AUTH.PROXY_AUTH, "BASIC realm=auth-realm")); this.targetState.setAuthScheme(this.authscheme1); this.targetState.setCredentials(this.creds1); this.targetState.setAuthScope(this.authscope1); this.proxyState.setAuthScheme(this.authscheme2); this.proxyState.setCredentials(this.creds2); this.proxyState.setAuthScope(this.authscope2); HttpContext context = new BasicHttpContext(); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target); context.setAttribute(ExecutionContext.HTTP_PROXY_HOST, this.proxy); context.setAttribute(ClientContext.TARGET_AUTH_STATE, this.targetState); context.setAttribute(ClientContext.PROXY_AUTH_STATE, this.proxyState); HttpResponseInterceptor interceptor = new ResponseAuthCache(); interceptor.process(response, context); AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE); Assert.assertNotNull(authCache); Assert.assertSame(this.authscheme1, authCache.get(this.target)); Assert.assertSame(this.authscheme2, authCache.get(this.proxy)); }
@Test public void testNoAuthStateInitialized() throws Exception { HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); HttpContext context = new BasicHttpContext(); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target); context.setAttribute(ExecutionContext.HTTP_PROXY_HOST, this.proxy); HttpResponseInterceptor interceptor = new ResponseAuthCache(); interceptor.process(response, context); AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE); Assert.assertNull(authCache); }
@Test public void testNoAuthSchemeSelected() throws Exception { HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); HttpContext context = new BasicHttpContext(); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target); context.setAttribute(ExecutionContext.HTTP_PROXY_HOST, this.proxy); context.setAttribute(ClientContext.TARGET_AUTH_STATE, this.targetState); context.setAttribute(ClientContext.PROXY_AUTH_STATE, this.proxyState); HttpResponseInterceptor interceptor = new ResponseAuthCache(); interceptor.process(response, context); AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE); Assert.assertNull(authCache); }
@Test public void testAuthSchemeNotCompleted() throws Exception { HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); this.targetState.setAuthScheme(this.authscheme1); this.targetState.setCredentials(this.creds1); this.targetState.setAuthScope(this.authscope1); this.proxyState.setAuthScheme(this.authscheme2); this.proxyState.setCredentials(this.creds2); this.proxyState.setAuthScope(this.authscope2); HttpContext context = new BasicHttpContext(); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target); context.setAttribute(ExecutionContext.HTTP_PROXY_HOST, this.proxy); context.setAttribute(ClientContext.TARGET_AUTH_STATE, this.targetState); context.setAttribute(ClientContext.PROXY_AUTH_STATE, this.proxyState); HttpResponseInterceptor interceptor = new ResponseAuthCache(); interceptor.process(response, context); AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE); Assert.assertNull(authCache); }
@Test public void testNotChallenged() throws Exception { HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); this.authscheme1.processChallenge( new BasicHeader(AUTH.WWW_AUTH, "BASIC realm=auth-realm")); this.authscheme2.processChallenge( new BasicHeader(AUTH.PROXY_AUTH, "BASIC realm=auth-realm")); this.targetState.setAuthScheme(this.authscheme1); this.targetState.setCredentials(this.creds1); this.targetState.setAuthScope(null); this.proxyState.setAuthScheme(this.authscheme2); this.proxyState.setCredentials(this.creds2); this.proxyState.setAuthScope(null); HttpContext context = new BasicHttpContext(); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target); context.setAttribute(ExecutionContext.HTTP_PROXY_HOST, this.proxy); context.setAttribute(ClientContext.TARGET_AUTH_STATE, this.targetState); context.setAttribute(ClientContext.PROXY_AUTH_STATE, this.proxyState); HttpResponseInterceptor interceptor = new ResponseAuthCache(); interceptor.process(response, context); AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE); Assert.assertNotNull(authCache); Assert.assertNull(authCache.get(this.target)); Assert.assertNull(authCache.get(this.proxy)); }
@Test public void testInvalidateCachingOnAuthFailure() throws Exception { HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); this.authscheme1.processChallenge( new BasicHeader(AUTH.WWW_AUTH, "BASIC realm=auth-realm")); this.authscheme2.processChallenge( new BasicHeader(AUTH.PROXY_AUTH, "BASIC realm=auth-realm")); this.targetState.setAuthScheme(this.authscheme1); this.targetState.setCredentials(null); this.targetState.setAuthScope(this.authscope1); this.proxyState.setAuthScheme(this.authscheme2); this.proxyState.setCredentials(null); this.proxyState.setAuthScope(this.authscope2); HttpContext context = new BasicHttpContext(); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target); context.setAttribute(ExecutionContext.HTTP_PROXY_HOST, this.proxy); context.setAttribute(ClientContext.TARGET_AUTH_STATE, this.targetState); context.setAttribute(ClientContext.PROXY_AUTH_STATE, this.proxyState); AuthCache authCache = new BasicAuthCache(); authCache.put(this.target, this.authscheme1); authCache.put(this.proxy, this.authscheme2); context.setAttribute(ClientContext.AUTH_CACHE, authCache); HttpResponseInterceptor interceptor = new ResponseAuthCache(); interceptor.process(response, context); Assert.assertNull(authCache.get(this.target)); Assert.assertNull(authCache.get(this.proxy)); }