Reworked auth caching code; added more test cases for protocol interceptors
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1075508 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
a5df9fe83c
commit
9d55aee86f
|
@ -42,7 +42,6 @@ import org.apache.http.auth.AuthState;
|
||||||
import org.apache.http.auth.Credentials;
|
import org.apache.http.auth.Credentials;
|
||||||
import org.apache.http.client.AuthCache;
|
import org.apache.http.client.AuthCache;
|
||||||
import org.apache.http.client.CredentialsProvider;
|
import org.apache.http.client.CredentialsProvider;
|
||||||
import org.apache.http.impl.client.BasicAuthCache;
|
|
||||||
import org.apache.http.protocol.ExecutionContext;
|
import org.apache.http.protocol.ExecutionContext;
|
||||||
import org.apache.http.protocol.HttpContext;
|
import org.apache.http.protocol.HttpContext;
|
||||||
|
|
||||||
|
@ -73,34 +72,30 @@ public class RequestAuthCache implements HttpRequestInterceptor {
|
||||||
|
|
||||||
AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE);
|
AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE);
|
||||||
if (authCache == null) {
|
if (authCache == null) {
|
||||||
context.setAttribute(ClientContext.AUTH_CACHE, new BasicAuthCache());
|
return;
|
||||||
} else {
|
}
|
||||||
|
|
||||||
CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(
|
CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(
|
||||||
ClientContext.CREDS_PROVIDER);
|
ClientContext.CREDS_PROVIDER);
|
||||||
|
if (credsProvider == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
|
HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
|
||||||
if (target != null) {
|
AuthState targetState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
|
||||||
|
if (target != null && targetState != null) {
|
||||||
AuthScheme authScheme = authCache.get(target);
|
AuthScheme authScheme = authCache.get(target);
|
||||||
if (authScheme != null) {
|
if (authScheme != null) {
|
||||||
doPreemptiveAuth(
|
doPreemptiveAuth(target, authScheme, targetState, credsProvider);
|
||||||
target,
|
|
||||||
authScheme,
|
|
||||||
(AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE),
|
|
||||||
credsProvider);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpHost proxy = (HttpHost) context.getAttribute(ExecutionContext.HTTP_PROXY_HOST);
|
HttpHost proxy = (HttpHost) context.getAttribute(ExecutionContext.HTTP_PROXY_HOST);
|
||||||
if (proxy != null) {
|
AuthState proxyState = (AuthState) context.getAttribute(ClientContext.PROXY_AUTH_STATE);
|
||||||
|
if (proxy != null && proxyState != null) {
|
||||||
AuthScheme authScheme = authCache.get(proxy);
|
AuthScheme authScheme = authCache.get(proxy);
|
||||||
if (authScheme != null) {
|
if (authScheme != null) {
|
||||||
doPreemptiveAuth(
|
doPreemptiveAuth(proxy, authScheme, proxyState, credsProvider);
|
||||||
proxy,
|
|
||||||
authScheme,
|
|
||||||
(AuthState) context.getAttribute(ClientContext.PROXY_AUTH_STATE),
|
|
||||||
credsProvider);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -115,8 +110,8 @@ public class RequestAuthCache implements HttpRequestInterceptor {
|
||||||
this.log.debug("Re-using cached '" + schemeName + "' auth scheme for " + host);
|
this.log.debug("Re-using cached '" + schemeName + "' auth scheme for " + host);
|
||||||
}
|
}
|
||||||
|
|
||||||
Credentials creds = credsProvider.getCredentials(
|
AuthScope authScope = new AuthScope(host, AuthScope.ANY_REALM, schemeName);
|
||||||
new AuthScope(host, AuthScope.ANY_REALM, schemeName));
|
Credentials creds = credsProvider.getCredentials(authScope);
|
||||||
|
|
||||||
if (creds != null) {
|
if (creds != null) {
|
||||||
authState.setAuthScheme(authScheme);
|
authState.setAuthScheme(authScheme);
|
||||||
|
|
|
@ -79,6 +79,9 @@ public class RequestProxyAuthentication implements HttpRequestInterceptor {
|
||||||
|
|
||||||
HttpRoutedConnection conn = (HttpRoutedConnection) context.getAttribute(
|
HttpRoutedConnection conn = (HttpRoutedConnection) context.getAttribute(
|
||||||
ExecutionContext.HTTP_CONNECTION);
|
ExecutionContext.HTTP_CONNECTION);
|
||||||
|
if (conn == null) {
|
||||||
|
throw new IllegalStateException("Client connection not specified in HTTP context");
|
||||||
|
}
|
||||||
HttpRoute route = conn.getRoute();
|
HttpRoute route = conn.getRoute();
|
||||||
if (route.isTunnelled()) {
|
if (route.isTunnelled()) {
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -40,6 +40,7 @@ import org.apache.http.auth.AuthScheme;
|
||||||
import org.apache.http.auth.AuthState;
|
import org.apache.http.auth.AuthState;
|
||||||
import org.apache.http.client.AuthCache;
|
import org.apache.http.client.AuthCache;
|
||||||
import org.apache.http.client.params.AuthPolicy;
|
import org.apache.http.client.params.AuthPolicy;
|
||||||
|
import org.apache.http.impl.client.BasicAuthCache;
|
||||||
import org.apache.http.protocol.ExecutionContext;
|
import org.apache.http.protocol.ExecutionContext;
|
||||||
import org.apache.http.protocol.HttpContext;
|
import org.apache.http.protocol.HttpContext;
|
||||||
|
|
||||||
|
@ -69,36 +70,49 @@ public class ResponseAuthCache implements HttpResponseInterceptor {
|
||||||
throw new IllegalArgumentException("HTTP context may not be null");
|
throw new IllegalArgumentException("HTTP context may not be null");
|
||||||
}
|
}
|
||||||
AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE);
|
AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE);
|
||||||
if (authCache != null) {
|
|
||||||
cache(authCache,
|
HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
|
||||||
(HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST),
|
AuthState targetState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
|
||||||
(AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE));
|
if (target != null && targetState != null) {
|
||||||
cache(authCache,
|
if (isCachable(targetState)) {
|
||||||
(HttpHost) context.getAttribute(ExecutionContext.HTTP_PROXY_HOST),
|
if (authCache == null) {
|
||||||
(AuthState) context.getAttribute(ClientContext.PROXY_AUTH_STATE));
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isCachable(final AuthState authState) {
|
||||||
|
AuthScheme authScheme = authState.getAuthScheme();
|
||||||
|
if (authScheme == null || !authScheme.isComplete()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
String schemeName = authScheme.getSchemeName();
|
||||||
|
return schemeName.equalsIgnoreCase(AuthPolicy.BASIC) ||
|
||||||
|
schemeName.equalsIgnoreCase(AuthPolicy.DIGEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void cache(final AuthCache authCache, final HttpHost host, final AuthState authState) {
|
private void cache(final AuthCache authCache, final HttpHost host, final AuthState authState) {
|
||||||
if (authState == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
AuthScheme authScheme = authState.getAuthScheme();
|
AuthScheme authScheme = authState.getAuthScheme();
|
||||||
if (authScheme == null || !authScheme.isComplete()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
String schemeName = authScheme.getSchemeName();
|
|
||||||
if (!schemeName.equalsIgnoreCase(AuthPolicy.BASIC) &&
|
|
||||||
!schemeName.equalsIgnoreCase(AuthPolicy.DIGEST)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (authState.getAuthScope() != null) {
|
if (authState.getAuthScope() != null) {
|
||||||
if (authState.getCredentials() != null) {
|
if (authState.getCredentials() != null) {
|
||||||
if (this.log.isDebugEnabled()) {
|
if (this.log.isDebugEnabled()) {
|
||||||
this.log.debug("Caching '" + schemeName + "' auth scheme for " + host);
|
this.log.debug("Caching '" + authScheme.getSchemeName() +
|
||||||
|
"' auth scheme for " + host);
|
||||||
}
|
}
|
||||||
authCache.put(host, authScheme);
|
authCache.put(host, authScheme);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -0,0 +1,53 @@
|
||||||
|
/*
|
||||||
|
* ====================================================================
|
||||||
|
*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright ownership.
|
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||||
|
* (the "License"); you may not use this file except in compliance with
|
||||||
|
* the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
* ====================================================================
|
||||||
|
*
|
||||||
|
* This software consists of voluntary contributions made by many
|
||||||
|
* individuals on behalf of the Apache Software Foundation. For more
|
||||||
|
* information on the Apache Software Foundation, please see
|
||||||
|
* <http://www.apache.org/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.apache.http.client.protocol;
|
||||||
|
|
||||||
|
import junit.framework.Assert;
|
||||||
|
|
||||||
|
import org.apache.http.Header;
|
||||||
|
import org.apache.http.HttpRequest;
|
||||||
|
import org.apache.http.HttpRequestInterceptor;
|
||||||
|
import org.apache.http.message.BasicHttpRequest;
|
||||||
|
import org.apache.http.protocol.BasicHttpContext;
|
||||||
|
import org.apache.http.protocol.HttpContext;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class TestRequestAcceptEncoding {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAcceptEncoding() throws Exception {
|
||||||
|
HttpRequest request = new BasicHttpRequest("GET", "/");
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
|
||||||
|
HttpRequestInterceptor interceptor = new RequestAcceptEncoding();
|
||||||
|
interceptor.process(request, context);
|
||||||
|
Header header = request.getFirstHeader("Accept-Encoding");
|
||||||
|
Assert.assertNotNull(header);
|
||||||
|
Assert.assertEquals("gzip,deflate", header.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,216 @@
|
||||||
|
/*
|
||||||
|
* ====================================================================
|
||||||
|
*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright ownership.
|
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||||
|
* (the "License"); you may not use this file except in compliance with
|
||||||
|
* the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
* ====================================================================
|
||||||
|
*
|
||||||
|
* This software consists of voluntary contributions made by many
|
||||||
|
* individuals on behalf of the Apache Software Foundation. For more
|
||||||
|
* information on the Apache Software Foundation, please see
|
||||||
|
* <http://www.apache.org/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.apache.http.client.protocol;
|
||||||
|
|
||||||
|
import junit.framework.Assert;
|
||||||
|
|
||||||
|
import org.apache.http.HttpHost;
|
||||||
|
import org.apache.http.HttpRequest;
|
||||||
|
import org.apache.http.HttpRequestInterceptor;
|
||||||
|
import org.apache.http.auth.AuthScope;
|
||||||
|
import org.apache.http.auth.AuthState;
|
||||||
|
import org.apache.http.auth.Credentials;
|
||||||
|
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||||
|
import org.apache.http.client.AuthCache;
|
||||||
|
import org.apache.http.impl.auth.BasicScheme;
|
||||||
|
import org.apache.http.impl.client.BasicAuthCache;
|
||||||
|
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||||
|
import org.apache.http.message.BasicHttpRequest;
|
||||||
|
import org.apache.http.protocol.BasicHttpContext;
|
||||||
|
import org.apache.http.protocol.ExecutionContext;
|
||||||
|
import org.apache.http.protocol.HttpContext;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class TestRequestAuthCache {
|
||||||
|
|
||||||
|
private HttpHost target;
|
||||||
|
private HttpHost proxy;
|
||||||
|
private Credentials creds1;
|
||||||
|
private Credentials creds2;
|
||||||
|
private AuthScope authscope1;
|
||||||
|
private AuthScope authscope2;
|
||||||
|
private BasicScheme authscheme1;
|
||||||
|
private BasicScheme authscheme2;
|
||||||
|
private BasicCredentialsProvider credProvider;
|
||||||
|
private AuthState targetState;
|
||||||
|
private AuthState proxyState;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
this.target = new HttpHost("localhost", 80);
|
||||||
|
this.proxy = new HttpHost("localhost", 8080);
|
||||||
|
|
||||||
|
this.credProvider = new BasicCredentialsProvider();
|
||||||
|
this.creds1 = new UsernamePasswordCredentials("user1", "secret1");
|
||||||
|
this.creds2 = new UsernamePasswordCredentials("user2", "secret2");
|
||||||
|
this.authscope1 = new AuthScope(this.target);
|
||||||
|
this.authscope2 = new AuthScope(this.proxy);
|
||||||
|
this.authscheme1 = new BasicScheme();
|
||||||
|
this.authscheme2 = new BasicScheme();
|
||||||
|
|
||||||
|
this.credProvider.setCredentials(this.authscope1, this.creds1);
|
||||||
|
this.credProvider.setCredentials(this.authscope2, this.creds2);
|
||||||
|
|
||||||
|
this.targetState = new AuthState();
|
||||||
|
this.proxyState = new AuthState();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected=IllegalArgumentException.class)
|
||||||
|
public void testRequestParameterCheck() throws Exception {
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
HttpRequestInterceptor interceptor = new RequestAuthCache();
|
||||||
|
interceptor.process(null, context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected=IllegalArgumentException.class)
|
||||||
|
public void testContextParameterCheck() throws Exception {
|
||||||
|
HttpRequest request = new BasicHttpRequest("GET", "/");
|
||||||
|
HttpRequestInterceptor interceptor = new RequestAuthCache();
|
||||||
|
interceptor.process(request, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPreemptiveTargetAndProxyAuth() throws Exception {
|
||||||
|
HttpRequest request = new BasicHttpRequest("GET", "/");
|
||||||
|
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
context.setAttribute(ClientContext.CREDS_PROVIDER, this.credProvider);
|
||||||
|
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);
|
||||||
|
|
||||||
|
HttpRequestInterceptor interceptor = new RequestAuthCache();
|
||||||
|
interceptor.process(request, context);
|
||||||
|
Assert.assertSame(this.authscheme1, this.targetState.getAuthScheme());
|
||||||
|
Assert.assertSame(this.creds1, this.targetState.getCredentials());
|
||||||
|
Assert.assertSame(this.authscheme2, this.proxyState.getAuthScheme());
|
||||||
|
Assert.assertSame(this.creds2, this.proxyState.getCredentials());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCredentialsProviderNotSet() throws Exception {
|
||||||
|
HttpRequest request = new BasicHttpRequest("GET", "/");
|
||||||
|
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
context.setAttribute(ClientContext.CREDS_PROVIDER, null);
|
||||||
|
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);
|
||||||
|
|
||||||
|
HttpRequestInterceptor interceptor = new RequestAuthCache();
|
||||||
|
interceptor.process(request, context);
|
||||||
|
Assert.assertNull(this.targetState.getAuthScheme());
|
||||||
|
Assert.assertNull(this.targetState.getCredentials());
|
||||||
|
Assert.assertNull(this.proxyState.getAuthScheme());
|
||||||
|
Assert.assertNull(this.proxyState.getCredentials());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAuthCacheNotSet() throws Exception {
|
||||||
|
HttpRequest request = new BasicHttpRequest("GET", "/");
|
||||||
|
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
context.setAttribute(ClientContext.CREDS_PROVIDER, this.credProvider);
|
||||||
|
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);
|
||||||
|
context.setAttribute(ClientContext.AUTH_CACHE, null);
|
||||||
|
|
||||||
|
HttpRequestInterceptor interceptor = new RequestAuthCache();
|
||||||
|
interceptor.process(request, context);
|
||||||
|
Assert.assertNull(this.targetState.getAuthScheme());
|
||||||
|
Assert.assertNull(this.targetState.getCredentials());
|
||||||
|
Assert.assertNull(this.proxyState.getAuthScheme());
|
||||||
|
Assert.assertNull(this.proxyState.getCredentials());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAuthCacheEmpty() throws Exception {
|
||||||
|
HttpRequest request = new BasicHttpRequest("GET", "/");
|
||||||
|
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
context.setAttribute(ClientContext.CREDS_PROVIDER, this.credProvider);
|
||||||
|
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();
|
||||||
|
context.setAttribute(ClientContext.AUTH_CACHE, authCache);
|
||||||
|
|
||||||
|
HttpRequestInterceptor interceptor = new RequestAuthCache();
|
||||||
|
interceptor.process(request, context);
|
||||||
|
Assert.assertNull(this.targetState.getAuthScheme());
|
||||||
|
Assert.assertNull(this.targetState.getCredentials());
|
||||||
|
Assert.assertNull(this.proxyState.getAuthScheme());
|
||||||
|
Assert.assertNull(this.proxyState.getCredentials());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNoMatchingCredentials() throws Exception {
|
||||||
|
HttpRequest request = new BasicHttpRequest("GET", "/");
|
||||||
|
|
||||||
|
this.credProvider.clear();
|
||||||
|
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
context.setAttribute(ClientContext.CREDS_PROVIDER, this.credProvider);
|
||||||
|
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);
|
||||||
|
|
||||||
|
HttpRequestInterceptor interceptor = new RequestAuthCache();
|
||||||
|
interceptor.process(request, context);
|
||||||
|
Assert.assertNull(this.targetState.getAuthScheme());
|
||||||
|
Assert.assertNull(this.targetState.getCredentials());
|
||||||
|
Assert.assertNull(this.proxyState.getAuthScheme());
|
||||||
|
Assert.assertNull(this.proxyState.getCredentials());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,201 @@
|
||||||
|
/*
|
||||||
|
* ====================================================================
|
||||||
|
*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright ownership.
|
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||||
|
* (the "License"); you may not use this file except in compliance with
|
||||||
|
* the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
* ====================================================================
|
||||||
|
*
|
||||||
|
* This software consists of voluntary contributions made by many
|
||||||
|
* individuals on behalf of the Apache Software Foundation. For more
|
||||||
|
* information on the Apache Software Foundation, please see
|
||||||
|
* <http://www.apache.org/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.apache.http.client.protocol;
|
||||||
|
|
||||||
|
import junit.framework.Assert;
|
||||||
|
|
||||||
|
import org.apache.http.Header;
|
||||||
|
import org.apache.http.HttpHost;
|
||||||
|
import org.apache.http.HttpRequest;
|
||||||
|
import org.apache.http.HttpRequestInterceptor;
|
||||||
|
import org.apache.http.conn.HttpRoutedConnection;
|
||||||
|
import org.apache.http.conn.routing.HttpRoute;
|
||||||
|
import org.apache.http.conn.routing.RouteInfo.LayerType;
|
||||||
|
import org.apache.http.conn.routing.RouteInfo.TunnelType;
|
||||||
|
import org.apache.http.message.BasicHttpRequest;
|
||||||
|
import org.apache.http.protocol.BasicHttpContext;
|
||||||
|
import org.apache.http.protocol.ExecutionContext;
|
||||||
|
import org.apache.http.protocol.HTTP;
|
||||||
|
import org.apache.http.protocol.HttpContext;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
|
public class TestRequestClientConnControl {
|
||||||
|
|
||||||
|
@Test(expected=IllegalArgumentException.class)
|
||||||
|
public void testRequestParameterCheck() throws Exception {
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
HttpRequestInterceptor interceptor = new RequestClientConnControl();
|
||||||
|
interceptor.process(null, context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected=IllegalStateException.class)
|
||||||
|
public void testConnectionInContextCheck() throws Exception {
|
||||||
|
HttpRequest request = new BasicHttpRequest("GET", "/");
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
HttpRequestInterceptor interceptor = new RequestClientConnControl();
|
||||||
|
interceptor.process(request, context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConnectionKeepAliveForConnectRequest() throws Exception {
|
||||||
|
HttpRequest request = new BasicHttpRequest("CONNECT", "www.somedomain.com");
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
|
||||||
|
HttpRequestInterceptor interceptor = new RequestClientConnControl();
|
||||||
|
interceptor.process(request, context);
|
||||||
|
Header header1 = request.getFirstHeader("Proxy-Connection");
|
||||||
|
Assert.assertNotNull(header1);
|
||||||
|
Assert.assertEquals(HTTP.CONN_KEEP_ALIVE, header1.getValue());
|
||||||
|
Header header2 = request.getFirstHeader(HTTP.CONN_DIRECTIVE);
|
||||||
|
Assert.assertNull(header2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConnectionKeepAliveForDirectRequests() throws Exception {
|
||||||
|
HttpRequest request = new BasicHttpRequest("GET", "/");
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
|
||||||
|
HttpHost target = new HttpHost("localhost", 80, "http");
|
||||||
|
HttpRoute route = new HttpRoute(target, null, false);
|
||||||
|
|
||||||
|
HttpRoutedConnection conn = Mockito.mock(HttpRoutedConnection.class);
|
||||||
|
Mockito.when(conn.getRoute()).thenReturn(route);
|
||||||
|
|
||||||
|
context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
|
||||||
|
|
||||||
|
HttpRequestInterceptor interceptor = new RequestClientConnControl();
|
||||||
|
interceptor.process(request, context);
|
||||||
|
|
||||||
|
Header header1 = request.getFirstHeader(HTTP.CONN_DIRECTIVE);
|
||||||
|
Assert.assertNotNull(header1);
|
||||||
|
Assert.assertEquals(HTTP.CONN_KEEP_ALIVE, header1.getValue());
|
||||||
|
Header header2 = request.getFirstHeader("Proxy-Connection");
|
||||||
|
Assert.assertNull(header2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConnectionKeepAliveForTunneledRequests() throws Exception {
|
||||||
|
HttpRequest request = new BasicHttpRequest("GET", "/");
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
|
||||||
|
HttpHost target = new HttpHost("localhost", 443, "https");
|
||||||
|
HttpHost proxy = new HttpHost("localhost", 8080);
|
||||||
|
HttpRoute route = new HttpRoute(target, null, proxy, true,
|
||||||
|
TunnelType.TUNNELLED, LayerType.LAYERED);
|
||||||
|
|
||||||
|
HttpRoutedConnection conn = Mockito.mock(HttpRoutedConnection.class);
|
||||||
|
Mockito.when(conn.getRoute()).thenReturn(route);
|
||||||
|
|
||||||
|
context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
|
||||||
|
|
||||||
|
HttpRequestInterceptor interceptor = new RequestClientConnControl();
|
||||||
|
interceptor.process(request, context);
|
||||||
|
|
||||||
|
Header header1 = request.getFirstHeader(HTTP.CONN_DIRECTIVE);
|
||||||
|
Assert.assertNotNull(header1);
|
||||||
|
Assert.assertEquals(HTTP.CONN_KEEP_ALIVE, header1.getValue());
|
||||||
|
Header header2 = request.getFirstHeader("Proxy-Connection");
|
||||||
|
Assert.assertNull(header2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testProxyConnectionKeepAliveForRequestsOverProxy() throws Exception {
|
||||||
|
HttpRequest request = new BasicHttpRequest("GET", "/");
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
|
||||||
|
HttpHost target = new HttpHost("localhost", 80, "http");
|
||||||
|
HttpHost proxy = new HttpHost("localhost", 8080);
|
||||||
|
HttpRoute route = new HttpRoute(target, null, proxy, false,
|
||||||
|
TunnelType.PLAIN, LayerType.PLAIN);
|
||||||
|
|
||||||
|
HttpRoutedConnection conn = Mockito.mock(HttpRoutedConnection.class);
|
||||||
|
Mockito.when(conn.getRoute()).thenReturn(route);
|
||||||
|
|
||||||
|
context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
|
||||||
|
|
||||||
|
HttpRequestInterceptor interceptor = new RequestClientConnControl();
|
||||||
|
interceptor.process(request, context);
|
||||||
|
|
||||||
|
Header header1 = request.getFirstHeader("Proxy-Connection");
|
||||||
|
Assert.assertNotNull(header1);
|
||||||
|
Assert.assertEquals(HTTP.CONN_KEEP_ALIVE, header1.getValue());
|
||||||
|
Header header2 = request.getFirstHeader(HTTP.CONN_DIRECTIVE);
|
||||||
|
Assert.assertNull(header2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPreserveCustomConnectionHeader() throws Exception {
|
||||||
|
HttpRequest request = new BasicHttpRequest("GET", "/");
|
||||||
|
request.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
|
||||||
|
HttpHost target = new HttpHost("localhost", 443, "https");
|
||||||
|
HttpHost proxy = new HttpHost("localhost", 8080);
|
||||||
|
HttpRoute route = new HttpRoute(target, null, proxy, true,
|
||||||
|
TunnelType.TUNNELLED, LayerType.LAYERED);
|
||||||
|
|
||||||
|
HttpRoutedConnection conn = Mockito.mock(HttpRoutedConnection.class);
|
||||||
|
Mockito.when(conn.getRoute()).thenReturn(route);
|
||||||
|
|
||||||
|
context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
|
||||||
|
|
||||||
|
HttpRequestInterceptor interceptor = new RequestClientConnControl();
|
||||||
|
interceptor.process(request, context);
|
||||||
|
|
||||||
|
Header header1 = request.getFirstHeader(HTTP.CONN_DIRECTIVE);
|
||||||
|
Assert.assertNotNull(header1);
|
||||||
|
Assert.assertEquals(HTTP.CONN_CLOSE, header1.getValue());
|
||||||
|
Header header2 = request.getFirstHeader("Proxy-Connection");
|
||||||
|
Assert.assertNull(header2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPreserveCustomProxyConnectionHeader() throws Exception {
|
||||||
|
HttpRequest request = new BasicHttpRequest("GET", "/");
|
||||||
|
request.addHeader("Proxy-Connection", HTTP.CONN_CLOSE);
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
|
||||||
|
HttpHost target = new HttpHost("localhost", 80, "http");
|
||||||
|
HttpHost proxy = new HttpHost("localhost", 8080);
|
||||||
|
HttpRoute route = new HttpRoute(target, null, proxy, false,
|
||||||
|
TunnelType.PLAIN, LayerType.PLAIN);
|
||||||
|
|
||||||
|
HttpRoutedConnection conn = Mockito.mock(HttpRoutedConnection.class);
|
||||||
|
Mockito.when(conn.getRoute()).thenReturn(route);
|
||||||
|
|
||||||
|
context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
|
||||||
|
|
||||||
|
HttpRequestInterceptor interceptor = new RequestClientConnControl();
|
||||||
|
interceptor.process(request, context);
|
||||||
|
|
||||||
|
Header header1 = request.getFirstHeader("Proxy-Connection");
|
||||||
|
Assert.assertNotNull(header1);
|
||||||
|
Assert.assertEquals(HTTP.CONN_CLOSE, header1.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,85 @@
|
||||||
|
/*
|
||||||
|
* ====================================================================
|
||||||
|
*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright ownership.
|
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||||
|
* (the "License"); you may not use this file except in compliance with
|
||||||
|
* the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
* ====================================================================
|
||||||
|
*
|
||||||
|
* This software consists of voluntary contributions made by many
|
||||||
|
* individuals on behalf of the Apache Software Foundation. For more
|
||||||
|
* information on the Apache Software Foundation, please see
|
||||||
|
* <http://www.apache.org/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.apache.http.client.protocol;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import junit.framework.Assert;
|
||||||
|
|
||||||
|
import org.apache.http.Header;
|
||||||
|
import org.apache.http.HttpRequest;
|
||||||
|
import org.apache.http.HttpRequestInterceptor;
|
||||||
|
import org.apache.http.client.params.AllClientPNames;
|
||||||
|
import org.apache.http.message.BasicHeader;
|
||||||
|
import org.apache.http.message.BasicHttpRequest;
|
||||||
|
import org.apache.http.protocol.BasicHttpContext;
|
||||||
|
import org.apache.http.protocol.HttpContext;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class TestRequestDefaultHeaders {
|
||||||
|
|
||||||
|
@Test(expected=IllegalArgumentException.class)
|
||||||
|
public void testRequestParameterCheck() throws Exception {
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
HttpRequestInterceptor interceptor = new RequestDefaultHeaders();
|
||||||
|
interceptor.process(null, context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNoDefaultHeadersForConnectRequest() throws Exception {
|
||||||
|
HttpRequest request = new BasicHttpRequest("CONNECT", "www.somedomain.com");
|
||||||
|
List<Header> defheaders = new ArrayList<Header>();
|
||||||
|
defheaders.add(new BasicHeader("custom", "stuff"));
|
||||||
|
request.getParams().setParameter(AllClientPNames.DEFAULT_HEADERS, defheaders);
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
|
||||||
|
HttpRequestInterceptor interceptor = new RequestDefaultHeaders();
|
||||||
|
interceptor.process(request, context);
|
||||||
|
Header header1 = request.getFirstHeader("custom");
|
||||||
|
Assert.assertNull(header1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDefaultHeaders() throws Exception {
|
||||||
|
HttpRequest request = new BasicHttpRequest("GET", "/");
|
||||||
|
request.addHeader("custom", "stuff");
|
||||||
|
List<Header> defheaders = new ArrayList<Header>();
|
||||||
|
defheaders.add(new BasicHeader("custom", "more stuff"));
|
||||||
|
request.getParams().setParameter(AllClientPNames.DEFAULT_HEADERS, defheaders);
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
|
||||||
|
HttpRequestInterceptor interceptor = new RequestDefaultHeaders();
|
||||||
|
interceptor.process(request, context);
|
||||||
|
Header[] headers = request.getHeaders("custom");
|
||||||
|
Assert.assertNotNull(headers);
|
||||||
|
Assert.assertEquals(2, headers.length);
|
||||||
|
Assert.assertEquals("stuff", headers[0].getValue());
|
||||||
|
Assert.assertEquals("more stuff", headers[1].getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -28,7 +28,6 @@ package org.apache.http.client.protocol;
|
||||||
|
|
||||||
import junit.framework.Assert;
|
import junit.framework.Assert;
|
||||||
|
|
||||||
import org.apache.commons.codec.binary.Base64;
|
|
||||||
import org.apache.http.Header;
|
import org.apache.http.Header;
|
||||||
import org.apache.http.HttpHost;
|
import org.apache.http.HttpHost;
|
||||||
import org.apache.http.HttpRequest;
|
import org.apache.http.HttpRequest;
|
||||||
|
@ -53,12 +52,34 @@ import org.mockito.Mockito;
|
||||||
|
|
||||||
public class TestRequestProxyAuthentication {
|
public class TestRequestProxyAuthentication {
|
||||||
|
|
||||||
|
@Test(expected=IllegalArgumentException.class)
|
||||||
|
public void testRequestParameterCheck() throws Exception {
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
HttpRequestInterceptor interceptor = new RequestProxyAuthentication();
|
||||||
|
interceptor.process(null, context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected=IllegalArgumentException.class)
|
||||||
|
public void testContextParameterCheck() throws Exception {
|
||||||
|
HttpRequest request = new BasicHttpRequest("GET", "/");
|
||||||
|
HttpRequestInterceptor interceptor = new RequestProxyAuthentication();
|
||||||
|
interceptor.process(request, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected=IllegalStateException.class)
|
||||||
|
public void testConnectionInContextCheck() throws Exception {
|
||||||
|
HttpRequest request = new BasicHttpRequest("GET", "/");
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
HttpRequestInterceptor interceptor = new RequestProxyAuthentication();
|
||||||
|
interceptor.process(request, context);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testProxyPlainConnection() throws Exception {
|
public void testProxyAuthOverPlainConnection() throws Exception {
|
||||||
HttpRequest request = new BasicHttpRequest("GET", "/");
|
HttpRequest request = new BasicHttpRequest("GET", "/");
|
||||||
HttpContext context = new BasicHttpContext();
|
HttpContext context = new BasicHttpContext();
|
||||||
|
|
||||||
HttpHost target = new HttpHost("localhost", 80, "https");
|
HttpHost target = new HttpHost("localhost", 443, "https");
|
||||||
HttpHost proxy = new HttpHost("localhost", 8080);
|
HttpHost proxy = new HttpHost("localhost", 8080);
|
||||||
HttpRoute route = new HttpRoute(target, null, proxy, false,
|
HttpRoute route = new HttpRoute(target, null, proxy, false,
|
||||||
TunnelType.PLAIN, LayerType.PLAIN);
|
TunnelType.PLAIN, LayerType.PLAIN);
|
||||||
|
@ -69,8 +90,6 @@ public class TestRequestProxyAuthentication {
|
||||||
BasicScheme authscheme = new BasicScheme();
|
BasicScheme authscheme = new BasicScheme();
|
||||||
Credentials creds = new UsernamePasswordCredentials("user", "secret");
|
Credentials creds = new UsernamePasswordCredentials("user", "secret");
|
||||||
AuthScope authscope = new AuthScope("localhost", 8080, "auth-realm", "http");
|
AuthScope authscope = new AuthScope("localhost", 8080, "auth-realm", "http");
|
||||||
authscheme.authenticate(creds, request, context);
|
|
||||||
|
|
||||||
BasicHeader challenge = new BasicHeader(AUTH.PROXY_AUTH, "BASIC realm=auth-realm");
|
BasicHeader challenge = new BasicHeader(AUTH.PROXY_AUTH, "BASIC realm=auth-realm");
|
||||||
authscheme.processChallenge(challenge);
|
authscheme.processChallenge(challenge);
|
||||||
|
|
||||||
|
@ -90,11 +109,11 @@ public class TestRequestProxyAuthentication {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testProxyTunneledConnection() throws Exception {
|
public void testProxyAuthOverTunneledConnection() throws Exception {
|
||||||
HttpRequest request = new BasicHttpRequest("GET", "/");
|
HttpRequest request = new BasicHttpRequest("GET", "/");
|
||||||
HttpContext context = new BasicHttpContext();
|
HttpContext context = new BasicHttpContext();
|
||||||
|
|
||||||
HttpHost target = new HttpHost("localhost", 80, "https");
|
HttpHost target = new HttpHost("localhost", 443, "https");
|
||||||
HttpHost proxy = new HttpHost("localhost", 8080);
|
HttpHost proxy = new HttpHost("localhost", 8080);
|
||||||
HttpRoute route = new HttpRoute(target, null, proxy, true,
|
HttpRoute route = new HttpRoute(target, null, proxy, true,
|
||||||
TunnelType.TUNNELLED, LayerType.LAYERED);
|
TunnelType.TUNNELLED, LayerType.LAYERED);
|
||||||
|
@ -105,7 +124,6 @@ public class TestRequestProxyAuthentication {
|
||||||
BasicScheme authscheme = new BasicScheme();
|
BasicScheme authscheme = new BasicScheme();
|
||||||
Credentials creds = new UsernamePasswordCredentials("user", "secret");
|
Credentials creds = new UsernamePasswordCredentials("user", "secret");
|
||||||
AuthScope authscope = new AuthScope("localhost", 8080, "auth-realm", "http");
|
AuthScope authscope = new AuthScope("localhost", 8080, "auth-realm", "http");
|
||||||
authscheme.authenticate(creds, request, context);
|
|
||||||
|
|
||||||
BasicHeader challenge = new BasicHeader(AUTH.PROXY_AUTH, "BASIC realm=auth-realm");
|
BasicHeader challenge = new BasicHeader(AUTH.PROXY_AUTH, "BASIC realm=auth-realm");
|
||||||
authscheme.processChallenge(challenge);
|
authscheme.processChallenge(challenge);
|
||||||
|
@ -124,4 +142,159 @@ public class TestRequestProxyAuthentication {
|
||||||
Assert.assertNull(header);
|
Assert.assertNull(header);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPreserveAuthHeader() throws Exception {
|
||||||
|
HttpRequest request = new BasicHttpRequest("GET", "/");
|
||||||
|
request.addHeader(AUTH.PROXY_AUTH_RESP, "Basic c3R1ZmY6c3R1ZmY=");
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
|
||||||
|
HttpHost target = new HttpHost("localhost", 443, "https");
|
||||||
|
HttpHost proxy = new HttpHost("localhost", 8080);
|
||||||
|
HttpRoute route = new HttpRoute(target, null, proxy, true,
|
||||||
|
TunnelType.TUNNELLED, LayerType.LAYERED);
|
||||||
|
|
||||||
|
HttpRoutedConnection conn = Mockito.mock(HttpRoutedConnection.class);
|
||||||
|
Mockito.when(conn.getRoute()).thenReturn(route);
|
||||||
|
|
||||||
|
BasicScheme authscheme = new BasicScheme();
|
||||||
|
Credentials creds = new UsernamePasswordCredentials("user", "secret");
|
||||||
|
AuthScope authscope = new AuthScope("localhost", 8080, "auth-realm", "http");
|
||||||
|
|
||||||
|
BasicHeader challenge = new BasicHeader(AUTH.PROXY_AUTH, "BASIC realm=auth-realm");
|
||||||
|
authscheme.processChallenge(challenge);
|
||||||
|
|
||||||
|
AuthState authstate = new AuthState();
|
||||||
|
authstate.setAuthScheme(authscheme);
|
||||||
|
authstate.setAuthScope(authscope);
|
||||||
|
authstate.setCredentials(creds);
|
||||||
|
|
||||||
|
context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
|
||||||
|
context.setAttribute(ClientContext.PROXY_AUTH_STATE, authstate);
|
||||||
|
|
||||||
|
HttpRequestInterceptor interceptor = new RequestProxyAuthentication();
|
||||||
|
interceptor.process(request, context);
|
||||||
|
Header header = request.getFirstHeader(AUTH.PROXY_AUTH_RESP);
|
||||||
|
Assert.assertNotNull(header);
|
||||||
|
Assert.assertEquals("Basic c3R1ZmY6c3R1ZmY=", header.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAuthStateNotSet() throws Exception {
|
||||||
|
HttpRequest request = new BasicHttpRequest("GET", "/");
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
|
||||||
|
HttpHost target = new HttpHost("localhost", 80, "http");
|
||||||
|
HttpHost proxy = new HttpHost("localhost", 8080);
|
||||||
|
HttpRoute route = new HttpRoute(target, null, proxy, false,
|
||||||
|
TunnelType.PLAIN, LayerType.PLAIN);
|
||||||
|
|
||||||
|
HttpRoutedConnection conn = Mockito.mock(HttpRoutedConnection.class);
|
||||||
|
Mockito.when(conn.getRoute()).thenReturn(route);
|
||||||
|
|
||||||
|
context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
|
||||||
|
context.setAttribute(ClientContext.PROXY_AUTH_STATE, null);
|
||||||
|
|
||||||
|
HttpRequestInterceptor interceptor = new RequestProxyAuthentication();
|
||||||
|
interceptor.process(request, context);
|
||||||
|
Header header = request.getFirstHeader(AUTH.PROXY_AUTH_RESP);
|
||||||
|
Assert.assertNull(header);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAuthSchemeNotSet() throws Exception {
|
||||||
|
HttpRequest request = new BasicHttpRequest("GET", "/");
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
|
||||||
|
HttpHost target = new HttpHost("localhost", 80, "http");
|
||||||
|
HttpHost proxy = new HttpHost("localhost", 8080);
|
||||||
|
HttpRoute route = new HttpRoute(target, null, proxy, false,
|
||||||
|
TunnelType.PLAIN, LayerType.PLAIN);
|
||||||
|
|
||||||
|
HttpRoutedConnection conn = Mockito.mock(HttpRoutedConnection.class);
|
||||||
|
Mockito.when(conn.getRoute()).thenReturn(route);
|
||||||
|
|
||||||
|
AuthState authstate = new AuthState();
|
||||||
|
|
||||||
|
context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
|
||||||
|
context.setAttribute(ClientContext.PROXY_AUTH_STATE, authstate);
|
||||||
|
|
||||||
|
HttpRequestInterceptor interceptor = new RequestProxyAuthentication();
|
||||||
|
interceptor.process(request, context);
|
||||||
|
Header header = request.getFirstHeader(AUTH.PROXY_AUTH_RESP);
|
||||||
|
Assert.assertNull(header);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAuthCredentialsNotSet() throws Exception {
|
||||||
|
HttpRequest request = new BasicHttpRequest("GET", "/");
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
|
||||||
|
HttpHost target = new HttpHost("localhost", 80, "http");
|
||||||
|
HttpHost proxy = new HttpHost("localhost", 8080);
|
||||||
|
HttpRoute route = new HttpRoute(target, null, proxy, false,
|
||||||
|
TunnelType.PLAIN, LayerType.PLAIN);
|
||||||
|
|
||||||
|
HttpRoutedConnection conn = Mockito.mock(HttpRoutedConnection.class);
|
||||||
|
Mockito.when(conn.getRoute()).thenReturn(route);
|
||||||
|
|
||||||
|
AuthState authstate = new AuthState();
|
||||||
|
|
||||||
|
BasicScheme authscheme = new BasicScheme();
|
||||||
|
BasicHeader challenge = new BasicHeader(AUTH.PROXY_AUTH, "BASIC realm=auth-realm");
|
||||||
|
authscheme.processChallenge(challenge);
|
||||||
|
|
||||||
|
authstate.setAuthScheme(authscheme);
|
||||||
|
|
||||||
|
context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
|
||||||
|
context.setAttribute(ClientContext.PROXY_AUTH_STATE, authstate);
|
||||||
|
|
||||||
|
HttpRequestInterceptor interceptor = new RequestProxyAuthentication();
|
||||||
|
interceptor.process(request, context);
|
||||||
|
Header header = request.getFirstHeader(AUTH.PROXY_AUTH_RESP);
|
||||||
|
Assert.assertNull(header);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConnectionBasedAuthOnlyIfChallenged() throws Exception {
|
||||||
|
HttpRequest request = new BasicHttpRequest("GET", "/");
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
|
||||||
|
HttpHost target = new HttpHost("localhost", 80, "http");
|
||||||
|
HttpHost proxy = new HttpHost("localhost", 8080);
|
||||||
|
HttpRoute route = new HttpRoute(target, null, proxy, false,
|
||||||
|
TunnelType.PLAIN, LayerType.PLAIN);
|
||||||
|
|
||||||
|
HttpRoutedConnection conn = Mockito.mock(HttpRoutedConnection.class);
|
||||||
|
Mockito.when(conn.getRoute()).thenReturn(route);
|
||||||
|
|
||||||
|
AuthState authstate = new AuthState();
|
||||||
|
|
||||||
|
BasicScheme authscheme = new BasicScheme() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isConnectionBased() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
BasicHeader challenge = new BasicHeader(AUTH.PROXY_AUTH, "BASIC realm=auth-realm");
|
||||||
|
authscheme.processChallenge(challenge);
|
||||||
|
|
||||||
|
Credentials creds = new UsernamePasswordCredentials("user", "secret");
|
||||||
|
|
||||||
|
authstate.setAuthScheme(authscheme);
|
||||||
|
authstate.setCredentials(creds);
|
||||||
|
// No challenge
|
||||||
|
authstate.setAuthScope(null);
|
||||||
|
|
||||||
|
context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
|
||||||
|
context.setAttribute(ClientContext.PROXY_AUTH_STATE, authstate);
|
||||||
|
|
||||||
|
HttpRequestInterceptor interceptor = new RequestProxyAuthentication();
|
||||||
|
interceptor.process(request, context);
|
||||||
|
Header header = request.getFirstHeader(AUTH.PROXY_AUTH_RESP);
|
||||||
|
Assert.assertNull(header);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,221 @@
|
||||||
|
/*
|
||||||
|
* ====================================================================
|
||||||
|
*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright ownership.
|
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||||
|
* (the "License"); you may not use this file except in compliance with
|
||||||
|
* the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
* ====================================================================
|
||||||
|
*
|
||||||
|
* This software consists of voluntary contributions made by many
|
||||||
|
* individuals on behalf of the Apache Software Foundation. For more
|
||||||
|
* information on the Apache Software Foundation, please see
|
||||||
|
* <http://www.apache.org/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.apache.http.client.protocol;
|
||||||
|
|
||||||
|
import junit.framework.Assert;
|
||||||
|
|
||||||
|
import org.apache.http.Header;
|
||||||
|
import org.apache.http.HttpRequest;
|
||||||
|
import org.apache.http.HttpRequestInterceptor;
|
||||||
|
import org.apache.http.auth.AUTH;
|
||||||
|
import org.apache.http.auth.AuthScope;
|
||||||
|
import org.apache.http.auth.AuthState;
|
||||||
|
import org.apache.http.auth.Credentials;
|
||||||
|
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||||
|
import org.apache.http.impl.auth.BasicScheme;
|
||||||
|
import org.apache.http.message.BasicHeader;
|
||||||
|
import org.apache.http.message.BasicHttpRequest;
|
||||||
|
import org.apache.http.protocol.BasicHttpContext;
|
||||||
|
import org.apache.http.protocol.HttpContext;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class TestRequestTargetAuthentication {
|
||||||
|
|
||||||
|
@Test(expected=IllegalArgumentException.class)
|
||||||
|
public void testRequestParameterCheck() throws Exception {
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
HttpRequestInterceptor interceptor = new RequestTargetAuthentication();
|
||||||
|
interceptor.process(null, context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected=IllegalArgumentException.class)
|
||||||
|
public void testContextParameterCheck() throws Exception {
|
||||||
|
HttpRequest request = new BasicHttpRequest("GET", "/");
|
||||||
|
HttpRequestInterceptor interceptor = new RequestTargetAuthentication();
|
||||||
|
interceptor.process(request, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@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");
|
||||||
|
AuthScope authscope = new AuthScope("localhost", 8080, "auth-realm", "http");
|
||||||
|
BasicHeader challenge = new BasicHeader(AUTH.WWW_AUTH, "BASIC realm=auth-realm");
|
||||||
|
authscheme.processChallenge(challenge);
|
||||||
|
|
||||||
|
AuthState authstate = new AuthState();
|
||||||
|
authstate.setAuthScheme(authscheme);
|
||||||
|
authstate.setAuthScope(authscope);
|
||||||
|
authstate.setCredentials(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());
|
||||||
|
}
|
||||||
|
|
||||||
|
@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");
|
||||||
|
AuthScope authscope = new AuthScope("localhost", 8080, "auth-realm", "http");
|
||||||
|
BasicHeader challenge = new BasicHeader(AUTH.WWW_AUTH, "BASIC realm=auth-realm");
|
||||||
|
authscheme.processChallenge(challenge);
|
||||||
|
|
||||||
|
AuthState authstate = new AuthState();
|
||||||
|
authstate.setAuthScheme(authscheme);
|
||||||
|
authstate.setAuthScope(authscope);
|
||||||
|
authstate.setCredentials(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 testPreserveAuthHeader() throws Exception {
|
||||||
|
HttpRequest request = new BasicHttpRequest("GET", "/");
|
||||||
|
request.addHeader(AUTH.WWW_AUTH_RESP, "Basic c3R1ZmY6c3R1ZmY=");
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
|
||||||
|
BasicScheme authscheme = new BasicScheme();
|
||||||
|
Credentials creds = new UsernamePasswordCredentials("user", "secret");
|
||||||
|
AuthScope authscope = new AuthScope("localhost", 8080, "auth-realm", "http");
|
||||||
|
|
||||||
|
BasicHeader challenge = new BasicHeader(AUTH.WWW_AUTH, "BASIC realm=auth-realm");
|
||||||
|
authscheme.processChallenge(challenge);
|
||||||
|
|
||||||
|
AuthState authstate = new AuthState();
|
||||||
|
authstate.setAuthScheme(authscheme);
|
||||||
|
authstate.setAuthScope(authscope);
|
||||||
|
authstate.setCredentials(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 c3R1ZmY6c3R1ZmY=", header.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@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 testAuthSchemeNotSet() throws Exception {
|
||||||
|
HttpRequest request = new BasicHttpRequest("GET", "/");
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
|
||||||
|
AuthState authstate = new AuthState();
|
||||||
|
|
||||||
|
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 testAuthCredentialsNotSet() throws Exception {
|
||||||
|
HttpRequest request = new BasicHttpRequest("GET", "/");
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
|
||||||
|
AuthState authstate = new AuthState();
|
||||||
|
|
||||||
|
BasicScheme authscheme = new BasicScheme();
|
||||||
|
BasicHeader challenge = new BasicHeader(AUTH.WWW_AUTH, "BASIC realm=auth-realm");
|
||||||
|
authscheme.processChallenge(challenge);
|
||||||
|
|
||||||
|
authstate.setAuthScheme(authscheme);
|
||||||
|
|
||||||
|
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 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.setAuthScheme(authscheme);
|
||||||
|
authstate.setCredentials(creds);
|
||||||
|
// No challenge
|
||||||
|
authstate.setAuthScope(null);
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,251 @@
|
||||||
|
/*
|
||||||
|
* ====================================================================
|
||||||
|
*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright ownership.
|
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||||
|
* (the "License"); you may not use this file except in compliance with
|
||||||
|
* the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
* ====================================================================
|
||||||
|
*
|
||||||
|
* This software consists of voluntary contributions made by many
|
||||||
|
* individuals on behalf of the Apache Software Foundation. For more
|
||||||
|
* information on the Apache Software Foundation, please see
|
||||||
|
* <http://www.apache.org/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.apache.http.client.protocol;
|
||||||
|
|
||||||
|
import junit.framework.Assert;
|
||||||
|
|
||||||
|
import org.apache.http.HttpHost;
|
||||||
|
import org.apache.http.HttpResponse;
|
||||||
|
import org.apache.http.HttpResponseInterceptor;
|
||||||
|
import org.apache.http.HttpVersion;
|
||||||
|
import org.apache.http.auth.AUTH;
|
||||||
|
import org.apache.http.auth.AuthScope;
|
||||||
|
import org.apache.http.auth.AuthState;
|
||||||
|
import org.apache.http.auth.Credentials;
|
||||||
|
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||||
|
import org.apache.http.client.AuthCache;
|
||||||
|
import org.apache.http.impl.auth.BasicScheme;
|
||||||
|
import org.apache.http.impl.client.BasicAuthCache;
|
||||||
|
import org.apache.http.message.BasicHeader;
|
||||||
|
import org.apache.http.message.BasicHttpResponse;
|
||||||
|
import org.apache.http.protocol.BasicHttpContext;
|
||||||
|
import org.apache.http.protocol.ExecutionContext;
|
||||||
|
import org.apache.http.protocol.HttpContext;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class TestResponseAuthCache {
|
||||||
|
|
||||||
|
private HttpHost target;
|
||||||
|
private HttpHost proxy;
|
||||||
|
private Credentials creds1;
|
||||||
|
private Credentials creds2;
|
||||||
|
private AuthScope authscope1;
|
||||||
|
private AuthScope authscope2;
|
||||||
|
private BasicScheme authscheme1;
|
||||||
|
private BasicScheme authscheme2;
|
||||||
|
private AuthState targetState;
|
||||||
|
private AuthState proxyState;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
this.target = new HttpHost("localhost", 80);
|
||||||
|
this.proxy = new HttpHost("localhost", 8080);
|
||||||
|
|
||||||
|
this.creds1 = new UsernamePasswordCredentials("user1", "secret1");
|
||||||
|
this.creds2 = new UsernamePasswordCredentials("user2", "secret2");
|
||||||
|
this.authscope1 = new AuthScope(this.target);
|
||||||
|
this.authscope2 = new AuthScope(this.proxy);
|
||||||
|
this.authscheme1 = new BasicScheme();
|
||||||
|
this.authscheme2 = new BasicScheme();
|
||||||
|
|
||||||
|
this.targetState = new AuthState();
|
||||||
|
this.proxyState = new AuthState();
|
||||||
|
}
|
||||||
|
|
||||||
|
@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));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,143 @@
|
||||||
|
/*
|
||||||
|
* ====================================================================
|
||||||
|
*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright ownership.
|
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||||
|
* (the "License"); you may not use this file except in compliance with
|
||||||
|
* the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
* ====================================================================
|
||||||
|
*
|
||||||
|
* This software consists of voluntary contributions made by many
|
||||||
|
* individuals on behalf of the Apache Software Foundation. For more
|
||||||
|
* information on the Apache Software Foundation, please see
|
||||||
|
* <http://www.apache.org/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.apache.http.client.protocol;
|
||||||
|
|
||||||
|
import junit.framework.Assert;
|
||||||
|
|
||||||
|
import org.apache.http.HttpEntity;
|
||||||
|
import org.apache.http.HttpException;
|
||||||
|
import org.apache.http.HttpResponse;
|
||||||
|
import org.apache.http.HttpResponseInterceptor;
|
||||||
|
import org.apache.http.HttpVersion;
|
||||||
|
import org.apache.http.client.entity.DeflateDecompressingEntity;
|
||||||
|
import org.apache.http.client.entity.GzipDecompressingEntity;
|
||||||
|
import org.apache.http.entity.StringEntity;
|
||||||
|
import org.apache.http.message.BasicHttpResponse;
|
||||||
|
import org.apache.http.protocol.BasicHttpContext;
|
||||||
|
import org.apache.http.protocol.HttpContext;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class TestResponseContentEncoding {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testContentEncodingNoEntity() throws Exception {
|
||||||
|
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
|
||||||
|
HttpResponseInterceptor interceptor = new ResponseContentEncoding();
|
||||||
|
interceptor.process(response, context);
|
||||||
|
HttpEntity entity = response.getEntity();
|
||||||
|
Assert.assertNull(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNoContentEncoding() throws Exception {
|
||||||
|
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
|
||||||
|
StringEntity original = new StringEntity("plain stuff");
|
||||||
|
response.setEntity(original);
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
|
||||||
|
HttpResponseInterceptor interceptor = new ResponseContentEncoding();
|
||||||
|
interceptor.process(response, context);
|
||||||
|
HttpEntity entity = response.getEntity();
|
||||||
|
Assert.assertNotNull(entity);
|
||||||
|
Assert.assertTrue(entity instanceof StringEntity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGzipContentEncoding() throws Exception {
|
||||||
|
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
|
||||||
|
StringEntity original = new StringEntity("encoded stuff");
|
||||||
|
original.setContentEncoding("GZip");
|
||||||
|
response.setEntity(original);
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
|
||||||
|
HttpResponseInterceptor interceptor = new ResponseContentEncoding();
|
||||||
|
interceptor.process(response, context);
|
||||||
|
HttpEntity entity = response.getEntity();
|
||||||
|
Assert.assertNotNull(entity);
|
||||||
|
Assert.assertTrue(entity instanceof GzipDecompressingEntity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testXGzipContentEncoding() throws Exception {
|
||||||
|
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
|
||||||
|
StringEntity original = new StringEntity("encoded stuff");
|
||||||
|
original.setContentEncoding("x-gzip");
|
||||||
|
response.setEntity(original);
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
|
||||||
|
HttpResponseInterceptor interceptor = new ResponseContentEncoding();
|
||||||
|
interceptor.process(response, context);
|
||||||
|
HttpEntity entity = response.getEntity();
|
||||||
|
Assert.assertNotNull(entity);
|
||||||
|
Assert.assertTrue(entity instanceof GzipDecompressingEntity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeflateContentEncoding() throws Exception {
|
||||||
|
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
|
||||||
|
StringEntity original = new StringEntity("encoded stuff");
|
||||||
|
original.setContentEncoding("deFlaTe");
|
||||||
|
response.setEntity(original);
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
|
||||||
|
HttpResponseInterceptor interceptor = new ResponseContentEncoding();
|
||||||
|
interceptor.process(response, context);
|
||||||
|
HttpEntity entity = response.getEntity();
|
||||||
|
Assert.assertNotNull(entity);
|
||||||
|
Assert.assertTrue(entity instanceof DeflateDecompressingEntity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIdentityContentEncoding() throws Exception {
|
||||||
|
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
|
||||||
|
StringEntity original = new StringEntity("encoded stuff");
|
||||||
|
original.setContentEncoding("identity");
|
||||||
|
response.setEntity(original);
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
|
||||||
|
HttpResponseInterceptor interceptor = new ResponseContentEncoding();
|
||||||
|
interceptor.process(response, context);
|
||||||
|
HttpEntity entity = response.getEntity();
|
||||||
|
Assert.assertNotNull(entity);
|
||||||
|
Assert.assertTrue(entity instanceof StringEntity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected=HttpException.class)
|
||||||
|
public void testUnknownContentEncoding() throws Exception {
|
||||||
|
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
|
||||||
|
StringEntity original = new StringEntity("encoded stuff");
|
||||||
|
original.setContentEncoding("whatever");
|
||||||
|
response.setEntity(original);
|
||||||
|
HttpContext context = new BasicHttpContext();
|
||||||
|
|
||||||
|
HttpResponseInterceptor interceptor = new ResponseContentEncoding();
|
||||||
|
interceptor.process(response, context);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue