Search in sources :

Example 11 with RecordingOkAuthenticator

use of okhttp3.internal.RecordingOkAuthenticator in project okhttp by square.

the class CallTest method proxyAuthenticateOnConnect.

/** Respond to a proxy authorization challenge. */
@Test
public void proxyAuthenticateOnConnect() throws Exception {
    server.useHttps(sslClient.socketFactory, true);
    server.enqueue(new MockResponse().setResponseCode(407).addHeader("Proxy-Authenticate: Basic realm=\"localhost\""));
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END).clearHeaders());
    server.enqueue(new MockResponse().setBody("response body"));
    client = client.newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).proxy(server.toProxyAddress()).proxyAuthenticator(new RecordingOkAuthenticator("password")).hostnameVerifier(new RecordingHostnameVerifier()).build();
    Request request = new Request.Builder().url("https://android.com/foo").build();
    Response response = client.newCall(request).execute();
    assertEquals("response body", response.body().string());
    RecordedRequest connect1 = server.takeRequest();
    assertEquals("CONNECT android.com:443 HTTP/1.1", connect1.getRequestLine());
    assertNull(connect1.getHeader("Proxy-Authorization"));
    RecordedRequest connect2 = server.takeRequest();
    assertEquals("CONNECT android.com:443 HTTP/1.1", connect2.getRequestLine());
    assertEquals("password", connect2.getHeader("Proxy-Authorization"));
    RecordedRequest get = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", get.getRequestLine());
    assertNull(get.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 12 with RecordingOkAuthenticator

use of okhttp3.internal.RecordingOkAuthenticator in project okhttp by square.

the class CallTest method postBodyRetransmittedAfterAuthorizationFail.

private void postBodyRetransmittedAfterAuthorizationFail(String body) throws Exception {
    server.enqueue(new MockResponse().setResponseCode(401));
    server.enqueue(new MockResponse());
    Request request = new Request.Builder().url(server.url("/")).method("POST", RequestBody.create(null, body)).build();
    String credential = Credentials.basic("jesse", "secret");
    client = client.newBuilder().authenticator(new RecordingOkAuthenticator(credential)).build();
    Response response = client.newCall(request).execute();
    assertEquals(200, response.code());
    response.body().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"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) RecordingOkAuthenticator(okhttp3.internal.RecordingOkAuthenticator) RecordedRequest(okhttp3.mockwebserver.RecordedRequest)

Example 13 with RecordingOkAuthenticator

use of okhttp3.internal.RecordingOkAuthenticator in project okhttp by square.

the class CallTest 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", "secret");
    client = client.newBuilder().authenticator(new RecordingOkAuthenticator(credential)).build();
    try {
        client.newCall(new Request.Builder().url(server.url("/0")).build()).execute();
        fail();
    } catch (IOException expected) {
        assertEquals("Too many follow-up requests: 21", expected.getMessage());
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordingOkAuthenticator(okhttp3.internal.RecordingOkAuthenticator) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) Test(org.junit.Test)

Example 14 with RecordingOkAuthenticator

use of okhttp3.internal.RecordingOkAuthenticator 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 15 with RecordingOkAuthenticator

use of okhttp3.internal.RecordingOkAuthenticator 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)

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