Search in sources :

Example 6 with RecordingOkAuthenticator

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

the class URLConnectionTest method authenticateCallsTrackedAsRedirects.

@Test
public void authenticateCallsTrackedAsRedirects() throws Exception {
    server.enqueue(new MockResponse().setResponseCode(302).addHeader("Location: /b"));
    server.enqueue(new MockResponse().setResponseCode(401).addHeader("WWW-Authenticate: Basic realm=\"protected area\""));
    server.enqueue(new MockResponse().setBody("c"));
    RecordingOkAuthenticator authenticator = new RecordingOkAuthenticator(Credentials.basic("jesse", "peanutbutter"));
    urlFactory.setClient(urlFactory.client().newBuilder().authenticator(authenticator).build());
    assertContent("c", urlFactory.open(server.url("/a").url()));
    Response challengeResponse = authenticator.responses.get(0);
    assertEquals("/b", challengeResponse.request().url().url().getPath());
    Response redirectedBy = challengeResponse.priorResponse();
    assertEquals("/a", redirectedBy.request().url().url().getPath());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) RecordingOkAuthenticator(okhttp3.internal.RecordingOkAuthenticator) Test(org.junit.Test)

Example 7 with RecordingOkAuthenticator

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

the class URLConnectionTest method attemptAuthorization20Times.

@Test
public void attemptAuthorization20Times() throws Exception {
    for (int i = 0; i < 20; i++) {
        server.enqueue(new MockResponse().setResponseCode(401));
    }
    server.enqueue(new MockResponse().setBody("Success!"));
    String credential = Credentials.basic("jesse", "peanutbutter");
    urlFactory.setClient(urlFactory.client().newBuilder().authenticator(new RecordingOkAuthenticator(credential)).build());
    connection = urlFactory.open(server.url("/0").url());
    assertContent("Success!", connection);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordingOkAuthenticator(okhttp3.internal.RecordingOkAuthenticator) Test(org.junit.Test)

Example 8 with RecordingOkAuthenticator

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

the class CallTest method attemptAuthorization20Times.

@Test
public void attemptAuthorization20Times() throws Exception {
    for (int i = 0; i < 20; i++) {
        server.enqueue(new MockResponse().setResponseCode(401));
    }
    server.enqueue(new MockResponse().setBody("Success!"));
    String credential = Credentials.basic("jesse", "secret");
    client = client.newBuilder().authenticator(new RecordingOkAuthenticator(credential)).build();
    executeSynchronously("/").assertCode(200).assertBody("Success!");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordingOkAuthenticator(okhttp3.internal.RecordingOkAuthenticator) Test(org.junit.Test)

Example 9 with RecordingOkAuthenticator

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

the class CallTest method tooManyProxyAuthFailuresWithConnectionClose.

@Test
public void tooManyProxyAuthFailuresWithConnectionClose() throws IOException {
    server.useHttps(sslClient.socketFactory, true);
    server.setProtocols(Collections.singletonList(Protocol.HTTP_1_1));
    for (int i = 0; i < 21; i++) {
        server.enqueue(new MockResponse().setResponseCode(407).addHeader("Proxy-Authenticate: Basic realm=\"localhost\"").addHeader("Connection: close"));
    }
    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();
    try {
        client.newCall(request).execute();
        fail();
    } catch (ProtocolException expected) {
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) SSLProtocolException(javax.net.ssl.SSLProtocolException) ProtocolException(java.net.ProtocolException) RecordingOkAuthenticator(okhttp3.internal.RecordingOkAuthenticator) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 10 with RecordingOkAuthenticator

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

the class CallTest method proxyAuthenticateOnConnectWithConnectionClose.

/**
   * OkHttp has a bug where a `Connection: close` response header is not honored when establishing a
   * TLS tunnel. https://github.com/square/okhttp/issues/2426
   */
@Test
public void proxyAuthenticateOnConnectWithConnectionClose() throws Exception {
    server.useHttps(sslClient.socketFactory, true);
    server.setProtocols(Collections.singletonList(Protocol.HTTP_1_1));
    server.enqueue(new MockResponse().setResponseCode(407).addHeader("Proxy-Authenticate: Basic realm=\"localhost\"").addHeader("Connection: close"));
    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());
    // First CONNECT call needs a new connection.
    assertEquals(0, server.takeRequest().getSequenceNumber());
    // Second CONNECT call needs a new connection.
    assertEquals(0, server.takeRequest().getSequenceNumber());
    // GET reuses the connection from the second connect.
    assertEquals(1, server.takeRequest().getSequenceNumber());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) 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