Path: blob/master/test/jdk/java/net/ProxySelector/ProxyTest.java
41149 views
/*1* Copyright (c) 2003, 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*/2223/*24* @test25* @bug 469651226* @summary HTTP client: Improve proxy server configuration and selection27* @modules java.base/sun.net.www28* @library ../../../sun/net/www/httptest/ /test/lib29* @build ClosedChannelList TestHttpServer HttpTransaction HttpCallback30* @compile ProxyTest.java31* @run main/othervm -Dhttp.proxyHost=inexistant -Dhttp.proxyPort=8080 ProxyTest32*/3334import java.net.*;35import java.io.*;36import java.util.List;37import jdk.test.lib.net.URIBuilder;3839public class ProxyTest implements HttpCallback {40static TestHttpServer server;4142public ProxyTest() {43}4445public void request(HttpTransaction req) {46req.setResponseEntityBody("Hello .");47try {48req.sendResponse(200, "Ok");49req.orderlyClose();50} catch (IOException e) {51}52}5354static public class MyProxySelector extends ProxySelector {55private static volatile URI lastURI;56private final static List<Proxy> NO_PROXY = List.of(Proxy.NO_PROXY);5758public java.util.List<Proxy> select(URI uri) {59System.out.println("Selecting no proxy for " + uri);60lastURI = uri;61return NO_PROXY;62}6364public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {65}6667public static URI lastURI() { return lastURI; }68}6970public static void main(String[] args) {71ProxySelector defSelector = ProxySelector.getDefault();72if (defSelector == null)73throw new RuntimeException("Default ProxySelector is null");74ProxySelector.setDefault(new MyProxySelector());75try {76InetAddress loopback = InetAddress.getLoopbackAddress();77server = new TestHttpServer(new ProxyTest(), 1, 10, loopback, 0);78URL url = URIBuilder.newBuilder()79.scheme("http")80.loopback()81.port(server.getLocalPort())82.toURL();83System.out.println("client opening connection to: " + url);84HttpURLConnection urlc = (HttpURLConnection)url.openConnection();85InputStream is = urlc.getInputStream();86is.close();87URI lastURI = MyProxySelector.lastURI();88if (!String.valueOf(lastURI).equals(url + "/")) {89throw new AssertionError("Custom proxy was not used: last URI was " + lastURI);90}91} catch (Exception e) {92throw new RuntimeException(e);93} finally {94if (server != null) {95server.terminate();96}97}98}99}100101102