1
0
mirror of https://github.com/square/okhttp.git synced 2026-01-17 08:42:25 +03:00

Fix a regression in authenticating HTTP proxies.

We were using the regular authenticator, not the proxy authenticator.

Closes: https://github.com/square/okhttp/issues/2414
This commit is contained in:
jwilson
2016-03-19 16:50:22 -04:00
parent b6cfe4cd10
commit 8e6aeb4ebe
3 changed files with 35 additions and 5 deletions

View File

@@ -2197,6 +2197,34 @@ public final class CallTest {
assertNull(get.getHeader("Proxy-Authorization"));
}
/** Confirm that the proxy authenticator works for unencrypted HTTP proxies. */
@Test public void httpProxyAuthenticate() throws Exception {
server.enqueue(new MockResponse()
.setResponseCode(407)
.addHeader("Proxy-Authenticate: Basic realm=\"localhost\""));
server.enqueue(new MockResponse()
.setBody("response body"));
client = client.newBuilder()
.proxy(server.toProxyAddress())
.proxyAuthenticator(new RecordingOkAuthenticator("password"))
.build();
Request request = new Request.Builder()
.url("http://android.com/foo")
.build();
Response response = client.newCall(request).execute();
assertEquals("response body", response.body().string());
RecordedRequest get1 = server.takeRequest();
assertEquals("GET http://android.com/foo HTTP/1.1", get1.getRequestLine());
assertNull(get1.getHeader("Proxy-Authorization"));
RecordedRequest get2 = server.takeRequest();
assertEquals("GET http://android.com/foo HTTP/1.1", get2.getRequestLine());
assertEquals("password", get2.getHeader("Proxy-Authorization"));
}
/**
* Confirm that we don't send the Proxy-Authorization header from the request to the proxy server.
* We used to have that behavior but it is problematic because unrelated requests end up sharing

View File

@@ -1596,15 +1596,16 @@ public final class URLConnectionTest {
.setBody("Please authenticate.");
server.enqueue(pleaseAuthenticate);
urlFactory.setClient(urlFactory.client().newBuilder()
.authenticator(new JavaNetAuthenticator())
.build());
if (proxy) {
urlFactory.setClient(urlFactory.client().newBuilder()
.proxy(server.toProxyAddress())
.proxyAuthenticator(new JavaNetAuthenticator())
.build());
connection = urlFactory.open(new URL("http://android.com"));
connection = urlFactory.open(new URL("http://android.com/"));
} else {
urlFactory.setClient(urlFactory.client().newBuilder()
.authenticator(new JavaNetAuthenticator())
.build());
connection = urlFactory.open(server.url("/").url());
}
assertEquals(responseCode, connection.getResponseCode());

View File

@@ -891,7 +891,8 @@ public final class HttpEngine {
if (selectedProxy.type() != Proxy.Type.HTTP) {
throw new ProtocolException("Received HTTP_PROXY_AUTH (407) code while not using proxy");
}
// fall-through
return client.proxyAuthenticator().authenticate(route, userResponse);
case HTTP_UNAUTHORIZED:
return client.authenticator().authenticate(route, userResponse);