View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.client.protocol;
28  
29  import org.apache.http.HttpHost;
30  import org.apache.http.HttpRequest;
31  import org.apache.http.HttpRequestInterceptor;
32  import org.apache.http.auth.AuthProtocolState;
33  import org.apache.http.auth.AuthScope;
34  import org.apache.http.auth.AuthState;
35  import org.apache.http.auth.Credentials;
36  import org.apache.http.auth.UsernamePasswordCredentials;
37  import org.apache.http.client.AuthCache;
38  import org.apache.http.conn.routing.HttpRoute;
39  import org.apache.http.impl.auth.BasicScheme;
40  import org.apache.http.impl.client.BasicAuthCache;
41  import org.apache.http.impl.client.BasicCredentialsProvider;
42  import org.apache.http.message.BasicHttpRequest;
43  import org.apache.http.protocol.HttpCoreContext;
44  import org.junit.Assert;
45  import org.junit.Before;
46  import org.junit.Test;
47  
48  public class TestRequestAuthCache {
49  
50      private HttpHost target;
51      private HttpHost proxy;
52      private Credentials creds1;
53      private Credentials creds2;
54      private AuthScope authscope1;
55      private AuthScope authscope2;
56      private BasicScheme authscheme1;
57      private BasicScheme authscheme2;
58      private BasicCredentialsProvider credProvider;
59      private AuthState targetState;
60      private AuthState proxyState;
61  
62      @Before
63      public void setUp() {
64          this.target = new HttpHost("localhost", 80);
65          this.proxy = new HttpHost("localhost", 8080);
66  
67          this.credProvider = new BasicCredentialsProvider();
68          this.creds1 = new UsernamePasswordCredentials("user1", "secret1");
69          this.creds2 = new UsernamePasswordCredentials("user2", "secret2");
70          this.authscope1 = new AuthScope(this.target);
71          this.authscope2 = new AuthScope(this.proxy);
72          this.authscheme1 = new BasicScheme();
73          this.authscheme2 = new BasicScheme();
74  
75          this.credProvider.setCredentials(this.authscope1, this.creds1);
76          this.credProvider.setCredentials(this.authscope2, this.creds2);
77  
78          this.targetState = new AuthState();
79          this.proxyState = new AuthState();
80      }
81  
82      @Test(expected=IllegalArgumentException.class)
83      public void testRequestParameterCheck() throws Exception {
84          final HttpClientContext context = HttpClientContext.create();
85          final HttpRequestInterceptor interceptor = new RequestAuthCache();
86          interceptor.process(null, context);
87      }
88  
89      @Test(expected=IllegalArgumentException.class)
90      public void testContextParameterCheck() throws Exception {
91          final HttpRequest request = new BasicHttpRequest("GET", "/");
92          final HttpRequestInterceptor interceptor = new RequestAuthCache();
93          interceptor.process(request, null);
94      }
95  
96      @Test
97      public void testPreemptiveTargetAndProxyAuth() throws Exception {
98          final HttpRequest request = new BasicHttpRequest("GET", "/");
99  
100         final HttpClientContext context = HttpClientContext.create();
101         context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credProvider);
102         context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target);
103         context.setAttribute(HttpClientContext.HTTP_ROUTE, new HttpRoute(this.target, null, this.proxy, false));
104         context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, this.targetState);
105         context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, this.proxyState);
106 
107         final AuthCache authCache = new BasicAuthCache();
108         authCache.put(this.target, this.authscheme1);
109         authCache.put(this.proxy, this.authscheme2);
110 
111         context.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
112 
113         final HttpRequestInterceptor interceptor = new RequestAuthCache();
114         interceptor.process(request, context);
115         Assert.assertNotNull(this.targetState.getAuthScheme());
116         Assert.assertSame(this.creds1, this.targetState.getCredentials());
117         Assert.assertNotNull(this.proxyState.getAuthScheme());
118         Assert.assertSame(this.creds2, this.proxyState.getCredentials());
119     }
120 
121     @Test
122     public void testCredentialsProviderNotSet() throws Exception {
123         final HttpRequest request = new BasicHttpRequest("GET", "/");
124 
125         final HttpClientContext context = HttpClientContext.create();
126         context.setAttribute(HttpClientContext.CREDS_PROVIDER, null);
127         context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target);
128         context.setAttribute(HttpClientContext.HTTP_ROUTE, new HttpRoute(this.target, null, this.proxy, false));
129         context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, this.targetState);
130         context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, this.proxyState);
131 
132         final AuthCache authCache = new BasicAuthCache();
133         authCache.put(this.target, this.authscheme1);
134         authCache.put(this.proxy, this.authscheme2);
135 
136         context.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
137 
138         final HttpRequestInterceptor interceptor = new RequestAuthCache();
139         interceptor.process(request, context);
140         Assert.assertNull(this.targetState.getAuthScheme());
141         Assert.assertNull(this.targetState.getCredentials());
142         Assert.assertNull(this.proxyState.getAuthScheme());
143         Assert.assertNull(this.proxyState.getCredentials());
144     }
145 
146     @Test
147     public void testAuthCacheNotSet() throws Exception {
148         final HttpRequest request = new BasicHttpRequest("GET", "/");
149 
150         final HttpClientContext context = HttpClientContext.create();
151         context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credProvider);
152         context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target);
153         context.setAttribute(HttpClientContext.HTTP_ROUTE, new HttpRoute(this.target, null, this.proxy, false));
154         context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, this.targetState);
155         context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, this.proxyState);
156         context.setAttribute(HttpClientContext.AUTH_CACHE, null);
157 
158         final HttpRequestInterceptor interceptor = new RequestAuthCache();
159         interceptor.process(request, context);
160         Assert.assertNull(this.targetState.getAuthScheme());
161         Assert.assertNull(this.targetState.getCredentials());
162         Assert.assertNull(this.proxyState.getAuthScheme());
163         Assert.assertNull(this.proxyState.getCredentials());
164     }
165 
166     @Test
167     public void testAuthCacheEmpty() throws Exception {
168         final HttpRequest request = new BasicHttpRequest("GET", "/");
169 
170         final HttpClientContext context = HttpClientContext.create();
171         context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credProvider);
172         context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target);
173         context.setAttribute(HttpClientContext.HTTP_ROUTE, new HttpRoute(this.target, null, this.proxy, false));
174         context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, this.targetState);
175         context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, this.proxyState);
176 
177         final AuthCache authCache = new BasicAuthCache();
178         context.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
179 
180         final HttpRequestInterceptor interceptor = new RequestAuthCache();
181         interceptor.process(request, context);
182         Assert.assertNull(this.targetState.getAuthScheme());
183         Assert.assertNull(this.targetState.getCredentials());
184         Assert.assertNull(this.proxyState.getAuthScheme());
185         Assert.assertNull(this.proxyState.getCredentials());
186     }
187 
188     @Test
189     public void testNoMatchingCredentials() throws Exception {
190         final HttpRequest request = new BasicHttpRequest("GET", "/");
191 
192         this.credProvider.clear();
193 
194         final HttpClientContext context = HttpClientContext.create();
195         context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credProvider);
196         context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target);
197         context.setAttribute(HttpClientContext.HTTP_ROUTE, new HttpRoute(this.target, null, this.proxy, false));
198         context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, this.targetState);
199         context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, this.proxyState);
200 
201         final AuthCache authCache = new BasicAuthCache();
202         authCache.put(this.target, this.authscheme1);
203         authCache.put(this.proxy, this.authscheme2);
204 
205         context.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
206 
207         final HttpRequestInterceptor interceptor = new RequestAuthCache();
208         interceptor.process(request, context);
209         Assert.assertNull(this.targetState.getAuthScheme());
210         Assert.assertNull(this.targetState.getCredentials());
211         Assert.assertNull(this.proxyState.getAuthScheme());
212         Assert.assertNull(this.proxyState.getCredentials());
213     }
214 
215     @Test
216     public void testAuthSchemeAlreadySet() throws Exception {
217         final HttpRequest request = new BasicHttpRequest("GET", "/");
218 
219         final HttpClientContext context = HttpClientContext.create();
220         context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credProvider);
221         context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target);
222         context.setAttribute(HttpClientContext.HTTP_ROUTE, new HttpRoute(this.target, null, this.proxy, false));
223         context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, this.targetState);
224         context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, this.proxyState);
225 
226         final AuthCache authCache = new BasicAuthCache();
227         authCache.put(this.target, this.authscheme1);
228         authCache.put(this.proxy, this.authscheme2);
229 
230         context.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
231 
232         this.targetState.setState(AuthProtocolState.CHALLENGED);
233         this.targetState.update(new BasicScheme(), new UsernamePasswordCredentials("user3", "secret3"));
234         this.proxyState.setState(AuthProtocolState.CHALLENGED);
235         this.proxyState.update(new BasicScheme(), new UsernamePasswordCredentials("user4", "secret4"));
236 
237         final HttpRequestInterceptor interceptor = new RequestAuthCache();
238         interceptor.process(request, context);
239         Assert.assertNotSame(this.authscheme1, this.targetState.getAuthScheme());
240         Assert.assertNotSame(this.creds1, this.targetState.getCredentials());
241         Assert.assertNotSame(this.authscheme2, this.proxyState.getAuthScheme());
242         Assert.assertNotSame(this.creds2, this.proxyState.getCredentials());
243     }
244 
245 }