Home | History | Annotate | Download | only in http
      1 /*
      2  * Copyright (C) 2014 Square, Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.squareup.okhttp.internal.http;
     17 
     18 import java.io.IOException;
     19 import java.net.InetSocketAddress;
     20 import java.net.Proxy;
     21 import java.net.ProxySelector;
     22 import java.net.SocketAddress;
     23 import java.net.URI;
     24 import java.util.ArrayList;
     25 import java.util.Arrays;
     26 import java.util.List;
     27 
     28 import static org.junit.Assert.assertEquals;
     29 
     30 public final class RecordingProxySelector extends ProxySelector {
     31   final List<URI> requestedUris = new ArrayList<>();
     32   List<Proxy> proxies = new ArrayList<>();
     33   final List<String> failures = new ArrayList<>();
     34 
     35   @Override public List<Proxy> select(URI uri) {
     36     requestedUris.add(uri);
     37     return proxies;
     38   }
     39 
     40   public void assertRequests(URI... expectedUris) {
     41     assertEquals(Arrays.asList(expectedUris), requestedUris);
     42     requestedUris.clear();
     43   }
     44 
     45   @Override public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
     46     InetSocketAddress socketAddress = (InetSocketAddress) sa;
     47     failures.add(
     48         String.format("%s %s:%d %s", uri, socketAddress.getHostName(), socketAddress.getPort(),
     49             ioe.getMessage()));
     50   }
     51 }
     52