Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/httpclient/ConnectExceptionTest.java
41152 views
1
/*
2
* Copyright (c) 2018, 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
* @summary Expect ConnectException for all non-security related connect errors
27
* @bug 8204864
28
* @run testng/othervm ConnectExceptionTest
29
* @run testng/othervm/java.security.policy=noPermissions.policy ConnectExceptionTest
30
*/
31
32
import java.io.IOException;
33
import java.net.ConnectException;
34
import java.net.InetSocketAddress;
35
import java.net.Proxy;
36
import java.net.ProxySelector;
37
import java.net.SocketAddress;
38
import java.net.URI;
39
import java.net.http.HttpClient;
40
import java.net.http.HttpRequest;
41
import java.net.http.HttpRequest.BodyPublishers;
42
import java.net.http.HttpResponse;
43
import java.net.http.HttpResponse.BodyHandlers;
44
import java.util.List;
45
import java.util.concurrent.ExecutionException;
46
import org.testng.annotations.DataProvider;
47
import org.testng.annotations.Test;
48
import static java.lang.System.out;
49
import static org.testng.Assert.assertTrue;
50
import static org.testng.Assert.fail;
51
52
public class ConnectExceptionTest {
53
54
static final ProxySelector INVALID_PROXY = new ProxySelector() {
55
final List<Proxy> proxy = List.of(new Proxy(Proxy.Type.HTTP,
56
InetSocketAddress.createUnresolved("proxy.invalid", 8080)));
57
@Override public List<Proxy> select(URI uri) { return proxy; }
58
@Override public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { }
59
@Override public String toString() { return "INVALID_PROXY"; }
60
};
61
62
static final ProxySelector NO_PROXY = new ProxySelector() {
63
@Override public List<Proxy> select(URI uri) { return List.of(Proxy.NO_PROXY); }
64
@Override public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { }
65
@Override public String toString() { return "NO_PROXY"; }
66
};
67
68
@DataProvider(name = "uris")
69
public Object[][] uris() {
70
return new Object[][]{
71
{ "http://test.invalid/", NO_PROXY },
72
{ "https://test.invalid/", NO_PROXY },
73
{ "http://test.invalid/", INVALID_PROXY },
74
{ "https://test.invalid/", INVALID_PROXY },
75
};
76
}
77
78
@Test(dataProvider = "uris")
79
void testSynchronousGET(String uriString, ProxySelector proxy) throws Exception {
80
out.printf("%n---%ntestSynchronousGET starting uri:%s, proxy:%s%n", uriString, proxy);
81
HttpClient client = HttpClient.newBuilder().proxy(proxy).build();
82
83
URI uri = URI.create(uriString);
84
HttpRequest request = HttpRequest.newBuilder(uri).build();
85
try {
86
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
87
fail("UNEXPECTED response: " + response + ", body:" + response.body());
88
} catch (ConnectException ioe) {
89
out.println("Caught expected: " + ioe);
90
//ioe.printStackTrace(out);
91
} catch (SecurityException expectedIfSMIsSet) {
92
out.println("Caught expected: " + expectedIfSMIsSet);
93
assertTrue(System.getSecurityManager() != null);
94
}
95
}
96
97
@Test(dataProvider = "uris")
98
void testSynchronousPOST(String uriString, ProxySelector proxy) throws Exception {
99
out.printf("%n---%ntestSynchronousPOST starting uri:%s, proxy:%s%n", uriString, proxy);
100
HttpClient client = HttpClient.newBuilder().proxy(proxy).build();
101
102
URI uri = URI.create(uriString);
103
HttpRequest request = HttpRequest.newBuilder(uri)
104
.POST(BodyPublishers.ofString("Does not matter"))
105
.build();
106
try {
107
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
108
fail("UNEXPECTED response: " + response + ", body:" + response.body());
109
} catch (ConnectException ioe) {
110
out.println("Caught expected: " + ioe);
111
//ioe.printStackTrace(out);
112
} catch (SecurityException expectedIfSMIsSet) {
113
out.println("Caught expected: " + expectedIfSMIsSet);
114
assertTrue(System.getSecurityManager() != null);
115
}
116
}
117
118
@Test(dataProvider = "uris")
119
void testAsynchronousGET(String uriString, ProxySelector proxy) throws Exception {
120
out.printf("%n---%ntestAsynchronousGET starting uri:%s, proxy:%s%n", uriString, proxy);
121
HttpClient client = HttpClient.newBuilder().proxy(proxy).build();
122
123
URI uri = URI.create(uriString);
124
HttpRequest request = HttpRequest.newBuilder(uri).build();
125
try {
126
HttpResponse<String> response = client.sendAsync(request, BodyHandlers.ofString()).get();
127
fail("UNEXPECTED response: " + response + ", body:" + response.body());
128
} catch (ExecutionException ee) {
129
Throwable t = ee.getCause();
130
if (t instanceof ConnectException) {
131
out.println("Caught expected: " + t);
132
} else if (t instanceof SecurityException) {
133
out.println("Caught expected: " + t);
134
assertTrue(System.getSecurityManager() != null);
135
} else {
136
t.printStackTrace(out);
137
fail("Unexpected exception: " + t);
138
}
139
}
140
}
141
142
@Test(dataProvider = "uris")
143
void testAsynchronousPOST(String uriString, ProxySelector proxy) throws Exception {
144
out.printf("%n---%ntestAsynchronousPOST starting uri:%s, proxy:%s%n", uriString, proxy);
145
HttpClient client = HttpClient.newBuilder().proxy(proxy).build();
146
147
URI uri = URI.create(uriString);
148
HttpRequest request = HttpRequest.newBuilder(uri)
149
.POST(BodyPublishers.ofString("Does not matter"))
150
.build();
151
try {
152
HttpResponse<String> response = client.sendAsync(request, BodyHandlers.ofString()).get();
153
fail("UNEXPECTED response: " + response + ", body:" + response.body());
154
} catch (ExecutionException ee) {
155
Throwable t = ee.getCause();
156
if (t instanceof ConnectException) {
157
out.println("Caught expected: " + t);
158
} else if (t instanceof SecurityException) {
159
out.println("Caught expected: " + t);
160
assertTrue(System.getSecurityManager() != null);
161
} else {
162
t.printStackTrace(out);
163
fail("Unexpected exception: " + t);
164
}
165
}
166
}
167
}
168
169