changeset 54805:46cf212cdcca jdk-13+14

8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder Reviewed-by: dfuchs, chegar Contributed-by: aeubanks@google.com
author aeubanks
date Wed, 27 Mar 2019 09:06:43 -0700
parents 440cbcf3b268
children 7d5a4a48e876
files test/jdk/com/sun/net/httpserver/TestLogging.java test/jdk/com/sun/net/httpserver/bugs/6725892/Test.java test/jdk/com/sun/net/httpserver/bugs/B6373555.java test/jdk/com/sun/net/httpserver/bugs/B6401598.java test/jdk/java/net/HttpCookie/ExpiredCookieTest.java test/jdk/java/net/HttpURLConnection/NoProxyTest.java test/jdk/java/net/ProxySelector/NullSelector.java test/jdk/java/net/ResponseCache/Test2.java test/jdk/java/net/URLClassLoader/closetest/CloseTest.java test/jdk/java/net/URLConnection/TimeoutTest.java test/jdk/java/net/URLPermission/OpenURL.java test/jdk/java/net/httpclient/AuthSchemesTest.java test/jdk/java/net/httpclient/LargeResponseContent.java test/jdk/sun/net/www/http/KeepAliveCache/KeepAliveTimerThread.java test/jdk/sun/net/www/protocol/http/6550798/test.java test/jdk/sun/net/www/protocol/http/B6890349.java test/jdk/sun/net/www/protocol/http/B8012625.java test/jdk/sun/net/www/protocol/http/NoNTLM.java test/jdk/sun/net/www/protocol/http/RedirectOnPost.java test/jdk/sun/net/www/protocol/http/ResponseCacheStream.java test/jdk/sun/net/www/protocol/http/RetryUponTimeout.java test/jdk/sun/net/www/protocol/http/SetChunkedStreamingMode.java test/jdk/sun/net/www/protocol/http/UserAgent.java test/jdk/sun/net/www/protocol/https/HttpsURLConnection/IPAddressDNSIdentities.java test/jdk/sun/net/www/protocol/https/HttpsURLConnection/IPAddressIPIdentities.java test/lib/jdk/test/lib/net/URIBuilder.java
diffstat 26 files changed, 369 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
--- a/test/jdk/com/sun/net/httpserver/TestLogging.java	Wed Mar 27 13:00:57 2019 -0700
+++ b/test/jdk/com/sun/net/httpserver/TestLogging.java	Wed Mar 27 09:06:43 2019 -0700
@@ -24,6 +24,7 @@
 /**
  * @test
  * @bug 6422914
+ * @library /test/lib
  * @summary change httpserver exception printouts
  */
 
@@ -37,6 +38,7 @@
 import java.security.*;
 import java.security.cert.*;
 import javax.net.ssl.*;
+import jdk.test.lib.net.URIBuilder;
 
 public class TestLogging extends Test {
 
@@ -63,13 +65,25 @@
 
             int p1 = s1.getAddress().getPort();
 
-            URL url = new URL ("http://127.0.0.1:"+p1+"/test1/smallfile.txt");
+            URL url = URIBuilder.newBuilder()
+                .scheme("http")
+                .loopback()
+                .port(p1)
+                .path("/test1/smallfile.txt")
+                .toURLUnchecked();
+            System.out.println("URL: " + url);
             HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
             InputStream is = urlc.getInputStream();
             while (is.read() != -1) ;
             is.close();
 
-            url = new URL ("http://127.0.0.1:"+p1+"/test1/doesntexist.txt");
+            url = URIBuilder.newBuilder()
+                .scheme("http")
+                .loopback()
+                .port(p1)
+                .path("/test1/doesntexist.txt")
+                .toURLUnchecked();
+            System.out.println("URL: " + url);
             urlc = (HttpURLConnection)url.openConnection();
             try {
                 is = urlc.getInputStream();
--- a/test/jdk/com/sun/net/httpserver/bugs/6725892/Test.java	Wed Mar 27 13:00:57 2019 -0700
+++ b/test/jdk/com/sun/net/httpserver/bugs/6725892/Test.java	Wed Mar 27 09:06:43 2019 -0700
@@ -24,6 +24,7 @@
 /**
  * @test
  * @bug 6725892
+ * @library /test/lib
  * @run main/othervm -Dsun.net.httpserver.maxReqTime=2 Test
  * @summary
  */
@@ -36,6 +37,8 @@
 import java.net.*;
 import javax.net.ssl.*;
 
+import jdk.test.lib.net.URIBuilder;
+
 public class Test {
 
     static HttpServer s1;
@@ -76,7 +79,13 @@
 
             port = s1.getAddress().getPort();
             System.out.println ("Server on port " + port);
-            url = new URL ("http://127.0.0.1:"+port+"/foo");
+            url = URIBuilder.newBuilder()
+                .scheme("http")
+                .loopback()
+                .port(port)
+                .path("/foo")
+                .toURLUnchecked();
+            System.out.println("URL: " + url);
             test1();
             test2();
             test3();
--- a/test/jdk/com/sun/net/httpserver/bugs/B6373555.java	Wed Mar 27 13:00:57 2019 -0700
+++ b/test/jdk/com/sun/net/httpserver/bugs/B6373555.java	Wed Mar 27 09:06:43 2019 -0700
@@ -24,6 +24,7 @@
 /**
  * @test
  * @bug 6373555
+ * @library /test/lib
  * @summary HTTP Server failing to answer client requests
  */
 
@@ -32,6 +33,7 @@
 import java.util.*;
 import com.sun.net.httpserver.*;
 import java.util.concurrent.*;
+import jdk.test.lib.net.URIBuilder;
 
 public class B6373555 {
 
@@ -96,7 +98,13 @@
             try {
                 Thread.sleep(10);
                 byte[] buf = getBuf();
-                URL url = new URL("http://127.0.0.1:"+port+"/test");
+                URL url = URIBuilder.newBuilder()
+                    .scheme("http")
+                    .loopback()
+                    .port(port)
+                    .path("/test")
+                    .toURLUnchecked();
+                System.out.println("URL: " + url);
                 HttpURLConnection con = (HttpURLConnection)url.openConnection();
                 con.setDoOutput(true);
                 con.setDoInput(true);
--- a/test/jdk/com/sun/net/httpserver/bugs/B6401598.java	Wed Mar 27 13:00:57 2019 -0700
+++ b/test/jdk/com/sun/net/httpserver/bugs/B6401598.java	Wed Mar 27 09:06:43 2019 -0700
@@ -23,6 +23,7 @@
 
 /**
  * @test
+ * @library /test/lib
  * @bug 6401598
  * @summary  new HttpServer cannot serve binary stream data
  */
@@ -31,9 +32,12 @@
 import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.util.concurrent.*;
 
+import jdk.test.lib.net.URIBuilder;
+
 import com.sun.net.httpserver.HttpExchange;
 import com.sun.net.httpserver.HttpHandler;
 import com.sun.net.httpserver.HttpServer;
@@ -90,7 +94,14 @@
                         short counter;
 
                         for (counter = 0; counter < 1000; counter++) {
-                                HttpURLConnection connection = getHttpURLConnection(new URL("http://127.0.0.1:"+port+"/server/"), 10000);
+                                URL url = URIBuilder.newBuilder()
+                                    .scheme("http")
+                                    .loopback()
+                                    .port(port)
+                                    .path("/server/")
+                                    .toURLUnchecked();
+                                System.out.println("URL: " + url);
+                                HttpURLConnection connection = getHttpURLConnection(url, 10000);
 
                                 OutputStream os = connection.getOutputStream();
 
--- a/test/jdk/java/net/HttpCookie/ExpiredCookieTest.java	Wed Mar 27 13:00:57 2019 -0700
+++ b/test/jdk/java/net/HttpCookie/ExpiredCookieTest.java	Wed Mar 27 09:06:43 2019 -0700
@@ -24,12 +24,14 @@
 /*
  * @test
  * @bug 8000525
+ * @library /test/lib
  */
 
 import java.net.*;
 import java.util.*;
 import java.io.*;
 import java.text.*;
+import jdk.test.lib.net.URIBuilder;
 
 public class ExpiredCookieTest {
     // lifted from HttpCookie.java
@@ -81,7 +83,13 @@
                 "TEST4=TEST4; Path=/; Expires=" + datestring.toString());
 
             header.put("Set-Cookie", values);
-            cm.put(new URI("http://127.0.0.1/"), header);
+            URI uri = URIBuilder.newBuilder()
+                .scheme("http")
+                .loopback()
+                .path("/")
+                .buildUnchecked();
+            System.out.println("URI: " + uri);
+            cm.put(uri, header);
 
             CookieStore cookieJar =  cm.getCookieStore();
             List <HttpCookie> cookies = cookieJar.getCookies();
--- a/test/jdk/java/net/HttpURLConnection/NoProxyTest.java	Wed Mar 27 13:00:57 2019 -0700
+++ b/test/jdk/java/net/HttpURLConnection/NoProxyTest.java	Wed Mar 27 09:06:43 2019 -0700
@@ -24,11 +24,13 @@
  /*
  * @test
  * @bug 8144008
+ * @library /test/lib
  * @summary Setting NO_PROXY on HTTP URL connections does not stop proxying
  * @run main/othervm NoProxyTest
  */
 
 import java.io.IOException;
+import java.net.InetAddress;
 import java.net.MalformedURLException;
 import java.net.Proxy;
 import java.net.ProxySelector;
@@ -37,6 +39,7 @@
 import java.net.URL;
 import java.net.URLConnection;
 import java.util.List;
+import jdk.test.lib.net.URIBuilder;
 
 public class NoProxyTest {
 
@@ -52,7 +55,12 @@
     public static void main(String args[]) throws MalformedURLException {
         ProxySelector.setDefault(new NoProxyTestSelector());
 
-        URL url = URI.create("http://127.0.0.1/").toURL();
+        URL url = URIBuilder.newBuilder()
+            .scheme("http")
+            .loopback()
+            .path("/")
+            .toURLUnchecked();
+        System.out.println("URL: " + url);
         URLConnection connection;
         try {
             connection = url.openConnection(Proxy.NO_PROXY);
--- a/test/jdk/java/net/ProxySelector/NullSelector.java	Wed Mar 27 13:00:57 2019 -0700
+++ b/test/jdk/java/net/ProxySelector/NullSelector.java	Wed Mar 27 09:06:43 2019 -0700
@@ -23,15 +23,22 @@
 
 /* @test
  * @bug 6215885
+ * @library /test/lib
  * @summary URLConnection.openConnection NPE if ProxySelector.setDefault is set to null
  */
 
 import java.net.*;
 import java.io.*;
+import jdk.test.lib.net.URIBuilder;
 
 public class NullSelector {
     public static void main(String[] args) throws Exception {
-        URL url = new URL("http://127.0.0.1/");
+        URL url = URIBuilder.newBuilder()
+            .scheme("http")
+            .loopback()
+            .path("/")
+            .toURLUnchecked();
+        System.out.println("URL: " + url);
         ProxySelector.setDefault(null);
         URLConnection con = url.openConnection();
         con.setConnectTimeout(500);
--- a/test/jdk/java/net/ResponseCache/Test2.java	Wed Mar 27 13:00:57 2019 -0700
+++ b/test/jdk/java/net/ResponseCache/Test2.java	Wed Mar 27 09:06:43 2019 -0700
@@ -24,6 +24,7 @@
 /**
  * @test
  * @bug 8042622
+ * @library /test/lib
  * @summary Check for CRL results in IllegalArgumentException "white space not allowed"
  * @modules jdk.httpserver
  * @run main/othervm Test2
@@ -38,6 +39,7 @@
 import java.security.*;
 import javax.security.auth.callback.*;
 import javax.net.ssl.*;
+import jdk.test.lib.net.URIBuilder;
 
 public class Test2 {
 
@@ -63,7 +65,7 @@
 
     static int port;
 
-    static String urlstring, redirstring;
+    static URL url, redir;
 
     public static void main (String[] args) throws Exception {
         Handler handler = new Handler();
@@ -78,10 +80,21 @@
         server.setExecutor (executor);
         server.start ();
 
-        urlstring = "http://127.0.0.1:" + Integer.toString(port)+"/test/foo";
-        redirstring = urlstring + "/redirect/bar";
+        url = URIBuilder.newBuilder()
+            .scheme("http")
+            .loopback()
+            .port(port)
+            .path("/test/foo")
+            .toURLUnchecked();
+        System.out.println("URL: " + url);
+        redir = URIBuilder.newBuilder()
+            .scheme("http")
+            .loopback()
+            .port(port)
+            .path("/test/foo/redirect/bar")
+            .toURLUnchecked();
+        System.out.println("Redir URL: " + redir);
 
-        URL url = new URL (urlstring);
         HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
         urlc.addRequestProperty("X-Foo", "bar");
         urlc.setInstanceFollowRedirects(true);
@@ -114,7 +127,7 @@
             Headers rmap = t.getResponseHeaders();
             invocation ++;
             if (invocation == 1) {
-                rmap.add("Location", redirstring);
+                rmap.add("Location", redir.toString());
                 while (is.read () != -1) ;
                 is.close();
                 System.out.println ("sending response");
--- a/test/jdk/java/net/URLClassLoader/closetest/CloseTest.java	Wed Mar 27 13:00:57 2019 -0700
+++ b/test/jdk/java/net/URLClassLoader/closetest/CloseTest.java	Wed Mar 27 09:06:43 2019 -0700
@@ -42,6 +42,7 @@
 import java.io.IOException;
 import java.lang.reflect.Method;
 import java.net.URLClassLoader;
+import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.URL;
 import java.nio.file.Files;
@@ -49,6 +50,7 @@
 import java.nio.file.Paths;
 
 import jdk.test.lib.compiler.CompilerUtils;
+import jdk.test.lib.net.URIBuilder;
 import jdk.test.lib.util.JarUtils;
 
 import com.sun.net.httpserver.HttpContext;
@@ -157,8 +159,12 @@
 
     static URL getServerURL() throws Exception {
         int port = httpServer.getAddress().getPort();
-        String s = "http://127.0.0.1:" + port + "/";
-        return new URL(s);
+        return URIBuilder.newBuilder()
+            .scheme("http")
+            .loopback()
+            .port(port)
+            .path("/")
+            .toURL();
     }
 
     static void startHttpServer(String docroot) throws Exception {
--- a/test/jdk/java/net/URLConnection/TimeoutTest.java	Wed Mar 27 13:00:57 2019 -0700
+++ b/test/jdk/java/net/URLConnection/TimeoutTest.java	Wed Mar 27 09:06:43 2019 -0700
@@ -24,12 +24,14 @@
 /*
  * @test
  * @bug 4389976
+ * @library /test/lib
  * @summary    can't unblock read() of InputStream from URL connection
  * @run main/timeout=40/othervm -Dsun.net.client.defaultReadTimeout=2000 TimeoutTest
  */
 
 import java.io.*;
 import java.net.*;
+import jdk.test.lib.net.URIBuilder;
 
 public class TimeoutTest {
 
@@ -68,7 +70,12 @@
         ServerSocket ss = new ServerSocket(0);
         Server s = new Server (ss);
         try{
-            URL url = new URL ("http://127.0.0.1:"+ss.getLocalPort());
+            URL url = URIBuilder.newBuilder()
+                .scheme("http")
+                .loopback()
+                .port(ss.getLocalPort())
+                .toURL();
+            System.out.println("URL: " + url);
             URLConnection urlc = url.openConnection ();
             InputStream is = urlc.getInputStream ();
             throw new RuntimeException("Should have received timeout");
--- a/test/jdk/java/net/URLPermission/OpenURL.java	Wed Mar 27 13:00:57 2019 -0700
+++ b/test/jdk/java/net/URLPermission/OpenURL.java	Wed Mar 27 09:06:43 2019 -0700
@@ -24,11 +24,13 @@
 /*
  * @test
  * @bug 8029354
+ * @library /test/lib
  * @run main/othervm OpenURL
  */
 
 import java.net.*;
 import java.io.*;
+import jdk.test.lib.net.URIBuilder;
 
 public class OpenURL {
 
@@ -37,7 +39,13 @@
         System.setSecurityManager(new SecurityManager());
 
         try {
-            URL url = new URL ("http://joe@127.0.0.1/a/b");
+            URL url = URIBuilder.newBuilder()
+                .scheme("http")
+                .userInfo("joe")
+                .loopback()
+                .path("/a/b")
+                .toURL();
+            System.out.println("URL: " + url);
             HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
             InputStream is = urlc.getInputStream();
             // error will throw exception other than SecurityException
--- a/test/jdk/java/net/httpclient/AuthSchemesTest.java	Wed Mar 27 13:00:57 2019 -0700
+++ b/test/jdk/java/net/httpclient/AuthSchemesTest.java	Wed Mar 27 09:06:43 2019 -0700
@@ -24,6 +24,7 @@
 /**
  * @test
  * @bug 8217237
+ * @library /test/lib
  * @modules java.net.http
  * @run main/othervm AuthSchemesTest
  * @summary HttpClient does not deal well with multi-valued WWW-Authenticate challenge headers
@@ -37,6 +38,7 @@
 import java.net.http.HttpClient;
 import java.net.http.HttpRequest;
 import java.net.http.HttpResponse;
+import jdk.test.lib.net.URIBuilder;
 
 public class AuthSchemesTest {
     static class BasicServer extends Thread {
@@ -142,7 +144,13 @@
                 .authenticator(authenticator)
                 .build();
         server.start();
-        URI uri = URI.create("http://127.0.0.1:" + port + "/foo");
+        URI uri = URIBuilder.newBuilder()
+            .scheme("http")
+            .loopback()
+            .port(port)
+            .path("/foo")
+            .build();
+        System.out.println("URI: " + uri);
         HttpRequest request = HttpRequest.newBuilder(uri)
                 .GET()
                 .build();
--- a/test/jdk/java/net/httpclient/LargeResponseContent.java	Wed Mar 27 13:00:57 2019 -0700
+++ b/test/jdk/java/net/httpclient/LargeResponseContent.java	Wed Mar 27 09:06:43 2019 -0700
@@ -39,10 +39,12 @@
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.CompletionStage;
 import java.util.concurrent.Flow;
+import jdk.test.lib.net.URIBuilder;
 
 /**
  * @test
  * @bug 8212926
+ * @library /test/lib
  * @summary Basic tests for response timeouts
  * @run main/othervm LargeResponseContent
  */
@@ -60,7 +62,13 @@
     }
 
     void runClient() throws IOException, InterruptedException {
-        URI uri = URI.create("http://127.0.0.1:" + Integer.toString(port) + "/foo");
+        URI uri = URIBuilder.newBuilder()
+            .scheme("http")
+            .loopback()
+            .port(port)
+            .path("/foo")
+            .buildUnchecked();
+        System.out.println("URI: " + uri);
         HttpClient client = HttpClient.newHttpClient();
         HttpRequest request = HttpRequest.newBuilder(uri)
                 .GET()
--- a/test/jdk/sun/net/www/http/KeepAliveCache/KeepAliveTimerThread.java	Wed Mar 27 13:00:57 2019 -0700
+++ b/test/jdk/sun/net/www/http/KeepAliveCache/KeepAliveTimerThread.java	Wed Mar 27 09:06:43 2019 -0700
@@ -23,24 +23,26 @@
 
 /*
  * @test
+ * @library /test/lib
  * @bug 4701299
  * @summary Keep-Alive-Timer thread management in KeepAliveCache causes memory leak
  */
+
 import java.net.*;
 import java.io.*;
+import jdk.test.lib.net.URIBuilder;
 
 public class KeepAliveTimerThread {
     static class Fetcher implements Runnable {
-        String url;
+        URL url;
 
-        Fetcher(String url) {
+        Fetcher(URL url) {
             this.url = url;
         }
 
         public void run() {
             try {
-                InputStream in =
-                    (new URL(url)).openConnection().getInputStream();
+                InputStream in = url.openConnection().getInputStream();
                 byte b[] = new byte[128];
                 int n;
                 do {
@@ -105,7 +107,12 @@
         Server s = new Server (ss);
         s.start();
 
-        String url = "http://127.0.0.1:"+ss.getLocalPort();
+        URL url = URIBuilder.newBuilder()
+            .scheme("http")
+            .loopback()
+            .port(ss.getLocalPort())
+            .toURL();
+        System.out.println("URL: " + url);
 
         // start fetch in its own thread group
         ThreadGroup grp = new ThreadGroup("MyGroup");
--- a/test/jdk/sun/net/www/protocol/http/6550798/test.java	Wed Mar 27 13:00:57 2019 -0700
+++ b/test/jdk/sun/net/www/protocol/http/6550798/test.java	Wed Mar 27 09:06:43 2019 -0700
@@ -24,6 +24,7 @@
 /**
  * @test
  * @bug 6550798
+ * @library /test/lib
  * @summary Using InputStream.skip with ResponseCache will cause partial data to be cached
  * @modules jdk.httpserver
  * @run main/othervm test
@@ -33,6 +34,8 @@
 import com.sun.net.httpserver.*;
 import java.io.*;
 
+import jdk.test.lib.net.URIBuilder;
+
 public class test {
 
     final static int LEN = 16 * 1024;
@@ -58,7 +61,13 @@
         s.start();
 
         System.out.println("http request with cache hander");
-        URL u = new URL("http://127.0.0.1:"+s.getAddress().getPort()+"/f");
+        URL u = URIBuilder.newBuilder()
+            .scheme("http")
+            .loopback()
+            .port(s.getAddress().getPort())
+            .path("/f")
+            .toURL();
+        System.out.println("URL: " + u);
         URLConnection conn = u.openConnection();
 
         InputStream is = null;
--- a/test/jdk/sun/net/www/protocol/http/B6890349.java	Wed Mar 27 13:00:57 2019 -0700
+++ b/test/jdk/sun/net/www/protocol/http/B6890349.java	Wed Mar 27 09:06:43 2019 -0700
@@ -39,7 +39,11 @@
             System.out.println ("listening on "  + port);
             B6890349 t = new B6890349 (server);
             t.start();
-            URL u = new URL ("http://127.0.0.1:"+port+"/foo\nbar");
+            URL u = new URL("http",
+                InetAddress.getLoopbackAddress().getHostAddress(),
+                port,
+                "/foo\nbar");
+            System.out.println("URL: " + u);
             HttpURLConnection urlc = (HttpURLConnection)u.openConnection ();
             InputStream is = urlc.getInputStream();
             throw new RuntimeException ("Test failed");
--- a/test/jdk/sun/net/www/protocol/http/B8012625.java	Wed Mar 27 13:00:57 2019 -0700
+++ b/test/jdk/sun/net/www/protocol/http/B8012625.java	Wed Mar 27 09:06:43 2019 -0700
@@ -24,6 +24,7 @@
 /**
  * @test
  * @bug 8012625
+ * @library /test/lib
  * @modules jdk.httpserver
  * @run main B8012625
  */
@@ -34,6 +35,9 @@
 import java.net.*;
 import java.io.*;
 import java.util.concurrent.*;
+
+import jdk.test.lib.net.URIBuilder;
+
 import com.sun.net.httpserver.*;
 
 public class B8012625 implements HttpHandler {
@@ -44,8 +48,13 @@
     }
 
     public void run() throws Exception {
-        String u = "http://127.0.0.1:" + port + "/foo";
-        URL url = new URL(u);
+        URL url = URIBuilder.newBuilder()
+            .scheme("http")
+            .loopback()
+            .port(port)
+            .path("/foo")
+            .toURL();
+        System.out.println("URL: " + url);
         HttpURLConnection uc = (HttpURLConnection)url.openConnection();
         uc.setDoOutput(true);
         uc.setRequestMethod("POST");
--- a/test/jdk/sun/net/www/protocol/http/NoNTLM.java	Wed Mar 27 13:00:57 2019 -0700
+++ b/test/jdk/sun/net/www/protocol/http/NoNTLM.java	Wed Mar 27 09:06:43 2019 -0700
@@ -23,6 +23,7 @@
 
 /* @test
  * @bug 8004502
+ * @library /test/lib
  * @summary Sanity check that NTLM will not be selected by the http protocol
  *    handler when running on a profile that does not support NTLM
  * @modules java.base/sun.net.www
@@ -34,11 +35,13 @@
 import java.lang.reflect.Field;
 import java.net.Authenticator;
 import java.net.HttpURLConnection;
+import java.net.InetAddress;
 import java.net.PasswordAuthentication;
 import java.net.Proxy;
 import java.net.ServerSocket;
 import java.net.Socket;
 import java.net.URL;
+import jdk.test.lib.net.URIBuilder;
 import sun.net.www.MessageHeader;
 
 public class NoNTLM {
@@ -57,7 +60,13 @@
         private volatile int respCode;
 
         Client(int port) throws IOException {
-            this.url = new URL("http://127.0.0.1:" + port + "/foo.html");
+            this.url = URIBuilder.newBuilder()
+                .scheme("http")
+                .loopback()
+                .port(port)
+                .path("/foo.html")
+                .toURLUnchecked();
+            System.out.println("Client URL: " + this.url);
         }
 
         public void run() {
--- a/test/jdk/sun/net/www/protocol/http/RedirectOnPost.java	Wed Mar 27 13:00:57 2019 -0700
+++ b/test/jdk/sun/net/www/protocol/http/RedirectOnPost.java	Wed Mar 27 09:06:43 2019 -0700
@@ -39,6 +39,7 @@
 import java.util.concurrent.*;
 import javax.net.ssl.*;
 import jdk.test.lib.net.SimpleSSLContext;
+import jdk.test.lib.net.URIBuilder;
 
 public class RedirectOnPost {
 
@@ -55,8 +56,8 @@
             int sslPort = httpsServer.getAddress().getPort();
             httpServer.start();
             httpsServer.start();
-            runTest("http://127.0.0.1:"+port+"/test/", null);
-            runTest("https://127.0.0.1:"+sslPort+"/test/", ctx);
+            runTest("http", port, null);
+            runTest("https", sslPort, ctx);
             System.out.println("Main thread waiting");
         } finally {
             httpServer.stop(0);
@@ -65,10 +66,17 @@
         }
     }
 
-    public static void runTest(String baseURL, SSLContext ctx) throws Exception
+    public static void runTest(String scheme, int port, SSLContext ctx) throws Exception
     {
         byte[] buf = "Hello world".getBytes();
-        URL url = new URL(baseURL + "a");
+
+        URL url = URIBuilder.newBuilder()
+            .scheme(scheme)
+            .loopback()
+            .port(port)
+            .path("/test/a")
+            .toURL();
+        System.out.println("URL: " + url);
         HttpURLConnection con = (HttpURLConnection)url.openConnection();
         if (con instanceof HttpsURLConnection) {
             HttpsURLConnection ssl = (HttpsURLConnection)con;
@@ -107,10 +115,12 @@
 
     static class Handler implements HttpHandler {
 
-        String baseURL;
+        String scheme;
+        int port;
 
-        Handler(String baseURL) {
-            this.baseURL = baseURL;
+        Handler(String scheme, int port) {
+          this.scheme = scheme;
+          this.port = port;
         }
 
         int calls = 0;
@@ -118,6 +128,12 @@
         public void handle(HttpExchange msg) {
             try {
                 String method = msg.getRequestMethod();
+                URL baseURL = URIBuilder.newBuilder()
+                    .scheme(scheme)
+                    .loopback()
+                    .path("/test/b")
+                    .port(port)
+                    .toURLUnchecked();
                 System.out.println ("Server: " + baseURL);
                 if (calls++ == 0) {
                     System.out.println ("Server: redirecting");
@@ -125,7 +141,7 @@
                     byte[] buf = readFully(is);
                     is.close();
                     Headers h = msg.getResponseHeaders();
-                    h.add("Location", baseURL + "b");
+                    h.add("Location", baseURL.toString());
                     msg.sendResponseHeaders(302, -1);
                     msg.close();
                 } else {
@@ -153,9 +169,9 @@
         HttpServer testServer = HttpServer.create(inetAddress, 15);
         int port = testServer.getAddress().getPort();
         testServer.setExecutor(execs);
-        String base = "http://127.0.0.1:"+port+"/test";
+
         HttpContext context = testServer.createContext("/test");
-        context.setHandler(new Handler(base));
+        context.setHandler(new Handler("http", port));
         return testServer;
     }
 
@@ -169,9 +185,9 @@
         int port = testServer.getAddress().getPort();
         testServer.setExecutor(execs);
         testServer.setHttpsConfigurator(new HttpsConfigurator (ctx));
-        String base = "https://127.0.0.1:"+port+"/test";
+
         HttpContext context = testServer.createContext("/test");
-        context.setHandler(new Handler(base));
+        context.setHandler(new Handler("https", port));
         return testServer;
     }
 }
--- a/test/jdk/sun/net/www/protocol/http/ResponseCacheStream.java	Wed Mar 27 13:00:57 2019 -0700
+++ b/test/jdk/sun/net/www/protocol/http/ResponseCacheStream.java	Wed Mar 27 09:06:43 2019 -0700
@@ -24,6 +24,7 @@
 /*
  * @test
  * @bug 6262486
+ * @library /test/lib
  * @modules java.base/sun.net.www
  * @library ../../httptest/
  * @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction
@@ -34,6 +35,7 @@
 import java.net.*;
 import java.io.*;
 import java.util.*;
+import jdk.test.lib.net.URIBuilder;
 
 public class ResponseCacheStream implements HttpCallback {
 
@@ -100,7 +102,12 @@
             ResponseCache.setDefault(cache);
             server = new TestHttpServer (new ResponseCacheStream());
             System.out.println ("Server: listening on port: " + server.getLocalPort());
-            URL url = new URL ("http://127.0.0.1:"+server.getLocalPort()+"/");
+            URL url = URIBuilder.newBuilder()
+                .scheme("http")
+                .loopback()
+                .port(server.getLocalPort())
+                .path("/")
+                .toURL();
             System.out.println ("Client: connecting to " + url);
             HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
             InputStream is = urlc.getInputStream();
--- a/test/jdk/sun/net/www/protocol/http/RetryUponTimeout.java	Wed Mar 27 13:00:57 2019 -0700
+++ b/test/jdk/sun/net/www/protocol/http/RetryUponTimeout.java	Wed Mar 27 09:06:43 2019 -0700
@@ -24,12 +24,14 @@
 /**
  * @test
  * @bug 4772077
+ * @library /test/lib
  * @summary  using defaultReadTimeout appear to retry request upon timeout
  * @modules java.base/sun.net.www
  */
 
 import java.net.*;
 import java.io.*;
+import jdk.test.lib.net.URIBuilder;
 import sun.net.www.*;
 
 public class RetryUponTimeout implements Runnable {
@@ -63,7 +65,12 @@
             int port = server.getLocalPort ();
             new Thread(new RetryUponTimeout()).start ();
 
-            URL url = new URL("http://127.0.0.1:"+port);
+            URL url = URIBuilder.newBuilder()
+                .scheme("http")
+                .loopback()
+                .port(port)
+                .toURL();
+            System.out.println("URL: " + url);
             java.net.URLConnection uc = url.openConnection();
             uc.setReadTimeout(1000);
             uc.getInputStream();
--- a/test/jdk/sun/net/www/protocol/http/SetChunkedStreamingMode.java	Wed Mar 27 13:00:57 2019 -0700
+++ b/test/jdk/sun/net/www/protocol/http/SetChunkedStreamingMode.java	Wed Mar 27 09:06:43 2019 -0700
@@ -26,6 +26,7 @@
  * @bug 5049976
  * @modules java.base/sun.net.www
  * @library ../../httptest/
+ * @library /test/lib
  * @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction
  * @run main SetChunkedStreamingMode
  * @summary Unspecified NPE is thrown when streaming output mode is enabled
@@ -33,6 +34,7 @@
 
 import java.io.*;
 import java.net.*;
+import jdk.test.lib.net.URIBuilder;
 
 public class SetChunkedStreamingMode implements HttpCallback {
 
@@ -67,7 +69,12 @@
         try {
             server = new TestHttpServer (new SetChunkedStreamingMode(), 1, 10, 0);
             System.out.println ("Server: listening on port: " + server.getLocalPort());
-            URL url = new URL ("http://127.0.0.1:"+server.getLocalPort()+"/");
+            URL url = URIBuilder.newBuilder()
+                .scheme("http")
+                .loopback()
+                .port(server.getLocalPort())
+                .path("/")
+                .toURL();
             System.out.println ("Client: connecting to " + url);
             HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
             urlc.setChunkedStreamingMode (0);
--- a/test/jdk/sun/net/www/protocol/http/UserAgent.java	Wed Mar 27 13:00:57 2019 -0700
+++ b/test/jdk/sun/net/www/protocol/http/UserAgent.java	Wed Mar 27 09:06:43 2019 -0700
@@ -24,6 +24,7 @@
 /**
  * @test
  * @bug 4512200
+ * @library /test/lib
  * @modules java.base/sun.net.www
  * @run main/othervm -Dhttp.agent=foo UserAgent
  * @summary  HTTP header "User-Agent" format incorrect
@@ -32,6 +33,7 @@
 import java.io.*;
 import java.util.*;
 import java.net.*;
+import jdk.test.lib.net.URIBuilder;
 import sun.net.www.MessageHeader;
 
 class Server extends Thread {
@@ -89,7 +91,12 @@
         Server s = new Server (server);
         s.start ();
         int port = server.getLocalPort ();
-        URL url = new URL ("http://127.0.0.1:"+port);
+        URL url = URIBuilder.newBuilder()
+            .scheme("http")
+            .loopback()
+            .port(port)
+            .toURL();
+        System.out.println("URL: " + url);
         URLConnection urlc = url.openConnection ();
         urlc.getInputStream ();
         s.join ();
--- a/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/IPAddressDNSIdentities.java	Wed Mar 27 13:00:57 2019 -0700
+++ b/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/IPAddressDNSIdentities.java	Wed Mar 27 09:06:43 2019 -0700
@@ -23,6 +23,7 @@
 
 /* @test
  * @bug 6766775
+ * @library /test/lib
  * @summary X509 certificate hostname checking is broken in JDK1.6.0_10
  * @run main/othervm IPAddressDNSIdentities
  *
@@ -42,6 +43,7 @@
 import java.security.spec.*;
 import java.security.interfaces.*;
 import java.math.BigInteger;
+import jdk.test.lib.net.URIBuilder;
 
 /*
  * Certificates and key used in the test.
@@ -710,7 +712,12 @@
             HttpsURLConnection http = null;
 
             /* establish http connection to server */
-            URL url = new URL("https://127.0.0.1:" + serverPort+"/");
+            URL url = URIBuilder.newBuilder()
+                .scheme("https")
+                .loopback()
+                .port(serverPort)
+                .path("/")
+                .toURL();
             System.out.println("url is "+url.toString());
 
             try {
--- a/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/IPAddressIPIdentities.java	Wed Mar 27 13:00:57 2019 -0700
+++ b/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/IPAddressIPIdentities.java	Wed Mar 27 09:06:43 2019 -0700
@@ -28,6 +28,7 @@
 
 /* @test
  * @summary X509 certificate hostname checking is broken in JDK1.6.0_10
+ * @library /test/lib
  * @bug 6766775
  * @run main/othervm IPAddressIPIdentities
  * @author Xuelei Fan
@@ -45,6 +46,7 @@
 import java.security.spec.*;
 import java.security.interfaces.*;
 import java.math.BigInteger;
+import jdk.test.lib.net.URIBuilder;
 
 /*
  * Certificates and key used in the test.
@@ -714,7 +716,12 @@
             HttpsURLConnection http = null;
 
             /* establish http connection to server */
-            URL url = new URL("https://127.0.0.1:" + serverPort+"/");
+            URL url = URIBuilder.newBuilder()
+                .scheme("https")
+                .loopback()
+                .port(serverPort)
+                .path("/")
+                .toURL();
             System.out.println("url is "+url.toString());
 
             try {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/lib/jdk/test/lib/net/URIBuilder.java	Wed Mar 27 09:06:43 2019 -0700
@@ -0,0 +1,111 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2019, Google and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package jdk.test.lib.net;
+
+import java.net.InetAddress;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+
+public class URIBuilder {
+
+    public static URIBuilder newBuilder() {
+        return new URIBuilder();
+    }
+
+    private String scheme;
+    private String userInfo;
+    private String host;
+    private int port;
+    private String path;
+    private String query;
+    private String fragment;
+
+    private URIBuilder() {}
+
+    public URIBuilder scheme(String scheme) {
+        this.scheme = scheme;
+        return this;
+    }
+
+    public URIBuilder userInfo(String userInfo) {
+        this.userInfo = userInfo;
+        return this;
+    }
+
+    public URIBuilder host(String host) {
+        this.host = host;
+        return this;
+    }
+
+    public URIBuilder loopback() {
+        return host(InetAddress.getLoopbackAddress().getHostAddress());
+    }
+
+    public URIBuilder port(int port) {
+        this.port = port;
+        return this;
+    }
+
+    public URIBuilder path(String path) {
+        this.path = path;
+        return this;
+    }
+
+    public URIBuilder query(String query) {
+        this.query = query;
+        return this;
+    }
+
+    public URIBuilder fragment(String fragment) {
+        this.fragment = fragment;
+        return this;
+    }
+
+    public URI build() throws URISyntaxException {
+        return new URI(scheme, userInfo, host, port, path, query, fragment);
+    }
+
+    public URI buildUnchecked() {
+        try {
+            return build();
+        } catch (URISyntaxException e) {
+            throw new IllegalArgumentException(e);
+        }
+    }
+
+    public URL toURL() throws URISyntaxException, MalformedURLException {
+        return build().toURL();
+    }
+
+    public URL toURLUnchecked() {
+        try {
+            return toURL();
+        } catch (URISyntaxException | MalformedURLException e) {
+            throw new IllegalArgumentException(e);
+        }
+    }
+}