use of okhttp3.Authenticator 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.Authenticator 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"));
}
use of okhttp3.Authenticator in project okhttp by square.
the class CallTest method redirectsDoNotIncludeTooManyAuthHeaders.
@Test
public void redirectsDoNotIncludeTooManyAuthHeaders() throws Exception {
server2.enqueue(new MockResponse().setBody("Page 2"));
server.enqueue(new MockResponse().setResponseCode(401));
server.enqueue(new MockResponse().setResponseCode(302).addHeader("Location: " + server2.url("/b")));
client = client.newBuilder().authenticator(new RecordingOkAuthenticator(Credentials.basic("jesse", "secret"))).build();
Request request = new Request.Builder().url(server.url("/a")).build();
Response response = client.newCall(request).execute();
assertEquals("Page 2", response.body().string());
RecordedRequest redirectRequest = server2.takeRequest();
assertNull(redirectRequest.getHeader("Authorization"));
assertEquals("/b", redirectRequest.getPath());
}
use of okhttp3.Authenticator in project okhttp by square.
the class URLConnectionTest method authenticateWithGet.
@Test
public void authenticateWithGet() throws Exception {
MockResponse pleaseAuthenticate = new MockResponse().setResponseCode(401).addHeader("WWW-Authenticate: Basic realm=\"protected area\"").setBody("Please authenticate.");
// fail auth three times...
server.enqueue(pleaseAuthenticate);
server.enqueue(pleaseAuthenticate);
server.enqueue(pleaseAuthenticate);
// ...then succeed the fourth time
server.enqueue(new MockResponse().setBody("Successful auth!"));
Authenticator.setDefault(new RecordingAuthenticator());
urlFactory.setClient(urlFactory.client().newBuilder().authenticator(new JavaNetAuthenticator()).build());
connection = urlFactory.open(server.url("/").url());
assertEquals("Successful auth!", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
// no authorization header for the first request...
RecordedRequest request = server.takeRequest();
assertNull(request.getHeader("Authorization"));
// ...but the three requests that follow requests include an authorization header
for (int i = 0; i < 3; i++) {
request = server.takeRequest();
assertEquals("GET / HTTP/1.1", request.getRequestLine());
assertEquals("Basic " + RecordingAuthenticator.BASE_64_CREDENTIALS, request.getHeader("Authorization"));
}
}
use of okhttp3.Authenticator in project okhttp by square.
the class URLConnectionTest method authenticateWithPost.
@Test
public void authenticateWithPost() throws Exception {
MockResponse pleaseAuthenticate = new MockResponse().setResponseCode(401).addHeader("WWW-Authenticate: Basic realm=\"protected area\"").setBody("Please authenticate.");
// fail auth three times...
server.enqueue(pleaseAuthenticate);
server.enqueue(pleaseAuthenticate);
server.enqueue(pleaseAuthenticate);
// ...then succeed the fourth time
server.enqueue(new MockResponse().setBody("Successful auth!"));
Authenticator.setDefault(new RecordingAuthenticator());
urlFactory.setClient(urlFactory.client().newBuilder().authenticator(new JavaNetAuthenticator()).build());
connection = urlFactory.open(server.url("/").url());
connection.setDoOutput(true);
byte[] requestBody = { 'A', 'B', 'C', 'D' };
OutputStream outputStream = connection.getOutputStream();
outputStream.write(requestBody);
outputStream.close();
assertEquals("Successful auth!", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
// no authorization header for the first request...
RecordedRequest request = server.takeRequest();
assertNull(request.getHeader("Authorization"));
// ...but the three requests that follow include an authorization header
for (int i = 0; i < 3; i++) {
request = server.takeRequest();
assertEquals("POST / HTTP/1.1", request.getRequestLine());
assertEquals("Basic " + RecordingAuthenticator.BASE_64_CREDENTIALS, request.getHeader("Authorization"));
assertEquals("ABCD", request.getBody().readUtf8());
}
}
Aggregations