author | jboes |
Fri, 08 Nov 2019 11:15:16 +0000 | |
changeset 59029 | 3786a0962570 |
parent 55649 | ad8e3b295615 |
permissions | -rw-r--r-- |
40737 | 1 |
/* |
55649
ad8e3b295615
8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved. |
40737 | 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 |
|
55649
ad8e3b295615
8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents:
47216
diff
changeset
|
27 |
* @library /test/lib |
40737 | 28 |
* @summary When proxy is set HttpURLConnection should not use DIRECT connection. |
29 |
* @run main/othervm HttpURLConWithProxy |
|
30 |
*/ |
|
31 |
import java.io.IOException; |
|
55649
ad8e3b295615
8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents:
47216
diff
changeset
|
32 |
import java.net.InetAddress; |
40737 | 33 |
import java.net.InetSocketAddress; |
34 |
import java.net.Proxy; |
|
35 |
import java.net.ProxySelector; |
|
36 |
import java.net.ServerSocket; |
|
37 |
import java.net.SocketAddress; |
|
38 |
import java.net.URI; |
|
39 |
import java.net.URL; |
|
40 |
import java.net.URLConnection; |
|
41 |
import java.util.ArrayList; |
|
42 |
import java.util.List; |
|
55649
ad8e3b295615
8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents:
47216
diff
changeset
|
43 |
import jdk.test.lib.net.URIBuilder; |
40737 | 44 |
|
45 |
public class HttpURLConWithProxy { |
|
46 |
||
55649
ad8e3b295615
8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents:
47216
diff
changeset
|
47 |
public static void main(String... arg) throws Exception { |
40737 | 48 |
// Remove the default nonProxyHosts to use localhost for testing |
49 |
System.setProperty("http.nonProxyHosts", ""); |
|
50 |
||
51 |
System.setProperty("http.proxyHost", "1.1.1.1"); |
|
52 |
System.setProperty("http.proxyPort", "1111"); |
|
53 |
||
54 |
ServerSocket ss; |
|
55 |
URL url; |
|
56 |
URLConnection con; |
|
55649
ad8e3b295615
8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents:
47216
diff
changeset
|
57 |
InetAddress loopback = InetAddress.getLoopbackAddress(); |
ad8e3b295615
8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents:
47216
diff
changeset
|
58 |
InetSocketAddress address = new InetSocketAddress(loopback, 0); |
40737 | 59 |
|
60 |
// Test1: using Proxy set by System Property: |
|
61 |
try { |
|
55649
ad8e3b295615
8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents:
47216
diff
changeset
|
62 |
ss = new ServerSocket(); |
ad8e3b295615
8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents:
47216
diff
changeset
|
63 |
ss.bind(address); |
ad8e3b295615
8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents:
47216
diff
changeset
|
64 |
url = URIBuilder.newBuilder() |
ad8e3b295615
8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents:
47216
diff
changeset
|
65 |
.scheme("http") |
ad8e3b295615
8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents:
47216
diff
changeset
|
66 |
.loopback() |
ad8e3b295615
8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents:
47216
diff
changeset
|
67 |
.port(ss.getLocalPort()) |
ad8e3b295615
8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents:
47216
diff
changeset
|
68 |
.toURL(); |
40737 | 69 |
con = url.openConnection(); |
70 |
con.setConnectTimeout(10 * 1000); |
|
71 |
con.connect(); |
|
72 |
throw new RuntimeException("Shouldn't use DIRECT connection " |
|
73 |
+ "when proxy is invalid/down"); |
|
74 |
} catch (IOException ie) { |
|
75 |
System.out.println("Test1 Passed with: " + ie.getMessage()); |
|
76 |
} |
|
77 |
||
78 |
// Test2: using custom ProxySelector implementation |
|
79 |
MyProxySelector myProxySel = new MyProxySelector(); |
|
80 |
ProxySelector.setDefault(myProxySel); |
|
81 |
try { |
|
55649
ad8e3b295615
8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents:
47216
diff
changeset
|
82 |
ss = new ServerSocket(); |
ad8e3b295615
8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents:
47216
diff
changeset
|
83 |
ss.bind(address); |
ad8e3b295615
8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents:
47216
diff
changeset
|
84 |
url = URIBuilder.newBuilder() |
ad8e3b295615
8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents:
47216
diff
changeset
|
85 |
.scheme("http") |
ad8e3b295615
8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents:
47216
diff
changeset
|
86 |
.loopback() |
ad8e3b295615
8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents:
47216
diff
changeset
|
87 |
.port(ss.getLocalPort()) |
ad8e3b295615
8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents:
47216
diff
changeset
|
88 |
.toURL(); |
40737 | 89 |
con = url.openConnection(); |
90 |
con.setConnectTimeout(10 * 1000); |
|
91 |
con.connect(); |
|
92 |
throw new RuntimeException("Shouldn't use DIRECT connection " |
|
93 |
+ "when proxy is invalid/down"); |
|
94 |
} catch (IOException ie) { |
|
95 |
System.out.println("Test2 Passed with: " + ie.getMessage()); |
|
96 |
} |
|
97 |
} |
|
98 |
} |
|
99 |
||
100 |
||
101 |
class MyProxySelector extends ProxySelector { |
|
102 |
||
103 |
List<Proxy> proxies = new ArrayList<>(); |
|
104 |
||
105 |
MyProxySelector() { |
|
106 |
Proxy p1 = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("2.2.2.2", 2222)); |
|
107 |
Proxy p2 = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("3.3.3.3", 3333)); |
|
108 |
proxies.add(p1); |
|
109 |
proxies.add(p2); |
|
110 |
} |
|
111 |
||
112 |
@Override |
|
113 |
public List<Proxy> select(URI uri) { |
|
114 |
return proxies; |
|
115 |
} |
|
116 |
||
117 |
@Override |
|
118 |
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { |
|
119 |
// System.out.println("MyProxySelector.connectFailed(): "+sa); |
|
120 |
} |
|
121 |
} |