use of okhttp3.internal.RecordingOkAuthenticator in project okhttp by square.
the class URLConnectionTest method customTokenAuthenticator.
@Test
public void customTokenAuthenticator() throws Exception {
MockResponse pleaseAuthenticate = new MockResponse().setResponseCode(401).addHeader("WWW-Authenticate: Bearer realm=\"oauthed\"").setBody("Please authenticate.");
server.enqueue(pleaseAuthenticate);
server.enqueue(new MockResponse().setBody("A"));
RecordingOkAuthenticator authenticator = new RecordingOkAuthenticator("oauthed abc123");
urlFactory.setClient(urlFactory.client().newBuilder().authenticator(authenticator).build());
assertContent("A", urlFactory.open(server.url("/private").url()));
assertNull(server.takeRequest().getHeader("Authorization"));
assertEquals("oauthed abc123", server.takeRequest().getHeader("Authorization"));
Response response = authenticator.onlyResponse();
assertEquals("/private", response.request().url().url().getPath());
assertEquals(Arrays.asList(new Challenge("Bearer", "oauthed")), response.challenges());
}
use of okhttp3.internal.RecordingOkAuthenticator in project okhttp by square.
the class URLConnectionTest 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", "peanutbutter");
urlFactory.setClient(urlFactory.client().newBuilder().authenticator(new RecordingOkAuthenticator(credential)).build());
connection = urlFactory.open(server.url("/").url());
try {
connection.getInputStream();
fail();
} catch (ProtocolException expected) {
assertEquals(401, connection.getResponseCode());
assertEquals("Too many follow-up requests: 21", expected.getMessage());
}
}
use of okhttp3.internal.RecordingOkAuthenticator 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.internal.RecordingOkAuthenticator 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.internal.RecordingOkAuthenticator 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());
}
Aggregations