author | chegar |
Tue, 18 Jun 2019 18:38:53 +0100 | |
changeset 55405 | 17f24c0e2f01 |
parent 52121 | 934969c63223 |
permissions | -rw-r--r-- |
49765 | 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 Exercise a streaming subscriber ( InputStream ) without holding a |
|
27 |
* strong (or any ) reference to the client. |
|
28 |
* @modules java.base/sun.net.www.http |
|
29 |
* java.net.http/jdk.internal.net.http.common |
|
30 |
* java.net.http/jdk.internal.net.http.frame |
|
31 |
* java.net.http/jdk.internal.net.http.hpack |
|
32 |
* java.logging |
|
33 |
* jdk.httpserver |
|
52121
934969c63223
8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents:
50681
diff
changeset
|
34 |
* @library /test/lib http2/server |
49765 | 35 |
* @build Http2TestServer |
52121
934969c63223
8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents:
50681
diff
changeset
|
36 |
* @build jdk.test.lib.net.SimpleSSLContext |
49765 | 37 |
* @run testng/othervm |
38 |
* -Djdk.httpclient.HttpClient.log=trace,headers,requests |
|
39 |
* StreamingBody |
|
40 |
*/ |
|
41 |
||
42 |
import com.sun.net.httpserver.HttpServer; |
|
43 |
import com.sun.net.httpserver.HttpsConfigurator; |
|
44 |
import com.sun.net.httpserver.HttpsServer; |
|
45 |
import java.io.IOException; |
|
46 |
import java.io.InputStream; |
|
47 |
import java.io.OutputStream; |
|
48 |
import java.net.InetAddress; |
|
49 |
import java.net.InetSocketAddress; |
|
50 |
import java.net.URI; |
|
51 |
import java.net.http.HttpClient; |
|
52 |
import java.net.http.HttpRequest; |
|
53 |
import java.net.http.HttpResponse; |
|
54 |
import java.net.http.HttpResponse.BodyHandlers; |
|
55 |
import javax.net.ssl.SSLContext; |
|
52121
934969c63223
8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents:
50681
diff
changeset
|
56 |
import jdk.test.lib.net.SimpleSSLContext; |
49765 | 57 |
import org.testng.annotations.AfterTest; |
58 |
import org.testng.annotations.BeforeTest; |
|
59 |
import org.testng.annotations.DataProvider; |
|
60 |
import org.testng.annotations.Test; |
|
61 |
import static java.lang.System.out; |
|
62 |
import static java.nio.charset.StandardCharsets.UTF_8; |
|
63 |
import static java.net.http.HttpClient.Builder.NO_PROXY; |
|
64 |
import static org.testng.Assert.assertEquals; |
|
65 |
||
66 |
public class StreamingBody implements HttpServerAdapters { |
|
67 |
||
68 |
SSLContext sslContext; |
|
69 |
HttpTestServer httpTestServer; // HTTP/1.1 [ 4 servers ] |
|
70 |
HttpTestServer httpsTestServer; // HTTPS/1.1 |
|
71 |
HttpTestServer http2TestServer; // HTTP/2 ( h2c ) |
|
72 |
HttpTestServer https2TestServer; // HTTP/2 ( h2 ) |
|
73 |
String httpURI; |
|
74 |
String httpsURI; |
|
75 |
String http2URI; |
|
76 |
String https2URI; |
|
77 |
||
78 |
static final String MESSAGE = "StreamingBody message body"; |
|
79 |
static final int ITERATIONS = 100; |
|
80 |
||
81 |
@DataProvider(name = "positive") |
|
82 |
public Object[][] positive() { |
|
83 |
return new Object[][] { |
|
84 |
{ httpURI, }, |
|
85 |
{ httpsURI, }, |
|
86 |
{ http2URI, }, |
|
87 |
{ https2URI, }, |
|
88 |
}; |
|
89 |
} |
|
90 |
||
91 |
@Test(dataProvider = "positive") |
|
92 |
void test(String uriString) throws Exception { |
|
93 |
out.printf("%n---- starting (%s) ----%n", uriString); |
|
94 |
URI uri = URI.create(uriString); |
|
95 |
HttpRequest request = HttpRequest.newBuilder(uri).build(); |
|
96 |
||
97 |
for (int i=0; i< ITERATIONS; i++) { |
|
98 |
out.println("iteration: " + i); |
|
99 |
HttpResponse<InputStream> response = HttpClient.newBuilder() |
|
100 |
.sslContext(sslContext) |
|
101 |
.proxy(NO_PROXY) |
|
102 |
.build() |
|
103 |
.sendAsync(request, BodyHandlers.ofInputStream()) |
|
104 |
.join(); |
|
105 |
||
106 |
String body = new String(response.body().readAllBytes(), UTF_8); |
|
107 |
out.println("Got response: " + response); |
|
108 |
out.println("Got body Path: " + body); |
|
109 |
||
110 |
assertEquals(response.statusCode(), 200); |
|
111 |
assertEquals(body, MESSAGE); |
|
112 |
} |
|
113 |
} |
|
114 |
||
115 |
||
116 |
// -- Infrastructure |
|
117 |
||
118 |
@BeforeTest |
|
119 |
public void setup() throws Exception { |
|
120 |
sslContext = new SimpleSSLContext().get(); |
|
121 |
if (sslContext == null) |
|
122 |
throw new AssertionError("Unexpected null sslContext"); |
|
123 |
||
124 |
InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0); |
|
125 |
||
126 |
httpTestServer = HttpTestServer.of(HttpServer.create(sa, 0)); |
|
127 |
httpTestServer.addHandler(new MessageHandler(), "/http1/streamingbody/"); |
|
128 |
httpURI = "http://" + httpTestServer.serverAuthority() + "/http1/streamingbody/w"; |
|
129 |
||
130 |
HttpsServer httpsServer = HttpsServer.create(sa, 0); |
|
131 |
httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext)); |
|
132 |
httpsTestServer = HttpTestServer.of(httpsServer); |
|
133 |
httpsTestServer.addHandler(new MessageHandler(),"/https1/streamingbody/"); |
|
134 |
httpsURI = "https://" + httpsTestServer.serverAuthority() + "/https1/streamingbody/x"; |
|
135 |
||
136 |
http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0)); |
|
137 |
http2TestServer.addHandler(new MessageHandler(), "/http2/streamingbody/"); |
|
138 |
http2URI = "http://" + http2TestServer.serverAuthority() + "/http2/streamingbody/y"; |
|
50681 | 139 |
https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, sslContext)); |
49765 | 140 |
https2TestServer.addHandler(new MessageHandler(), "/https2/streamingbody/"); |
141 |
https2URI = "https://" + https2TestServer.serverAuthority() + "/https2/streamingbody/z"; |
|
142 |
||
143 |
httpTestServer.start(); |
|
144 |
httpsTestServer.start(); |
|
145 |
http2TestServer.start(); |
|
146 |
https2TestServer.start(); |
|
147 |
} |
|
148 |
||
149 |
@AfterTest |
|
150 |
public void teardown() throws Exception { |
|
151 |
httpTestServer.stop(); |
|
152 |
httpsTestServer.stop(); |
|
153 |
http2TestServer.stop(); |
|
154 |
https2TestServer.stop(); |
|
155 |
} |
|
156 |
||
157 |
static class MessageHandler implements HttpTestHandler { |
|
158 |
@Override |
|
159 |
public void handle(HttpTestExchange t) throws IOException { |
|
160 |
System.out.println("MessageHandler for: " + t.getRequestURI()); |
|
161 |
try (InputStream is = t.getRequestBody(); |
|
162 |
OutputStream os = t.getResponseBody()) { |
|
163 |
is.readAllBytes(); |
|
164 |
byte[] bytes = MESSAGE.getBytes(UTF_8); |
|
165 |
t.sendResponseHeaders(200, bytes.length); |
|
166 |
os.write(bytes); |
|
167 |
} |
|
168 |
} |
|
169 |
} |
|
170 |
} |