1 package com.atlassian.sal.core.net;
2
3 import org.apache.http.HttpHeaders;
4 import org.apache.http.HttpHost;
5 import org.apache.http.auth.AuthScheme;
6 import org.apache.http.auth.ChallengeState;
7 import org.apache.http.impl.auth.BasicScheme;
8 import org.apache.http.impl.auth.DigestScheme;
9 import org.apache.http.message.BasicHeader;
10 import org.junit.BeforeClass;
11 import org.junit.Test;
12
13 import static org.hamcrest.MatcherAssert.assertThat;
14 import static org.hamcrest.Matchers.instanceOf;
15 import static org.hamcrest.Matchers.is;
16 import static org.hamcrest.Matchers.nullValue;
17
18 public class AllPortsAuthCacheTest {
19 private static AllPortsAuthCache authCache = new AllPortsAuthCache();
20 private static final String hostName = "sample.host";
21 private static final HttpHost specificHost = new HttpHost(hostName, 80);
22 private static final AuthScheme specificAuthScheme = new BasicScheme();
23 private static final AuthScheme allPortsAuthScheme = new DigestScheme();
24
25 @BeforeClass
26 public static void before() {
27 authCache.put(specificHost, specificAuthScheme);
28 authCache.put(new HttpHost(hostName), allPortsAuthScheme);
29 }
30
31 @Test
32 public void assertSpecificFoundFirst() {
33
34 assertThat(authCache.get(specificHost), is(instanceOf(BasicScheme.class)));
35 }
36
37 @Test
38 public void assertAllPortsFound() {
39 assertThat(authCache.get(new HttpHost(hostName, 8090)), is(instanceOf(DigestScheme.class)));
40 }
41
42 @Test
43 public void assertOtherHostNotFound() {
44 assertThat(authCache.get(new HttpHost("other.host", 80)), is(nullValue()));
45 }
46
47 @Test
48 public void assertChallengeStateSurvivesSerialisation() throws Exception {
49 AllPortsAuthCache localCache = new AllPortsAuthCache();
50
51 BasicScheme proxyScheme = new BasicScheme();
52 proxyScheme.processChallenge(
53 new BasicHeader(HttpHeaders.PROXY_AUTHENTICATE, "Basic "));
54
55 localCache.put(specificHost, proxyScheme);
56
57 BasicScheme outOfCache = (BasicScheme) localCache.get(specificHost);
58 assertThat(outOfCache.getChallengeState(), is(ChallengeState.PROXY));
59 }
60 }