Search in sources :

Example 21 with Authenticator

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

use of okhttp3.Authenticator in project okhttp by square.

the class RouteSelectorTest method proxySelectorReturnsNull.

@Test
public void proxySelectorReturnsNull() throws Exception {
    ProxySelector nullProxySelector = new ProxySelector() {

        @Override
        public List<Proxy> select(URI uri) {
            assertEquals(uriHost, uri.getHost());
            return null;
        }

        @Override
        public void connectFailed(URI uri, SocketAddress socketAddress, IOException e) {
            throw new AssertionError();
        }
    };
    Address address = new Address(uriHost, uriPort, dns, socketFactory, null, null, null, authenticator, null, protocols, connectionSpecs, nullProxySelector);
    RouteSelector routeSelector = new RouteSelector(address, routeDatabase);
    assertTrue(routeSelector.hasNext());
    dns.set(uriHost, dns.allocate(1));
    assertRoute(routeSelector.next(), address, NO_PROXY, dns.lookup(uriHost, 0), uriPort);
    dns.assertRequests(uriHost);
    assertFalse(routeSelector.hasNext());
}
Also used : ProxySelector(java.net.ProxySelector) RecordingProxySelector(okhttp3.internal.http.RecordingProxySelector) Proxy(java.net.Proxy) SocketAddress(java.net.SocketAddress) Address(okhttp3.Address) InetAddress(java.net.InetAddress) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) URI(java.net.URI) Test(org.junit.Test)

Example 23 with Authenticator

use of okhttp3.Authenticator in project okhttp by square.

the class RouteSelectorTest method explicitDirectProxy.

@Test
public void explicitDirectProxy() throws Exception {
    Address address = new Address(uriHost, uriPort, dns, socketFactory, null, null, null, authenticator, NO_PROXY, protocols, connectionSpecs, proxySelector);
    RouteSelector routeSelector = new RouteSelector(address, routeDatabase);
    assertTrue(routeSelector.hasNext());
    dns.set(uriHost, dns.allocate(2));
    assertRoute(routeSelector.next(), address, NO_PROXY, dns.lookup(uriHost, 0), uriPort);
    assertRoute(routeSelector.next(), address, NO_PROXY, dns.lookup(uriHost, 1), uriPort);
    assertFalse(routeSelector.hasNext());
    dns.assertRequests(uriHost);
    // No proxy selector requests!
    proxySelector.assertRequests();
}
Also used : SocketAddress(java.net.SocketAddress) Address(okhttp3.Address) InetAddress(java.net.InetAddress) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 24 with Authenticator

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

use of okhttp3.Authenticator in project okhttp by square.

the class HttpOverHttp2Test method authenticate.

@Test
public void authenticate() throws Exception {
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_UNAUTHORIZED).addHeader("www-authenticate: Basic realm=\"protected area\"").setBody("Please authenticate."));
    server.enqueue(new MockResponse().setBody("Successful auth!"));
    String credential = Credentials.basic("username", "password");
    client = client.newBuilder().authenticator(new RecordingOkAuthenticator(credential)).build();
    Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response = call.execute();
    assertEquals("Successful auth!", response.body().string());
    RecordedRequest denied = server.takeRequest();
    assertNull(denied.getHeader("Authorization"));
    RecordedRequest accepted = server.takeRequest();
    assertEquals("GET / HTTP/1.1", accepted.getRequestLine());
    assertEquals(credential, accepted.getHeader("Authorization"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Response(okhttp3.Response) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Call(okhttp3.Call) RecordingOkAuthenticator(okhttp3.internal.RecordingOkAuthenticator) Request(okhttp3.Request) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) 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