test/jdk/java/net/HttpURLConnection/HttpURLProxySelectionTest.java
changeset 57888 41b68dc5e0b9
parent 57879 095c2f21dd10
child 58679 9c3209ff7550
equal deleted inserted replaced
57885:cb836bd08d58 57888:41b68dc5e0b9
    22  */
    22  */
    23 
    23 
    24 import com.sun.net.httpserver.HttpExchange;
    24 import com.sun.net.httpserver.HttpExchange;
    25 import com.sun.net.httpserver.HttpHandler;
    25 import com.sun.net.httpserver.HttpHandler;
    26 import com.sun.net.httpserver.HttpServer;
    26 import com.sun.net.httpserver.HttpServer;
       
    27 import jdk.test.lib.net.URIBuilder;
    27 import org.testng.Assert;
    28 import org.testng.Assert;
    28 import org.testng.annotations.AfterTest;
    29 import org.testng.annotations.AfterTest;
    29 import org.testng.annotations.BeforeTest;
    30 import org.testng.annotations.BeforeTest;
    30 import org.testng.annotations.Test;
    31 import org.testng.annotations.Test;
    31 import sun.net.spi.DefaultProxySelector;
    32 import sun.net.spi.DefaultProxySelector;
    41 import java.net.URL;
    42 import java.net.URL;
    42 import java.util.List;
    43 import java.util.List;
    43 
    44 
    44 /**
    45 /**
    45  * @test
    46  * @test
    46  * @bug 6563286 6797318 8177648
    47  * @bug 6563286 6797318 8177648 8230220
    47  * @summary Tests that sun.net.www.protocol.http.HttpURLConnection when dealing with
    48  * @summary Tests that sun.net.www.protocol.http.HttpURLConnection when dealing with
    48  * sun.net.spi.DefaultProxySelector#select() handles any IllegalArgumentException
    49  * sun.net.spi.DefaultProxySelector#select() handles any IllegalArgumentException
    49  * correctly
    50  * correctly
       
    51  * @library /test/lib
    50  * @run testng HttpURLProxySelectionTest
    52  * @run testng HttpURLProxySelectionTest
    51  * @modules java.base/sun.net.spi:+open
    53  * @modules java.base/sun.net.spi:+open
    52  */
    54  */
    53 public class HttpURLProxySelectionTest {
    55 public class HttpURLProxySelectionTest {
    54 
    56 
    86      *
    88      *
    87      * @throws Exception
    89      * @throws Exception
    88      */
    90      */
    89     @Test
    91     @Test
    90     public void test() throws Exception {
    92     public void test() throws Exception {
    91         final String targetURL = "http://" + server.getAddress().getHostName() + ":"
    93         final URL targetURL = URIBuilder.newBuilder()
    92                 + server.getAddress().getPort() + WEB_APP_CONTEXT;
    94                 .scheme("http")
       
    95                 .host(server.getAddress().getAddress())
       
    96                 .port(server.getAddress().getPort())
       
    97                 .path(WEB_APP_CONTEXT)
       
    98                 .toURL();
    93         System.out.println("Sending request to " + targetURL);
    99         System.out.println("Sending request to " + targetURL);
    94         final HttpURLConnection conn = (HttpURLConnection) new URL(targetURL).openConnection();
   100         final HttpURLConnection conn = (HttpURLConnection) targetURL.openConnection();
    95         try {
   101         try {
    96             conn.getResponseCode();
   102             conn.getResponseCode();
    97             Assert.fail("Request to " + targetURL + " was expected to fail during redirect");
   103             Assert.fail("Request to " + targetURL + " was expected to fail during redirect");
    98         } catch (IOException ioe) {
   104         } catch (IOException ioe) {
    99             // expected because of the redirect to an invalid URL, for which a proxy can't be selected
   105             // expected because of the redirect to an invalid URL, for which a proxy can't be selected
   119         System.out.println("Server started on " + server.getAddress());
   125         System.out.println("Server started on " + server.getAddress());
   120         return server;
   126         return server;
   121     }
   127     }
   122 
   128 
   123     private static class SimpleHandler implements HttpHandler {
   129     private static class SimpleHandler implements HttpHandler {
   124         private boolean redirectSent = false;
   130         private volatile boolean redirectSent = false;
   125 
   131 
   126         @Override
   132         @Override
   127         public void handle(final HttpExchange httpExchange) throws IOException {
   133         public void handle(final HttpExchange httpExchange) throws IOException {
   128             final String redirectURL;
   134             final String redirectURL;
   129             try {
   135             try {
   133             }
   139             }
   134             httpExchange.getResponseHeaders().add("Location", redirectURL);
   140             httpExchange.getResponseHeaders().add("Location", redirectURL);
   135             final URI requestURI = httpExchange.getRequestURI();
   141             final URI requestURI = httpExchange.getRequestURI();
   136             System.out.println("Handling " + httpExchange.getRequestMethod() + " request "
   142             System.out.println("Handling " + httpExchange.getRequestMethod() + " request "
   137                     + requestURI + " responding with redirect to " + redirectURL);
   143                     + requestURI + " responding with redirect to " + redirectURL);
       
   144             this.redirectSent = true;
   138             httpExchange.sendResponseHeaders(301, -1);
   145             httpExchange.sendResponseHeaders(301, -1);
   139             this.redirectSent = true;
       
   140         }
   146         }
   141 
   147 
   142     }
   148     }
   143 
   149 
   144     private static class CustomProxySelector extends DefaultProxySelector {
   150     private static class CustomProxySelector extends DefaultProxySelector {
   145 
   151 
   146         private boolean selectorUsedForRedirect = false;
   152         private volatile boolean selectorUsedForRedirect = false;
   147 
   153 
   148         @Override
   154         @Override
   149         public List<Proxy> select(final URI uri) {
   155         public List<Proxy> select(final URI uri) {
   150             if (uri.toString().contains("/irrelevant")) {
   156             if (uri.toString().contains("/irrelevant")) {
   151                 this.selectorUsedForRedirect = true;
   157                 this.selectorUsedForRedirect = true;