Search in sources :

Example 46 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 47 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 48 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 49 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)

Example 50 with Authenticator

use of okhttp3.Authenticator in project run-wallet-android by runplay.

the class IotaMsgCore method postConstruct.

/**
 * added header for IRI
 */
private void postConstruct() {
    final String nodeUrl = protocol + "://" + host + ":" + port;
    // Create OkHttpBuilder
    Authenticator auth = new Authenticator() {

        @Nullable
        @Override
        public Request authenticate(Route route, okhttp3.Response response) throws IOException {
            if (responseCount(response) >= 3) {
                // If we've failed 3 times, give up. - in real life, never give up!!
                return null;
            }
            String credential = Credentials.basic(uname, upassword);
            return response.request().newBuilder().header("Authorization", credential).build();
        }
    };
    final OkHttpClient client = new OkHttpClient.Builder().readTimeout(5000, TimeUnit.SECONDS).authenticator(auth).addInterceptor(new Interceptor() {

        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            Request newRequest;
            newRequest = request.newBuilder().addHeader(X_IOTA_API_VERSION_HEADER_NAME, X_IOTA_API_VERSION_HEADER_VALUE).build();
            return chain.proceed(newRequest);
        }
    }).connectTimeout(5000, TimeUnit.SECONDS).build();
    // use client to create Retrofit service
    final Retrofit retrofit = new Retrofit.Builder().baseUrl(nodeUrl).addConverterFactory(GsonConverterFactory.create()).client(client).build();
    service = retrofit.create(IotaAPIService.class);
    log.debug("Jota-API Java proxy pointing to node url: '{}'", nodeUrl);
}
Also used : IotaAPIService(jota.IotaAPIService) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) IOException(java.io.IOException) Response(retrofit2.Response) Retrofit(retrofit2.Retrofit) Interceptor(okhttp3.Interceptor) Authenticator(okhttp3.Authenticator) Route(okhttp3.Route)

Aggregations

Test (org.junit.Test)37 Request (okhttp3.Request)27 OkHttpClient (okhttp3.OkHttpClient)26 Response (okhttp3.Response)24 MockResponse (okhttp3.mockwebserver.MockResponse)23 Authenticator (okhttp3.Authenticator)20 IOException (java.io.IOException)15 RecordingOkAuthenticator (okhttp3.internal.RecordingOkAuthenticator)15 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)13 Route (okhttp3.Route)12 BasicAuthenticator (com.burgstaller.okhttp.basic.BasicAuthenticator)9 DigestAuthenticator (com.burgstaller.okhttp.digest.DigestAuthenticator)8 CachingAuthenticator (com.burgstaller.okhttp.digest.CachingAuthenticator)7 InetSocketAddress (java.net.InetSocketAddress)7 RecordingAuthenticator (okhttp3.internal.RecordingAuthenticator)6 Credentials (com.burgstaller.okhttp.digest.Credentials)5 InetAddress (java.net.InetAddress)5 Proxy (java.net.Proxy)5 Interceptor (okhttp3.Interceptor)5 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)5