author | jjg |
Wed, 04 Jan 2017 18:33:20 -0800 | |
changeset 43029 | 1cd1c816581e |
parent 42460 | 7133f144981a |
child 43788 | 22a618ec8268 |
permissions | -rw-r--r-- |
37720 | 1 |
/* |
2 |
* Copyright (c) 2015, 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 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 |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
29 |
* @modules jdk.incubator.httpclient/jdk.incubator.http.internal.common |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
30 |
* jdk.incubator.httpclient/jdk.incubator.http.internal.frame |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
31 |
* jdk.incubator.httpclient/jdk.incubator.http.internal.hpack |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
32 |
* @run testng/othervm -Djdk.httpclient.HttpClient.log=ssl,requests,responses,errors BasicTest |
37720 | 33 |
*/ |
34 |
||
35 |
import java.net.*; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
36 |
import jdk.incubator.http.*; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
37 |
import static jdk.incubator.http.HttpClient.Version.HTTP_2; |
37720 | 38 |
import javax.net.ssl.*; |
39 |
import java.nio.file.*; |
|
40 |
import java.util.concurrent.*; |
|
41 |
import jdk.testlibrary.SimpleSSLContext; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
42 |
import static jdk.incubator.http.HttpRequest.BodyProcessor.fromFile; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
43 |
import static jdk.incubator.http.HttpRequest.BodyProcessor.fromString; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
44 |
import static jdk.incubator.http.HttpResponse.BodyHandler.asFile; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
45 |
import static jdk.incubator.http.HttpResponse.BodyHandler.asString; |
37720 | 46 |
|
47 |
import org.testng.annotations.Test; |
|
48 |
||
49 |
@Test |
|
50 |
public class BasicTest { |
|
51 |
static int httpPort, httpsPort; |
|
52 |
static Http2TestServer httpServer, httpsServer; |
|
53 |
static HttpClient client = null; |
|
54 |
static ExecutorService exec; |
|
55 |
static SSLContext sslContext; |
|
56 |
||
57 |
static String httpURIString, httpsURIString; |
|
58 |
||
59 |
static void initialize() throws Exception { |
|
60 |
try { |
|
61 |
SimpleSSLContext sslct = new SimpleSSLContext(); |
|
62 |
sslContext = sslct.get(); |
|
63 |
client = getClient(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
64 |
httpServer = new Http2TestServer(false, 0, exec, sslContext); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
65 |
httpServer.addHandler(new EchoHandler(), "/"); |
37720 | 66 |
httpPort = httpServer.getAddress().getPort(); |
67 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
68 |
httpsServer = new Http2TestServer(true, 0, exec, sslContext); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
69 |
httpsServer.addHandler(new EchoHandler(), "/"); |
37720 | 70 |
|
71 |
httpsPort = httpsServer.getAddress().getPort(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
72 |
httpURIString = "http://127.0.0.1:" + httpPort + "/foo/"; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
73 |
httpsURIString = "https://127.0.0.1:" + httpsPort + "/bar/"; |
37720 | 74 |
|
75 |
httpServer.start(); |
|
76 |
httpsServer.start(); |
|
77 |
} catch (Throwable e) { |
|
78 |
System.err.println("Throwing now"); |
|
79 |
e.printStackTrace(); |
|
80 |
throw e; |
|
81 |
} |
|
82 |
} |
|
83 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
84 |
@Test(timeOut=3000000) |
37720 | 85 |
public static void test() throws Exception { |
86 |
try { |
|
87 |
initialize(); |
|
88 |
simpleTest(false); |
|
89 |
simpleTest(true); |
|
90 |
streamTest(false); |
|
91 |
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
|
92 |
paramsTest(); |
37720 | 93 |
Thread.sleep(1000 * 4); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
94 |
} catch (Throwable tt) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
95 |
System.err.println("tt caught"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
96 |
tt.printStackTrace(); |
37720 | 97 |
} finally { |
98 |
httpServer.stop(); |
|
99 |
httpsServer.stop(); |
|
100 |
exec.shutdownNow(); |
|
101 |
} |
|
102 |
} |
|
103 |
||
104 |
static HttpClient getClient() { |
|
105 |
if (client == null) { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
106 |
exec = Executors.newCachedThreadPool(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
107 |
client = HttpClient.newBuilder() |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
108 |
.executor(exec) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
109 |
.sslContext(sslContext) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
110 |
.version(HTTP_2) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
111 |
.build(); |
37720 | 112 |
} |
113 |
return client; |
|
114 |
} |
|
115 |
||
116 |
static URI getURI(boolean secure) { |
|
117 |
if (secure) |
|
118 |
return URI.create(httpsURIString); |
|
119 |
else |
|
120 |
return URI.create(httpURIString); |
|
121 |
} |
|
122 |
||
123 |
static void checkStatus(int expected, int found) throws Exception { |
|
124 |
if (expected != found) { |
|
125 |
System.err.printf ("Test failed: wrong status code %d/%d\n", |
|
126 |
expected, found); |
|
127 |
throw new RuntimeException("Test failed"); |
|
128 |
} |
|
129 |
} |
|
130 |
||
131 |
static void checkStrings(String expected, String found) throws Exception { |
|
132 |
if (!expected.equals(found)) { |
|
133 |
System.err.printf ("Test failed: wrong string %s/%s\n", |
|
134 |
expected, found); |
|
135 |
throw new RuntimeException("Test failed"); |
|
136 |
} |
|
137 |
} |
|
138 |
||
139 |
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
|
140 |
return TestUtil.compareFiles(path1, path2); |
37720 | 141 |
} |
142 |
||
143 |
static Path tempFile() { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
144 |
return TestUtil.tempFile(); |
37720 | 145 |
} |
146 |
||
147 |
static final String SIMPLE_STRING = "Hello world Goodbye world"; |
|
148 |
||
149 |
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
|
150 |
static final int FILESIZE = 64 * 1024 + 200; |
37720 | 151 |
|
152 |
static void streamTest(boolean secure) throws Exception { |
|
153 |
URI uri = getURI(secure); |
|
154 |
System.err.printf("streamTest %b to %s\n" , secure, uri); |
|
155 |
||
156 |
HttpClient client = getClient(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
157 |
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
|
158 |
HttpRequest req = HttpRequest.newBuilder(uri) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
159 |
.POST(fromFile(src)) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
160 |
.build(); |
37720 | 161 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
162 |
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
|
163 |
dest.toFile().delete(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
164 |
CompletableFuture<Path> response = client.sendAsync(req, asFile(dest)) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
165 |
.thenApply(resp -> { |
37720 | 166 |
if (resp.statusCode() != 200) |
167 |
throw new RuntimeException(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
168 |
return resp.body(); |
37720 | 169 |
}); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
170 |
response.join(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
171 |
compareFiles(src, dest); |
37720 | 172 |
System.err.println("DONE"); |
173 |
} |
|
174 |
||
37799
635c430d5a99
8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents:
37720
diff
changeset
|
175 |
static void paramsTest() throws Exception { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
176 |
Http2TestServer server = new Http2TestServer(true, 0, exec, sslContext); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
177 |
server.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
|
178 |
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
|
179 |
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
|
180 |
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
|
181 |
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
|
182 |
} else { |
635c430d5a99
8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents:
37720
diff
changeset
|
183 |
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
|
184 |
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
|
185 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
186 |
}), "/"); |
37799
635c430d5a99
8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents:
37720
diff
changeset
|
187 |
server.start(); |
635c430d5a99
8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents:
37720
diff
changeset
|
188 |
int port = server.getAddress().getPort(); |
635c430d5a99
8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents:
37720
diff
changeset
|
189 |
URI u = new URI("https://127.0.0.1:"+port+"/foo"); |
635c430d5a99
8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents:
37720
diff
changeset
|
190 |
HttpClient client = getClient(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
191 |
HttpRequest req = HttpRequest.newBuilder(u).build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
192 |
HttpResponse<String> resp = client.send(req, asString()); |
37799
635c430d5a99
8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents:
37720
diff
changeset
|
193 |
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
|
194 |
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
|
195 |
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
|
196 |
+ 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
|
197 |
} |
635c430d5a99
8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents:
37720
diff
changeset
|
198 |
} |
37720 | 199 |
|
200 |
static void simpleTest(boolean secure) throws Exception { |
|
201 |
URI uri = getURI(secure); |
|
202 |
System.err.println("Request to " + uri); |
|
203 |
||
204 |
// Do a simple warmup request |
|
205 |
||
206 |
HttpClient client = getClient(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
207 |
HttpRequest req = HttpRequest.newBuilder(uri) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
208 |
.POST(fromString(SIMPLE_STRING)) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
209 |
.build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
210 |
HttpResponse<String> response = client.send(req, asString()); |
37720 | 211 |
HttpHeaders h = response.headers(); |
212 |
||
213 |
checkStatus(200, response.statusCode()); |
|
214 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
215 |
String responseBody = response.body(); |
37720 | 216 |
checkStrings(SIMPLE_STRING, responseBody); |
217 |
||
218 |
checkStrings(h.firstValue("x-hello").get(), "world"); |
|
219 |
checkStrings(h.firstValue("x-bye").get(), "universe"); |
|
220 |
||
221 |
// Do loops asynchronously |
|
222 |
||
223 |
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
|
224 |
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
|
225 |
HttpRequest request = HttpRequest.newBuilder(uri) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
226 |
.POST(fromFile(tempFile())) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
227 |
.build(); |
37720 | 228 |
for (int i = 0; i < LOOPS; i++) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
229 |
responses[i] = client.sendAsync(request, asFile(tempFile())) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
230 |
//.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
|
231 |
.thenApply(resp -> { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
232 |
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
|
233 |
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
|
234 |
return compareFiles(resp.body(), source); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
235 |
}); |
37720 | 236 |
Thread.sleep(100); |
237 |
} |
|
238 |
CompletableFuture.allOf(responses).join(); |
|
239 |
System.err.println("DONE"); |
|
240 |
} |
|
241 |
} |