Search in sources :

Example 6 with Authenticator

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

use of okhttp3.Authenticator in project okhttp by square.

the class URLConnectionTest method customBasicAuthenticator.

@Test
public void customBasicAuthenticator() throws Exception {
    MockResponse pleaseAuthenticate = new MockResponse().setResponseCode(401).addHeader("WWW-Authenticate: Basic realm=\"protected area\"").setBody("Please authenticate.");
    server.enqueue(pleaseAuthenticate);
    server.enqueue(new MockResponse().setBody("A"));
    String credential = Credentials.basic("jesse", "peanutbutter");
    RecordingOkAuthenticator authenticator = new RecordingOkAuthenticator(credential);
    urlFactory.setClient(urlFactory.client().newBuilder().authenticator(authenticator).build());
    assertContent("A", urlFactory.open(server.url("/private").url()));
    assertNull(server.takeRequest().getHeader("Authorization"));
    assertEquals(credential, server.takeRequest().getHeader("Authorization"));
    assertEquals(Proxy.NO_PROXY, authenticator.onlyProxy());
    Response response = authenticator.onlyResponse();
    assertEquals("/private", response.request().url().url().getPath());
    assertEquals(Arrays.asList(new Challenge("Basic", "protected area")), response.challenges());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) RecordingOkAuthenticator(okhttp3.internal.RecordingOkAuthenticator) Test(org.junit.Test)

Example 8 with Authenticator

use of okhttp3.Authenticator in project okhttp by square.

the class URLConnectionTest method postBodyRetransmittedAfterAuthorizationFail.

private void postBodyRetransmittedAfterAuthorizationFail(String body) throws Exception {
    server.enqueue(new MockResponse().setResponseCode(401));
    server.enqueue(new MockResponse());
    String credential = Credentials.basic("jesse", "secret");
    urlFactory.setClient(urlFactory.client().newBuilder().authenticator(new RecordingOkAuthenticator(credential)).build());
    connection = urlFactory.open(server.url("/").url());
    connection.setDoOutput(true);
    OutputStream outputStream = connection.getOutputStream();
    outputStream.write(body.getBytes("UTF-8"));
    outputStream.close();
    assertEquals(200, connection.getResponseCode());
    connection.getInputStream().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 : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) RecordingOkAuthenticator(okhttp3.internal.RecordingOkAuthenticator) OutputStream(java.io.OutputStream)

Example 9 with Authenticator

use of okhttp3.Authenticator in project okhttp by square.

the class URLConnectionTest method redirectWithAuthentication.

@Test
public void redirectWithAuthentication() 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").url()));
    urlFactory.setClient(urlFactory.client().newBuilder().authenticator(new RecordingOkAuthenticator(Credentials.basic("jesse", "secret"))).build());
    assertContent("Page 2", urlFactory.open(server.url("/a").url()));
    RecordedRequest redirectRequest = server2.takeRequest();
    assertNull(redirectRequest.getHeader("Authorization"));
    assertEquals("/b", redirectRequest.getPath());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) RecordingOkAuthenticator(okhttp3.internal.RecordingOkAuthenticator) Test(org.junit.Test)

Example 10 with Authenticator

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

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