1 package com.atlassian.marketplace.client.http;
2
3 import java.net.URI;
4
5 import com.atlassian.marketplace.client.http.HttpConfiguration.Credentials;
6 import com.atlassian.marketplace.client.http.HttpConfiguration.ProxyAuthMethod;
7 import com.atlassian.marketplace.client.http.HttpConfiguration.ProxyAuthParams;
8 import com.atlassian.marketplace.client.http.HttpConfiguration.ProxyConfiguration;
9 import com.atlassian.marketplace.client.http.HttpConfiguration.ProxyHost;
10 import com.atlassian.marketplace.client.impl.CommonsHttpTransport;
11 import com.atlassian.utt.http.TestHttpServer.RequestProperties;
12
13 import org.hamcrest.Matchers;
14 import org.junit.Test;
15
16 import static com.atlassian.fugue.Option.some;
17 import static com.atlassian.utt.http.TestHttpServer.BASIC_AUTH_CHALLENGE;
18 import static com.atlassian.utt.http.TestHttpServer.DIGEST_AUTH_CHALLENGE;
19 import static com.atlassian.utt.http.TestHttpServer.proxyAuthChallenge;
20 import static com.atlassian.utt.http.TestHttpServer.requests;
21 import static com.atlassian.utt.http.TestHttpServer.status;
22 import static com.atlassian.utt.http.TestHttpServer.RequestProperties.method;
23 import static com.atlassian.utt.http.TestHttpServer.RequestProperties.requestHasNoProxyAuth;
24 import static com.atlassian.utt.http.TestHttpServer.RequestProperties.requestHasProxyAuth;
25 import static com.atlassian.utt.http.TestHttpServer.RequestProperties.requestHasProxyBasicAuth;
26 import static com.atlassian.utt.http.TestHttpServer.RequestProperties.requestHasProxyDigestAuth;
27 import static com.atlassian.utt.http.TestHttpServer.RequestProperties.uri;
28 import static org.hamcrest.MatcherAssert.assertThat;
29 import static org.hamcrest.Matchers.allOf;
30 import static org.hamcrest.Matchers.contains;
31 import static org.hamcrest.Matchers.equalTo;
32
33 public class CommonsHttpTransportProxyTest extends CommonsHttpTransportTestBase
34 {
35 private static final int IMAGINARY_UNDERLYING_SERVER_PORT = 8111;
36 private static final URI IMAGINARY_UNDERLYING_SERVER_URI =
37 URI.create("http://localhost:" + IMAGINARY_UNDERLYING_SERVER_PORT);
38 private static final URI ABSOLUTE_TEST_URI = IMAGINARY_UNDERLYING_SERVER_URI.resolve(TEST_URI);
39
40 private static final String AUTH_USERNAME = "user";
41 private static final String AUTH_PASSWORD = "pass";
42 private static final String AUTH_HASH = "dXNlcjpwYXNz";
43
44 @Test
45 public void unauthedProxyRequest() throws Exception
46 {
47 http = makeHttp(baseProxyConfig());
48 resp = http.get(ABSOLUTE_TEST_URI);
49 assertThat(server, requests().is(
50 contains(
51 allOf(
52 method().is(equalTo("GET")),
53 uri().is(equalTo(ABSOLUTE_TEST_URI)),
54 requestHasNoProxyAuth()
55 )
56 )
57 ));
58 }
59
60 @Test
61 public void basicAuthProxyRequestUsesPreemptiveAuth() throws Exception
62 {
63 http = makeHttp(basicAuthProxyConfig());
64 resp = http.get(ABSOLUTE_TEST_URI);
65 assertThat(server, requests().is(
66 contains(
67 allOf(
68 method().is(equalTo("GET")),
69 uri().is(equalTo(ABSOLUTE_TEST_URI)),
70 requestHasProxyBasicAuth(AUTH_HASH)
71 )
72 )
73 ));
74 }
75
76 @SuppressWarnings("unchecked")
77 @Test
78 public void digestAuthProxyRequestRespondsToChallenge() throws Exception
79 {
80 http = makeHttp(digestAuthProxyConfig());
81 server.respondIf(
82 requestHasNoProxyAuth(),
83 proxyAuthChallenge(DIGEST_AUTH_CHALLENGE)
84 );
85 server.respondIf(
86 requestHasProxyAuth(),
87 status(200).body("ok")
88 );
89 resp = http.get(ABSOLUTE_TEST_URI);
90 assertThat(server, requests().is(Matchers.<RequestProperties>contains(
91 requestHasNoProxyAuth(),
92 requestHasProxyDigestAuth()
93 )));
94 }
95
96 @SuppressWarnings("unchecked")
97 @Test
98 public void multiAuthProxyRequestRespondsToChallenge() throws Exception
99 {
100 http = makeHttp(digestAuthProxyConfig());
101 server.respondIf(
102 requestHasNoProxyAuth(),
103 status(407)
104 .header("Proxy-Authenticate", "Negotiate")
105 .header("Proxy-Authenticate", DIGEST_AUTH_CHALLENGE)
106 .header("Proxy-Authenticate", BASIC_AUTH_CHALLENGE)
107 );
108 server.respondIf(
109 requestHasProxyAuth(),
110 status(200).body("ok")
111 );
112 resp = http.get(ABSOLUTE_TEST_URI);
113 assertThat(server, requests().is(Matchers.<RequestProperties>contains(
114 requestHasNoProxyAuth(),
115 requestHasProxyDigestAuth()
116 )));
117 }
118
119 private CommonsHttpTransport makeHttp(ProxyConfiguration.Builder builder)
120 {
121 return new CommonsHttpTransport(HttpConfiguration.builder()
122 .proxyConfiguration(some(builder.build())).build(), IMAGINARY_UNDERLYING_SERVER_URI);
123 }
124
125 private ProxyConfiguration.Builder baseProxyConfig()
126 {
127 return ProxyConfiguration.builder()
128 .proxyHost(some(new ProxyHost("localhost", serverPort)));
129 }
130
131 private ProxyConfiguration.Builder basicAuthProxyConfig()
132 {
133 ProxyAuthParams proxyAuth =
134 new ProxyAuthParams(new Credentials(AUTH_USERNAME, AUTH_PASSWORD), ProxyAuthMethod.BASIC);
135
136 return baseProxyConfig().authParams(some(proxyAuth));
137 }
138
139 private ProxyConfiguration.Builder digestAuthProxyConfig()
140 {
141 ProxyAuthParams proxyAuth =
142 new ProxyAuthParams(new Credentials(AUTH_USERNAME, AUTH_PASSWORD), ProxyAuthMethod.DIGEST);
143
144 return baseProxyConfig().authParams(some(proxyAuth));
145 }
146 }