use of okhttp3.Authenticator in project okhttp by square.
the class URLConnectionTest method authCallsForHeader.
private List<String> authCallsForHeader(String authHeader) throws IOException {
boolean proxy = authHeader.startsWith("Proxy-");
int responseCode = proxy ? 407 : 401;
RecordingAuthenticator authenticator = new RecordingAuthenticator(null);
Authenticator.setDefault(authenticator);
MockResponse pleaseAuthenticate = new MockResponse().setResponseCode(responseCode).addHeader(authHeader).setBody("Please authenticate.");
server.enqueue(pleaseAuthenticate);
if (proxy) {
urlFactory.setClient(urlFactory.client().newBuilder().proxy(server.toProxyAddress()).proxyAuthenticator(new JavaNetAuthenticator()).build());
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());
connection.getErrorStream().close();
return authenticator.calls;
}
use of okhttp3.Authenticator in project okhttp by square.
the class URLConnectionTest method customBasicAuthenticator.
@Test
public void customBasicAuthenticator() throws Exception {
MockResponse pleaseAuthenticate = new MockResponse().setResponseCode(401).addHeader("WWW-Authenticate: Basic realm=\"protected area\"").setBody("Please authenticate.");
server.enqueue(pleaseAuthenticate);
server.enqueue(new MockResponse().setBody("A"));
String credential = Credentials.basic("jesse", "peanutbutter");
RecordingOkAuthenticator authenticator = new RecordingOkAuthenticator(credential);
urlFactory.setClient(urlFactory.client().newBuilder().authenticator(authenticator).build());
assertContent("A", urlFactory.open(server.url("/private").url()));
assertNull(server.takeRequest().getHeader("Authorization"));
assertEquals(credential, server.takeRequest().getHeader("Authorization"));
assertEquals(Proxy.NO_PROXY, authenticator.onlyProxy());
Response response = authenticator.onlyResponse();
assertEquals("/private", response.request().url().url().getPath());
assertEquals(Arrays.asList(new Challenge("Basic", "protected area")), response.challenges());
}
use of okhttp3.Authenticator in project okhttp by square.
the class URLConnectionTest method postBodyRetransmittedAfterAuthorizationFail.
private void postBodyRetransmittedAfterAuthorizationFail(String body) throws Exception {
server.enqueue(new MockResponse().setResponseCode(401));
server.enqueue(new MockResponse());
String credential = Credentials.basic("jesse", "secret");
urlFactory.setClient(urlFactory.client().newBuilder().authenticator(new RecordingOkAuthenticator(credential)).build());
connection = urlFactory.open(server.url("/").url());
connection.setDoOutput(true);
OutputStream outputStream = connection.getOutputStream();
outputStream.write(body.getBytes("UTF-8"));
outputStream.close();
assertEquals(200, connection.getResponseCode());
connection.getInputStream().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.Authenticator in project okhttp by square.
the class URLConnectionTest method redirectWithAuthentication.
@Test
public void redirectWithAuthentication() 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").url()));
urlFactory.setClient(urlFactory.client().newBuilder().authenticator(new RecordingOkAuthenticator(Credentials.basic("jesse", "secret"))).build());
assertContent("Page 2", urlFactory.open(server.url("/a").url()));
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 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());
}
Aggregations