Path: blob/master/test/jdk/java/net/HttpURLConnection/HttpURLConWithProxy.java
41149 views
/*1* Copyright (c) 2016, 2020, 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 8161016 818336926* @library /test/lib27* @summary When proxy is set HttpURLConnection should not use DIRECT connection.28* @run main/othervm HttpURLConWithProxy29*/30import java.io.IOException;31import java.net.InetAddress;32import java.net.InetSocketAddress;33import java.net.Proxy;34import java.net.ProxySelector;35import java.net.ServerSocket;36import java.net.SocketAddress;37import java.net.URI;38import java.net.URL;39import java.net.HttpURLConnection;40import java.util.ArrayList;41import java.util.List;42import jdk.test.lib.net.URIBuilder;43import java.util.logging.Handler;44import java.util.logging.Level;45import java.util.logging.Logger;46import java.util.logging.LogRecord;4748public class HttpURLConWithProxy {4950private static Logger logger =51Logger.getLogger("sun.net.www.protocol.http.HttpURLConnection");5253public static void main(String... arg) throws Exception {54// Remove the default nonProxyHosts to use localhost for testing55System.setProperty("http.nonProxyHosts", "");5657System.setProperty("http.proxyHost", "1.1.1.1");58System.setProperty("http.proxyPort", "1111");5960// Use the logger to help verify the Proxy was used61logger.setLevel(Level.ALL);62Handler h = new ProxyHandler();63h.setLevel(Level.ALL);64logger.addHandler(h);6566ServerSocket ss;67URL url;68HttpURLConnection con;69InetAddress loopback = InetAddress.getLoopbackAddress();70InetSocketAddress address = new InetSocketAddress(loopback, 0);7172// Test1: using Proxy set by System Property:73try {74ss = new ServerSocket();75ss.bind(address);76url = URIBuilder.newBuilder()77.scheme("http")78.loopback()79.port(ss.getLocalPort())80.toURL();81con = (HttpURLConnection) url.openConnection();82con.setConnectTimeout(10 * 1000);83con.connect();84if(con.usingProxy()){85System.out.println("Test1 Passed with: Connection succeeded with proxy");86} else {87throw new RuntimeException("Shouldn't use DIRECT connection "88+ "when proxy is invalid/down");89}90} catch (IOException ie) {91if(!ProxyHandler.proxyRetried) {92throw new RuntimeException("Connection not retried with proxy");93}94System.out.println("Test1 Passed with: " + ie.getMessage());95}9697// Test2: using custom ProxySelector implementation98ProxyHandler.proxyRetried = false;99MyProxySelector myProxySel = new MyProxySelector();100ProxySelector.setDefault(myProxySel);101try {102ss = new ServerSocket();103ss.bind(address);104url = URIBuilder.newBuilder()105.scheme("http")106.loopback()107.port(ss.getLocalPort())108.toURL();109con = (HttpURLConnection) url.openConnection();110con.setConnectTimeout(10 * 1000);111con.connect();112if(con.usingProxy()){113System.out.println("Test2 Passed with: Connection succeeded with proxy");114} else {115throw new RuntimeException("Shouldn't use DIRECT connection "116+ "when proxy is invalid/down");117}118} catch (IOException ie) {119if(!ProxyHandler.proxyRetried) {120throw new RuntimeException("Connection not retried with proxy");121}122System.out.println("Test2 Passed with: " + ie.getMessage());123}124}125}126127128class MyProxySelector extends ProxySelector {129130List<Proxy> proxies = new ArrayList<>();131132MyProxySelector() {133Proxy p1 = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("2.2.2.2", 2222));134Proxy p2 = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("3.3.3.3", 3333));135proxies.add(p1);136proxies.add(p2);137}138139@Override140public List<Proxy> select(URI uri) {141return proxies;142}143144@Override145public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {146// System.out.println("MyProxySelector.connectFailed(): "+sa);147}148}149150class ProxyHandler extends Handler {151public static boolean proxyRetried = false;152153@Override154public void publish(LogRecord record) {155if (record.getMessage().contains("Retrying with proxy")) {156proxyRetried = true;157}158}159160@Override161public void flush() {162}163164@Override165public void close() throws SecurityException {166}167}168169170