Path: blob/master/test/jdk/java/net/HttpURLConnection/HttpURLProxySelectionTest.java
41149 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import com.sun.net.httpserver.HttpExchange;24import com.sun.net.httpserver.HttpHandler;25import com.sun.net.httpserver.HttpServer;26import jdk.test.lib.net.URIBuilder;27import org.testng.Assert;28import org.testng.annotations.AfterTest;29import org.testng.annotations.BeforeTest;30import org.testng.annotations.Test;31import sun.net.spi.DefaultProxySelector;3233import java.io.IOException;34import java.net.HttpURLConnection;35import java.net.InetAddress;36import java.net.InetSocketAddress;37import java.net.Proxy;38import java.net.ProxySelector;39import java.net.URI;40import java.net.URISyntaxException;41import java.net.URL;42import java.util.List;4344/**45* @test46* @bug 6563286 6797318 8177648 823022047* @summary Tests that sun.net.www.protocol.http.HttpURLConnection when dealing with48* sun.net.spi.DefaultProxySelector#select() handles any IllegalArgumentException49* correctly50* @library /test/lib51* @run testng HttpURLProxySelectionTest52* @modules java.base/sun.net.spi:+open53*/54public class HttpURLProxySelectionTest {5556private static final String WEB_APP_CONTEXT = "/httpurlproxytest";5758private HttpServer server;59private SimpleHandler handler;60private ProxySelector previousDefault;61private CustomProxySelector ourProxySelector = new CustomProxySelector();6263@BeforeTest64public void beforeTest() throws Exception {65previousDefault = ProxySelector.getDefault();66ProxySelector.setDefault(ourProxySelector);67handler = new SimpleHandler();68server = createServer(handler);69}7071@AfterTest72public void afterTest() {73try {74if (server != null) {75final int delaySeconds = 0;76server.stop(delaySeconds);77}78} finally {79ProxySelector.setDefault(previousDefault);80}81}8283/**84* - Test initiates a HTTP request to server85* - Server receives request and sends a 301 redirect to an URI which doesn't have a "host"86* - Redirect is expected to fail with IOException (caused by IllegalArgumentException from DefaultProxySelector)87*88* @throws Exception89*/90@Test91public void test() throws Exception {92final URL targetURL = URIBuilder.newBuilder()93.scheme("http")94.host(server.getAddress().getAddress())95.port(server.getAddress().getPort())96.path(WEB_APP_CONTEXT)97.toURL();98System.out.println("Sending request to " + targetURL);99final HttpURLConnection conn = (HttpURLConnection) targetURL.openConnection();100try {101conn.getResponseCode();102Assert.fail("Request to " + targetURL + " was expected to fail during redirect");103} catch (IOException ioe) {104// expected because of the redirect to an invalid URL, for which a proxy can't be selected105106// make sure the it was indeed a redirect107Assert.assertTrue(handler.redirectSent, "Server was expected to send a redirect, but didn't");108Assert.assertTrue(ourProxySelector.selectorUsedForRedirect, "Proxy selector wasn't used for redirect");109110// make sure the IOException was caused by an IllegalArgumentException111Assert.assertTrue(ioe.getCause() instanceof IllegalArgumentException, "Unexpected cause in the IOException");112}113}114115116private static HttpServer createServer(final HttpHandler handler) throws IOException {117final InetSocketAddress serverAddr = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);118final int backlog = -1;119final HttpServer server = HttpServer.create(serverAddr, backlog);120// setup the handler121server.createContext(WEB_APP_CONTEXT, handler);122// start the server123server.start();124System.out.println("Server started on " + server.getAddress());125return server;126}127128private static class SimpleHandler implements HttpHandler {129private volatile boolean redirectSent = false;130131@Override132public void handle(final HttpExchange httpExchange) throws IOException {133final String redirectURL;134try {135redirectURL = new URI("http", "/irrelevant", null).toString();136} catch (URISyntaxException e) {137throw new IOException(e);138}139httpExchange.getResponseHeaders().add("Location", redirectURL);140final URI requestURI = httpExchange.getRequestURI();141System.out.println("Handling " + httpExchange.getRequestMethod() + " request "142+ requestURI + " responding with redirect to " + redirectURL);143this.redirectSent = true;144httpExchange.sendResponseHeaders(301, -1);145}146147}148149private static class CustomProxySelector extends DefaultProxySelector {150151private volatile boolean selectorUsedForRedirect = false;152153@Override154public List<Proxy> select(final URI uri) {155if (uri.toString().contains("/irrelevant")) {156this.selectorUsedForRedirect = true;157}158return super.select(uri);159}160}161}162163164