use of okhttp3.internal.RecordingOkAuthenticator in project okhttp by square.
the class URLConnectionTest method authenticateCallsTrackedAsRedirects.
@Test
public void authenticateCallsTrackedAsRedirects() throws Exception {
server.enqueue(new MockResponse().setResponseCode(302).addHeader("Location: /b"));
server.enqueue(new MockResponse().setResponseCode(401).addHeader("WWW-Authenticate: Basic realm=\"protected area\""));
server.enqueue(new MockResponse().setBody("c"));
RecordingOkAuthenticator authenticator = new RecordingOkAuthenticator(Credentials.basic("jesse", "peanutbutter"));
urlFactory.setClient(urlFactory.client().newBuilder().authenticator(authenticator).build());
assertContent("c", urlFactory.open(server.url("/a").url()));
Response challengeResponse = authenticator.responses.get(0);
assertEquals("/b", challengeResponse.request().url().url().getPath());
Response redirectedBy = challengeResponse.priorResponse();
assertEquals("/a", redirectedBy.request().url().url().getPath());
}
use of okhttp3.internal.RecordingOkAuthenticator in project okhttp by square.
the class URLConnectionTest method attemptAuthorization20Times.
@Test
public void attemptAuthorization20Times() throws Exception {
for (int i = 0; i < 20; i++) {
server.enqueue(new MockResponse().setResponseCode(401));
}
server.enqueue(new MockResponse().setBody("Success!"));
String credential = Credentials.basic("jesse", "peanutbutter");
urlFactory.setClient(urlFactory.client().newBuilder().authenticator(new RecordingOkAuthenticator(credential)).build());
connection = urlFactory.open(server.url("/0").url());
assertContent("Success!", connection);
}
use of okhttp3.internal.RecordingOkAuthenticator in project okhttp by square.
the class CallTest method attemptAuthorization20Times.
@Test
public void attemptAuthorization20Times() throws Exception {
for (int i = 0; i < 20; i++) {
server.enqueue(new MockResponse().setResponseCode(401));
}
server.enqueue(new MockResponse().setBody("Success!"));
String credential = Credentials.basic("jesse", "secret");
client = client.newBuilder().authenticator(new RecordingOkAuthenticator(credential)).build();
executeSynchronously("/").assertCode(200).assertBody("Success!");
}
use of okhttp3.internal.RecordingOkAuthenticator in project okhttp by square.
the class CallTest method tooManyProxyAuthFailuresWithConnectionClose.
@Test
public void tooManyProxyAuthFailuresWithConnectionClose() throws IOException {
server.useHttps(sslClient.socketFactory, true);
server.setProtocols(Collections.singletonList(Protocol.HTTP_1_1));
for (int i = 0; i < 21; i++) {
server.enqueue(new MockResponse().setResponseCode(407).addHeader("Proxy-Authenticate: Basic realm=\"localhost\"").addHeader("Connection: close"));
}
client = client.newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).proxy(server.toProxyAddress()).proxyAuthenticator(new RecordingOkAuthenticator("password")).hostnameVerifier(new RecordingHostnameVerifier()).build();
Request request = new Request.Builder().url("https://android.com/foo").build();
try {
client.newCall(request).execute();
fail();
} catch (ProtocolException expected) {
}
}
use of okhttp3.internal.RecordingOkAuthenticator in project okhttp by square.
the class CallTest method proxyAuthenticateOnConnectWithConnectionClose.
/**
* OkHttp has a bug where a `Connection: close` response header is not honored when establishing a
* TLS tunnel. https://github.com/square/okhttp/issues/2426
*/
@Test
public void proxyAuthenticateOnConnectWithConnectionClose() throws Exception {
server.useHttps(sslClient.socketFactory, true);
server.setProtocols(Collections.singletonList(Protocol.HTTP_1_1));
server.enqueue(new MockResponse().setResponseCode(407).addHeader("Proxy-Authenticate: Basic realm=\"localhost\"").addHeader("Connection: close"));
server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END).clearHeaders());
server.enqueue(new MockResponse().setBody("response body"));
client = client.newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).proxy(server.toProxyAddress()).proxyAuthenticator(new RecordingOkAuthenticator("password")).hostnameVerifier(new RecordingHostnameVerifier()).build();
Request request = new Request.Builder().url("https://android.com/foo").build();
Response response = client.newCall(request).execute();
assertEquals("response body", response.body().string());
// First CONNECT call needs a new connection.
assertEquals(0, server.takeRequest().getSequenceNumber());
// Second CONNECT call needs a new connection.
assertEquals(0, server.takeRequest().getSequenceNumber());
// GET reuses the connection from the second connect.
assertEquals(1, server.takeRequest().getSequenceNumber());
}
Aggregations