diff --git a/okhttp/src/main/java/com/squareup/okhttp/internal/Dns.java b/okhttp/src/main/java/com/squareup/okhttp/internal/Dns.java index 69b2d37e9..a89b293c0 100644 --- a/okhttp/src/main/java/com/squareup/okhttp/internal/Dns.java +++ b/okhttp/src/main/java/com/squareup/okhttp/internal/Dns.java @@ -25,6 +25,7 @@ import java.net.UnknownHostException; public interface Dns { Dns DEFAULT = new Dns() { @Override public InetAddress[] getAllByName(String host) throws UnknownHostException { + if (host == null) throw new UnknownHostException("host == null"); return InetAddress.getAllByName(host); } }; diff --git a/okhttp/src/test/java/com/squareup/okhttp/internal/http/URLConnectionTest.java b/okhttp/src/test/java/com/squareup/okhttp/internal/http/URLConnectionTest.java index 05fd46973..36312ed37 100644 --- a/okhttp/src/test/java/com/squareup/okhttp/internal/http/URLConnectionTest.java +++ b/okhttp/src/test/java/com/squareup/okhttp/internal/http/URLConnectionTest.java @@ -399,6 +399,17 @@ public final class URLConnectionTest { testServerClosesOutput(SHUTDOWN_OUTPUT_AT_END); } + @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"); + HttpURLConnection connection = client.open(url); + try { + connection.connect(); + fail(); + } catch (UnknownHostException expected) { + } + } + private void testServerClosesOutput(SocketPolicy socketPolicy) throws Exception { server.enqueue(new MockResponse().setBody("This connection won't pool properly") .setSocketPolicy(socketPolicy));