author | chegar |
Tue, 17 Apr 2018 08:54:17 -0700 | |
changeset 49765 | ee6f7a61f3a5 |
parent 48083 | b1c1b4ef4be2 |
child 50681 | 4254bed3c09d |
child 56451 | 9585061fdb04 |
permissions | -rw-r--r-- |
37720 | 1 |
/* |
49765 | 2 |
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
37720 | 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 8087112 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
27 |
* @library /lib/testlibrary server |
37720 | 28 |
* @build jdk.testlibrary.SimpleSSLContext |
48083 | 29 |
* @modules java.base/sun.net.www.http |
49765 | 30 |
* java.net.http/jdk.internal.net.http.common |
31 |
* java.net.http/jdk.internal.net.http.frame |
|
32 |
* java.net.http/jdk.internal.net.http.hpack |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
33 |
* @run testng/othervm -Djdk.httpclient.HttpClient.log=ssl,requests,responses,errors BasicTest |
37720 | 34 |
*/ |
35 |
||
48083 | 36 |
import java.io.IOException; |
37720 | 37 |
import java.net.*; |
38 |
import javax.net.ssl.*; |
|
49765 | 39 |
import java.net.http.HttpClient; |
40 |
import java.net.http.HttpHeaders; |
|
41 |
import java.net.http.HttpRequest; |
|
42 |
import java.net.http.HttpRequest.BodyPublishers; |
|
43 |
import java.net.http.HttpResponse; |
|
44 |
import java.net.http.HttpResponse.BodyHandlers; |
|
37720 | 45 |
import java.nio.file.*; |
46 |
import java.util.concurrent.*; |
|
48083 | 47 |
import java.util.Collections; |
48 |
import java.util.LinkedList; |
|
49 |
import java.util.List; |
|
37720 | 50 |
import jdk.testlibrary.SimpleSSLContext; |
51 |
import org.testng.annotations.Test; |
|
49765 | 52 |
import static java.net.http.HttpClient.Version.HTTP_2; |
37720 | 53 |
|
54 |
@Test |
|
55 |
public class BasicTest { |
|
56 |
static int httpPort, httpsPort; |
|
57 |
static Http2TestServer httpServer, httpsServer; |
|
58 |
static HttpClient client = null; |
|
48083 | 59 |
static ExecutorService clientExec; |
60 |
static ExecutorService serverExec; |
|
37720 | 61 |
static SSLContext sslContext; |
62 |
||
48083 | 63 |
static String pingURIString, httpURIString, httpsURIString; |
37720 | 64 |
|
65 |
static void initialize() throws Exception { |
|
66 |
try { |
|
67 |
SimpleSSLContext sslct = new SimpleSSLContext(); |
|
68 |
sslContext = sslct.get(); |
|
69 |
client = getClient(); |
|
48083 | 70 |
httpServer = new Http2TestServer(false, 0, serverExec, sslContext); |
45535 | 71 |
httpServer.addHandler(new Http2EchoHandler(), "/"); |
48083 | 72 |
httpServer.addHandler(new EchoWithPingHandler(), "/ping"); |
37720 | 73 |
httpPort = httpServer.getAddress().getPort(); |
74 |
||
48083 | 75 |
httpsServer = new Http2TestServer(true, 0, serverExec, sslContext); |
45535 | 76 |
httpsServer.addHandler(new Http2EchoHandler(), "/"); |
37720 | 77 |
|
78 |
httpsPort = httpsServer.getAddress().getPort(); |
|
49765 | 79 |
httpURIString = "http://localhost:" + httpPort + "/foo/"; |
80 |
pingURIString = "http://localhost:" + httpPort + "/ping/"; |
|
81 |
httpsURIString = "https://localhost:" + httpsPort + "/bar/"; |
|
37720 | 82 |
|
83 |
httpServer.start(); |
|
84 |
httpsServer.start(); |
|
85 |
} catch (Throwable e) { |
|
86 |
System.err.println("Throwing now"); |
|
87 |
e.printStackTrace(); |
|
88 |
throw e; |
|
89 |
} |
|
90 |
} |
|
91 |
||
48083 | 92 |
static List<CompletableFuture<Long>> cfs = Collections |
93 |
.synchronizedList( new LinkedList<>()); |
|
94 |
||
95 |
static CompletableFuture<Long> currentCF; |
|
96 |
||
97 |
static class EchoWithPingHandler extends Http2EchoHandler { |
|
98 |
private final Object lock = new Object(); |
|
99 |
||
100 |
@Override |
|
101 |
public void handle(Http2TestExchange exchange) throws IOException { |
|
102 |
// for now only one ping active at a time. don't want to saturate |
|
103 |
synchronized(lock) { |
|
104 |
CompletableFuture<Long> cf = currentCF; |
|
105 |
if (cf == null || cf.isDone()) { |
|
106 |
cf = exchange.sendPing(); |
|
107 |
assert cf != null; |
|
108 |
cfs.add(cf); |
|
109 |
currentCF = cf; |
|
110 |
} |
|
111 |
} |
|
112 |
super.handle(exchange); |
|
113 |
} |
|
114 |
} |
|
115 |
||
116 |
@Test |
|
37720 | 117 |
public static void test() throws Exception { |
118 |
try { |
|
119 |
initialize(); |
|
48083 | 120 |
warmup(false); |
121 |
warmup(true); |
|
122 |
simpleTest(false, false); |
|
123 |
simpleTest(false, true); |
|
124 |
simpleTest(true, false); |
|
37720 | 125 |
streamTest(false); |
126 |
streamTest(true); |
|
37799
635c430d5a99
8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents:
37720
diff
changeset
|
127 |
paramsTest(); |
48083 | 128 |
CompletableFuture.allOf(cfs.toArray(new CompletableFuture[0])).join(); |
129 |
synchronized (cfs) { |
|
130 |
for (CompletableFuture<Long> cf : cfs) { |
|
131 |
System.out.printf("Ping ack received in %d millisec\n", cf.get()); |
|
132 |
} |
|
133 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
134 |
} catch (Throwable tt) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
135 |
System.err.println("tt caught"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
136 |
tt.printStackTrace(); |
43788
22a618ec8268
8174833: java/net/httpclient/http2/BasicTest.java always fails but always report success
dfuchs
parents:
42460
diff
changeset
|
137 |
throw tt; |
37720 | 138 |
} finally { |
139 |
httpServer.stop(); |
|
140 |
httpsServer.stop(); |
|
48083 | 141 |
//clientExec.shutdown(); |
37720 | 142 |
} |
143 |
} |
|
144 |
||
145 |
static HttpClient getClient() { |
|
146 |
if (client == null) { |
|
48083 | 147 |
serverExec = Executors.newCachedThreadPool(); |
148 |
clientExec = Executors.newCachedThreadPool(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
149 |
client = HttpClient.newBuilder() |
48083 | 150 |
.executor(clientExec) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
151 |
.sslContext(sslContext) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
152 |
.version(HTTP_2) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
153 |
.build(); |
37720 | 154 |
} |
155 |
return client; |
|
156 |
} |
|
157 |
||
158 |
static URI getURI(boolean secure) { |
|
48083 | 159 |
return getURI(secure, false); |
160 |
} |
|
161 |
||
162 |
static URI getURI(boolean secure, boolean ping) { |
|
37720 | 163 |
if (secure) |
164 |
return URI.create(httpsURIString); |
|
165 |
else |
|
48083 | 166 |
return URI.create(ping ? pingURIString: httpURIString); |
37720 | 167 |
} |
168 |
||
169 |
static void checkStatus(int expected, int found) throws Exception { |
|
170 |
if (expected != found) { |
|
171 |
System.err.printf ("Test failed: wrong status code %d/%d\n", |
|
172 |
expected, found); |
|
173 |
throw new RuntimeException("Test failed"); |
|
174 |
} |
|
175 |
} |
|
176 |
||
177 |
static void checkStrings(String expected, String found) throws Exception { |
|
178 |
if (!expected.equals(found)) { |
|
179 |
System.err.printf ("Test failed: wrong string %s/%s\n", |
|
180 |
expected, found); |
|
181 |
throw new RuntimeException("Test failed"); |
|
182 |
} |
|
183 |
} |
|
184 |
||
185 |
static Void compareFiles(Path path1, Path path2) { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
186 |
return TestUtil.compareFiles(path1, path2); |
37720 | 187 |
} |
188 |
||
189 |
static Path tempFile() { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
190 |
return TestUtil.tempFile(); |
37720 | 191 |
} |
192 |
||
193 |
static final String SIMPLE_STRING = "Hello world Goodbye world"; |
|
194 |
||
195 |
static final int LOOPS = 13; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
196 |
static final int FILESIZE = 64 * 1024 + 200; |
37720 | 197 |
|
198 |
static void streamTest(boolean secure) throws Exception { |
|
199 |
URI uri = getURI(secure); |
|
200 |
System.err.printf("streamTest %b to %s\n" , secure, uri); |
|
201 |
||
202 |
HttpClient client = getClient(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
203 |
Path src = TestUtil.getAFile(FILESIZE * 4); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
204 |
HttpRequest req = HttpRequest.newBuilder(uri) |
49765 | 205 |
.POST(BodyPublishers.ofFile(src)) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
206 |
.build(); |
37720 | 207 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
208 |
Path dest = Paths.get("streamtest.txt"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
209 |
dest.toFile().delete(); |
49765 | 210 |
CompletableFuture<Path> response = client.sendAsync(req, BodyHandlers.ofFile(dest)) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
211 |
.thenApply(resp -> { |
37720 | 212 |
if (resp.statusCode() != 200) |
213 |
throw new RuntimeException(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
214 |
return resp.body(); |
37720 | 215 |
}); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
216 |
response.join(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
217 |
compareFiles(src, dest); |
48083 | 218 |
System.err.println("streamTest: DONE"); |
37720 | 219 |
} |
220 |
||
37799
635c430d5a99
8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents:
37720
diff
changeset
|
221 |
static void paramsTest() throws Exception { |
48083 | 222 |
httpsServer.addHandler((t -> { |
37799
635c430d5a99
8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents:
37720
diff
changeset
|
223 |
SSLSession s = t.getSSLSession(); |
635c430d5a99
8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents:
37720
diff
changeset
|
224 |
String prot = s.getProtocol(); |
635c430d5a99
8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents:
37720
diff
changeset
|
225 |
if (prot.equals("TLSv1.2")) { |
635c430d5a99
8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents:
37720
diff
changeset
|
226 |
t.sendResponseHeaders(200, -1); |
635c430d5a99
8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents:
37720
diff
changeset
|
227 |
} else { |
635c430d5a99
8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents:
37720
diff
changeset
|
228 |
System.err.printf("Protocols =%s\n", prot); |
635c430d5a99
8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents:
37720
diff
changeset
|
229 |
t.sendResponseHeaders(500, -1); |
635c430d5a99
8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents:
37720
diff
changeset
|
230 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
231 |
}), "/"); |
49765 | 232 |
URI u = new URI("https://localhost:"+httpsPort+"/foo"); |
37799
635c430d5a99
8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents:
37720
diff
changeset
|
233 |
HttpClient client = getClient(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
234 |
HttpRequest req = HttpRequest.newBuilder(u).build(); |
49765 | 235 |
HttpResponse<String> resp = client.send(req, BodyHandlers.ofString()); |
37799
635c430d5a99
8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents:
37720
diff
changeset
|
236 |
int stat = resp.statusCode(); |
635c430d5a99
8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents:
37720
diff
changeset
|
237 |
if (stat != 200) { |
635c430d5a99
8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents:
37720
diff
changeset
|
238 |
throw new RuntimeException("paramsTest failed " |
635c430d5a99
8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents:
37720
diff
changeset
|
239 |
+ Integer.toString(stat)); |
635c430d5a99
8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents:
37720
diff
changeset
|
240 |
} |
48083 | 241 |
System.err.println("paramsTest: DONE"); |
37799
635c430d5a99
8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents:
37720
diff
changeset
|
242 |
} |
37720 | 243 |
|
48083 | 244 |
static void warmup(boolean secure) throws Exception { |
37720 | 245 |
URI uri = getURI(secure); |
246 |
System.err.println("Request to " + uri); |
|
247 |
||
248 |
// Do a simple warmup request |
|
249 |
||
250 |
HttpClient client = getClient(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
251 |
HttpRequest req = HttpRequest.newBuilder(uri) |
49765 | 252 |
.POST(BodyPublishers.ofString(SIMPLE_STRING)) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
253 |
.build(); |
49765 | 254 |
HttpResponse<String> response = client.send(req, BodyHandlers.ofString()); |
48083 | 255 |
checkStatus(200, response.statusCode()); |
256 |
String responseBody = response.body(); |
|
37720 | 257 |
HttpHeaders h = response.headers(); |
258 |
checkStrings(SIMPLE_STRING, responseBody); |
|
259 |
checkStrings(h.firstValue("x-hello").get(), "world"); |
|
260 |
checkStrings(h.firstValue("x-bye").get(), "universe"); |
|
48083 | 261 |
} |
262 |
||
263 |
static void simpleTest(boolean secure, boolean ping) throws Exception { |
|
264 |
URI uri = getURI(secure, ping); |
|
265 |
System.err.println("Request to " + uri); |
|
37720 | 266 |
|
267 |
// Do loops asynchronously |
|
268 |
||
269 |
CompletableFuture[] responses = new CompletableFuture[LOOPS]; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
270 |
final Path source = TestUtil.getAFile(FILESIZE); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
271 |
HttpRequest request = HttpRequest.newBuilder(uri) |
49765 | 272 |
.POST(BodyPublishers.ofFile(source)) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
273 |
.build(); |
37720 | 274 |
for (int i = 0; i < LOOPS; i++) { |
49765 | 275 |
responses[i] = client.sendAsync(request, BodyHandlers.ofFile(tempFile())) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
276 |
//.thenApply(resp -> compareFiles(resp.body(), source)); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
277 |
.thenApply(resp -> { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
278 |
System.out.printf("Resp status %d body size %d\n", |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
279 |
resp.statusCode(), resp.body().toFile().length()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
280 |
return compareFiles(resp.body(), source); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
281 |
}); |
37720 | 282 |
Thread.sleep(100); |
283 |
} |
|
284 |
CompletableFuture.allOf(responses).join(); |
|
48083 | 285 |
System.err.println("simpleTest: DONE"); |
37720 | 286 |
} |
287 |
} |