use of okhttp3.Dns in project okhttp by square.
the class URLConnectionTest method unexpectedExceptionSync.
/** Confirm that runtime exceptions thrown inside of OkHttp propagate to the caller. */
@Test
public void unexpectedExceptionSync() throws Exception {
urlFactory.setClient(urlFactory.client().newBuilder().dns(new Dns() {
@Override
public List<InetAddress> lookup(String hostname) {
throw new RuntimeException("boom!");
}
}).build());
server.enqueue(new MockResponse());
HttpURLConnection connection = urlFactory.open(server.url("/").url());
try {
// Use the synchronous implementation.
connection.getResponseCode();
fail();
} catch (RuntimeException expected) {
assertEquals("boom!", expected.getMessage());
}
}
use of okhttp3.Dns 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());
}
use of okhttp3.Dns 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();
}
use of okhttp3.Dns in project okhttp by square.
the class URLConnectionTest method invalidHost.
@Test
public void invalidHost() throws Exception {
// Note that 1234.1.1.1 is an invalid host in a URI, but URL isn't as strict.
URL url = new URL("http://1234.1.1.1/index.html");
urlFactory.setClient(urlFactory.client().newBuilder().dns(new FakeDns()).build());
HttpURLConnection connection = urlFactory.open(url);
try {
connection.connect();
fail();
} catch (UnknownHostException expected) {
}
}
use of okhttp3.Dns in project okhttp by square.
the class HttpOverHttp2Test method recoverFromMultipleRefusedStreamsRequiresNewConnection.
@Test
public void recoverFromMultipleRefusedStreamsRequiresNewConnection() throws Exception {
server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.RESET_STREAM_AT_START).setHttp2ErrorCode(ErrorCode.REFUSED_STREAM.httpCode));
server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.RESET_STREAM_AT_START).setHttp2ErrorCode(ErrorCode.REFUSED_STREAM.httpCode));
server.enqueue(new MockResponse().setBody("abc"));
client = client.newBuilder().dns(new DoubleInetAddressDns()).build();
Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
Response response = call.execute();
assertEquals("abc", response.body().string());
// New connection.
assertEquals(0, server.takeRequest().getSequenceNumber());
// Reused connection.
assertEquals(1, server.takeRequest().getSequenceNumber());
// New connection.
assertEquals(0, server.takeRequest().getSequenceNumber());
}
Aggregations