author | chegar |
Thu, 29 Mar 2018 17:52:06 +0100 | |
branch | http-client-branch |
changeset 56369 | 24a8fafec3ff |
parent 56265 | ec34ae013fbe |
child 56451 | 9585061fdb04 |
permissions | -rw-r--r-- |
48523 | 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 |
|
56092
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
29 |
* java.net.http/jdk.internal.net.http.common |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
30 |
* java.net.http/jdk.internal.net.http.frame |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
31 |
* java.net.http/jdk.internal.net.http.hpack |
56093 | 32 |
* @run testng/othervm |
33 |
* -Djdk.internal.httpclient.debug=true |
|
34 |
* -Djdk.httpclient.HttpClient.log=errors,requests,responses,trace |
|
35 |
* ImplicitPushCancel |
|
48523 | 36 |
*/ |
37 |
||
38 |
import java.io.ByteArrayInputStream; |
|
39 |
import java.io.IOException; |
|
40 |
import java.io.InputStream; |
|
41 |
import java.io.OutputStream; |
|
42 |
import java.net.URI; |
|
43 |
import java.util.Map; |
|
44 |
import java.util.Objects; |
|
56019 | 45 |
import java.util.concurrent.CompletableFuture; |
46 |
import java.util.concurrent.ConcurrentHashMap; |
|
47 |
import java.util.concurrent.ConcurrentMap; |
|
48 |
||
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56034
diff
changeset
|
49 |
import java.net.http.HttpClient; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56034
diff
changeset
|
50 |
import java.net.http.HttpRequest; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56034
diff
changeset
|
51 |
import java.net.http.HttpResponse; |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56093
diff
changeset
|
52 |
import java.net.http.HttpResponse.BodyHandlers; |
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56034
diff
changeset
|
53 |
import java.net.http.HttpResponse.PushPromiseHandler; |
56092
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
54 |
import jdk.internal.net.http.common.HttpHeadersImpl; |
48523 | 55 |
import org.testng.annotations.AfterTest; |
56 |
import org.testng.annotations.BeforeTest; |
|
57 |
import org.testng.annotations.Test; |
|
58 |
import static java.nio.charset.StandardCharsets.UTF_8; |
|
59 |
import static org.testng.Assert.assertEquals; |
|
60 |
||
61 |
public class ImplicitPushCancel { |
|
62 |
||
63 |
static Map<String,String> PUSH_PROMISES = Map.of( |
|
64 |
"/x/y/z/1", "the first push promise body", |
|
65 |
"/x/y/z/2", "the second push promise body", |
|
66 |
"/x/y/z/3", "the third push promise body", |
|
67 |
"/x/y/z/4", "the fourth push promise body", |
|
68 |
"/x/y/z/5", "the fifth push promise body", |
|
69 |
"/x/y/z/6", "the sixth push promise body", |
|
70 |
"/x/y/z/7", "the seventh push promise body", |
|
71 |
"/x/y/z/8", "the eight push promise body", |
|
72 |
"/x/y/z/9", "the ninth push promise body" |
|
73 |
); |
|
74 |
static final String MAIN_RESPONSE_BODY = "the main response body"; |
|
75 |
||
76 |
Http2TestServer server; |
|
77 |
URI uri; |
|
78 |
||
79 |
@BeforeTest |
|
80 |
public void setup() throws Exception { |
|
81 |
server = new Http2TestServer(false, 0); |
|
82 |
Http2Handler handler = new ServerPushHandler(MAIN_RESPONSE_BODY, |
|
83 |
PUSH_PROMISES); |
|
84 |
server.addHandler(handler, "/"); |
|
85 |
server.start(); |
|
86 |
int port = server.getAddress().getPort(); |
|
87 |
System.err.println("Server listening on port " + port); |
|
56265
ec34ae013fbe
http-client-branch: tests should bind to the loopback only
chegar
parents:
56167
diff
changeset
|
88 |
uri = new URI("http://localhost:" + port + "/foo/a/b/c"); |
48523 | 89 |
} |
90 |
||
91 |
@AfterTest |
|
92 |
public void teardown() { |
|
93 |
server.stop(); |
|
94 |
} |
|
95 |
||
96 |
static final <T> HttpResponse<T> assert200ResponseCode(HttpResponse<T> response) { |
|
97 |
assertEquals(response.statusCode(), 200); |
|
98 |
return response; |
|
99 |
} |
|
100 |
||
101 |
/* |
|
102 |
* With a handler not capable of accepting push promises, then all push |
|
103 |
* promises should be rejected / cancelled, without interfering with the |
|
104 |
* main response. |
|
105 |
*/ |
|
106 |
@Test |
|
107 |
public void test() throws Exception { |
|
108 |
HttpClient client = HttpClient.newHttpClient(); |
|
109 |
||
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56093
diff
changeset
|
110 |
client.sendAsync(HttpRequest.newBuilder(uri).build(), BodyHandlers.ofString()) |
48523 | 111 |
.thenApply(ImplicitPushCancel::assert200ResponseCode) |
112 |
.thenApply(HttpResponse::body) |
|
113 |
.thenAccept(body -> body.equals(MAIN_RESPONSE_BODY)) |
|
114 |
.join(); |
|
115 |
||
56019 | 116 |
ConcurrentMap<HttpRequest, CompletableFuture<HttpResponse<String>>> promises |
117 |
= new ConcurrentHashMap<>(); |
|
118 |
PushPromiseHandler<String> pph = PushPromiseHandler |
|
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56093
diff
changeset
|
119 |
.of((r) -> BodyHandlers.ofString(), promises); |
56019 | 120 |
HttpResponse<String> main = client.sendAsync( |
48523 | 121 |
HttpRequest.newBuilder(uri).build(), |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56093
diff
changeset
|
122 |
BodyHandlers.ofString(), |
56019 | 123 |
pph) |
124 |
.join(); |
|
48523 | 125 |
|
56019 | 126 |
promises.entrySet().stream().forEach(e -> System.out.println(e.getKey() + ":" + e.getValue().join().body())); |
48523 | 127 |
|
56019 | 128 |
promises.putIfAbsent(main.request(), CompletableFuture.completedFuture(main)); |
129 |
promises.entrySet().stream().forEach(entry -> { |
|
48523 | 130 |
HttpRequest request = entry.getKey(); |
131 |
HttpResponse<String> response = entry.getValue().join(); |
|
132 |
assertEquals(response.statusCode(), 200); |
|
133 |
if (PUSH_PROMISES.containsKey(request.uri().getPath())) { |
|
134 |
assertEquals(response.body(), PUSH_PROMISES.get(request.uri().getPath())); |
|
135 |
} else { |
|
136 |
assertEquals(response.body(), MAIN_RESPONSE_BODY); |
|
137 |
} |
|
138 |
||
139 |
} ); |
|
140 |
} |
|
141 |
||
142 |
// --- server push handler --- |
|
143 |
static class ServerPushHandler implements Http2Handler { |
|
144 |
||
145 |
private final String mainResponseBody; |
|
146 |
private final Map<String,String> promises; |
|
147 |
||
148 |
public ServerPushHandler(String mainResponseBody, |
|
149 |
Map<String,String> promises) |
|
150 |
throws Exception |
|
151 |
{ |
|
152 |
Objects.requireNonNull(promises); |
|
153 |
this.mainResponseBody = mainResponseBody; |
|
154 |
this.promises = promises; |
|
155 |
} |
|
156 |
||
157 |
public void handle(Http2TestExchange exchange) throws IOException { |
|
158 |
System.err.println("Server: handle " + exchange); |
|
159 |
try (InputStream is = exchange.getRequestBody()) { |
|
160 |
is.readAllBytes(); |
|
161 |
} |
|
162 |
||
163 |
if (exchange.serverPushAllowed()) { |
|
164 |
pushPromises(exchange); |
|
165 |
} |
|
166 |
||
167 |
// response data for the main response |
|
168 |
try (OutputStream os = exchange.getResponseBody()) { |
|
169 |
byte[] bytes = mainResponseBody.getBytes(UTF_8); |
|
170 |
exchange.sendResponseHeaders(200, bytes.length); |
|
171 |
os.write(bytes); |
|
172 |
} |
|
173 |
} |
|
174 |
||
175 |
private void pushPromises(Http2TestExchange exchange) throws IOException { |
|
176 |
URI requestURI = exchange.getRequestURI(); |
|
177 |
for (Map.Entry<String,String> promise : promises.entrySet()) { |
|
178 |
URI uri = requestURI.resolve(promise.getKey()); |
|
179 |
InputStream is = new ByteArrayInputStream(promise.getValue().getBytes(UTF_8)); |
|
180 |
HttpHeadersImpl headers = new HttpHeadersImpl(); |
|
181 |
exchange.serverPush(uri, headers, is); |
|
182 |
} |
|
183 |
System.err.println("Server: All pushes sent"); |
|
184 |
} |
|
185 |
} |
|
186 |
} |
|
187 |