private static final String NONE = "NONE";
private static final String P11KEYSTORE = "PKCS11";
+ private static final String PROXY_AUTHORIZATION = "Proxy-Authorization";
private String userAgent;
private Proxy proxy;
builder.addNetworkInterceptor(this::addUserAgent);
if (proxyLogin != null) {
builder.proxyAuthenticator((route, response) -> {
+ if (response.request().header(PROXY_AUTHORIZATION) != null) {
+ // Give up, we've already attempted to authenticate.
+ return null;
+ }
if (HttpURLConnection.HTTP_PROXY_AUTH == response.code()) {
String credential = Credentials.basic(proxyLogin, nullToEmpty(proxyPassword));
- return response.request().newBuilder().header("Proxy-Authorization", credential).build();
+ return response.request().newBuilder().header(PROXY_AUTHORIZATION, credential).build();
}
return null;
});
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
+import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
serverUrl = server.url("").url().toString();
}
+ @After
+ public void stop() throws Exception {
+ server.close();
+ }
+
@Test
public void test_default_settings() throws Exception {
answerHelloWorld();
@Test
public void use_proxy_authentication() throws Exception {
- MockWebServer proxy = new MockWebServer();
- proxy.start();
- underTest = HttpConnector.newBuilder()
- .url(serverUrl)
- .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy.getHostName(), proxy.getPort())))
- .proxyCredentials("theProxyLogin", "theProxyPassword")
- .build();
-
- GetRequest request = new GetRequest("api/issues/search");
- proxy.enqueue(new MockResponse().setResponseCode(407));
- proxy.enqueue(new MockResponse().setBody("OK!"));
- underTest.call(request);
+ try (MockWebServer proxy = new MockWebServer()) {
+ proxy.start();
+
+ underTest = HttpConnector.newBuilder()
+ .url(serverUrl)
+ .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy.getHostName(), proxy.getPort())))
+ .proxyCredentials("theProxyLogin", "theProxyPassword")
+ .build();
+
+ GetRequest request = new GetRequest("api/issues/search");
+ proxy.enqueue(new MockResponse().setResponseCode(407));
+ proxy.enqueue(new MockResponse().setBody("OK!"));
+ underTest.call(request);
+
+ RecordedRequest recordedRequest = proxy.takeRequest();
+ assertThat(recordedRequest.getHeader("Proxy-Authorization")).isNull();
+ recordedRequest = proxy.takeRequest();
+ assertThat(recordedRequest.getHeader("Proxy-Authorization")).isEqualTo(basic("theProxyLogin", "theProxyPassword"));
+ }
+ }
- RecordedRequest recordedRequest = proxy.takeRequest();
- assertThat(recordedRequest.getHeader("Proxy-Authorization")).isNull();
- recordedRequest = proxy.takeRequest();
- assertThat(recordedRequest.getHeader("Proxy-Authorization")).isEqualTo(basic("theProxyLogin", "theProxyPassword"));
+ @Test
+ public void use_proxy_authentication_wrong_crendentials() throws Exception {
+ try (MockWebServer proxy = new MockWebServer()) {
+ proxy.start();
+
+ underTest = HttpConnector.newBuilder()
+ .url(serverUrl)
+ .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy.getHostName(), proxy.getPort())))
+ .proxyCredentials("theProxyLogin", "wrongPassword")
+ .build();
+
+ GetRequest request = new GetRequest("api/issues/search");
+ proxy.enqueue(new MockResponse().setResponseCode(407));
+ proxy.enqueue(new MockResponse().setResponseCode(407));
+ proxy.enqueue(new MockResponse().setResponseCode(407));
+ underTest.call(request);
+
+ RecordedRequest recordedRequest = proxy.takeRequest();
+ assertThat(recordedRequest.getHeader("Proxy-Authorization")).isNull();
+ recordedRequest = proxy.takeRequest();
+ assertThat(recordedRequest.getHeader("Proxy-Authorization")).isEqualTo(basic("theProxyLogin", "wrongPassword"));
+ assertThat(proxy.getRequestCount()).isEqualTo(2);
+ }
}
@Test