author | dfuchs |
Fri, 19 Jan 2018 16:48:12 +0000 | |
branch | http-client-branch |
changeset 56034 | 43b531ed872b |
parent 56010 | 782b2f2d1e76 |
child 56037 | c026ce4a1a81 |
permissions | -rw-r--r-- |
37720 | 1 |
/* |
48083 | 2 |
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. |
37720 | 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 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
26 |
* @bug 8087112 8159814 |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
27 |
* @library /lib/testlibrary server |
37720 | 28 |
* @build jdk.testlibrary.SimpleSSLContext |
48083 | 29 |
* @modules java.base/sun.net.www.http |
30 |
* jdk.incubator.httpclient/jdk.incubator.http.internal.common |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
31 |
* jdk.incubator.httpclient/jdk.incubator.http.internal.frame |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
32 |
* jdk.incubator.httpclient/jdk.incubator.http.internal.hpack |
56010 | 33 |
* @run testng/othervm -Djdk.httpclient.HttpClient.log=errors,requests,responses ServerPush |
37720 | 34 |
*/ |
35 |
||
36 |
import java.io.*; |
|
37 |
import java.net.*; |
|
56010 | 38 |
import java.nio.ByteBuffer; |
37720 | 39 |
import java.nio.file.*; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
40 |
import jdk.incubator.http.*; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
41 |
import jdk.incubator.http.HttpResponse.BodyHandler; |
56010 | 42 |
import jdk.incubator.http.HttpResponse.PushPromiseHandler; |
37720 | 43 |
import java.util.*; |
44 |
import java.util.concurrent.*; |
|
56010 | 45 |
import java.util.function.Consumer; |
46 |
import java.util.function.UnaryOperator; |
|
47 |
||
48 |
import org.testng.annotations.AfterTest; |
|
49 |
import org.testng.annotations.BeforeTest; |
|
37720 | 50 |
import org.testng.annotations.Test; |
56010 | 51 |
import static java.nio.charset.StandardCharsets.UTF_8; |
52 |
import static org.testng.Assert.*; |
|
53 |
||
37720 | 54 |
|
55 |
public class ServerPush { |
|
56 |
||
57 |
static final int LOOPS = 13; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
58 |
static final int FILE_SIZE = 512 * 1024 + 343; |
37720 | 59 |
|
60 |
static Path tempFile; |
|
61 |
||
56010 | 62 |
Http2TestServer server; |
63 |
URI uri; |
|
37720 | 64 |
|
56010 | 65 |
@BeforeTest |
66 |
public void setup() throws Exception { |
|
67 |
server = new Http2TestServer(false, 0); |
|
68 |
server.addHandler(new PushHandler(FILE_SIZE, LOOPS), "/"); |
|
69 |
tempFile = TestUtil.getAFile(FILE_SIZE); |
|
70 |
System.out.println("Using temp file:" + tempFile); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
71 |
|
56010 | 72 |
System.err.println("Server listening on port " + server.getAddress().getPort()); |
73 |
server.start(); |
|
74 |
int port = server.getAddress().getPort(); |
|
75 |
uri = new URI("http://127.0.0.1:" + port + "/foo/a/b/c"); |
|
76 |
} |
|
37720 | 77 |
|
56010 | 78 |
@AfterTest |
79 |
public void teardown() { |
|
80 |
server.stop(); |
|
81 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
82 |
|
56010 | 83 |
static final UnaryOperator<HttpResponse<?>> |
84 |
assert200ResponseCode = (response) -> { |
|
85 |
assertEquals(response.statusCode(), 200); |
|
86 |
return response; |
|
87 |
}; |
|
88 |
||
89 |
interface Peeker<T> extends UnaryOperator<T> { |
|
90 |
void peek(T t); |
|
91 |
||
92 |
default T apply(T t) |
|
93 |
{ |
|
94 |
peek(t); |
|
95 |
return t; |
|
37720 | 96 |
} |
97 |
} |
|
56010 | 98 |
|
99 |
@Test |
|
100 |
public void testTypeString() throws Exception { |
|
101 |
// use multi-level path |
|
102 |
HttpRequest request = HttpRequest.newBuilder(uri).GET().build(); |
|
103 |
||
104 |
String tempFileAsString = new String(Files.readAllBytes(tempFile), UTF_8); |
|
105 |
||
106 |
// Attempt 2 |
|
107 |
HttpClient client = HttpClient.newBuilder() |
|
108 |
.version(HttpClient.Version.HTTP_2) |
|
109 |
.build(); |
|
110 |
||
111 |
ConcurrentMap<HttpRequest, CompletableFuture<HttpResponse<String>>> results = new ConcurrentHashMap<>(); |
|
112 |
||
113 |
||
114 |
// Example 2 - of(...) building your own Map, everything as a String |
|
115 |
PushPromiseHandler<String> pph = (initial, pushRequest, acceptor) -> { |
|
116 |
BodyHandler<String> s = BodyHandler.asString(UTF_8); |
|
117 |
CompletableFuture<HttpResponse<String>> cf = acceptor.apply(s); |
|
118 |
results.put(pushRequest, cf); |
|
119 |
}; |
|
120 |
||
121 |
CompletableFuture<HttpResponse<String>> cf = |
|
122 |
client.sendAsync(request, BodyHandler.asString(), pph); |
|
123 |
cf.join(); |
|
124 |
results.put(request, cf); |
|
125 |
||
126 |
System.err.println(results.size()); |
|
127 |
Set<HttpRequest> requests = results.keySet(); |
|
128 |
||
129 |
System.err.println("results.size: " + results.size()); |
|
130 |
for (HttpRequest r : requests) { |
|
131 |
String result = results.get(r).get().body(); |
|
132 |
if (!result.equals(tempFileAsString)) { |
|
133 |
System.err.println("Got [" + result + ", expected [" + tempFileAsString + "]"); |
|
134 |
} |
|
135 |
} |
|
136 |
if (requests.size() != LOOPS + 1) |
|
137 |
throw new RuntimeException("Some results missing, expected:" + LOOPS + 1 + ", got:" + results.size()); |
|
138 |
} |
|
139 |
||
140 |
// --- Path --- |
|
141 |
||
142 |
static final Path dir = Paths.get(".", "serverPush"); |
|
143 |
static BodyHandler<Path> requestToPath(HttpRequest req) { |
|
144 |
URI u = req.uri(); |
|
145 |
Path path = Paths.get(dir.toString(), u.getPath()); |
|
146 |
try { |
|
147 |
Files.createDirectories(path.getParent()); |
|
148 |
} catch (IOException ee) { |
|
149 |
throw new UncheckedIOException(ee); |
|
150 |
} |
|
151 |
return BodyHandler.asFile(path); |
|
152 |
}; |
|
153 |
||
154 |
@Test |
|
155 |
public void testTypePath() throws Exception { |
|
156 |
HttpClient client = HttpClient.newHttpClient(); |
|
157 |
HttpRequest request = HttpRequest.newBuilder(uri).GET().build(); |
|
158 |
||
159 |
ConcurrentMap<HttpRequest, CompletableFuture<HttpResponse<Path>>> map |
|
160 |
= new ConcurrentHashMap<>(); |
|
161 |
||
162 |
// Example 4 - of(...) building your own Map, everything as a Path |
|
163 |
PushPromiseHandler<Path> pushPromiseHandler = (initial, pushRequest, acceptor) -> { |
|
164 |
BodyHandler<Path> pp = requestToPath(pushRequest); |
|
165 |
CompletableFuture<HttpResponse<Path>> cf = acceptor.apply(pp); |
|
166 |
map.put(pushRequest, cf); |
|
167 |
}; |
|
168 |
||
169 |
CompletableFuture<HttpResponse<Path>> cf = |
|
170 |
client.sendAsync(request, requestToPath(request), pushPromiseHandler); |
|
171 |
cf.join(); |
|
172 |
map.put(request, cf); |
|
173 |
||
174 |
System.err.println("map.size: " + map.size()); |
|
175 |
for (HttpRequest r : map.keySet()) { |
|
176 |
Path path = map.get(r).get().body(); |
|
177 |
String fileAsString = new String(Files.readAllBytes(path), UTF_8); |
|
178 |
String tempFileAsString = new String(Files.readAllBytes(tempFile), UTF_8); |
|
179 |
assertEquals(fileAsString, tempFileAsString); |
|
180 |
} |
|
181 |
assertEquals(map.size(), LOOPS + 1); |
|
182 |
} |
|
183 |
||
184 |
// --- Consumer<byte[]> --- |
|
185 |
||
186 |
static class ByteArrayConsumer implements Consumer<Optional<byte[]>> { |
|
187 |
volatile List<byte[]> listByteArrays = new ArrayList<>(); |
|
188 |
volatile byte[] accumulatedBytes; |
|
189 |
||
190 |
public byte[] getAccumulatedBytes() { return accumulatedBytes; } |
|
191 |
||
192 |
@Override |
|
193 |
public void accept(Optional<byte[]> optionalBytes) { |
|
194 |
assert accumulatedBytes == null; |
|
195 |
if (!optionalBytes.isPresent()) { |
|
196 |
int size = listByteArrays.stream().mapToInt(ba -> ba.length).sum(); |
|
197 |
ByteBuffer bb = ByteBuffer.allocate(size); |
|
198 |
listByteArrays.stream().forEach(ba -> bb.put(ba)); |
|
199 |
accumulatedBytes = bb.array(); |
|
200 |
} else { |
|
201 |
listByteArrays.add(optionalBytes.get()); |
|
202 |
} |
|
203 |
} |
|
204 |
} |
|
205 |
||
206 |
@Test |
|
207 |
public void testTypeByteArrayConsumer() throws Exception { |
|
208 |
HttpClient client = HttpClient.newHttpClient(); |
|
209 |
HttpRequest request = HttpRequest.newBuilder(uri).GET().build(); |
|
210 |
||
211 |
ConcurrentMap<HttpRequest, CompletableFuture<HttpResponse<Void>>> resultMap |
|
212 |
= new ConcurrentHashMap<>(); |
|
213 |
Map<HttpRequest,ByteArrayConsumer> byteArrayConsumerMap |
|
214 |
= new ConcurrentHashMap<>(); |
|
215 |
||
216 |
ByteArrayConsumer bac = new ByteArrayConsumer(); |
|
217 |
byteArrayConsumerMap.put(request, bac); |
|
218 |
||
219 |
// Example 5 - withXXX and everything as a consumer of optional byte[] |
|
220 |
PushPromiseHandler<Void> pushPromiseHandler = |
|
56034
43b531ed872b
http-client-branch: renamed PushPromiseHandler.withPushPromises into PushPromiseHandler.of
dfuchs
parents:
56010
diff
changeset
|
221 |
PushPromiseHandler.of(pushRequest -> { |
56010 | 222 |
ByteArrayConsumer bc = new ByteArrayConsumer(); |
223 |
byteArrayConsumerMap.put(pushRequest, bc); |
|
224 |
return BodyHandler.asByteArrayConsumer(bc); |
|
225 |
}, |
|
226 |
resultMap); |
|
227 |
||
228 |
CompletableFuture<HttpResponse<Void>> cf = |
|
229 |
client.sendAsync(request, BodyHandler.asByteArrayConsumer(bac), pushPromiseHandler); |
|
230 |
cf.join(); |
|
231 |
resultMap.put(request, cf); |
|
232 |
||
233 |
System.err.println("map.size: " + resultMap.size()); |
|
234 |
for (HttpRequest r : resultMap.keySet()) { |
|
235 |
resultMap.get(r).join(); |
|
236 |
byte[] ba = byteArrayConsumerMap.get(r).getAccumulatedBytes(); |
|
237 |
String result = new String(ba, UTF_8); |
|
238 |
System.out.println("HEGO result=" + result); |
|
239 |
System.out.println("HEGO result.length=" + result.length()); |
|
240 |
System.err.printf("%s -> %s\n", r.uri().toString(), result); |
|
241 |
String tempFileAsString = new String(Files.readAllBytes(tempFile), UTF_8); |
|
242 |
System.out.println("HEGO tempFileAsString=" + tempFileAsString); |
|
243 |
System.out.println("HEGO tempFileAsString.length=" + tempFileAsString.length()); |
|
244 |
assertEquals(result, tempFileAsString); |
|
245 |
} |
|
246 |
||
247 |
assertEquals(resultMap.size(), LOOPS + 1); |
|
248 |
} |
|
37720 | 249 |
} |