Search in sources :

Example 6 with JavaNetAuthenticator

use of okhttp3.JavaNetAuthenticator in project okhttp by square.

the class URLConnectionTest method authenticateWithPost.

@Test
public void authenticateWithPost() 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());
    connection.setDoOutput(true);
    byte[] requestBody = { 'A', 'B', 'C', 'D' };
    OutputStream outputStream = connection.getOutputStream();
    outputStream.write(requestBody);
    outputStream.close();
    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 include an authorization header
    for (int i = 0; i < 3; i++) {
        request = server.takeRequest();
        assertEquals("POST / HTTP/1.1", request.getRequestLine());
        assertEquals("Basic " + RecordingAuthenticator.BASE_64_CREDENTIALS, request.getHeader("Authorization"));
        assertEquals("ABCD", request.getBody().readUtf8());
    }
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) OutputStream(java.io.OutputStream) RecordingAuthenticator(okhttp3.internal.RecordingAuthenticator) Test(org.junit.Test)

Example 7 with JavaNetAuthenticator

use of okhttp3.JavaNetAuthenticator in project okhttp by square.

the class URLConnectionTest method authenticateRealmUppercase.

/** https://github.com/square/okhttp/issues/342 */
@Test
public void authenticateRealmUppercase() throws Exception {
    server.enqueue(new MockResponse().setResponseCode(401).addHeader("wWw-aUtHeNtIcAtE: bAsIc rEaLm=\"pRoTeCtEd aReA\"").setBody("Please authenticate."));
    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));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordingAuthenticator(okhttp3.internal.RecordingAuthenticator) Test(org.junit.Test)

Example 8 with JavaNetAuthenticator

use of okhttp3.JavaNetAuthenticator in project cloudbreak by hortonworks.

the class AzureClient method connect.

private void connect() {
    AzureTokenCredentials creds = new ApplicationTokenCredentials(clientId, tenantId, secretKey, AzureEnvironment.AZURE).withDefaultSubscriptionId(subscriptionId);
    azure = Azure.configure().withProxyAuthenticator(new JavaNetAuthenticator()).withLogLevel(LogLevel.BASIC).authenticate(creds).withSubscription(subscriptionId);
}
Also used : ApplicationTokenCredentials(com.microsoft.azure.credentials.ApplicationTokenCredentials) JavaNetAuthenticator(okhttp3.JavaNetAuthenticator) AzureTokenCredentials(com.microsoft.azure.credentials.AzureTokenCredentials)

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