test/jdk/java/net/HttpURLConnection/HttpURLProxySelectionTest.java
branchdatagramsocketimpl-branch
changeset 58678 9cf78a70fa4f
child 58679 9c3209ff7550
equal deleted inserted replaced
58677:13588c901957 58678:9cf78a70fa4f
       
     1 /*
       
     2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     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
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 import com.sun.net.httpserver.HttpExchange;
       
    25 import com.sun.net.httpserver.HttpHandler;
       
    26 import com.sun.net.httpserver.HttpServer;
       
    27 import jdk.test.lib.net.URIBuilder;
       
    28 import org.testng.Assert;
       
    29 import org.testng.annotations.AfterTest;
       
    30 import org.testng.annotations.BeforeTest;
       
    31 import org.testng.annotations.Test;
       
    32 import sun.net.spi.DefaultProxySelector;
       
    33 
       
    34 import java.io.IOException;
       
    35 import java.net.HttpURLConnection;
       
    36 import java.net.InetAddress;
       
    37 import java.net.InetSocketAddress;
       
    38 import java.net.Proxy;
       
    39 import java.net.ProxySelector;
       
    40 import java.net.URI;
       
    41 import java.net.URISyntaxException;
       
    42 import java.net.URL;
       
    43 import java.util.List;
       
    44 
       
    45 /**
       
    46  * @test
       
    47  * @bug 6563286 6797318 8177648 8230220
       
    48  * @summary Tests that sun.net.www.protocol.http.HttpURLConnection when dealing with
       
    49  * sun.net.spi.DefaultProxySelector#select() handles any IllegalArgumentException
       
    50  * correctly
       
    51  * @library /test/lib
       
    52  * @run testng HttpURLProxySelectionTest
       
    53  * @modules java.base/sun.net.spi:+open
       
    54  */
       
    55 public class HttpURLProxySelectionTest {
       
    56 
       
    57     private static final String WEB_APP_CONTEXT = "/httpurlproxytest";
       
    58 
       
    59     private HttpServer server;
       
    60     private SimpleHandler handler;
       
    61     private ProxySelector previousDefault;
       
    62     private CustomProxySelector ourProxySelector = new CustomProxySelector();
       
    63 
       
    64     @BeforeTest
       
    65     public void beforeTest() throws Exception {
       
    66         previousDefault = ProxySelector.getDefault();
       
    67         ProxySelector.setDefault(ourProxySelector);
       
    68         handler = new SimpleHandler();
       
    69         server = createServer(handler);
       
    70     }
       
    71 
       
    72     @AfterTest
       
    73     public void afterTest() {
       
    74         try {
       
    75             if (server != null) {
       
    76                 final int delaySeconds = 0;
       
    77                 server.stop(delaySeconds);
       
    78             }
       
    79         } finally {
       
    80             ProxySelector.setDefault(previousDefault);
       
    81         }
       
    82     }
       
    83 
       
    84     /**
       
    85      * - Test initiates a HTTP request to server
       
    86      * - Server receives request and sends a 301 redirect to an URI which doesn't have a "host"
       
    87      * - Redirect is expected to fail with IOException (caused by IllegalArgumentException from DefaultProxySelector)
       
    88      *
       
    89      * @throws Exception
       
    90      */
       
    91     @Test
       
    92     public void test() throws Exception {
       
    93         final URL targetURL = URIBuilder.newBuilder()
       
    94                 .scheme("http")
       
    95                 .host(server.getAddress().getAddress())
       
    96                 .port(server.getAddress().getPort())
       
    97                 .path(WEB_APP_CONTEXT)
       
    98                 .toURL();
       
    99         System.out.println("Sending request to " + targetURL);
       
   100         final HttpURLConnection conn = (HttpURLConnection) targetURL.openConnection();
       
   101         try {
       
   102             conn.getResponseCode();
       
   103             Assert.fail("Request to " + targetURL + " was expected to fail during redirect");
       
   104         } catch (IOException ioe) {
       
   105             // expected because of the redirect to an invalid URL, for which a proxy can't be selected
       
   106 
       
   107             // make sure the it was indeed a redirect
       
   108             Assert.assertTrue(handler.redirectSent, "Server was expected to send a redirect, but didn't");
       
   109             Assert.assertTrue(ourProxySelector.selectorUsedForRedirect, "Proxy selector wasn't used for redirect");
       
   110 
       
   111             // make sure the IOException was caused by an IllegalArgumentException
       
   112             Assert.assertTrue(ioe.getCause() instanceof IllegalArgumentException, "Unexpected cause in the IOException");
       
   113         }
       
   114     }
       
   115 
       
   116 
       
   117     private static HttpServer createServer(final HttpHandler handler) throws IOException {
       
   118         final InetSocketAddress serverAddr = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
       
   119         final int backlog = -1;
       
   120         final HttpServer server = HttpServer.create(serverAddr, backlog);
       
   121         // setup the handler
       
   122         server.createContext(WEB_APP_CONTEXT, handler);
       
   123         // start the server
       
   124         server.start();
       
   125         System.out.println("Server started on " + server.getAddress());
       
   126         return server;
       
   127     }
       
   128 
       
   129     private static class SimpleHandler implements HttpHandler {
       
   130         private volatile boolean redirectSent = false;
       
   131 
       
   132         @Override
       
   133         public void handle(final HttpExchange httpExchange) throws IOException {
       
   134             final String redirectURL;
       
   135             try {
       
   136                 redirectURL = new URI("http", "/irrelevant", null).toString();
       
   137             } catch (URISyntaxException e) {
       
   138                 throw new IOException(e);
       
   139             }
       
   140             httpExchange.getResponseHeaders().add("Location", redirectURL);
       
   141             final URI requestURI = httpExchange.getRequestURI();
       
   142             System.out.println("Handling " + httpExchange.getRequestMethod() + " request "
       
   143                     + requestURI + " responding with redirect to " + redirectURL);
       
   144             this.redirectSent = true;
       
   145             httpExchange.sendResponseHeaders(301, -1);
       
   146         }
       
   147 
       
   148     }
       
   149 
       
   150     private static class CustomProxySelector extends DefaultProxySelector {
       
   151 
       
   152         private volatile boolean selectorUsedForRedirect = false;
       
   153 
       
   154         @Override
       
   155         public List<Proxy> select(final URI uri) {
       
   156             if (uri.toString().contains("/irrelevant")) {
       
   157                 this.selectorUsedForRedirect = true;
       
   158             }
       
   159             return super.select(uri);
       
   160         }
       
   161     }
       
   162 }