Search in sources :

Example 6 with Route

use of okhttp3.Route in project okhttp by square.

the class CallTest method connectTimeoutsAttemptsAlternateRoute.

/**
   * Make a request with two routes. The first route will time out because it's connecting to a
   * special address that never connects. The automatic retry will succeed.
   */
@Test
public void connectTimeoutsAttemptsAlternateRoute() throws Exception {
    InetSocketAddress unreachableAddress = new InetSocketAddress("10.255.255.1", 8080);
    RecordingProxySelector proxySelector = new RecordingProxySelector();
    proxySelector.proxies.add(new Proxy(Proxy.Type.HTTP, unreachableAddress));
    proxySelector.proxies.add(server.toProxyAddress());
    server.enqueue(new MockResponse().setBody("success!"));
    client = client.newBuilder().proxySelector(proxySelector).readTimeout(100, TimeUnit.MILLISECONDS).connectTimeout(100, TimeUnit.MILLISECONDS).build();
    Request request = new Request.Builder().url("http://android.com/").build();
    executeSynchronously(request).assertCode(200).assertBody("success!");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) RecordingProxySelector(okhttp3.internal.http.RecordingProxySelector) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 7 with Route

use of okhttp3.Route in project okhttp by square.

the class CallTest method readTimeoutFails.

/**
   * Make a request with two routes. The first route will fail because the null server connects but
   * never responds. The manual retry will succeed.
   */
@Test
public void readTimeoutFails() throws Exception {
    InetSocketAddress nullServerAddress = startNullServer();
    RecordingProxySelector proxySelector = new RecordingProxySelector();
    proxySelector.proxies.add(new Proxy(Proxy.Type.HTTP, nullServerAddress));
    proxySelector.proxies.add(server.toProxyAddress());
    server.enqueue(new MockResponse().setBody("success!"));
    client = client.newBuilder().proxySelector(proxySelector).readTimeout(100, TimeUnit.MILLISECONDS).build();
    Request request = new Request.Builder().url("http://android.com/").build();
    executeSynchronously(request).assertFailure(SocketTimeoutException.class);
    executeSynchronously(request).assertCode(200).assertBody("success!");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) RecordingProxySelector(okhttp3.internal.http.RecordingProxySelector) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 8 with Route

use of okhttp3.Route in project azure-sdk-for-java by Azure.

the class KeyVaultCredentials method applyCredentialsFilter.

@Override
public void applyCredentialsFilter(OkHttpClient.Builder clientBuilder) {
    clientBuilder.addInterceptor(new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            HttpUrl url = chain.request().url();
            Map<String, String> challengeMap = cache.getCachedChallenge(url);
            if (challengeMap != null) {
                // Get the bearer token
                String credential = getAuthenticationCredentials(challengeMap);
                Request newRequest = chain.request().newBuilder().header(AUTHENTICATE, BEARER_TOKEP_REFIX + credential).build();
                return chain.proceed(newRequest);
            } else {
                // response
                return chain.proceed(chain.request());
            }
        }
    });
    // Caches the challenge for failed request and re-send the request with
    // access token.
    clientBuilder.authenticator(new Authenticator() {

        @Override
        public Request authenticate(Route route, Response response) throws IOException {
            // if challenge is not cached then extract and cache it
            String authenticateHeader = response.header(WWW_AUTHENTICATE);
            Map<String, String> challengeMap = extractChallenge(authenticateHeader, BEARER_TOKEP_REFIX);
            // Cache the challenge
            cache.addCachedChallenge(response.request().url(), challengeMap);
            // Get the bearer token from the callback by providing the
            // challenges
            String credential = getAuthenticationCredentials(challengeMap);
            if (credential == null) {
                return null;
            }
            // be cached anywhere in our code.
            return response.request().newBuilder().header(AUTHENTICATE, BEARER_TOKEP_REFIX + credential).build();
        }
    });
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) IOException(java.io.IOException) Interceptor(okhttp3.Interceptor) Map(java.util.Map) HashMap(java.util.HashMap) HttpUrl(okhttp3.HttpUrl) Authenticator(okhttp3.Authenticator) Route(okhttp3.Route)

Example 9 with Route

use of okhttp3.Route in project okhttp by square.

the class RouteSelectorTest method routeToString.

@Test
public void routeToString() throws Exception {
    Route route = new Route(httpAddress(), Proxy.NO_PROXY, InetSocketAddress.createUnresolved("host", 1234));
    assertEquals("Route{host:1234}", route.toString());
}
Also used : Route(okhttp3.Route) Test(org.junit.Test)

Example 10 with Route

use of okhttp3.Route in project okhttp by square.

the class RouteSelectorTest method failedRoutesAreLast.

@Test
public void failedRoutesAreLast() throws Exception {
    Address address = httpsAddress();
    RouteSelector routeSelector = new RouteSelector(address, routeDatabase);
    final int numberOfAddresses = 2;
    dns.set(uriHost, dns.allocate(numberOfAddresses));
    // Extract the regular sequence of routes from selector.
    List<Route> regularRoutes = new ArrayList<>();
    while (routeSelector.hasNext()) {
        regularRoutes.add(routeSelector.next());
    }
    // Check that we do indeed have more than one route.
    assertEquals(numberOfAddresses, regularRoutes.size());
    // Add first regular route as failed.
    routeDatabase.failed(regularRoutes.get(0));
    // Reset selector
    routeSelector = new RouteSelector(address, routeDatabase);
    List<Route> routesWithFailedRoute = new ArrayList<>();
    while (routeSelector.hasNext()) {
        routesWithFailedRoute.add(routeSelector.next());
    }
    assertEquals(regularRoutes.get(0), routesWithFailedRoute.get(routesWithFailedRoute.size() - 1));
    assertEquals(regularRoutes.size(), routesWithFailedRoute.size());
}
Also used : SocketAddress(java.net.SocketAddress) Address(okhttp3.Address) InetAddress(java.net.InetAddress) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) Route(okhttp3.Route) Test(org.junit.Test)

Aggregations

Route (okhttp3.Route)7 Test (org.junit.Test)7 IOException (java.io.IOException)4 InetSocketAddress (java.net.InetSocketAddress)4 MockResponse (okhttp3.mockwebserver.MockResponse)4 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)4 Proxy (java.net.Proxy)3 Request (okhttp3.Request)3 Response (okhttp3.Response)3 InetAddress (java.net.InetAddress)2 ProtocolException (java.net.ProtocolException)2 Socket (java.net.Socket)2 SocketAddress (java.net.SocketAddress)2 NoSuchElementException (java.util.NoSuchElementException)2 Address (okhttp3.Address)2 HttpUrl (okhttp3.HttpUrl)2 RecordingProxySelector (okhttp3.internal.http.RecordingProxySelector)2 ConnectionShutdownException (okhttp3.internal.http2.ConnectionShutdownException)2 InterruptedIOException (java.io.InterruptedIOException)1 HttpRetryException (java.net.HttpRetryException)1