use of okhttp3.Address in project okhttp by square.
the class RouteSelectorTest method proxySelectorDirectConnectionsAreSkipped.
@Test
public void proxySelectorDirectConnectionsAreSkipped() throws Exception {
Address address = httpAddress();
proxySelector.proxies.add(NO_PROXY);
RouteSelector routeSelector = new RouteSelector(address, routeDatabase);
proxySelector.assertRequests(address.url().uri());
// Only the origin server will be attempted.
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());
}
use of okhttp3.Address in project okhttp by square.
the class RouteSelectorTest method singleRouteReturnsFailedRoute.
@Test
public void singleRouteReturnsFailedRoute() throws Exception {
Address address = httpAddress();
RouteSelector routeSelector = new RouteSelector(address, routeDatabase);
assertTrue(routeSelector.hasNext());
dns.set(uriHost, dns.allocate(1));
Route route = routeSelector.next();
routeDatabase.failed(route);
routeSelector = new RouteSelector(address, routeDatabase);
assertRoute(routeSelector.next(), address, NO_PROXY, dns.lookup(uriHost, 0), uriPort);
assertFalse(routeSelector.hasNext());
try {
routeSelector.next();
fail();
} catch (NoSuchElementException expected) {
}
}
use of okhttp3.Address in project okhttp by square.
the class RouteSelectorTest method proxySelectorReturnsNoProxies.
@Test
public void proxySelectorReturnsNoProxies() throws Exception {
Address address = httpAddress();
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);
proxySelector.assertRequests(address.url().uri());
}
use of okhttp3.Address in project okhttp by square.
the class URLConnectionTest method redirectWithProxySelector.
@Test
public void redirectWithProxySelector() throws Exception {
final List<URI> proxySelectionRequests = new ArrayList<>();
urlFactory.setClient(urlFactory.client().newBuilder().proxySelector(new ProxySelector() {
@Override
public List<Proxy> select(URI uri) {
proxySelectionRequests.add(uri);
MockWebServer proxyServer = (uri.getPort() == server.getPort()) ? server : server2;
return Arrays.asList(proxyServer.toProxyAddress());
}
@Override
public void connectFailed(URI uri, SocketAddress address, IOException failure) {
throw new AssertionError();
}
}).build());
server2.enqueue(new MockResponse().setBody("This is the 2nd server!"));
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: " + server2.url("/b").url().toString()).setBody("This page has moved!"));
assertContent("This is the 2nd server!", urlFactory.open(server.url("/a").url()));
assertEquals(Arrays.asList(server.url("/").url().toURI(), server2.url("/").url().toURI()), proxySelectionRequests);
}
use of okhttp3.Address in project okhttp by square.
the class URLConnectionTest method testConnectViaSocketFactory.
public void testConnectViaSocketFactory(boolean useHttps) throws IOException {
SocketFactory uselessSocketFactory = new SocketFactory() {
public Socket createSocket() {
throw new IllegalArgumentException("useless");
}
public Socket createSocket(InetAddress host, int port) {
return null;
}
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) {
return null;
}
public Socket createSocket(String host, int port) {
return null;
}
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) {
return null;
}
};
if (useHttps) {
server.useHttps(sslClient.socketFactory, false);
urlFactory.setClient(urlFactory.client().newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(new RecordingHostnameVerifier()).build());
}
server.enqueue(new MockResponse().setStatus("HTTP/1.1 200 OK"));
urlFactory.setClient(urlFactory.client().newBuilder().socketFactory(uselessSocketFactory).build());
connection = urlFactory.open(server.url("/").url());
try {
connection.getResponseCode();
fail();
} catch (IllegalArgumentException expected) {
}
urlFactory.setClient(urlFactory.client().newBuilder().socketFactory(SocketFactory.getDefault()).build());
connection = urlFactory.open(server.url("/").url());
assertEquals(200, connection.getResponseCode());
}
Aggregations