|
1 /* |
|
2 * Copyright (c) 2016, 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 /* |
|
25 * @test |
|
26 * @bug 8161016 |
|
27 * @summary When proxy is set HttpURLConnection should not use DIRECT connection. |
|
28 * @run main/othervm HttpURLConWithProxy |
|
29 */ |
|
30 import java.io.IOException; |
|
31 import java.net.InetSocketAddress; |
|
32 import java.net.Proxy; |
|
33 import java.net.ProxySelector; |
|
34 import java.net.ServerSocket; |
|
35 import java.net.SocketAddress; |
|
36 import java.net.URI; |
|
37 import java.net.URL; |
|
38 import java.net.URLConnection; |
|
39 import java.util.ArrayList; |
|
40 import java.util.List; |
|
41 |
|
42 public class HttpURLConWithProxy { |
|
43 |
|
44 public static void main(String... arg) { |
|
45 // Remove the default nonProxyHosts to use localhost for testing |
|
46 System.setProperty("http.nonProxyHosts", ""); |
|
47 |
|
48 System.setProperty("http.proxyHost", "1.1.1.1"); |
|
49 System.setProperty("http.proxyPort", "1111"); |
|
50 |
|
51 ServerSocket ss; |
|
52 URL url; |
|
53 URLConnection con; |
|
54 |
|
55 // Test1: using Proxy set by System Property: |
|
56 try { |
|
57 ss = new ServerSocket(0); |
|
58 url = new URL("http://localhost:" + ss.getLocalPort()); |
|
59 con = url.openConnection(); |
|
60 con.setConnectTimeout(10 * 1000); |
|
61 con.connect(); |
|
62 throw new RuntimeException("Shouldn't use DIRECT connection " |
|
63 + "when proxy is invalid/down"); |
|
64 } catch (IOException ie) { |
|
65 System.out.println("Test1 Passed with: " + ie.getMessage()); |
|
66 } |
|
67 |
|
68 // Test2: using custom ProxySelector implementation |
|
69 MyProxySelector myProxySel = new MyProxySelector(); |
|
70 ProxySelector.setDefault(myProxySel); |
|
71 try { |
|
72 ss = new ServerSocket(0); |
|
73 url = new URL("http://localhost:" + ss.getLocalPort()); |
|
74 con = url.openConnection(); |
|
75 con.setConnectTimeout(10 * 1000); |
|
76 con.connect(); |
|
77 throw new RuntimeException("Shouldn't use DIRECT connection " |
|
78 + "when proxy is invalid/down"); |
|
79 } catch (IOException ie) { |
|
80 System.out.println("Test2 Passed with: " + ie.getMessage()); |
|
81 } |
|
82 } |
|
83 } |
|
84 |
|
85 |
|
86 class MyProxySelector extends ProxySelector { |
|
87 |
|
88 List<Proxy> proxies = new ArrayList<>(); |
|
89 |
|
90 MyProxySelector() { |
|
91 Proxy p1 = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("2.2.2.2", 2222)); |
|
92 Proxy p2 = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("3.3.3.3", 3333)); |
|
93 proxies.add(p1); |
|
94 proxies.add(p2); |
|
95 } |
|
96 |
|
97 @Override |
|
98 public List<Proxy> select(URI uri) { |
|
99 return proxies; |
|
100 } |
|
101 |
|
102 @Override |
|
103 public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { |
|
104 // System.out.println("MyProxySelector.connectFailed(): "+sa); |
|
105 } |
|
106 } |