use of okhttp3.internal.RecordingOkAuthenticator in project okhttp by square.
the class CallTest method proxyAuthenticateOnConnect.
/** Respond to a proxy authorization challenge. */
@Test
public void proxyAuthenticateOnConnect() throws Exception {
server.useHttps(sslClient.socketFactory, true);
server.enqueue(new MockResponse().setResponseCode(407).addHeader("Proxy-Authenticate: Basic realm=\"localhost\""));
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());
RecordedRequest connect1 = server.takeRequest();
assertEquals("CONNECT android.com:443 HTTP/1.1", connect1.getRequestLine());
assertNull(connect1.getHeader("Proxy-Authorization"));
RecordedRequest connect2 = server.takeRequest();
assertEquals("CONNECT android.com:443 HTTP/1.1", connect2.getRequestLine());
assertEquals("password", connect2.getHeader("Proxy-Authorization"));
RecordedRequest get = server.takeRequest();
assertEquals("GET /foo HTTP/1.1", get.getRequestLine());
assertNull(get.getHeader("Proxy-Authorization"));
}
use of okhttp3.internal.RecordingOkAuthenticator in project okhttp by square.
the class CallTest method postBodyRetransmittedAfterAuthorizationFail.
private void postBodyRetransmittedAfterAuthorizationFail(String body) throws Exception {
server.enqueue(new MockResponse().setResponseCode(401));
server.enqueue(new MockResponse());
Request request = new Request.Builder().url(server.url("/")).method("POST", RequestBody.create(null, body)).build();
String credential = Credentials.basic("jesse", "secret");
client = client.newBuilder().authenticator(new RecordingOkAuthenticator(credential)).build();
Response response = client.newCall(request).execute();
assertEquals(200, response.code());
response.body().close();
RecordedRequest recordedRequest1 = server.takeRequest();
assertEquals("POST", recordedRequest1.getMethod());
assertEquals(body, recordedRequest1.getBody().readUtf8());
assertNull(recordedRequest1.getHeader("Authorization"));
RecordedRequest recordedRequest2 = server.takeRequest();
assertEquals("POST", recordedRequest2.getMethod());
assertEquals(body, recordedRequest2.getBody().readUtf8());
assertEquals(credential, recordedRequest2.getHeader("Authorization"));
}
use of okhttp3.internal.RecordingOkAuthenticator in project okhttp by square.
the class CallTest method doesNotAttemptAuthorization21Times.
@Test
public void doesNotAttemptAuthorization21Times() throws Exception {
for (int i = 0; i < 21; i++) {
server.enqueue(new MockResponse().setResponseCode(401));
}
String credential = Credentials.basic("jesse", "secret");
client = client.newBuilder().authenticator(new RecordingOkAuthenticator(credential)).build();
try {
client.newCall(new Request.Builder().url(server.url("/0")).build()).execute();
fail();
} catch (IOException expected) {
assertEquals("Too many follow-up requests: 21", expected.getMessage());
}
}
use of okhttp3.internal.RecordingOkAuthenticator in project okhttp by square.
the class CallTest method gzipResponseAfterAuthenticationChallenge.
/** https://github.com/square/okhttp/issues/1927 */
@Test
public void gzipResponseAfterAuthenticationChallenge() throws Exception {
server.enqueue(new MockResponse().setResponseCode(401));
server.enqueue(new MockResponse().setBody(gzip("abcabcabc")).addHeader("Content-Encoding: gzip"));
client = client.newBuilder().authenticator(new RecordingOkAuthenticator("password")).build();
executeSynchronously("/").assertBody("abcabcabc");
}
use of okhttp3.internal.RecordingOkAuthenticator in project okhttp by square.
the class CallTest method httpProxyAuthenticate.
/** 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"));
}
Aggregations