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.hc.client5.http.impl.auth;
28  
29  import org.apache.hc.client5.http.HttpRoute;
30  import org.apache.hc.client5.http.auth.AuthCache;
31  import org.apache.hc.client5.http.auth.AuthExchange;
32  import org.apache.hc.client5.http.auth.AuthScope;
33  import org.apache.hc.client5.http.auth.Credentials;
34  import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
35  import org.apache.hc.client5.http.protocol.HttpClientContext;
36  import org.apache.hc.client5.http.protocol.RequestAuthCache;
37  import org.apache.hc.core5.http.HttpHost;
38  import org.apache.hc.core5.http.HttpRequest;
39  import org.apache.hc.core5.http.HttpRequestInterceptor;
40  import org.apache.hc.core5.http.message.BasicHttpRequest;
41  import org.junit.Assert;
42  import org.junit.Before;
43  import org.junit.Test;
44  
45  public class TestRequestAuthCache {
46  
47      private HttpHost target;
48      private HttpHost proxy;
49      private Credentials creds1;
50      private Credentials creds2;
51      private AuthScope authscope1;
52      private AuthScope authscope2;
53      private BasicScheme authscheme1;
54      private BasicScheme authscheme2;
55      private BasicCredentialsProvider credProvider;
56  
57      @Before
58      public void setUp() {
59          this.target = new HttpHost("localhost", 80);
60          this.proxy = new HttpHost("localhost", 8080);
61  
62          this.credProvider = new BasicCredentialsProvider();
63          this.creds1 = new UsernamePasswordCredentials("user1", "secret1".toCharArray());
64          this.creds2 = new UsernamePasswordCredentials("user2", "secret2".toCharArray());
65          this.authscope1 = new AuthScope(this.target);
66          this.authscope2 = new AuthScope(this.proxy);
67          this.authscheme1 = new BasicScheme();
68          this.authscheme2 = new BasicScheme();
69  
70          this.credProvider.setCredentials(this.authscope1, this.creds1);
71          this.credProvider.setCredentials(this.authscope2, this.creds2);
72      }
73  
74      @Test(expected=NullPointerException.class)
75      public void testRequestParameterCheck() throws Exception {
76          final HttpClientContext context = HttpClientContext.create();
77          final HttpRequestInterceptor interceptor = new RequestAuthCache();
78          interceptor.process(null, null, context);
79      }
80  
81      @Test(expected=NullPointerException.class)
82      public void testContextParameterCheck() throws Exception {
83          final HttpRequest request = new BasicHttpRequest("GET", "/");
84          final HttpRequestInterceptor interceptor = new RequestAuthCache();
85          interceptor.process(request, null, null);
86      }
87  
88      @Test
89      public void testPreemptiveTargetAndProxyAuth() throws Exception {
90          final HttpRequest request = new BasicHttpRequest("GET", "/");
91  
92          final HttpClientContext context = HttpClientContext.create();
93          context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credProvider);
94          context.setAttribute(HttpClientContext.HTTP_ROUTE, new HttpRoute(this.target, null, this.proxy, false));
95  
96          final AuthCache authCache = new BasicAuthCache();
97          authCache.put(this.target, this.authscheme1);
98          authCache.put(this.proxy, this.authscheme2);
99  
100         context.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
101 
102         final HttpRequestInterceptor interceptor = new RequestAuthCache();
103         interceptor.process(request, null, context);
104 
105         final AuthExchange targetAuthExchange = context.getAuthExchange(this.target);
106         final AuthExchange proxyAuthExchange = context.getAuthExchange(this.proxy);
107 
108         Assert.assertNotNull(targetAuthExchange);
109         Assert.assertNotNull(targetAuthExchange.getAuthScheme());
110         Assert.assertNotNull(proxyAuthExchange);
111         Assert.assertNotNull(proxyAuthExchange.getAuthScheme());
112     }
113 
114     @Test
115     public void testCredentialsProviderNotSet() throws Exception {
116         final HttpRequest request = new BasicHttpRequest("GET", "/");
117 
118         final HttpClientContext context = HttpClientContext.create();
119         context.setAttribute(HttpClientContext.CREDS_PROVIDER, null);
120         context.setAttribute(HttpClientContext.HTTP_ROUTE, new HttpRoute(this.target, null, this.proxy, false));
121 
122         final AuthCache authCache = new BasicAuthCache();
123         authCache.put(this.target, this.authscheme1);
124         authCache.put(this.proxy, this.authscheme2);
125 
126         context.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
127 
128         final HttpRequestInterceptor interceptor = new RequestAuthCache();
129         interceptor.process(request, null, context);
130 
131         final AuthExchange targetAuthExchange = context.getAuthExchange(this.target);
132         final AuthExchange proxyAuthExchange = context.getAuthExchange(this.proxy);
133 
134         Assert.assertNotNull(targetAuthExchange);
135         Assert.assertNull(targetAuthExchange.getAuthScheme());
136         Assert.assertNotNull(proxyAuthExchange);
137         Assert.assertNull(proxyAuthExchange.getAuthScheme());
138     }
139 
140     @Test
141     public void testAuthCacheNotSet() throws Exception {
142         final HttpRequest request = new BasicHttpRequest("GET", "/");
143 
144         final HttpClientContext context = HttpClientContext.create();
145         context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credProvider);
146         context.setAttribute(HttpClientContext.HTTP_ROUTE, new HttpRoute(this.target, null, this.proxy, false));
147         context.setAttribute(HttpClientContext.AUTH_CACHE, null);
148 
149         final HttpRequestInterceptor interceptor = new RequestAuthCache();
150         interceptor.process(request, null, context);
151 
152         final AuthExchange targetAuthExchange = context.getAuthExchange(this.target);
153         final AuthExchange proxyAuthExchange = context.getAuthExchange(this.proxy);
154 
155         Assert.assertNotNull(targetAuthExchange);
156         Assert.assertNull(targetAuthExchange.getAuthScheme());
157         Assert.assertNotNull(proxyAuthExchange);
158         Assert.assertNull(proxyAuthExchange.getAuthScheme());
159     }
160 
161     @Test
162     public void testAuthCacheEmpty() throws Exception {
163         final HttpRequest request = new BasicHttpRequest("GET", "/");
164 
165         final HttpClientContext context = HttpClientContext.create();
166         context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credProvider);
167         context.setAttribute(HttpClientContext.HTTP_ROUTE, new HttpRoute(this.target, null, this.proxy, false));
168 
169         final AuthCache authCache = new BasicAuthCache();
170         context.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
171 
172         final HttpRequestInterceptor interceptor = new RequestAuthCache();
173         interceptor.process(request, null, context);
174 
175         final AuthExchange targetAuthExchange = context.getAuthExchange(this.target);
176         final AuthExchange proxyAuthExchange = context.getAuthExchange(this.proxy);
177 
178         Assert.assertNotNull(targetAuthExchange);
179         Assert.assertNull(targetAuthExchange.getAuthScheme());
180         Assert.assertNotNull(proxyAuthExchange);
181         Assert.assertNull(proxyAuthExchange.getAuthScheme());
182     }
183 
184 }