1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
41
42 @SuppressWarnings("boxing")
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 }