Search in sources :

Example 16 with RecordingOkAuthenticator

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());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) RecordingOkAuthenticator(okhttp3.internal.RecordingOkAuthenticator) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 17 with RecordingOkAuthenticator

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"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Response(okhttp3.Response) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Call(okhttp3.Call) RecordingOkAuthenticator(okhttp3.internal.RecordingOkAuthenticator) Request(okhttp3.Request) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Aggregations

RecordingOkAuthenticator (okhttp3.internal.RecordingOkAuthenticator)17 MockResponse (okhttp3.mockwebserver.MockResponse)17 Test (org.junit.Test)15 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)9 ProtocolException (java.net.ProtocolException)2 SSLProtocolException (javax.net.ssl.SSLProtocolException)2 IOException (java.io.IOException)1 InterruptedIOException (java.io.InterruptedIOException)1 OutputStream (java.io.OutputStream)1 Call (okhttp3.Call)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1