author | chegar |
Wed, 07 Feb 2018 14:17:24 +0000 | |
branch | http-client-branch |
changeset 56089 | 42208b2f224e |
parent 56056 | 762b9b148767 |
child 56092 | fd85b2bf2b0d |
permissions | -rw-r--r-- |
56010 | 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 |
* @library /lib/testlibrary server |
|
27 |
* @build jdk.testlibrary.SimpleSSLContext |
|
28 |
* @modules java.base/sun.net.www.http |
|
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56056
diff
changeset
|
29 |
* java.net.http/java.net.http.internal.common |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56056
diff
changeset
|
30 |
* java.net.http/java.net.http.internal.frame |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56056
diff
changeset
|
31 |
* java.net.http/java.net.http.internal.hpack |
56056
762b9b148767
http-client-branch: long lines in ServerPushWithDiffTypes
chegar
parents:
56037
diff
changeset
|
32 |
* @run testng/othervm |
762b9b148767
http-client-branch: long lines in ServerPushWithDiffTypes
chegar
parents:
56037
diff
changeset
|
33 |
* -Djdk.internal.httpclient.debug=true |
762b9b148767
http-client-branch: long lines in ServerPushWithDiffTypes
chegar
parents:
56037
diff
changeset
|
34 |
* -Djdk.httpclient.HttpClient.log=errors,requests,responses |
762b9b148767
http-client-branch: long lines in ServerPushWithDiffTypes
chegar
parents:
56037
diff
changeset
|
35 |
* ServerPushWithDiffTypes |
56010 | 36 |
*/ |
37 |
||
38 |
import java.io.*; |
|
39 |
import java.net.*; |
|
40 |
import java.nio.ByteBuffer; |
|
41 |
import java.nio.file.*; |
|
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56056
diff
changeset
|
42 |
import java.net.http.*; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56056
diff
changeset
|
43 |
import java.net.http.HttpResponse.BodyHandler; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56056
diff
changeset
|
44 |
import java.net.http.HttpResponse.PushPromiseHandler; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56056
diff
changeset
|
45 |
import java.net.http.HttpResponse.BodySubscriber; |
56010 | 46 |
import java.util.*; |
47 |
import java.util.concurrent.*; |
|
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56056
diff
changeset
|
48 |
import java.net.http.internal.common.HttpHeadersImpl; |
56010 | 49 |
import org.testng.annotations.Test; |
50 |
import static java.nio.charset.StandardCharsets.UTF_8; |
|
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
51 |
import static org.testng.Assert.assertEquals; |
56010 | 52 |
|
53 |
public class ServerPushWithDiffTypes { |
|
54 |
||
55 |
static Map<String,String> PUSH_PROMISES = Map.of( |
|
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
56 |
"/x/y/z/1", "the first push promise body", |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
57 |
"/x/y/z/2", "the second push promise body", |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
58 |
"/x/y/z/3", "the third push promise body", |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
59 |
"/x/y/z/4", "the fourth push promise body", |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
60 |
"/x/y/z/5", "the fifth push promise body", |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
61 |
"/x/y/z/6", "the sixth push promise body", |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
62 |
"/x/y/z/7", "the seventh push promise body", |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
63 |
"/x/y/z/8", "the eight push promise body", |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
64 |
"/x/y/z/9", "the ninth push promise body" |
56010 | 65 |
); |
66 |
||
67 |
@Test |
|
68 |
public static void test() throws Exception { |
|
69 |
Http2TestServer server = null; |
|
70 |
try { |
|
71 |
server = new Http2TestServer(false, 0); |
|
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
72 |
Http2Handler handler = |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
73 |
new ServerPushHandler("the main response body", |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
74 |
PUSH_PROMISES); |
56010 | 75 |
server.addHandler(handler, "/"); |
76 |
server.start(); |
|
77 |
int port = server.getAddress().getPort(); |
|
78 |
System.err.println("Server listening on port " + port); |
|
79 |
||
80 |
HttpClient client = HttpClient.newHttpClient(); |
|
81 |
// use multi-level path |
|
82 |
URI uri = new URI("http://127.0.0.1:" + port + "/foo/a/b/c"); |
|
83 |
HttpRequest request = HttpRequest.newBuilder(uri).GET().build(); |
|
84 |
||
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
85 |
ConcurrentMap<HttpRequest,CompletableFuture<HttpResponse<BodyAndType<?>>>> |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
86 |
results = new ConcurrentHashMap<>(); |
56034
43b531ed872b
http-client-branch: renamed PushPromiseHandler.withPushPromises into PushPromiseHandler.of
dfuchs
parents:
56010
diff
changeset
|
87 |
PushPromiseHandler<BodyAndType<?>> bh = PushPromiseHandler.of( |
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
88 |
(pushRequest) -> new BodyAndTypeHandler(pushRequest), results); |
56010 | 89 |
|
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
90 |
CompletableFuture<HttpResponse<BodyAndType<?>>> cf = |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
91 |
client.sendAsync(request, new BodyAndTypeHandler(request), bh); |
56010 | 92 |
results.put(request, cf); |
93 |
cf.join(); |
|
94 |
||
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
95 |
assertEquals(results.size(), PUSH_PROMISES.size() + 1); |
56010 | 96 |
|
97 |
for (HttpRequest r : results.keySet()) { |
|
98 |
URI u = r.uri(); |
|
99 |
BodyAndType<?> body = results.get(r).get().body(); |
|
100 |
String result; |
|
101 |
// convert all body types to String for easier comparison |
|
102 |
if (body.type() == String.class) { |
|
103 |
result = (String)body.getBody(); |
|
104 |
} else if (body.type() == byte[].class) { |
|
105 |
byte[] bytes = (byte[])body.getBody(); |
|
106 |
result = new String(bytes, UTF_8); |
|
107 |
} else if (Path.class.isAssignableFrom(body.type())) { |
|
108 |
Path path = (Path)body.getBody(); |
|
109 |
result = new String(Files.readAllBytes(path), UTF_8); |
|
110 |
} else { |
|
111 |
throw new AssertionError("Unknown:" + body.type()); |
|
112 |
} |
|
113 |
||
114 |
System.err.printf("%s -> %s\n", u.toString(), result.toString()); |
|
115 |
String expected = PUSH_PROMISES.get(r.uri().getPath()); |
|
116 |
if (expected == null) |
|
117 |
expected = "the main response body"; |
|
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
118 |
assertEquals(result, expected); |
56010 | 119 |
} |
120 |
} finally { |
|
121 |
server.stop(); |
|
122 |
} |
|
123 |
} |
|
124 |
||
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
125 |
interface BodyAndType<T> { |
56010 | 126 |
Class<T> type(); |
127 |
T getBody(); |
|
128 |
} |
|
129 |
||
130 |
static final Path WORK_DIR = Paths.get("."); |
|
131 |
||
132 |
static class BodyAndTypeHandler implements BodyHandler<BodyAndType<?>> { |
|
133 |
int count; |
|
134 |
final HttpRequest request; |
|
135 |
||
136 |
BodyAndTypeHandler(HttpRequest request) { |
|
137 |
this.request = request; |
|
138 |
} |
|
139 |
||
140 |
@Override |
|
141 |
public HttpResponse.BodySubscriber<BodyAndType<?>> apply(int statusCode, |
|
142 |
HttpHeaders responseHeaders) { |
|
143 |
int whichType = count++ % 3; // real world may base this on the request metadata |
|
144 |
switch (whichType) { |
|
145 |
case 0: // String |
|
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
146 |
return new BodyAndTypeSubscriber(BodySubscriber.asString(UTF_8)); |
56010 | 147 |
case 1: // byte[] |
148 |
return new BodyAndTypeSubscriber(BodySubscriber.asByteArray()); |
|
149 |
case 2: // Path |
|
150 |
URI u = request.uri(); |
|
151 |
Path path = Paths.get(WORK_DIR.toString(), u.getPath()); |
|
152 |
try { |
|
153 |
Files.createDirectories(path.getParent()); |
|
154 |
} catch (IOException ee) { |
|
155 |
throw new UncheckedIOException(ee); |
|
156 |
} |
|
157 |
return new BodyAndTypeSubscriber(BodySubscriber.asFile(path)); |
|
158 |
default: |
|
159 |
throw new AssertionError("Unexpected " + whichType); |
|
160 |
} |
|
161 |
} |
|
162 |
} |
|
163 |
||
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
164 |
static class BodyAndTypeSubscriber<T> |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
165 |
implements HttpResponse.BodySubscriber<BodyAndType<T>> |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
166 |
{ |
56010 | 167 |
private static class BodyAndTypeImpl<T> implements BodyAndType<T> { |
168 |
private final Class<T> type; |
|
169 |
private final T body; |
|
170 |
public BodyAndTypeImpl(Class<T> type, T body) { this.type = type; this.body = body; } |
|
171 |
@Override public Class<T> type() { return type; } |
|
172 |
@Override public T getBody() { return body; } |
|
173 |
} |
|
174 |
||
175 |
private final BodySubscriber<?> bodySubscriber; |
|
176 |
private final CompletableFuture<BodyAndType<T>> cf; |
|
177 |
||
178 |
BodyAndTypeSubscriber(BodySubscriber bodySubscriber) { |
|
179 |
this.bodySubscriber = bodySubscriber; |
|
180 |
cf = new CompletableFuture<>(); |
|
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
181 |
bodySubscriber.getBody().whenComplete( |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
182 |
(r,t) -> cf.complete(new BodyAndTypeImpl(r.getClass(), r))); |
56010 | 183 |
} |
184 |
||
185 |
@Override |
|
186 |
public void onSubscribe(Flow.Subscription subscription) { |
|
187 |
bodySubscriber.onSubscribe(subscription); |
|
188 |
} |
|
189 |
||
190 |
@Override |
|
191 |
public void onNext(List<ByteBuffer> item) { |
|
192 |
bodySubscriber.onNext(item); |
|
193 |
} |
|
194 |
||
195 |
@Override |
|
196 |
public void onError(Throwable throwable) { |
|
197 |
bodySubscriber.onError(throwable); |
|
198 |
cf.completeExceptionally(throwable); |
|
199 |
} |
|
200 |
||
201 |
@Override |
|
202 |
public void onComplete() { |
|
203 |
bodySubscriber.onComplete(); |
|
204 |
} |
|
205 |
||
206 |
@Override |
|
207 |
public CompletionStage<BodyAndType<T>> getBody() { |
|
208 |
return cf; |
|
209 |
} |
|
210 |
} |
|
211 |
||
212 |
// --- server push handler --- |
|
213 |
static class ServerPushHandler implements Http2Handler { |
|
214 |
||
215 |
private final String mainResponseBody; |
|
216 |
private final Map<String,String> promises; |
|
217 |
||
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
218 |
public ServerPushHandler(String mainResponseBody, |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
219 |
Map<String,String> promises) |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
220 |
throws Exception |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
221 |
{ |
56010 | 222 |
Objects.requireNonNull(promises); |
223 |
this.mainResponseBody = mainResponseBody; |
|
224 |
this.promises = promises; |
|
225 |
} |
|
226 |
||
227 |
public void handle(Http2TestExchange exchange) throws IOException { |
|
228 |
System.err.println("Server: handle " + exchange); |
|
229 |
try (InputStream is = exchange.getRequestBody()) { |
|
230 |
is.readAllBytes(); |
|
231 |
} |
|
232 |
||
233 |
if (exchange.serverPushAllowed()) { |
|
234 |
pushPromises(exchange); |
|
235 |
} |
|
236 |
||
237 |
// response data for the main response |
|
238 |
try (OutputStream os = exchange.getResponseBody()) { |
|
239 |
byte[] bytes = mainResponseBody.getBytes(UTF_8); |
|
240 |
exchange.sendResponseHeaders(200, bytes.length); |
|
241 |
os.write(bytes); |
|
242 |
} |
|
243 |
} |
|
244 |
||
245 |
private void pushPromises(Http2TestExchange exchange) throws IOException { |
|
246 |
URI requestURI = exchange.getRequestURI(); |
|
247 |
for (Map.Entry<String,String> promise : promises.entrySet()) { |
|
248 |
URI uri = requestURI.resolve(promise.getKey()); |
|
249 |
InputStream is = new ByteArrayInputStream(promise.getValue().getBytes(UTF_8)); |
|
250 |
HttpHeadersImpl headers = new HttpHeadersImpl(); |
|
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
251 |
// TODO: add some check on headers, maybe |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
252 |
headers.addHeader("X-Promise-"+promise.getKey(), promise.getKey()); |
56010 | 253 |
exchange.serverPush(uri, headers, is); |
254 |
} |
|
255 |
System.err.println("Server: All pushes sent"); |
|
256 |
} |
|
257 |
} |
|
258 |
} |