Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/httpclient/FlowAdapterSubscriberTest.java
41149 views
1
/*
2
* Copyright (c) 2017, 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
import java.io.ByteArrayOutputStream;
25
import java.io.IOException;
26
import java.io.InputStream;
27
import java.io.OutputStream;
28
import java.io.UncheckedIOException;
29
import java.net.InetAddress;
30
import java.net.InetSocketAddress;
31
import java.net.URI;
32
import java.nio.ByteBuffer;
33
import java.util.Collection;
34
import java.util.List;
35
import java.util.concurrent.CompletableFuture;
36
import java.util.concurrent.Flow;
37
import java.util.concurrent.Flow.Subscriber;
38
import java.util.function.Function;
39
import java.util.function.Supplier;
40
import com.sun.net.httpserver.HttpExchange;
41
import com.sun.net.httpserver.HttpHandler;
42
import com.sun.net.httpserver.HttpServer;
43
import com.sun.net.httpserver.HttpsConfigurator;
44
import com.sun.net.httpserver.HttpsServer;
45
import java.net.http.HttpClient;
46
import java.net.http.HttpRequest;
47
import java.net.http.HttpRequest.BodyPublishers;
48
import java.net.http.HttpResponse;
49
import java.net.http.HttpResponse.BodyHandlers;
50
import java.net.http.HttpResponse.BodySubscribers;
51
import jdk.test.lib.net.SimpleSSLContext;
52
import org.testng.annotations.AfterTest;
53
import org.testng.annotations.BeforeTest;
54
import org.testng.annotations.DataProvider;
55
import org.testng.annotations.Test;
56
import javax.net.ssl.SSLContext;
57
import static java.nio.charset.StandardCharsets.UTF_8;
58
import static org.testng.Assert.assertEquals;
59
import static org.testng.Assert.assertThrows;
60
import static org.testng.Assert.assertTrue;
61
62
/*
63
* @test
64
* @summary Basic tests for Flow adapter Subscribers
65
* @modules java.base/sun.net.www.http
66
* java.net.http/jdk.internal.net.http.common
67
* java.net.http/jdk.internal.net.http.frame
68
* java.net.http/jdk.internal.net.http.hpack
69
* java.logging
70
* jdk.httpserver
71
* @library /test/lib http2/server
72
* @build Http2TestServer
73
* @build jdk.test.lib.net.SimpleSSLContext
74
* @run testng/othervm -Djdk.internal.httpclient.debug=true FlowAdapterSubscriberTest
75
*/
76
77
public class FlowAdapterSubscriberTest {
78
79
SSLContext sslContext;
80
HttpServer httpTestServer; // HTTP/1.1 [ 4 servers ]
81
HttpsServer httpsTestServer; // HTTPS/1.1
82
Http2TestServer http2TestServer; // HTTP/2 ( h2c )
83
Http2TestServer https2TestServer; // HTTP/2 ( h2 )
84
String httpURI;
85
String httpsURI;
86
String http2URI;
87
String https2URI;
88
static final long start = System.nanoTime();
89
public static String now() {
90
long now = System.nanoTime() - start;
91
long secs = now / 1000_000_000;
92
long mill = (now % 1000_000_000) / 1000_000;
93
long nan = now % 1000_000;
94
return String.format("[%d s, %d ms, %d ns] ", secs, mill, nan);
95
}
96
97
@DataProvider(name = "uris")
98
public Object[][] variants() {
99
return new Object[][]{
100
{ httpURI },
101
{ httpsURI },
102
{ http2URI },
103
{ https2URI },
104
};
105
}
106
107
static final Class<NullPointerException> NPE = NullPointerException.class;
108
109
@Test
110
public void testNull() {
111
System.out.printf(now() + "testNull() starting%n");
112
assertThrows(NPE, () -> BodyHandlers.fromSubscriber(null));
113
assertThrows(NPE, () -> BodyHandlers.fromSubscriber(null, Function.identity()));
114
assertThrows(NPE, () -> BodyHandlers.fromSubscriber(new ListSubscriber(), null));
115
assertThrows(NPE, () -> BodyHandlers.fromSubscriber(null, null));
116
117
assertThrows(NPE, () -> BodySubscribers.fromSubscriber(null));
118
assertThrows(NPE, () -> BodySubscribers.fromSubscriber(null, Function.identity()));
119
assertThrows(NPE, () -> BodySubscribers.fromSubscriber(new ListSubscriber(), null));
120
assertThrows(NPE, () -> BodySubscribers.fromSubscriber(null, null));
121
122
Subscriber subscriber = BodySubscribers.fromSubscriber(new ListSubscriber());
123
assertThrows(NPE, () -> subscriber.onSubscribe(null));
124
assertThrows(NPE, () -> subscriber.onNext(null));
125
assertThrows(NPE, () -> subscriber.onError(null));
126
}
127
128
// List<ByteBuffer>
129
130
@Test(dataProvider = "uris")
131
void testListWithFinisher(String url) {
132
System.out.printf(now() + "testListWithFinisher(%s) starting%n", url);
133
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
134
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
135
.POST(BodyPublishers.ofString("May the luck of the Irish be with you!")).build();
136
137
ListSubscriber subscriber = new ListSubscriber();
138
HttpResponse<String> response = client.sendAsync(request,
139
BodyHandlers.fromSubscriber(subscriber, Supplier::get)).join();
140
String text = response.body();
141
System.out.println(text);
142
assertEquals(response.statusCode(), 200);
143
assertEquals(text, "May the luck of the Irish be with you!");
144
}
145
146
@Test(dataProvider = "uris")
147
void testListWithoutFinisher(String url) {
148
System.out.printf(now() + "testListWithoutFinisher(%s) starting%n", url);
149
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
150
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
151
.POST(BodyPublishers.ofString("May the luck of the Irish be with you!")).build();
152
153
ListSubscriber subscriber = new ListSubscriber();
154
HttpResponse<Void> response = client.sendAsync(request,
155
BodyHandlers.fromSubscriber(subscriber)).join();
156
String text = subscriber.get();
157
System.out.println(text);
158
assertEquals(response.statusCode(), 200);
159
assertEquals(text, "May the luck of the Irish be with you!");
160
}
161
162
@Test(dataProvider = "uris")
163
void testListWithFinisherBlocking(String url) throws Exception {
164
System.out.printf(now() + "testListWithFinisherBlocking(%s) starting%n", url);
165
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
166
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
167
.POST(BodyPublishers.ofString("May the luck of the Irish be with you!")).build();
168
169
ListSubscriber subscriber = new ListSubscriber();
170
HttpResponse<String> response = client.send(request,
171
BodyHandlers.fromSubscriber(subscriber, Supplier::get));
172
String text = response.body();
173
System.out.println(text);
174
assertEquals(response.statusCode(), 200);
175
assertEquals(text, "May the luck of the Irish be with you!");
176
}
177
178
@Test(dataProvider = "uris")
179
void testListWithoutFinisherBlocking(String url) throws Exception {
180
System.out.printf(now() + "testListWithoutFinisherBlocking(%s) starting%n", url);
181
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
182
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
183
.POST(BodyPublishers.ofString("May the luck of the Irish be with you!")).build();
184
185
ListSubscriber subscriber = new ListSubscriber();
186
HttpResponse<Void> response = client.send(request,
187
BodyHandlers.fromSubscriber(subscriber));
188
String text = subscriber.get();
189
System.out.println(text);
190
assertEquals(response.statusCode(), 200);
191
assertEquals(text, "May the luck of the Irish be with you!");
192
}
193
194
// Collection<ByteBuffer>
195
196
@Test(dataProvider = "uris")
197
void testCollectionWithFinisher(String url) {
198
System.out.printf(now() + "testCollectionWithFinisher(%s) starting%n", url);
199
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
200
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
201
.POST(BodyPublishers.ofString("What's the craic?")).build();
202
203
CollectionSubscriber subscriber = new CollectionSubscriber();
204
HttpResponse<String> response = client.sendAsync(request,
205
BodyHandlers.fromSubscriber(subscriber, CollectionSubscriber::get)).join();
206
String text = response.body();
207
System.out.println(text);
208
assertEquals(response.statusCode(), 200);
209
assertEquals(text, "What's the craic?");
210
}
211
212
@Test(dataProvider = "uris")
213
void testCollectionWithoutFinisher(String url) {
214
System.out.printf(now() + "testCollectionWithoutFinisher(%s) starting%n", url);
215
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
216
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
217
.POST(BodyPublishers.ofString("What's the craic?")).build();
218
219
CollectionSubscriber subscriber = new CollectionSubscriber();
220
HttpResponse<Void> response = client.sendAsync(request,
221
BodyHandlers.fromSubscriber(subscriber)).join();
222
String text = subscriber.get();
223
System.out.println(text);
224
assertEquals(response.statusCode(), 200);
225
assertEquals(text, "What's the craic?");
226
}
227
228
@Test(dataProvider = "uris")
229
void testCollectionWithFinisherBlocking(String url) throws Exception {
230
System.out.printf(now() + "testCollectionWithFinisherBlocking(%s) starting%n", url);
231
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
232
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
233
.POST(BodyPublishers.ofString("What's the craic?")).build();
234
235
CollectionSubscriber subscriber = new CollectionSubscriber();
236
HttpResponse<String> response = client.send(request,
237
BodyHandlers.fromSubscriber(subscriber, CollectionSubscriber::get));
238
String text = response.body();
239
System.out.println(text);
240
assertEquals(response.statusCode(), 200);
241
assertEquals(text, "What's the craic?");
242
}
243
244
@Test(dataProvider = "uris")
245
void testCollectionWithoutFinisheBlocking(String url) throws Exception {
246
System.out.printf(now() + "testCollectionWithoutFinisheBlocking(%s) starting%n", url);
247
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
248
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
249
.POST(BodyPublishers.ofString("What's the craic?")).build();
250
251
CollectionSubscriber subscriber = new CollectionSubscriber();
252
HttpResponse<Void> response = client.send(request,
253
BodyHandlers.fromSubscriber(subscriber));
254
String text = subscriber.get();
255
System.out.println(text);
256
assertEquals(response.statusCode(), 200);
257
assertEquals(text, "What's the craic?");
258
}
259
260
// Iterable<ByteBuffer>
261
262
@Test(dataProvider = "uris")
263
void testIterableWithFinisher(String url) {
264
System.out.printf(now() + "testIterableWithFinisher(%s) starting%n", url);
265
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
266
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
267
.POST(BodyPublishers.ofString("We're sucking diesel now!")).build();
268
269
IterableSubscriber subscriber = new IterableSubscriber();
270
HttpResponse<String> response = client.sendAsync(request,
271
BodyHandlers.fromSubscriber(subscriber, Supplier::get)).join();
272
String text = response.body();
273
System.out.println(text);
274
assertEquals(response.statusCode(), 200);
275
assertEquals(text, "We're sucking diesel now!");
276
}
277
278
@Test(dataProvider = "uris")
279
void testIterableWithoutFinisher(String url) {
280
System.out.printf(now() + "testIterableWithoutFinisher(%s) starting%n", url);
281
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
282
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
283
.POST(BodyPublishers.ofString("We're sucking diesel now!")).build();
284
285
IterableSubscriber subscriber = new IterableSubscriber();
286
HttpResponse<Void> response = client.sendAsync(request,
287
BodyHandlers.fromSubscriber(subscriber)).join();
288
String text = subscriber.get();
289
System.out.println(text);
290
assertEquals(response.statusCode(), 200);
291
assertEquals(text, "We're sucking diesel now!");
292
}
293
294
@Test(dataProvider = "uris")
295
void testIterableWithFinisherBlocking(String url) throws Exception {
296
System.out.printf(now() + "testIterableWithFinisherBlocking(%s) starting%n", url);
297
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
298
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
299
.POST(BodyPublishers.ofString("We're sucking diesel now!")).build();
300
301
IterableSubscriber subscriber = new IterableSubscriber();
302
HttpResponse<String> response = client.send(request,
303
BodyHandlers.fromSubscriber(subscriber, Supplier::get));
304
String text = response.body();
305
System.out.println(text);
306
assertEquals(response.statusCode(), 200);
307
assertEquals(text, "We're sucking diesel now!");
308
}
309
310
@Test(dataProvider = "uris")
311
void testIterableWithoutFinisherBlocking(String url) throws Exception {
312
System.out.printf(now() + "testIterableWithoutFinisherBlocking(%s) starting%n", url);
313
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
314
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
315
.POST(BodyPublishers.ofString("We're sucking diesel now!")).build();
316
317
IterableSubscriber subscriber = new IterableSubscriber();
318
HttpResponse<Void> response = client.send(request,
319
BodyHandlers.fromSubscriber(subscriber));
320
String text = subscriber.get();
321
System.out.println(text);
322
assertEquals(response.statusCode(), 200);
323
assertEquals(text, "We're sucking diesel now!");
324
}
325
326
// Subscriber<Object>
327
328
@Test(dataProvider = "uris")
329
void testObjectWithFinisher(String url) {
330
System.out.printf(now() + "testObjectWithFinisher(%s) starting%n", url);
331
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
332
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
333
.POST(BodyPublishers.ofString("May the wind always be at your back.")).build();
334
335
ObjectSubscriber subscriber = new ObjectSubscriber();
336
HttpResponse<String> response = client.sendAsync(request,
337
BodyHandlers.fromSubscriber(subscriber, ObjectSubscriber::get)).join();
338
String text = response.body();
339
System.out.println(text);
340
assertEquals(response.statusCode(), 200);
341
assertTrue(text.length() != 0); // what else can be asserted!
342
}
343
344
@Test(dataProvider = "uris")
345
void testObjectWithoutFinisher(String url) {
346
System.out.printf(now() + "testObjectWithoutFinisher(%s) starting%n", url);
347
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
348
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
349
.POST(BodyPublishers.ofString("May the wind always be at your back.")).build();
350
351
ObjectSubscriber subscriber = new ObjectSubscriber();
352
HttpResponse<Void> response = client.sendAsync(request,
353
BodyHandlers.fromSubscriber(subscriber)).join();
354
String text = subscriber.get();
355
System.out.println(text);
356
assertEquals(response.statusCode(), 200);
357
assertTrue(text.length() != 0); // what else can be asserted!
358
}
359
360
@Test(dataProvider = "uris")
361
void testObjectWithFinisherBlocking(String url) throws Exception {
362
System.out.printf(now() + "testObjectWithFinisherBlocking(%s) starting%n", url);
363
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
364
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
365
.POST(BodyPublishers.ofString("May the wind always be at your back.")).build();
366
367
ObjectSubscriber subscriber = new ObjectSubscriber();
368
HttpResponse<String> response = client.send(request,
369
BodyHandlers.fromSubscriber(subscriber, ObjectSubscriber::get));
370
String text = response.body();
371
System.out.println(text);
372
assertEquals(response.statusCode(), 200);
373
assertTrue(text.length() != 0); // what else can be asserted!
374
}
375
376
@Test(dataProvider = "uris")
377
void testObjectWithoutFinisherBlocking(String url) throws Exception {
378
System.out.printf(now() + "testObjectWithoutFinisherBlocking(%s) starting%n", url);
379
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
380
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
381
.POST(BodyPublishers.ofString("May the wind always be at your back.")).build();
382
383
ObjectSubscriber subscriber = new ObjectSubscriber();
384
HttpResponse<Void> response = client.send(request,
385
BodyHandlers.fromSubscriber(subscriber));
386
String text = subscriber.get();
387
System.out.println(text);
388
assertEquals(response.statusCode(), 200);
389
assertTrue(text.length() != 0); // what else can be asserted!
390
}
391
392
393
// -- mapping using convenience handlers
394
395
@Test(dataProvider = "uris")
396
void mappingFromByteArray(String url) throws Exception {
397
System.out.printf(now() + "mappingFromByteArray(%s) starting%n", url);
398
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
399
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
400
.POST(BodyPublishers.ofString("We're sucking diesel now!")).build();
401
402
client.sendAsync(request, BodyHandlers.fromSubscriber(BodySubscribers.ofByteArray(),
403
bas -> new String(bas.getBody().toCompletableFuture().join(), UTF_8)))
404
.thenApply(FlowAdapterSubscriberTest::assert200ResponseCode)
405
.thenApply(HttpResponse::body)
406
.thenAccept(body -> assertEquals(body, "We're sucking diesel now!"))
407
.join();
408
}
409
410
@Test(dataProvider = "uris")
411
void mappingFromInputStream(String url) throws Exception {
412
System.out.printf(now() + "mappingFromInputStream(%s) starting%n", url);
413
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
414
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
415
.POST(BodyPublishers.ofString("May the wind always be at your back.")).build();
416
417
client.sendAsync(request, BodyHandlers.fromSubscriber(BodySubscribers.ofInputStream(),
418
ins -> {
419
InputStream is = ins.getBody().toCompletableFuture().join();
420
return new String(uncheckedReadAllBytes(is), UTF_8); } ))
421
.thenApply(FlowAdapterSubscriberTest::assert200ResponseCode)
422
.thenApply(HttpResponse::body)
423
.thenAccept(body -> assertEquals(body, "May the wind always be at your back."))
424
.join();
425
}
426
427
/** An abstract Subscriber that converts all received data into a String. */
428
static abstract class AbstractSubscriber implements Supplier<String> {
429
protected volatile Flow.Subscription subscription;
430
protected volatile ByteArrayOutputStream baos = new ByteArrayOutputStream();
431
protected volatile String text;
432
433
public void onSubscribe(Flow.Subscription subscription) {
434
this.subscription = subscription;
435
subscription.request(Long.MAX_VALUE);
436
}
437
public void onError(Throwable throwable) {
438
throw new RuntimeException(throwable);
439
}
440
public void onComplete() {
441
text = new String(baos.toByteArray(), UTF_8);
442
}
443
@Override public String get() { return text; }
444
}
445
446
static class ListSubscriber extends AbstractSubscriber
447
implements Flow.Subscriber<List<ByteBuffer>>, Supplier<String>
448
{
449
@Override public void onNext(List<ByteBuffer> item) {
450
for (ByteBuffer bb : item) {
451
byte[] ba = new byte[bb.remaining()];
452
bb.get(ba);
453
uncheckedWrite(baos, ba);
454
}
455
}
456
}
457
458
static class CollectionSubscriber extends AbstractSubscriber
459
implements Flow.Subscriber<Collection<ByteBuffer>>, Supplier<String>
460
{
461
@Override public void onNext(Collection<ByteBuffer> item) {
462
for (ByteBuffer bb : item) {
463
byte[] ba = new byte[bb.remaining()];
464
bb.get(ba);
465
uncheckedWrite(baos, ba);
466
}
467
}
468
}
469
470
static class IterableSubscriber extends AbstractSubscriber
471
implements Flow.Subscriber<Iterable<ByteBuffer>>, Supplier<String>
472
{
473
@Override public void onNext(Iterable<ByteBuffer> item) {
474
for (ByteBuffer bb : item) {
475
byte[] ba = new byte[bb.remaining()];
476
bb.get(ba);
477
uncheckedWrite(baos, ba);
478
}
479
}
480
}
481
482
static class ObjectSubscriber extends AbstractSubscriber
483
implements Flow.Subscriber<Object>, Supplier<String>
484
{
485
@Override public void onNext(Object item) {
486
// What can anyone do with Object, cast or toString it ?
487
uncheckedWrite(baos, item.toString().getBytes(UTF_8));
488
}
489
}
490
491
static void uncheckedWrite(ByteArrayOutputStream baos, byte[] ba) {
492
try {
493
baos.write(ba);
494
} catch (IOException e) {
495
throw new UncheckedIOException(e);
496
}
497
}
498
499
static byte[] uncheckedReadAllBytes(InputStream is) {
500
try {
501
return is.readAllBytes();
502
} catch (IOException e) {
503
throw new UncheckedIOException(e);
504
}
505
}
506
507
static final <T> HttpResponse<T> assert200ResponseCode(HttpResponse<T> response) {
508
assertEquals(response.statusCode(), 200);
509
return response;
510
}
511
512
static String serverAuthority(HttpServer server) {
513
return InetAddress.getLoopbackAddress().getHostName() + ":"
514
+ server.getAddress().getPort();
515
}
516
517
@BeforeTest
518
public void setup() throws Exception {
519
sslContext = new SimpleSSLContext().get();
520
if (sslContext == null)
521
throw new AssertionError("Unexpected null sslContext");
522
523
InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
524
httpTestServer = HttpServer.create(sa, 0);
525
httpTestServer.createContext("/http1/echo", new Http1EchoHandler());
526
httpURI = "http://" + serverAuthority(httpTestServer) + "/http1/echo";
527
528
httpsTestServer = HttpsServer.create(sa, 0);
529
httpsTestServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
530
httpsTestServer.createContext("/https1/echo", new Http1EchoHandler());
531
httpsURI = "https://" + serverAuthority(httpsTestServer) + "/https1/echo";
532
533
http2TestServer = new Http2TestServer("localhost", false, 0);
534
http2TestServer.addHandler(new Http2EchoHandler(), "/http2/echo");
535
http2URI = "http://" + http2TestServer.serverAuthority() + "/http2/echo";
536
537
https2TestServer = new Http2TestServer("localhost", true, sslContext);
538
https2TestServer.addHandler(new Http2EchoHandler(), "/https2/echo");
539
https2URI = "https://" + https2TestServer.serverAuthority() + "/https2/echo";
540
541
httpTestServer.start();
542
httpsTestServer.start();
543
http2TestServer.start();
544
https2TestServer.start();
545
}
546
547
@AfterTest
548
public void teardown() throws Exception {
549
httpTestServer.stop(0);
550
httpsTestServer.stop(0);
551
http2TestServer.stop();
552
https2TestServer.stop();
553
}
554
555
static class Http1EchoHandler implements HttpHandler {
556
@Override
557
public void handle(HttpExchange t) throws IOException {
558
try (InputStream is = t.getRequestBody();
559
OutputStream os = t.getResponseBody()) {
560
byte[] bytes = is.readAllBytes();
561
t.sendResponseHeaders(200, bytes.length);
562
os.write(bytes);
563
}
564
}
565
}
566
567
static class Http2EchoHandler implements Http2Handler {
568
@Override
569
public void handle(Http2TestExchange t) throws IOException {
570
try (InputStream is = t.getRequestBody();
571
OutputStream os = t.getResponseBody()) {
572
byte[] bytes = is.readAllBytes();
573
t.sendResponseHeaders(200, bytes.length);
574
os.write(bytes);
575
}
576
}
577
}
578
}
579
580