Search in sources :

Example 1 with JavaNetAuthenticator

use of okhttp3.JavaNetAuthenticator in project okhttp by square.

the class URLConnectionTest method authenticateWithGetAndTransparentGzip.

/** https://code.google.com/p/android/issues/detail?id=74026 */
@Test
public void authenticateWithGetAndTransparentGzip() 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
    MockResponse successfulResponse = new MockResponse().addHeader("Content-Encoding", "gzip").setBody(gzip("Successful auth!"));
    server.enqueue(successfulResponse);
    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)

Example 2 with JavaNetAuthenticator

use of okhttp3.JavaNetAuthenticator in project okhttp by square.

the class URLConnectionTest method testAuthenticateWithStreamingPost.

private void testAuthenticateWithStreamingPost(StreamingMode streamingMode) throws Exception {
    MockResponse pleaseAuthenticate = new MockResponse().setResponseCode(401).addHeader("WWW-Authenticate: Basic realm=\"protected area\"").setBody("Please authenticate.");
    server.enqueue(pleaseAuthenticate);
    Authenticator.setDefault(new RecordingAuthenticator());
    urlFactory.setClient(urlFactory.client().newBuilder().authenticator(new JavaNetAuthenticator()).build());
    connection = urlFactory.open(server.url("/").url());
    connection.setDoOutput(true);
    byte[] requestBody = { 'A', 'B', 'C', 'D' };
    if (streamingMode == StreamingMode.FIXED_LENGTH) {
        connection.setFixedLengthStreamingMode(requestBody.length);
    } else if (streamingMode == StreamingMode.CHUNKED) {
        connection.setChunkedStreamingMode(0);
    }
    OutputStream outputStream = connection.getOutputStream();
    outputStream.write(requestBody);
    outputStream.close();
    try {
        connection.getInputStream();
        fail();
    } catch (HttpRetryException expected) {
    }
    // no authorization header for the request...
    RecordedRequest request = server.takeRequest();
    assertNull(request.getHeader("Authorization"));
    assertEquals("ABCD", request.getBody().readUtf8());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) OutputStream(java.io.OutputStream) HttpRetryException(java.net.HttpRetryException) RecordingAuthenticator(okhttp3.internal.RecordingAuthenticator)

Example 3 with JavaNetAuthenticator

use of okhttp3.JavaNetAuthenticator in project okhttp by square.

the class URLConnectionTest method authCallsForHeader.

private List<String> authCallsForHeader(String authHeader) throws IOException {
    boolean proxy = authHeader.startsWith("Proxy-");
    int responseCode = proxy ? 407 : 401;
    RecordingAuthenticator authenticator = new RecordingAuthenticator(null);
    Authenticator.setDefault(authenticator);
    MockResponse pleaseAuthenticate = new MockResponse().setResponseCode(responseCode).addHeader(authHeader).setBody("Please authenticate.");
    server.enqueue(pleaseAuthenticate);
    if (proxy) {
        urlFactory.setClient(urlFactory.client().newBuilder().proxy(server.toProxyAddress()).proxyAuthenticator(new JavaNetAuthenticator()).build());
        connection = urlFactory.open(new URL("http://android.com/"));
    } else {
        urlFactory.setClient(urlFactory.client().newBuilder().authenticator(new JavaNetAuthenticator()).build());
        connection = urlFactory.open(server.url("/").url());
    }
    assertEquals(responseCode, connection.getResponseCode());
    connection.getErrorStream().close();
    return authenticator.calls;
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordingAuthenticator(okhttp3.internal.RecordingAuthenticator) URL(java.net.URL)

Example 4 with JavaNetAuthenticator

use of okhttp3.JavaNetAuthenticator in project okhttp by square.

the class URLConnectionTest method proxyAuthenticateOnConnect.

@Test
public void proxyAuthenticateOnConnect() throws Exception {
    Authenticator.setDefault(new RecordingAuthenticator());
    server.useHttps(sslClient.socketFactory, true);
    server.enqueue(new MockResponse().setResponseCode(407).addHeader("Proxy-Authenticate: Basic realm=\"localhost\""));
    server.enqueue(new MockResponse().setSocketPolicy(UPGRADE_TO_SSL_AT_END).clearHeaders());
    server.enqueue(new MockResponse().setBody("A"));
    urlFactory.setClient(urlFactory.client().newBuilder().proxyAuthenticator(new JavaNetAuthenticator()).proxy(server.toProxyAddress()).sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(new RecordingHostnameVerifier()).build());
    URL url = new URL("https://android.com/foo");
    connection = urlFactory.open(url);
    assertContent("A", connection);
    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("Basic " + RecordingAuthenticator.BASE_64_CREDENTIALS, connect2.getHeader("Proxy-Authorization"));
    RecordedRequest get = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", get.getRequestLine());
    assertNull(get.getHeader("Proxy-Authorization"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) RecordingAuthenticator(okhttp3.internal.RecordingAuthenticator) URL(java.net.URL) Test(org.junit.Test)

Example 5 with JavaNetAuthenticator

use of okhttp3.JavaNetAuthenticator 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

RecordingAuthenticator (okhttp3.internal.RecordingAuthenticator)7 MockResponse (okhttp3.mockwebserver.MockResponse)7 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)5 Test (org.junit.Test)5 OutputStream (java.io.OutputStream)2 URL (java.net.URL)2 ApplicationTokenCredentials (com.microsoft.azure.credentials.ApplicationTokenCredentials)1 AzureTokenCredentials (com.microsoft.azure.credentials.AzureTokenCredentials)1 HttpRetryException (java.net.HttpRetryException)1 JavaNetAuthenticator (okhttp3.JavaNetAuthenticator)1