Search in sources :

Example 16 with Authenticator

use of okhttp3.Authenticator in project okhttp by square.

the class AddressTest method differentProxySelectorsAreDifferent.

@Test
public void differentProxySelectorsAreDifferent() throws Exception {
    Address a = new Address("square.com", 80, dns, socketFactory, null, null, null, authenticator, null, protocols, connectionSpecs, new RecordingProxySelector());
    Address b = new Address("square.com", 80, dns, socketFactory, null, null, null, authenticator, null, protocols, connectionSpecs, new RecordingProxySelector());
    assertFalse(a.equals(b));
}
Also used : RecordingProxySelector(okhttp3.internal.http.RecordingProxySelector) Test(org.junit.Test)

Example 17 with Authenticator

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

Example 18 with Authenticator

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"));
}
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 19 with Authenticator

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());
}
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 20 with Authenticator

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

Aggregations

MockResponse (okhttp3.mockwebserver.MockResponse)20 Test (org.junit.Test)20 RecordingOkAuthenticator (okhttp3.internal.RecordingOkAuthenticator)14 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)10 RecordingAuthenticator (okhttp3.internal.RecordingAuthenticator)6 IOException (java.io.IOException)4 OutputStream (java.io.OutputStream)3 InetAddress (java.net.InetAddress)3 InetSocketAddress (java.net.InetSocketAddress)3 SocketAddress (java.net.SocketAddress)3 Address (okhttp3.Address)3 Request (okhttp3.Request)3 Authenticator (okhttp3.Authenticator)2 Interceptor (okhttp3.Interceptor)2 Response (okhttp3.Response)2 Route (okhttp3.Route)2 RecordingProxySelector (okhttp3.internal.http.RecordingProxySelector)2 InterruptedIOException (java.io.InterruptedIOException)1 HttpRetryException (java.net.HttpRetryException)1 ProtocolException (java.net.ProtocolException)1