use of okhttp3.internal.RecordingOkAuthenticator 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.internal.RecordingOkAuthenticator in project okhttp by square.
the class HttpOverHttp2Test method authenticate.
@Test
public void authenticate() throws Exception {
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_UNAUTHORIZED).addHeader("www-authenticate: Basic realm=\"protected area\"").setBody("Please authenticate."));
server.enqueue(new MockResponse().setBody("Successful auth!"));
String credential = Credentials.basic("username", "password");
client = client.newBuilder().authenticator(new RecordingOkAuthenticator(credential)).build();
Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
Response response = call.execute();
assertEquals("Successful auth!", response.body().string());
RecordedRequest denied = server.takeRequest();
assertNull(denied.getHeader("Authorization"));
RecordedRequest accepted = server.takeRequest();
assertEquals("GET / HTTP/1.1", accepted.getRequestLine());
assertEquals(credential, accepted.getHeader("Authorization"));
}
Aggregations