You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2011/02/28 20:57:51 UTC
svn commit: r1075508 - in /httpcomponents/httpclient/trunk/httpclient/src:
main/java/org/apache/http/client/protocol/
test/java/org/apache/http/client/protocol/
Author: olegk
Date: Mon Feb 28 19:57:51 2011
New Revision: 1075508
URL: http://svn.apache.org/viewvc?rev=1075508&view=rev
Log:
Reworked auth caching code; added more test cases for protocol interceptors
Added:
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestAcceptEncoding.java (with props)
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestAuthCache.java (with props)
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestClientConnControl.java (with props)
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestDefaultHeaders.java (with props)
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestTargetAuthentication.java (with props)
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestResponseAuthCache.java (with props)
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestResponseContentEncoding.java (with props)
Modified:
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/RequestAuthCache.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/RequestProxyAuthentication.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/ResponseAuthCache.java
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestProxyAuthentication.java
Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/RequestAuthCache.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/RequestAuthCache.java?rev=1075508&r1=1075507&r2=1075508&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/RequestAuthCache.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/RequestAuthCache.java Mon Feb 28 19:57:51 2011
@@ -42,7 +42,6 @@ import org.apache.http.auth.AuthState;
import org.apache.http.auth.Credentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
-import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
@@ -73,34 +72,30 @@ public class RequestAuthCache implements
AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE);
if (authCache == null) {
- context.setAttribute(ClientContext.AUTH_CACHE, new BasicAuthCache());
- } else {
+ return;
+ }
- CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(
- ClientContext.CREDS_PROVIDER);
+ CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(
+ ClientContext.CREDS_PROVIDER);
+ if (credsProvider == null) {
+ return;
+ }
- HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
- if (target != null) {
- AuthScheme authScheme = authCache.get(target);
- if (authScheme != null) {
- doPreemptiveAuth(
- target,
- authScheme,
- (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE),
- credsProvider);
- }
+ HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
+ AuthState targetState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
+ if (target != null && targetState != null) {
+ AuthScheme authScheme = authCache.get(target);
+ if (authScheme != null) {
+ doPreemptiveAuth(target, authScheme, targetState, credsProvider);
}
+ }
- HttpHost proxy = (HttpHost) context.getAttribute(ExecutionContext.HTTP_PROXY_HOST);
- if (proxy != null) {
- AuthScheme authScheme = authCache.get(proxy);
- if (authScheme != null) {
- doPreemptiveAuth(
- proxy,
- authScheme,
- (AuthState) context.getAttribute(ClientContext.PROXY_AUTH_STATE),
- credsProvider);
- }
+ HttpHost proxy = (HttpHost) context.getAttribute(ExecutionContext.HTTP_PROXY_HOST);
+ AuthState proxyState = (AuthState) context.getAttribute(ClientContext.PROXY_AUTH_STATE);
+ if (proxy != null && proxyState != null) {
+ AuthScheme authScheme = authCache.get(proxy);
+ if (authScheme != null) {
+ doPreemptiveAuth(proxy, authScheme, proxyState, credsProvider);
}
}
}
@@ -115,8 +110,8 @@ public class RequestAuthCache implements
this.log.debug("Re-using cached '" + schemeName + "' auth scheme for " + host);
}
- Credentials creds = credsProvider.getCredentials(
- new AuthScope(host, AuthScope.ANY_REALM, schemeName));
+ AuthScope authScope = new AuthScope(host, AuthScope.ANY_REALM, schemeName);
+ Credentials creds = credsProvider.getCredentials(authScope);
if (creds != null) {
authState.setAuthScheme(authScheme);
Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/RequestProxyAuthentication.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/RequestProxyAuthentication.java?rev=1075508&r1=1075507&r2=1075508&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/RequestProxyAuthentication.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/RequestProxyAuthentication.java Mon Feb 28 19:57:51 2011
@@ -79,6 +79,9 @@ public class RequestProxyAuthentication
HttpRoutedConnection conn = (HttpRoutedConnection) context.getAttribute(
ExecutionContext.HTTP_CONNECTION);
+ if (conn == null) {
+ throw new IllegalStateException("Client connection not specified in HTTP context");
+ }
HttpRoute route = conn.getRoute();
if (route.isTunnelled()) {
return;
Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/ResponseAuthCache.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/ResponseAuthCache.java?rev=1075508&r1=1075507&r2=1075508&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/ResponseAuthCache.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/ResponseAuthCache.java Mon Feb 28 19:57:51 2011
@@ -40,6 +40,7 @@ import org.apache.http.auth.AuthScheme;
import org.apache.http.auth.AuthState;
import org.apache.http.client.AuthCache;
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.HttpContext;
@@ -69,36 +70,49 @@ public class ResponseAuthCache implement
throw new IllegalArgumentException("HTTP context may not be null");
}
AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE);
- if (authCache != null) {
- cache(authCache,
- (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST),
- (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE));
- cache(authCache,
- (HttpHost) context.getAttribute(ExecutionContext.HTTP_PROXY_HOST),
- (AuthState) context.getAttribute(ClientContext.PROXY_AUTH_STATE));
+
+ 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);
+ }
}
- }
- private void cache(final AuthCache authCache, final HttpHost host, final AuthState authState) {
- if (authState == null) {
- return;
+ 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;
+ return false;
}
-
String schemeName = authScheme.getSchemeName();
- if (!schemeName.equalsIgnoreCase(AuthPolicy.BASIC) &&
- !schemeName.equalsIgnoreCase(AuthPolicy.DIGEST)) {
- return;
- }
+ return schemeName.equalsIgnoreCase(AuthPolicy.BASIC) ||
+ schemeName.equalsIgnoreCase(AuthPolicy.DIGEST);
+ }
+ private void cache(final AuthCache authCache, final HttpHost host, final AuthState authState) {
+ AuthScheme authScheme = authState.getAuthScheme();
if (authState.getAuthScope() != null) {
if (authState.getCredentials() != null) {
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);
} else {
Added: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestAcceptEncoding.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestAcceptEncoding.java?rev=1075508&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestAcceptEncoding.java (added)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestAcceptEncoding.java Mon Feb 28 19:57:51 2011
@@ -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());
+ }
+
+}
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestAcceptEncoding.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestAcceptEncoding.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestAcceptEncoding.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestAuthCache.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestAuthCache.java?rev=1075508&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestAuthCache.java (added)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestAuthCache.java Mon Feb 28 19:57:51 2011
@@ -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());
+ }
+
+}
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestAuthCache.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestAuthCache.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestAuthCache.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestClientConnControl.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestClientConnControl.java?rev=1075508&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestClientConnControl.java (added)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestClientConnControl.java Mon Feb 28 19:57:51 2011
@@ -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());
+ }
+
+}
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestClientConnControl.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestClientConnControl.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestClientConnControl.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestDefaultHeaders.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestDefaultHeaders.java?rev=1075508&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestDefaultHeaders.java (added)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestDefaultHeaders.java Mon Feb 28 19:57:51 2011
@@ -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());
+ }
+
+}
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestDefaultHeaders.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestDefaultHeaders.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestDefaultHeaders.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestProxyAuthentication.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestProxyAuthentication.java?rev=1075508&r1=1075507&r2=1075508&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestProxyAuthentication.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestProxyAuthentication.java Mon Feb 28 19:57:51 2011
@@ -28,7 +28,6 @@ package org.apache.http.client.protocol;
import junit.framework.Assert;
-import org.apache.commons.codec.binary.Base64;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
@@ -53,12 +52,34 @@ import org.mockito.Mockito;
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
- public void testProxyPlainConnection() throws Exception {
+ public void testProxyAuthOverPlainConnection() throws Exception {
HttpRequest request = new BasicHttpRequest("GET", "/");
HttpContext context = new BasicHttpContext();
- HttpHost target = new HttpHost("localhost", 80, "https");
+ HttpHost target = new HttpHost("localhost", 443, "https");
HttpHost proxy = new HttpHost("localhost", 8080);
HttpRoute route = new HttpRoute(target, null, proxy, false,
TunnelType.PLAIN, LayerType.PLAIN);
@@ -69,8 +90,6 @@ public class TestRequestProxyAuthenticat
BasicScheme authscheme = new BasicScheme();
Credentials creds = new UsernamePasswordCredentials("user", "secret");
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");
authscheme.processChallenge(challenge);
@@ -90,11 +109,11 @@ public class TestRequestProxyAuthenticat
}
@Test
- public void testProxyTunneledConnection() throws Exception {
+ public void testProxyAuthOverTunneledConnection() throws Exception {
HttpRequest request = new BasicHttpRequest("GET", "/");
HttpContext context = new BasicHttpContext();
- HttpHost target = new HttpHost("localhost", 80, "https");
+ 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);
@@ -105,7 +124,6 @@ public class TestRequestProxyAuthenticat
BasicScheme authscheme = new BasicScheme();
Credentials creds = new UsernamePasswordCredentials("user", "secret");
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");
authscheme.processChallenge(challenge);
@@ -124,4 +142,159 @@ public class TestRequestProxyAuthenticat
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);
+ }
+
}
Added: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestTargetAuthentication.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestTargetAuthentication.java?rev=1075508&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestTargetAuthentication.java (added)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestTargetAuthentication.java Mon Feb 28 19:57:51 2011
@@ -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);
+ }
+
+}
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestTargetAuthentication.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestTargetAuthentication.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRequestTargetAuthentication.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestResponseAuthCache.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestResponseAuthCache.java?rev=1075508&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestResponseAuthCache.java (added)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestResponseAuthCache.java Mon Feb 28 19:57:51 2011
@@ -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));
+ }
+
+}
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestResponseAuthCache.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestResponseAuthCache.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestResponseAuthCache.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestResponseContentEncoding.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestResponseContentEncoding.java?rev=1075508&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestResponseContentEncoding.java (added)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestResponseContentEncoding.java Mon Feb 28 19:57:51 2011
@@ -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);
+ }
+
+}
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestResponseContentEncoding.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestResponseContentEncoding.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestResponseContentEncoding.java
------------------------------------------------------------------------------
svn:mime-type = text/plain