test/jdk/java/net/URLConnection/URLConnectionHeaders.java
changeset 54938 8c977741c3c8
parent 47216 71c04702a3d5
child 58365 73950479184b
equal deleted inserted replaced
54937:7e5e0b326ed7 54938:8c977741c3c8
     1 /*
     1 /*
     2  * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    25  * @test
    25  * @test
    26  * @bug 4143541 4147035 4244362
    26  * @bug 4143541 4147035 4244362
    27  * @summary URLConnection cannot enumerate request properties,
    27  * @summary URLConnection cannot enumerate request properties,
    28  *          and URLConnection can neither get nor set multiple
    28  *          and URLConnection can neither get nor set multiple
    29  *          request properties w/ same key
    29  *          request properties w/ same key
       
    30  * @library /test/lib
    30  *
    31  *
    31  */
    32  */
    32 
    33 
    33 import java.net.*;
    34 import java.net.*;
    34 import java.util.*;
    35 import java.util.*;
    35 import java.io.*;
    36 import java.io.*;
       
    37 import jdk.test.lib.net.URIBuilder;
    36 
    38 
    37 public class URLConnectionHeaders {
    39 public class URLConnectionHeaders {
    38 
    40 
    39     static class XServer extends Thread {
    41     static class XServer extends Thread {
    40         ServerSocket srv;
    42         ServerSocket srv;
    75                 try { srv.close(); } catch (IOException unused) {}
    77                 try { srv.close(); } catch (IOException unused) {}
    76             }
    78             }
    77         }
    79         }
    78     }
    80     }
    79 
    81 
    80     public static void main(String[] args) {
    82     public static void main(String[] args) throws Exception {
    81         try {
    83         try {
    82             ServerSocket serversocket = new ServerSocket (0);
    84             InetAddress loopback = InetAddress.getLoopbackAddress();
    83             int port = serversocket.getLocalPort ();
    85             ServerSocket serversocket = new ServerSocket();
    84             XServer server = new XServer (serversocket);
    86             serversocket.bind(new InetSocketAddress(loopback, 0));
    85             server.start ();
    87             int port = serversocket.getLocalPort();
    86             Thread.sleep (200);
    88             XServer server = new XServer(serversocket);
    87             URL url = new URL ("http://localhost:"+port+"/index.html");
    89             server.start();
    88             URLConnection uc = url.openConnection ();
    90             Thread.sleep(200);
       
    91             URL url = URIBuilder.newBuilder()
       
    92                       .scheme("http")
       
    93                       .loopback()
       
    94                       .port(port)
       
    95                       .path("/index.html")
       
    96                       .toURL();
       
    97             URLConnection uc = url.openConnection();
    89 
    98 
    90             // add request properties
    99             // add request properties
    91             uc.addRequestProperty("Cookie", "cookie1");
   100             uc.addRequestProperty("Cookie", "cookie1");
    92             uc.addRequestProperty("Cookie", "cookie2");
   101             uc.addRequestProperty("Cookie", "cookie2");
    93             uc.addRequestProperty("Cookie", "cookie3");
   102             uc.addRequestProperty("Cookie", "cookie3");
   104                 throw new RuntimeException("getHeaderFields fails");
   113                 throw new RuntimeException("getHeaderFields fails");
   105             }
   114             }
   106 
   115 
   107         } catch (Exception e) {
   116         } catch (Exception e) {
   108             e.printStackTrace();
   117             e.printStackTrace();
       
   118             throw e;
   109         }
   119         }
   110     }
   120     }
   111 }
   121 }