Search in sources :

Example 1 with RecordingOkAuthenticator

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

the class URLConnectionTest method customTokenAuthenticator.

@Test
public void customTokenAuthenticator() throws Exception {
    MockResponse pleaseAuthenticate = new MockResponse().setResponseCode(401).addHeader("WWW-Authenticate: Bearer realm=\"oauthed\"").setBody("Please authenticate.");
    server.enqueue(pleaseAuthenticate);
    server.enqueue(new MockResponse().setBody("A"));
    RecordingOkAuthenticator authenticator = new RecordingOkAuthenticator("oauthed abc123");
    urlFactory.setClient(urlFactory.client().newBuilder().authenticator(authenticator).build());
    assertContent("A", urlFactory.open(server.url("/private").url()));
    assertNull(server.takeRequest().getHeader("Authorization"));
    assertEquals("oauthed abc123", server.takeRequest().getHeader("Authorization"));
    Response response = authenticator.onlyResponse();
    assertEquals("/private", response.request().url().url().getPath());
    assertEquals(Arrays.asList(new Challenge("Bearer", "oauthed")), response.challenges());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) RecordingOkAuthenticator(okhttp3.internal.RecordingOkAuthenticator) Test(org.junit.Test)

Example 2 with RecordingOkAuthenticator

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

the class URLConnectionTest 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", "peanutbutter");
    urlFactory.setClient(urlFactory.client().newBuilder().authenticator(new RecordingOkAuthenticator(credential)).build());
    connection = urlFactory.open(server.url("/").url());
    try {
        connection.getInputStream();
        fail();
    } catch (ProtocolException expected) {
        assertEquals(401, connection.getResponseCode());
        assertEquals("Too many follow-up requests: 21", expected.getMessage());
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) SSLProtocolException(javax.net.ssl.SSLProtocolException) ProtocolException(java.net.ProtocolException) RecordingOkAuthenticator(okhttp3.internal.RecordingOkAuthenticator) Test(org.junit.Test)

Example 3 with RecordingOkAuthenticator

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

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

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

Aggregations

RecordingOkAuthenticator (okhttp3.internal.RecordingOkAuthenticator)19 MockResponse (okhttp3.mockwebserver.MockResponse)17 Test (org.junit.Test)15 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)9 IOException (java.io.IOException)2 ProtocolException (java.net.ProtocolException)2 SSLProtocolException (javax.net.ssl.SSLProtocolException)2 MockResponse (mockwebserver3.MockResponse)2 Test (org.junit.jupiter.api.Test)2 InterruptedIOException (java.io.InterruptedIOException)1 OutputStream (java.io.OutputStream)1 MockDuplexResponseBody (mockwebserver3.internal.duplex.MockDuplexResponseBody)1 Call (okhttp3.Call)1 ConnectEnd (okhttp3.CallEvent.ConnectEnd)1 SecureConnectEnd (okhttp3.CallEvent.SecureConnectEnd)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1 AsyncRequestBody (okhttp3.internal.duplex.AsyncRequestBody)1 BufferedSink (okio.BufferedSink)1 BufferedSource (okio.BufferedSource)1