Made behaviour of protocol interceptors more somewhat consistent; added test cases for RequestAddCookies

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1076792 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2011-03-03 20:59:52 +00:00
parent 41d8c9bdc5
commit e9f6856f4e
10 changed files with 370 additions and 24 deletions

View File

@ -99,7 +99,7 @@ public class RequestAddCookies implements HttpRequestInterceptor {
CookieStore cookieStore = (CookieStore) context.getAttribute(
ClientContext.COOKIE_STORE);
if (cookieStore == null) {
this.log.info("Cookie store not available in HTTP context");
this.log.debug("Cookie store not specified in HTTP context");
return;
}
@ -107,7 +107,7 @@ public class RequestAddCookies implements HttpRequestInterceptor {
CookieSpecRegistry registry = (CookieSpecRegistry) context.getAttribute(
ClientContext.COOKIESPEC_REGISTRY);
if (registry == null) {
this.log.info("CookieSpec registry not available in HTTP context");
this.log.debug("CookieSpec registry not specified in HTTP context");
return;
}
@ -115,14 +115,16 @@ public class RequestAddCookies implements HttpRequestInterceptor {
HttpHost targetHost = (HttpHost) context.getAttribute(
ExecutionContext.HTTP_TARGET_HOST);
if (targetHost == null) {
throw new IllegalStateException("Target host not specified in HTTP context");
this.log.debug("Target host not set in the context");
return;
}
// Obtain the client connection (required)
HttpRoutedConnection conn = (HttpRoutedConnection) context.getAttribute(
ExecutionContext.HTTP_CONNECTION);
if (conn == null) {
throw new IllegalStateException("Client connection not specified in HTTP context");
this.log.debug("HTTP connection not set in the context");
return;
}
String policy = HttpClientParams.getCookiePolicy(request.getParams());

View File

@ -72,12 +72,14 @@ public class RequestAuthCache implements HttpRequestInterceptor {
AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE);
if (authCache == null) {
this.log.debug("Auth cache not set in the context");
return;
}
CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(
ClientContext.CREDS_PROVIDER);
if (credsProvider == null) {
this.log.debug("Credentials provider not set in the context");
return;
}

View File

@ -29,6 +29,8 @@ package org.apache.http.client.protocol;
import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.annotation.Immutable;
import org.apache.http.HttpException;
@ -50,6 +52,8 @@ import org.apache.http.protocol.HttpContext;
@Immutable
public class RequestClientConnControl implements HttpRequestInterceptor {
private final Log log = LogFactory.getLog(getClass());
private static final String PROXY_CONN_DIRECTIVE = "Proxy-Connection";
public RequestClientConnControl() {
@ -72,7 +76,8 @@ public class RequestClientConnControl implements HttpRequestInterceptor {
HttpRoutedConnection conn = (HttpRoutedConnection) context.getAttribute(
ExecutionContext.HTTP_CONNECTION);
if (conn == null) {
throw new IllegalStateException("Client connection not specified in HTTP context");
this.log.debug("HTTP connection not set in the context");
return;
}
HttpRoute route = conn.getRoute();

View File

@ -80,7 +80,8 @@ public class RequestProxyAuthentication implements HttpRequestInterceptor {
HttpRoutedConnection conn = (HttpRoutedConnection) context.getAttribute(
ExecutionContext.HTTP_CONNECTION);
if (conn == null) {
throw new IllegalStateException("Client connection not specified in HTTP context");
this.log.debug("HTTP connection not set in the context");
return;
}
HttpRoute route = conn.getRoute();
if (route.isTunnelled()) {
@ -91,6 +92,7 @@ public class RequestProxyAuthentication implements HttpRequestInterceptor {
AuthState authState = (AuthState) context.getAttribute(
ClientContext.PROXY_AUTH_STATE);
if (authState == null) {
this.log.debug("Proxy auth state not set in the context");
return;
}

View File

@ -83,6 +83,7 @@ public class RequestTargetAuthentication implements HttpRequestInterceptor {
AuthState authState = (AuthState) context.getAttribute(
ClientContext.TARGET_AUTH_STATE);
if (authState == null) {
this.log.debug("Target auth state not set in the context");
return;
}

View File

@ -75,20 +75,21 @@ public class ResponseProcessCookies implements HttpResponseInterceptor {
CookieSpec cookieSpec = (CookieSpec) context.getAttribute(
ClientContext.COOKIE_SPEC);
if (cookieSpec == null) {
this.log.debug("Cookie spec not specified in HTTP context");
return;
}
// Obtain cookie store
CookieStore cookieStore = (CookieStore) context.getAttribute(
ClientContext.COOKIE_STORE);
if (cookieStore == null) {
this.log.info("CookieStore not available in HTTP context");
this.log.debug("Cookie store not specified in HTTP context");
return;
}
// Obtain actual CookieOrigin instance
CookieOrigin cookieOrigin = (CookieOrigin) context.getAttribute(
ClientContext.COOKIE_ORIGIN);
if (cookieOrigin == null) {
this.log.info("CookieOrigin not available in HTTP context");
this.log.debug("Cookie origin not specified in HTTP context");
return;
}
HeaderIterator it = response.headerIterator(SM.SET_COOKIE);

View File

@ -0,0 +1,319 @@
/*
* ====================================================================
*
* 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 org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.client.CookieStore;
import org.apache.http.client.params.AllClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.conn.HttpRoutedConnection;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.cookie.CookieOrigin;
import org.apache.http.cookie.CookieSpec;
import org.apache.http.cookie.CookieSpecRegistry;
import org.apache.http.cookie.SM;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.impl.cookie.BestMatchSpecFactory;
import org.apache.http.impl.cookie.BrowserCompatSpecFactory;
import org.apache.http.impl.cookie.IgnoreSpecFactory;
import org.apache.http.impl.cookie.NetscapeDraftSpecFactory;
import org.apache.http.impl.cookie.RFC2109Spec;
import org.apache.http.impl.cookie.RFC2109SpecFactory;
import org.apache.http.impl.cookie.RFC2965SpecFactory;
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.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
public class TestRequestAddCookies {
private HttpHost target;
private CookieStore cookieStore;
private CookieSpecRegistry cookieSpecRegistry;
@Before
public void setUp() {
this.target = new HttpHost("localhost", 80);
this.cookieStore = new BasicCookieStore();
BasicClientCookie cookie1 = new BasicClientCookie("name1", "value1");
cookie1.setDomain("localhost");
cookie1.setPath("/");
this.cookieStore.addCookie(cookie1);
BasicClientCookie cookie2 = new BasicClientCookie("name2", "value2");
cookie2.setDomain("localhost");
cookie2.setPath("/");
this.cookieStore.addCookie(cookie2);
this.cookieSpecRegistry = new CookieSpecRegistry();
this.cookieSpecRegistry.register(
CookiePolicy.BEST_MATCH,
new BestMatchSpecFactory());
this.cookieSpecRegistry.register(
CookiePolicy.BROWSER_COMPATIBILITY,
new BrowserCompatSpecFactory());
this.cookieSpecRegistry.register(
CookiePolicy.NETSCAPE,
new NetscapeDraftSpecFactory());
this.cookieSpecRegistry.register(
CookiePolicy.RFC_2109,
new RFC2109SpecFactory());
this.cookieSpecRegistry.register(
CookiePolicy.RFC_2965,
new RFC2965SpecFactory());
this.cookieSpecRegistry.register(
CookiePolicy.IGNORE_COOKIES,
new IgnoreSpecFactory());
}
@Test(expected=IllegalArgumentException.class)
public void testRequestParameterCheck() throws Exception {
HttpContext context = new BasicHttpContext();
HttpRequestInterceptor interceptor = new RequestAddCookies();
interceptor.process(null, context);
}
@Test(expected=IllegalArgumentException.class)
public void testContextParameterCheck() throws Exception {
HttpRequest request = new BasicHttpRequest("GET", "/");
HttpRequestInterceptor interceptor = new RequestAddCookies();
interceptor.process(request, null);
}
@Test
public void testAddCookies() throws Exception {
HttpRequest request = new BasicHttpRequest("GET", "/");
HttpRoute route = new HttpRoute(this.target, null, false);
HttpRoutedConnection conn = Mockito.mock(HttpRoutedConnection.class);
Mockito.when(conn.getRoute()).thenReturn(route);
Mockito.when(conn.isSecure()).thenReturn(Boolean.FALSE);
HttpContext context = new BasicHttpContext();
context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target);
context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
context.setAttribute(ClientContext.COOKIE_STORE, this.cookieStore);
context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry);
HttpRequestInterceptor interceptor = new RequestAddCookies();
interceptor.process(request, context);
Header[] headers1 = request.getHeaders(SM.COOKIE);
Assert.assertNotNull(headers1);
Assert.assertEquals(1, headers1.length);
Assert.assertEquals("name1=value1; name2=value2", headers1[0].getValue());
Header[] headers2 = request.getHeaders(SM.COOKIE2);
Assert.assertNotNull(headers2);
Assert.assertEquals(1, headers2.length);
Assert.assertEquals("$Version=1", headers2[0].getValue());
CookieOrigin cookieOrigin = (CookieOrigin) context.getAttribute(
ClientContext.COOKIE_ORIGIN);
Assert.assertNotNull(cookieOrigin);
Assert.assertEquals("localhost", cookieOrigin.getHost());
Assert.assertEquals(80, cookieOrigin.getPort());
Assert.assertEquals("/", cookieOrigin.getPath());
Assert.assertFalse(cookieOrigin.isSecure());
}
@Test
public void testCookiesForConnectRequest() throws Exception {
HttpRequest request = new BasicHttpRequest("CONNECT", "www.somedomain.com");
HttpRoute route = new HttpRoute(this.target, null, false);
HttpRoutedConnection conn = Mockito.mock(HttpRoutedConnection.class);
Mockito.when(conn.getRoute()).thenReturn(route);
Mockito.when(conn.isSecure()).thenReturn(Boolean.FALSE);
HttpContext context = new BasicHttpContext();
context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target);
context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
context.setAttribute(ClientContext.COOKIE_STORE, this.cookieStore);
context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry);
HttpRequestInterceptor interceptor = new RequestAddCookies();
interceptor.process(request, context);
Header[] headers1 = request.getHeaders(SM.COOKIE);
Assert.assertNotNull(headers1);
Assert.assertEquals(0, headers1.length);
Header[] headers2 = request.getHeaders(SM.COOKIE2);
Assert.assertNotNull(headers2);
Assert.assertEquals(0, headers2.length);
}
@Test
public void testNoCookieStore() throws Exception {
HttpRequest request = new BasicHttpRequest("GET", "/");
HttpRoute route = new HttpRoute(this.target, null, false);
HttpRoutedConnection conn = Mockito.mock(HttpRoutedConnection.class);
Mockito.when(conn.getRoute()).thenReturn(route);
Mockito.when(conn.isSecure()).thenReturn(Boolean.FALSE);
HttpContext context = new BasicHttpContext();
context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target);
context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
context.setAttribute(ClientContext.COOKIE_STORE, null);
context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry);
HttpRequestInterceptor interceptor = new RequestAddCookies();
interceptor.process(request, context);
Header[] headers1 = request.getHeaders(SM.COOKIE);
Assert.assertNotNull(headers1);
Assert.assertEquals(0, headers1.length);
Header[] headers2 = request.getHeaders(SM.COOKIE2);
Assert.assertNotNull(headers2);
Assert.assertEquals(0, headers2.length);
}
@Test
public void testNoCookieSpecRegistry() throws Exception {
HttpRequest request = new BasicHttpRequest("GET", "/");
HttpRoute route = new HttpRoute(this.target, null, false);
HttpRoutedConnection conn = Mockito.mock(HttpRoutedConnection.class);
Mockito.when(conn.getRoute()).thenReturn(route);
Mockito.when(conn.isSecure()).thenReturn(Boolean.FALSE);
HttpContext context = new BasicHttpContext();
context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target);
context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
context.setAttribute(ClientContext.COOKIE_STORE, this.cookieStore);
context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, null);
HttpRequestInterceptor interceptor = new RequestAddCookies();
interceptor.process(request, context);
Header[] headers1 = request.getHeaders(SM.COOKIE);
Assert.assertNotNull(headers1);
Assert.assertEquals(0, headers1.length);
Header[] headers2 = request.getHeaders(SM.COOKIE2);
Assert.assertNotNull(headers2);
Assert.assertEquals(0, headers2.length);
}
@Test
public void testNoTargetHost() throws Exception {
HttpRequest request = new BasicHttpRequest("GET", "/");
HttpRoute route = new HttpRoute(this.target, null, false);
HttpRoutedConnection conn = Mockito.mock(HttpRoutedConnection.class);
Mockito.when(conn.getRoute()).thenReturn(route);
Mockito.when(conn.isSecure()).thenReturn(Boolean.FALSE);
HttpContext context = new BasicHttpContext();
context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, null);
context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
context.setAttribute(ClientContext.COOKIE_STORE, this.cookieStore);
context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry);
HttpRequestInterceptor interceptor = new RequestAddCookies();
interceptor.process(request, context);
Header[] headers1 = request.getHeaders(SM.COOKIE);
Assert.assertNotNull(headers1);
Assert.assertEquals(0, headers1.length);
Header[] headers2 = request.getHeaders(SM.COOKIE2);
Assert.assertNotNull(headers2);
Assert.assertEquals(0, headers2.length);
}
@Test
public void testNoHttpConnection() throws Exception {
HttpRequest request = new BasicHttpRequest("GET", "/");
HttpContext context = new BasicHttpContext();
context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target);
context.setAttribute(ExecutionContext.HTTP_CONNECTION, null);
context.setAttribute(ClientContext.COOKIE_STORE, this.cookieStore);
context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry);
HttpRequestInterceptor interceptor = new RequestAddCookies();
interceptor.process(request, context);
Header[] headers1 = request.getHeaders(SM.COOKIE);
Assert.assertNotNull(headers1);
Assert.assertEquals(0, headers1.length);
Header[] headers2 = request.getHeaders(SM.COOKIE2);
Assert.assertNotNull(headers2);
Assert.assertEquals(0, headers2.length);
}
@Test
public void testAddCookiesUsingExplicitCookieSpec() throws Exception {
HttpRequest request = new BasicHttpRequest("GET", "/");
request.getParams().setParameter(AllClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);
HttpRoute route = new HttpRoute(this.target, null, false);
HttpRoutedConnection conn = Mockito.mock(HttpRoutedConnection.class);
Mockito.when(conn.getRoute()).thenReturn(route);
Mockito.when(conn.isSecure()).thenReturn(Boolean.FALSE);
HttpContext context = new BasicHttpContext();
context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target);
context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
context.setAttribute(ClientContext.COOKIE_STORE, this.cookieStore);
context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry);
HttpRequestInterceptor interceptor = new RequestAddCookies();
interceptor.process(request, context);
CookieSpec cookieSpec = (CookieSpec) context.getAttribute(
ClientContext.COOKIE_SPEC);
Assert.assertTrue(cookieSpec instanceof RFC2109Spec);
Header[] headers1 = request.getHeaders(SM.COOKIE);
Assert.assertNotNull(headers1);
Assert.assertEquals(2, headers1.length);
Assert.assertEquals("$Version=0; name1=value1", headers1[0].getValue());
Assert.assertEquals("$Version=0; name2=value2", headers1[1].getValue());
CookieOrigin cookieOrigin = (CookieOrigin) context.getAttribute(
ClientContext.COOKIE_ORIGIN);
Assert.assertNotNull(cookieOrigin);
Assert.assertEquals("localhost", cookieOrigin.getHost());
Assert.assertEquals(80, cookieOrigin.getPort());
Assert.assertEquals("/", cookieOrigin.getPath());
Assert.assertFalse(cookieOrigin.isSecure());
}
}

View File

@ -213,4 +213,34 @@ public class TestRequestAuthCache {
Assert.assertNull(this.proxyState.getCredentials());
}
@Test
public void testAuthSchemeAlreadySet() 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);
this.targetState.setAuthScheme(new BasicScheme());
this.targetState.setCredentials(new UsernamePasswordCredentials("user3", "secret3"));
this.proxyState.setAuthScheme(new BasicScheme());
this.proxyState.setCredentials(new UsernamePasswordCredentials("user4", "secret4"));
HttpRequestInterceptor interceptor = new RequestAuthCache();
interceptor.process(request, context);
Assert.assertNotSame(this.authscheme1, this.targetState.getAuthScheme());
Assert.assertNotSame(this.creds1, this.targetState.getCredentials());
Assert.assertNotSame(this.authscheme2, this.proxyState.getAuthScheme());
Assert.assertNotSame(this.creds2, this.proxyState.getCredentials());
}
}

View File

@ -53,14 +53,6 @@ public class TestRequestClientConnControl {
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");

View File

@ -66,14 +66,6 @@ public class TestRequestProxyAuthentication {
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 testProxyAuthOverPlainConnection() throws Exception {
HttpRequest request = new BasicHttpRequest("GET", "/");