OSDN Git Service

Strip usage of the term 'localhost' from URLConnectionTest.
authorJesse Wilson <jessewilson@google.com>
Fri, 24 Sep 2010 02:39:13 +0000 (19:39 -0700)
committerJesse Wilson <jessewilson@google.com>
Fri, 24 Sep 2010 02:39:13 +0000 (19:39 -0700)
Change-Id: I8ea7923c3ea72728c7df64c13bdd6f94b91be121
http://b/3032912

luni/src/test/java/libcore/java/net/URLConnectionTest.java
support/src/test/java/tests/http/MockWebServer.java

index 7a6e4af..36791c1 100644 (file)
@@ -27,6 +27,7 @@ import java.net.CacheRequest;
 import java.net.CacheResponse;
 import java.net.HttpRetryException;
 import java.net.HttpURLConnection;
+import java.net.InetAddress;
 import java.net.PasswordAuthentication;
 import java.net.ResponseCache;
 import java.net.SocketTimeoutException;
@@ -76,11 +77,18 @@ public class URLConnectionTest extends junit.framework.TestCase {
     };
 
     private MockWebServer server = new MockWebServer();
+    private String hostname;
+
+    @Override protected void setUp() throws Exception {
+        super.setUp();
+        hostname = InetAddress.getLocalHost().getHostName();
+    }
 
     @Override protected void tearDown() throws Exception {
         ResponseCache.setDefault(null);
         Authenticator.setDefault(null);
         server.shutdown();
+        super.tearDown();
     }
 
     public void testRequestHeaders() throws IOException, InterruptedException {
@@ -1154,9 +1162,9 @@ public class URLConnectionTest extends junit.framework.TestCase {
                 readAscii(server.getUrl("/").openStream(), Integer.MAX_VALUE));
 
         RecordedRequest first = server.takeRequest();
-        assertContains(first.getHeaders(), "Host: localhost:" + server.getPort());
+        assertContains(first.getHeaders(), "Host: " + hostname + ":" + server.getPort());
         RecordedRequest second = server2.takeRequest();
-        assertContains(second.getHeaders(), "Host: localhost:" + server2.getPort());
+        assertContains(second.getHeaders(), "Host: " + hostname + ":" + server2.getPort());
         RecordedRequest third = server.takeRequest();
         assertEquals("Expected connection reuse", 1, third.getSequenceNumber());
 
@@ -1186,9 +1194,9 @@ public class URLConnectionTest extends junit.framework.TestCase {
             assertEquals("DEF", readAscii(url.openStream(), Integer.MAX_VALUE));
             assertEquals("GHI", readAscii(url.openStream(), Integer.MAX_VALUE));
 
-            assertEquals(Arrays.asList("verify localhost"), hostnameVerifier.calls);
+            assertEquals(Arrays.asList("verify " + hostname), hostnameVerifier.calls);
             assertEquals(Arrays.asList("checkServerTrusted ["
-                                       + "CN=localhost 1, "
+                                       + "CN=" + hostname + " 1, "
                                        + "CN=Test Intermediate Certificate Authority 1, "
                                        + "CN=Test Root Certificate Authority 1"
                                        + "] RSA"),
index 0e965b2..d790399 100644 (file)
@@ -22,12 +22,14 @@ import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.MalformedURLException;
 import java.net.Proxy;
 import java.net.ServerSocket;
 import java.net.Socket;
 import java.net.URL;
+import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashSet;
@@ -84,10 +86,11 @@ public final class MockWebServer {
      *
      * @param path the request path, such as "/".
      */
-    public URL getUrl(String path) throws MalformedURLException {
+    public URL getUrl(String path) throws MalformedURLException, UnknownHostException {
+        String host = InetAddress.getLocalHost().getHostName();
         return sslSocketFactory != null
-                ? new URL("https://localhost:" + getPort() + path)
-                : new URL("http://localhost:" + getPort() + path);
+                ? new URL("https://" + host + ":" + getPort() + path)
+                : new URL("http://" + host + ":" + getPort() + path);
     }
 
     /**