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  
28  package org.apache.http.impl.client;
29  
30  import org.apache.http.HttpHost;
31  import org.apache.http.auth.AuthScheme;
32  import org.apache.http.conn.SchemePortResolver;
33  import org.apache.http.impl.auth.BasicScheme;
34  import org.apache.http.impl.auth.NTLMScheme;
35  import org.junit.Assert;
36  import org.junit.Test;
37  import org.mockito.Mockito;
38  
39  /**
40   * Unit tests for {@link BasicAuthCache}.
41   */
42  @SuppressWarnings("boxing") // test code
43  public class TestBasicAuthCache {
44  
45      @Test
46      public void testBasicStoreRestore() throws Exception {
47          final BasicAuthCache cache = new BasicAuthCache();
48          final AuthScheme authScheme = new BasicScheme();
49          cache.put(new HttpHost("localhost", 80), authScheme);
50          Assert.assertNotNull(cache.get(new HttpHost("localhost", 80)));
51          cache.remove(new HttpHost("localhost", 80));
52          Assert.assertNull(cache.get(new HttpHost("localhost", 80)));
53          cache.put(new HttpHost("localhost", 80), authScheme);
54          cache.clear();
55          Assert.assertNull(cache.get(new HttpHost("localhost", 80)));
56      }
57  
58      @Test(expected = IllegalArgumentException.class)
59      public void testNullKey() throws Exception {
60          final BasicAuthCache cache = new BasicAuthCache();
61          final AuthScheme authScheme = new BasicScheme();
62          cache.put(null, authScheme);
63      }
64  
65      @Test
66      public void testNullAuthScheme() throws Exception {
67          final BasicAuthCache cache = new BasicAuthCache();
68          cache.put(new HttpHost("localhost", 80), null);
69          Assert.assertNull(cache.get(new HttpHost("localhost", 80)));
70      }
71  
72      @Test
73      public void testGetKey() throws Exception {
74          final BasicAuthCache cache = new BasicAuthCache();
75          final HttpHost target = new HttpHost("localhost", 443, "https");
76          Assert.assertSame(target, cache.getKey(target));
77          Assert.assertEquals(target, cache.getKey(new HttpHost("localhost", -1, "https")));
78      }
79  
80      @Test
81      public void testGetKeyWithSchemeRegistry() throws Exception {
82          final SchemePortResolver schemePortResolver = Mockito.mock(SchemePortResolver.class);
83          final BasicAuthCache cache = new BasicAuthCache(schemePortResolver);
84          Mockito.when(schemePortResolver.resolve(new HttpHost("localhost", -1, "https"))).thenReturn(443);
85          final HttpHost target = new HttpHost("localhost", 443, "https");
86          Assert.assertSame(target, cache.getKey(target));
87          Assert.assertEquals(target, cache.getKey(new HttpHost("localhost", -1, "https")));
88      }
89  
90      @Test
91      public void testStoreNonserializable() throws Exception {
92          final BasicAuthCache cache = new BasicAuthCache();
93          final AuthScheme authScheme = new NTLMScheme();
94          cache.put(new HttpHost("localhost", 80), authScheme);
95          Assert.assertNull(cache.get(new HttpHost("localhost", 80)));
96      }
97  
98  }