test/jdk/java/net/URLConnection/Responses.java
changeset 54746 61049e91eae5
parent 47216 71c04702a3d5
child 58009 0daf32316b47
child 58678 9cf78a70fa4f
equal deleted inserted replaced
54745:87d01c0d7b45 54746:61049e91eae5
     1 /*
     1 /*
     2  * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2002, 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.
    58     static class HttpServer implements Runnable {
    58     static class HttpServer implements Runnable {
    59         ServerSocket ss;
    59         ServerSocket ss;
    60 
    60 
    61         public HttpServer() {
    61         public HttpServer() {
    62             try {
    62             try {
    63                 ss = new ServerSocket(0);
    63                 InetAddress loopback = InetAddress.getLoopbackAddress();
       
    64                 ss = new ServerSocket();
       
    65                 ss.bind(new InetSocketAddress(loopback, 0));
    64             } catch (IOException ioe) {
    66             } catch (IOException ioe) {
    65                 throw new Error("Unable to create ServerSocket: " + ioe);
    67                 throw new Error("Unable to create ServerSocket: " + ioe);
    66             }
    68             }
    67         }
    69         }
    68 
    70 
    69         public int port() {
    71         public int port() {
    70             return ss.getLocalPort();
    72             return ss.getLocalPort();
       
    73         }
       
    74 
       
    75         public String authority() {
       
    76             InetAddress address = ss.getInetAddress();
       
    77             String hostaddr = address.isAnyLocalAddress()
       
    78                 ? "localhost" : address.getHostAddress();
       
    79             if (hostaddr.indexOf(':') > -1) {
       
    80                 hostaddr = "[" + hostaddr + "]";
       
    81             }
       
    82             return hostaddr + ":" + port();
    71         }
    83         }
    72 
    84 
    73         public void shutdown() throws IOException {
    85         public void shutdown() throws IOException {
    74             ss.close();
    86             ss.close();
    75         }
    87         }
   114 
   126 
   115         /* start the http server */
   127         /* start the http server */
   116         HttpServer svr = new HttpServer();
   128         HttpServer svr = new HttpServer();
   117         (new Thread(svr)).start();
   129         (new Thread(svr)).start();
   118 
   130 
   119         int port = svr.port();
   131         String authority = svr.authority();
       
   132         System.out.println("Server listening on: " + authority);
   120 
   133 
   121         /*
   134         /*
   122          * Iterate through each test case and check that getResponseCode
   135          * Iterate through each test case and check that getResponseCode
   123          * returns the expected result.
   136          * returns the expected result.
   124          */
   137          */
   127         for (int i=0; i<tests.length; i++) {
   140         for (int i=0; i<tests.length; i++) {
   128 
   141 
   129             System.out.println("******************");
   142             System.out.println("******************");
   130             System.out.println("Test with response: >" + tests[i][0] + "<");
   143             System.out.println("Test with response: >" + tests[i][0] + "<");
   131 
   144 
   132             URL url = new URL("http://localhost:" + port + "/" + i);
   145             URL url = new URL("http://" + authority + "/" + i);
   133             HttpURLConnection http = (HttpURLConnection)url.openConnection();
   146             HttpURLConnection http = (HttpURLConnection)url.openConnection();
   134 
   147 
   135             try {
   148             try {
   136 
   149 
   137                 // test getResponseCode
   150                 // test getResponseCode