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