author | chegar |
Wed, 07 Feb 2018 21:45:37 +0000 | |
branch | http-client-branch |
changeset 56092 | fd85b2bf2b0d |
parent 56089 | 42208b2f224e |
child 56093 | 22d94c4a3641 |
permissions | -rw-r--r-- |
37720 | 1 |
/* |
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
2 |
* Copyright (c) 2015, 2018, 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 |
56092
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
30 |
* java.net.http/jdk.internal.net.http.common |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
31 |
* java.net.http/jdk.internal.net.http.frame |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
32 |
* java.net.http/jdk.internal.net.http.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.*; |
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56037
diff
changeset
|
40 |
import java.net.http.*; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56037
diff
changeset
|
41 |
import java.net.http.HttpResponse.BodyHandler; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56037
diff
changeset
|
42 |
import java.net.http.HttpResponse.BodySubscriber; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56037
diff
changeset
|
43 |
import java.net.http.HttpResponse.PushPromiseHandler; |
37720 | 44 |
import java.util.*; |
45 |
import java.util.concurrent.*; |
|
56010 | 46 |
import java.util.function.Consumer; |
47 |
import java.util.function.UnaryOperator; |
|
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; |
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56037
diff
changeset
|
52 |
import static java.net.http.HttpResponse.BodyHandler.asByteArrayConsumer; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56037
diff
changeset
|
53 |
import static java.net.http.HttpResponse.BodyHandler.asFile; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56037
diff
changeset
|
54 |
import static java.net.http.HttpResponse.BodyHandler.asString; |
56010 | 55 |
import static org.testng.Assert.*; |
56 |
||
37720 | 57 |
|
58 |
public class ServerPush { |
|
59 |
||
60 |
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
|
61 |
static final int FILE_SIZE = 512 * 1024 + 343; |
37720 | 62 |
|
63 |
static Path tempFile; |
|
64 |
||
56010 | 65 |
Http2TestServer server; |
66 |
URI uri; |
|
37720 | 67 |
|
56010 | 68 |
@BeforeTest |
69 |
public void setup() throws Exception { |
|
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
70 |
tempFile = TestUtil.getAFile(FILE_SIZE); |
56010 | 71 |
server = new Http2TestServer(false, 0); |
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
72 |
server.addHandler(new PushHandler(tempFile, LOOPS), "/"); |
56010 | 73 |
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
|
74 |
|
56010 | 75 |
System.err.println("Server listening on port " + server.getAddress().getPort()); |
76 |
server.start(); |
|
77 |
int port = server.getAddress().getPort(); |
|
78 |
uri = new URI("http://127.0.0.1:" + port + "/foo/a/b/c"); |
|
79 |
} |
|
37720 | 80 |
|
56010 | 81 |
@AfterTest |
82 |
public void teardown() { |
|
83 |
server.stop(); |
|
84 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
85 |
|
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
86 |
// Test 1 - custom written push promise handler, everything as a String |
56010 | 87 |
@Test |
88 |
public void testTypeString() throws Exception { |
|
89 |
String tempFileAsString = new String(Files.readAllBytes(tempFile), UTF_8); |
|
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
90 |
ConcurrentMap<HttpRequest, CompletableFuture<HttpResponse<String>>> |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
91 |
resultMap = new ConcurrentHashMap<>(); |
56010 | 92 |
|
93 |
PushPromiseHandler<String> pph = (initial, pushRequest, acceptor) -> { |
|
94 |
BodyHandler<String> s = BodyHandler.asString(UTF_8); |
|
95 |
CompletableFuture<HttpResponse<String>> cf = acceptor.apply(s); |
|
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
96 |
resultMap.put(pushRequest, cf); |
56010 | 97 |
}; |
98 |
||
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
99 |
HttpClient client = HttpClient.newHttpClient(); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
100 |
HttpRequest request = HttpRequest.newBuilder(uri).GET().build(); |
56010 | 101 |
CompletableFuture<HttpResponse<String>> cf = |
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
102 |
client.sendAsync(request, asString(UTF_8), pph); |
56010 | 103 |
cf.join(); |
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
104 |
resultMap.put(request, cf); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
105 |
System.err.println("results.size: " + resultMap.size()); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
106 |
for (HttpRequest r : resultMap.keySet()) { |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
107 |
HttpResponse<String> response = resultMap.get(r).join(); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
108 |
assertEquals(response.statusCode(), 200); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
109 |
assertEquals(response.body(), tempFileAsString); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
110 |
} |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
111 |
assertEquals(resultMap.size(), LOOPS + 1); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
112 |
} |
56010 | 113 |
|
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
114 |
// Test 2 - of(...) populating the given Map, everything as a String |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
115 |
@Test |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
116 |
public void testTypeStringOfMap() throws Exception { |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
117 |
String tempFileAsString = new String(Files.readAllBytes(tempFile), UTF_8); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
118 |
ConcurrentMap<HttpRequest, CompletableFuture<HttpResponse<String>>> |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
119 |
resultMap = new ConcurrentHashMap<>(); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
120 |
|
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
121 |
PushPromiseHandler<String> pph = |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
122 |
PushPromiseHandler.of(pushPromise -> asString(UTF_8), |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
123 |
resultMap); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
124 |
|
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
125 |
HttpClient client = HttpClient.newHttpClient(); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
126 |
HttpRequest request = HttpRequest.newBuilder(uri).GET().build(); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
127 |
CompletableFuture<HttpResponse<String>> cf = |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
128 |
client.sendAsync(request, asString(UTF_8), pph); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
129 |
cf.join(); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
130 |
resultMap.put(request, cf); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
131 |
System.err.println("results.size: " + resultMap.size()); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
132 |
for (HttpRequest r : resultMap.keySet()) { |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
133 |
HttpResponse<String> response = resultMap.get(r).join(); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
134 |
assertEquals(response.statusCode(), 200); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
135 |
assertEquals(response.body(), tempFileAsString); |
56010 | 136 |
} |
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
137 |
assertEquals(resultMap.size(), LOOPS + 1); |
56010 | 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 |
} |
|
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
151 |
return asFile(path); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
152 |
} |
56010 | 153 |
|
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
154 |
// Test 3 - custom written push promise handler, everything as a Path |
56010 | 155 |
@Test |
156 |
public void testTypePath() throws Exception { |
|
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
157 |
String tempFileAsString = new String(Files.readAllBytes(tempFile), UTF_8); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
158 |
ConcurrentMap<HttpRequest, CompletableFuture<HttpResponse<Path>>> resultsMap |
56010 | 159 |
= new ConcurrentHashMap<>(); |
160 |
||
161 |
PushPromiseHandler<Path> pushPromiseHandler = (initial, pushRequest, acceptor) -> { |
|
162 |
BodyHandler<Path> pp = requestToPath(pushRequest); |
|
163 |
CompletableFuture<HttpResponse<Path>> cf = acceptor.apply(pp); |
|
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
164 |
resultsMap.put(pushRequest, cf); |
56010 | 165 |
}; |
166 |
||
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
167 |
HttpClient client = HttpClient.newHttpClient(); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
168 |
HttpRequest request = HttpRequest.newBuilder(uri).GET().build(); |
56010 | 169 |
CompletableFuture<HttpResponse<Path>> cf = |
170 |
client.sendAsync(request, requestToPath(request), pushPromiseHandler); |
|
171 |
cf.join(); |
|
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
172 |
resultsMap.put(request, cf); |
56010 | 173 |
|
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
174 |
for (HttpRequest r : resultsMap.keySet()) { |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
175 |
HttpResponse<Path> response = resultsMap.get(r).join(); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
176 |
assertEquals(response.statusCode(), 200); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
177 |
String fileAsString = new String(Files.readAllBytes(response.body()), UTF_8); |
56010 | 178 |
assertEquals(fileAsString, tempFileAsString); |
179 |
} |
|
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
180 |
assertEquals(resultsMap.size(), LOOPS + 1); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
181 |
} |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
182 |
|
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
183 |
// Test 4 - of(...) populating the given Map, everything as a Path |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
184 |
@Test |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
185 |
public void testTypePathOfMap() throws Exception { |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
186 |
String tempFileAsString = new String(Files.readAllBytes(tempFile), UTF_8); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
187 |
ConcurrentMap<HttpRequest, CompletableFuture<HttpResponse<Path>>> resultsMap |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
188 |
= new ConcurrentHashMap<>(); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
189 |
|
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
190 |
PushPromiseHandler<Path> pushPromiseHandler = |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
191 |
PushPromiseHandler.of(pushRequest -> requestToPath(pushRequest), |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
192 |
resultsMap); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
193 |
|
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
194 |
HttpClient client = HttpClient.newHttpClient(); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
195 |
HttpRequest request = HttpRequest.newBuilder(uri).GET().build(); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
196 |
CompletableFuture<HttpResponse<Path>> cf = |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
197 |
client.sendAsync(request, requestToPath(request), pushPromiseHandler); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
198 |
cf.join(); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
199 |
resultsMap.put(request, cf); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
200 |
|
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
201 |
for (HttpRequest r : resultsMap.keySet()) { |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
202 |
HttpResponse<Path> response = resultsMap.get(r).join(); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
203 |
assertEquals(response.statusCode(), 200); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
204 |
String fileAsString = new String(Files.readAllBytes(response.body()), UTF_8); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
205 |
assertEquals(fileAsString, tempFileAsString); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
206 |
} |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
207 |
assertEquals(resultsMap.size(), LOOPS + 1); |
56010 | 208 |
} |
209 |
||
210 |
// --- Consumer<byte[]> --- |
|
211 |
||
212 |
static class ByteArrayConsumer implements Consumer<Optional<byte[]>> { |
|
213 |
volatile List<byte[]> listByteArrays = new ArrayList<>(); |
|
214 |
volatile byte[] accumulatedBytes; |
|
215 |
||
216 |
public byte[] getAccumulatedBytes() { return accumulatedBytes; } |
|
217 |
||
218 |
@Override |
|
219 |
public void accept(Optional<byte[]> optionalBytes) { |
|
220 |
assert accumulatedBytes == null; |
|
221 |
if (!optionalBytes.isPresent()) { |
|
222 |
int size = listByteArrays.stream().mapToInt(ba -> ba.length).sum(); |
|
223 |
ByteBuffer bb = ByteBuffer.allocate(size); |
|
224 |
listByteArrays.stream().forEach(ba -> bb.put(ba)); |
|
225 |
accumulatedBytes = bb.array(); |
|
226 |
} else { |
|
227 |
listByteArrays.add(optionalBytes.get()); |
|
228 |
} |
|
229 |
} |
|
230 |
} |
|
231 |
||
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
232 |
// Test 5 - custom written handler, everything as a consumer of optional byte[] |
56010 | 233 |
@Test |
234 |
public void testTypeByteArrayConsumer() throws Exception { |
|
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
235 |
String tempFileAsString = new String(Files.readAllBytes(tempFile), UTF_8); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
236 |
ConcurrentMap<HttpRequest, CompletableFuture<HttpResponse<Void>>> resultsMap |
56010 | 237 |
= new ConcurrentHashMap<>(); |
238 |
Map<HttpRequest,ByteArrayConsumer> byteArrayConsumerMap |
|
239 |
= new ConcurrentHashMap<>(); |
|
240 |
||
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
241 |
HttpClient client = HttpClient.newHttpClient(); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
242 |
HttpRequest request = HttpRequest.newBuilder(uri).GET().build(); |
56010 | 243 |
ByteArrayConsumer bac = new ByteArrayConsumer(); |
244 |
byteArrayConsumerMap.put(request, bac); |
|
245 |
||
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
246 |
PushPromiseHandler<Void> pushPromiseHandler = (initial, pushRequest, acceptor) -> { |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
247 |
CompletableFuture<HttpResponse<Void>> cf = acceptor.apply( |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
248 |
(statusCode, headers) -> { |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
249 |
ByteArrayConsumer bc = new ByteArrayConsumer(); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
250 |
byteArrayConsumerMap.put(pushRequest, bc); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
251 |
return BodySubscriber.asByteArrayConsumer(bc); } ); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
252 |
resultsMap.put(pushRequest, cf); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
253 |
}; |
56010 | 254 |
|
255 |
CompletableFuture<HttpResponse<Void>> cf = |
|
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
256 |
client.sendAsync(request, asByteArrayConsumer(bac), pushPromiseHandler); |
56010 | 257 |
cf.join(); |
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
258 |
resultsMap.put(request, cf); |
56010 | 259 |
|
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
260 |
for (HttpRequest r : resultsMap.keySet()) { |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
261 |
HttpResponse<Void> response = resultsMap.get(r).join(); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
262 |
assertEquals(response.statusCode(), 200); |
56010 | 263 |
byte[] ba = byteArrayConsumerMap.get(r).getAccumulatedBytes(); |
264 |
String result = new String(ba, UTF_8); |
|
265 |
assertEquals(result, tempFileAsString); |
|
266 |
} |
|
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
267 |
assertEquals(resultsMap.size(), LOOPS + 1); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
268 |
} |
56010 | 269 |
|
56037
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
270 |
// Test 6 - of(...) populating the given Map, everything as a consumer of optional byte[] |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
271 |
@Test |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
272 |
public void testTypeByteArrayConsumerOfMap() throws Exception { |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
273 |
String tempFileAsString = new String(Files.readAllBytes(tempFile), UTF_8); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
274 |
ConcurrentMap<HttpRequest, CompletableFuture<HttpResponse<Void>>> resultsMap |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
275 |
= new ConcurrentHashMap<>(); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
276 |
Map<HttpRequest,ByteArrayConsumer> byteArrayConsumerMap |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
277 |
= new ConcurrentHashMap<>(); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
278 |
|
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
279 |
HttpClient client = HttpClient.newHttpClient(); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
280 |
HttpRequest request = HttpRequest.newBuilder(uri).GET().build(); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
281 |
ByteArrayConsumer bac = new ByteArrayConsumer(); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
282 |
byteArrayConsumerMap.put(request, bac); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
283 |
|
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
284 |
PushPromiseHandler<Void> pushPromiseHandler = |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
285 |
PushPromiseHandler.of( |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
286 |
pushRequest -> { |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
287 |
ByteArrayConsumer bc = new ByteArrayConsumer(); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
288 |
byteArrayConsumerMap.put(pushRequest, bc); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
289 |
return BodyHandler.asByteArrayConsumer(bc); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
290 |
}, |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
291 |
resultsMap); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
292 |
|
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
293 |
CompletableFuture<HttpResponse<Void>> cf = |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
294 |
client.sendAsync(request, asByteArrayConsumer(bac), pushPromiseHandler); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
295 |
cf.join(); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
296 |
resultsMap.put(request, cf); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
297 |
|
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
298 |
for (HttpRequest r : resultsMap.keySet()) { |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
299 |
HttpResponse<Void> response = resultsMap.get(r).join(); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
300 |
assertEquals(response.statusCode(), 200); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
301 |
byte[] ba = byteArrayConsumerMap.get(r).getAccumulatedBytes(); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
302 |
String result = new String(ba, UTF_8); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
303 |
assertEquals(result, tempFileAsString); |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
304 |
} |
c026ce4a1a81
http-client-branch: clean up and expand push promise tests
chegar
parents:
56034
diff
changeset
|
305 |
assertEquals(resultsMap.size(), LOOPS + 1); |
56010 | 306 |
} |
37720 | 307 |
} |