File: GZIPInputStreamTest.java

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (647 lines) | stat: -rw-r--r-- 31,590 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
/*
 * Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 8217264
 * @summary Tests that you can map an InputStream to a GZIPInputStream
 * @library /test/lib /test/jdk/java/net/httpclient/lib
 * @build jdk.test.lib.net.SimpleSSLContext jdk.httpclient.test.lib.common.HttpServerAdapters ReferenceTracker
 * @run testng/othervm GZIPInputStreamTest
 */

import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpsConfigurator;
import com.sun.net.httpserver.HttpsServer;
import jdk.test.lib.net.SimpleSSLContext;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandler;
import java.net.http.HttpResponse.BodyHandlers;
import java.net.http.HttpResponse.BodySubscribers;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import jdk.httpclient.test.lib.common.HttpServerAdapters;
import jdk.httpclient.test.lib.http2.Http2TestServer;

import static java.lang.System.out;
import static java.net.http.HttpClient.Version.HTTP_1_1;
import static java.net.http.HttpClient.Version.HTTP_2;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.testng.Assert.assertEquals;

public class GZIPInputStreamTest implements HttpServerAdapters {

    SSLContext sslContext;
    HttpTestServer httpTestServer;    // HTTP/1.1    [ 4 servers ]
    HttpTestServer httpsTestServer;   // HTTPS/1.1
    HttpTestServer http2TestServer;   // HTTP/2 ( h2c )
    HttpTestServer https2TestServer;  // HTTP/2 ( h2  )
    String httpURI;
    String httpsURI;
    String http2URI;
    String https2URI;

    static final int ITERATION_COUNT = 3;
    // a shared executor helps reduce the amount of threads created by the test
    // this test will block if the executor doesn't have at least two threads.
    static final Executor executor = Executors.newFixedThreadPool(2);
    static final Executor singleThreadExecutor = Executors.newSingleThreadExecutor();

    public static final String LOREM_IPSUM =
            "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
                    + "Proin et lorem a sem faucibus finibus. "
                    + "Nam nisl nibh, elementum laoreet rutrum quis, lobortis at sem. "
                    + "Aenean purus libero, vehicula sed auctor ac, finibus commodo massa. "
                    + "Etiam dapibus nisl ex, viverra iaculis sapien suscipit sit amet. "
                    + "Phasellus fringilla id orci sit amet convallis. "
                    + "Nam suscipit tempor felis sed feugiat. "
                    + "Mauris quis viverra justo, vitae vulputate turpis. "
                    + "Ut eu orci eget ante faucibus volutpat quis quis urna. "
                    + "Ut porttitor mattis diam, ac sollicitudin ligula volutpat vel. "
                    + "Quisque pretium leo sed augue lacinia, eu mollis dui tempor.\n\n"
                    + "Nullam at mi porttitor, condimentum enim et, tristique felis. "
                    + "Nulla ante elit, interdum id ante ac, dignissim suscipit urna. "
                    + "Sed rhoncus felis eget placerat tincidunt. "
                    + "Duis pellentesque, eros et laoreet lacinia, urna arcu elementum metus, "
                    + "et tempor nibh ante vel odio. "
                    + "Donec et dolor posuere, sagittis libero sit amet, imperdiet ligula. "
                    + "Sed aliquam nulla congue bibendum hendrerit. "
                    + "Morbi ut tincidunt turpis. "
                    + "Nullam semper ipsum et sem imperdiet, sit amet commodo turpis euismod. "
                    + "Nullam aliquet metus id libero elementum, ut pulvinar urna gravida. "
                    + "Nullam non rhoncus diam. "
                    + "Mauris sagittis bibendum odio, sed accumsan sem ullamcorper ut.\n\n"
                    + "Proin malesuada nisl a quam dignissim rhoncus. "
                    + "Pellentesque vitae dui velit. "
                    + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
                    + "Vivamus sagittis magna id magna vestibulum, nec lacinia odio maximus. "
                    + "Nunc commodo, nisl non sagittis posuere, tortor ligula accumsan diam, "
                    + "a rhoncus augue velit quis enim. "
                    + "Nulla et dictum mauris. "
                    + "Vivamus et accumsan mauris, et tincidunt nunc.\n\n"
                    + "Nullam non pharetra lectus. "
                    + "Fusce lobortis sapien ante, quis egestas tellus tincidunt efficitur. "
                    + "Proin tempus mollis urna, sit amet congue diam eleifend in. "
                    + "Ut auctor metus ipsum, at porta turpis consectetur sed. "
                    + "Ut malesuada euismod massa, ut elementum nisi mattis eget. "
                    + "Donec ultrices vel dolor at convallis. "
                    + "Nunc eget felis nec nunc faucibus finibus. "
                    + "Curabitur nec auctor metus, sit amet tristique lorem. "
                    + "Donec tempus fringilla suscipit. Cras sit amet ante elit. "
                    + "Ut sodales sagittis eros quis cursus. "
                    + "Maecenas finibus ante quis euismod rutrum. "
                    + "Aenean scelerisque placerat nisi. "
                    + "Fusce porta, nibh vel efficitur sodales, urna eros consequat tellus, "
                    + "at fringilla ex justo in mi. "
                    + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
                    + "Morbi accumsan, justo nec tincidunt pretium, justo ex consectetur ante, "
                    + "nec euismod diam velit vitae quam.\n\n"
                    + "Vestibulum ante ipsum primis in faucibus orci luctus et "
                    + "ultrices posuere cubilia Curae; "
                    + "Praesent eget consequat nunc, vel dapibus nulla. "
                    + "Maecenas egestas luctus consectetur. "
                    + "Duis lacus risus, sollicitudin sit amet justo sed, "
                    + "ultrices facilisis sapien. "
                    + "Mauris eget fermentum risus. "
                    + "Suspendisse potenti. Nam at tempor risus. "
                    + "Quisque lacus augue, dictum vel interdum quis, interdum et mi. "
                    + "In purus mauris, pellentesque et lectus eget, condimentum pretium odio."
                    + " Donec imperdiet congue laoreet. "
                    + "Cras pharetra hendrerit purus ac efficitur. \n";



    @DataProvider(name = "variants")
    public Object[][] variants() {
        return new Object[][]{
                { httpURI,   false },
                { httpURI,   true },
                { httpsURI,  false },
                { httpsURI,  true },
                { http2URI,  false },
                { http2URI,  true },
                { https2URI, false },
                { https2URI, true },
        };
    }

    final ReferenceTracker TRACKER = ReferenceTracker.INSTANCE;
    HttpClient newHttpClient() {
        return TRACKER.track(HttpClient.newBuilder()
                         .executor(executor)
                         .sslContext(sslContext)
                         .build());
    }

    HttpClient newSingleThreadClient() {
        return TRACKER.track(HttpClient.newBuilder()
                .executor(singleThreadExecutor)
                .sslContext(sslContext)
                .build());
    }

    HttpClient newInLineClient() {
        return TRACKER.track(HttpClient.newBuilder()
                .executor((r) -> r.run() )
                .sslContext(sslContext)
                .build());
    }

    @Test(dataProvider = "variants")
    public void testPlainSyncAsString(String uri, boolean sameClient) throws Exception {
        out.println("\nSmoke test: verify that the result we get from the server is correct.");
        out.println("Uses plain send() and `asString` to get the plain string.");
        out.println("Uses single threaded executor");
        HttpClient client = null;
        for (int i=0; i< ITERATION_COUNT; i++) {
            if (!sameClient || client == null)
                client = newSingleThreadClient(); // should work with 1 single thread

            HttpRequest req = HttpRequest.newBuilder(URI.create(uri +"/txt/LoremIpsum.txt"))
                    .build();
            BodyHandler<String> handler = BodyHandlers.ofString(UTF_8);
            HttpResponse<String> response = client.send(req, handler);
            String lorem = response.body();
            if (!LOREM_IPSUM.equals(lorem)) {
                out.println("Response doesn't match");
                out.println("[" + LOREM_IPSUM + "] != [" + lorem + "]");
                assertEquals(LOREM_IPSUM, lorem);
            } else {
                out.println("Received expected response.");
            }
        }
    }

    @Test(dataProvider = "variants")
    public void testPlainSyncAsInputStream(String uri, boolean sameClient) throws Exception {
        out.println("Uses plain send() and `asInputStream` - calls readAllBytes() from main thread");
        out.println("Uses single threaded executor");
        HttpClient client = null;
        for (int i=0; i< ITERATION_COUNT; i++) {
            if (!sameClient || client == null)
                client = newSingleThreadClient(); // should work with 1 single thread

            HttpRequest req = HttpRequest.newBuilder(URI.create(uri + "/txt/LoremIpsum.txt"))
                    .build();
            BodyHandler<InputStream> handler = BodyHandlers.ofInputStream();
            HttpResponse<InputStream> response = client.send(req, handler);
            String lorem = new String(response.body().readAllBytes(), UTF_8);
            if (!LOREM_IPSUM.equals(lorem)) {
                out.println("Response doesn't match");
                out.println("[" + LOREM_IPSUM + "] != [" + lorem + "]");
                assertEquals(LOREM_IPSUM, lorem);
            } else {
                out.println("Received expected response.");
            }
        }
    }

    @Test(dataProvider = "variants")
    public void testGZIPSyncAsInputStream(String uri, boolean sameClient) throws Exception {
        out.println("Uses plain send() and `asInputStream` - " +
                "creates GZIPInputStream and calls readAllBytes() from main thread");
        out.println("Uses single threaded executor");
        HttpClient client = null;
        for (int i=0; i< ITERATION_COUNT; i++) {
            if (!sameClient || client == null)
                client = newSingleThreadClient(); // should work with 1 single thread

            HttpRequest req = HttpRequest.newBuilder(URI.create(uri + "/gz/LoremIpsum.txt.gz"))
                    .build();
            BodyHandler<InputStream> handler = BodyHandlers.ofInputStream();
            HttpResponse<InputStream> response = client.send(req, handler);
            GZIPInputStream gz = new GZIPInputStream(response.body());
            String lorem = new String(gz.readAllBytes(), UTF_8);
            if (!LOREM_IPSUM.equals(lorem)) {
                out.println("Response doesn't match");
                out.println("[" + LOREM_IPSUM + "] != [" + lorem + "]");
                assertEquals(LOREM_IPSUM, lorem);
            } else {
                out.println("Received expected response.");
            }
        }
    }

    @Test(dataProvider = "variants")
    public void testGZIPSyncAsGZIPInputStream(String uri, boolean sameClient) throws Exception {
        out.println("Uses plain send() and a mapping subscriber to "+
                "create the GZIPInputStream. Calls readAllBytes() from main thread");
        out.println("Uses a fixed thread pool executor with 2 thread");
        HttpClient client = null;
        for (int i=0; i< ITERATION_COUNT; i++) {
            if (!sameClient || client == null)
                client = newHttpClient(); // needs at least 2 threads

            HttpRequest req = HttpRequest.newBuilder(URI.create(uri + "/gz/LoremIpsum.txt.gz"))
                    .build();
            // This is dangerous, because the finisher will block.
            // We support this, but the executor must have enough threads.
            BodyHandler<InputStream> handler = new GZIPBodyHandler();
            HttpResponse<InputStream> response = client.send(req, handler);
            String lorem = new String(response.body().readAllBytes(), UTF_8);
            if (!LOREM_IPSUM.equals(lorem)) {
                out.println("Response doesn't match");
                out.println("[" + LOREM_IPSUM + "] != [" + lorem + "]");
                assertEquals(LOREM_IPSUM, lorem);
            } else {
                out.println("Received expected response.");
            }
        }
    }

    @Test(dataProvider = "variants")
    public void testGZIPSyncAsGZIPInputStreamSupplier(String uri, boolean sameClient) throws Exception {
        out.println("Uses plain send() and a mapping subscriber to "+
                "create a Supplier<GZIPInputStream>. Calls Supplier.get() " +
                "and readAllBytes() from main thread");
        out.println("Uses a single threaded executor");
        HttpClient client = null;
        for (int i=0; i< ITERATION_COUNT; i++) {
            if (!sameClient || client == null)
                client = newSingleThreadClient(); // should work with 1 single thread

            HttpRequest req = HttpRequest.newBuilder(URI.create(uri + "/gz/LoremIpsum.txt.gz"))
                    .build();
            // This is dangerous, because the finisher will block.
            // We support this, but the executor must have enough threads.
            BodyHandler<Supplier<InputStream>> handler = new BodyHandler<Supplier<InputStream>>() {
                 public HttpResponse.BodySubscriber<Supplier<InputStream>> apply(
                         HttpResponse.ResponseInfo responseInfo)
                 {
                    String contentType = responseInfo.headers().firstValue("Content-Encoding")
                            .orElse("identity");
                    out.println("Content-Encoding: " + contentType);
                    if (contentType.equalsIgnoreCase("gzip")) {
                        // This is dangerous. Blocking in the mapping function can wedge the
                        // response. We do support it provided that there enough thread in
                        // the executor.
                        return BodySubscribers.mapping(BodySubscribers.ofInputStream(),
                                (is) -> (() -> {
                                    try {
                                        return new GZIPInputStream(is);
                                    } catch (IOException io) {
                                        throw new UncheckedIOException(io);
                                    }
                                }));
                    } else return BodySubscribers.mapping(BodySubscribers.ofInputStream(),
                            (is) -> (() -> is));
                }
            };
            HttpResponse<Supplier<InputStream>> response = client.send(req, handler);
            String lorem = new String(response.body().get().readAllBytes(), UTF_8);
            if (!LOREM_IPSUM.equals(lorem)) {
                out.println("Response doesn't match");
                out.println("[" + LOREM_IPSUM + "] != [" + lorem + "]");
                assertEquals(LOREM_IPSUM, lorem);
            } else {
                out.println("Received expected response.");
            }
        }
    }

    @Test(dataProvider = "variants")
    public void testPlainAsyncAsInputStreamBlocks(String uri, boolean sameClient) throws Exception {
        out.println("Uses sendAsync() and `asInputStream`. Registers a dependent action "+
                "that calls readAllBytes()");
        out.println("Uses a fixed thread pool executor with two threads");
        HttpClient client = null;
        for (int i=0; i< ITERATION_COUNT; i++) {
            if (!sameClient || client == null)
                client = newHttpClient(); // needs at least 2 threads

            HttpRequest req = HttpRequest.newBuilder(URI.create(uri + "/txt/LoremIpsum.txt"))
                    .build();
            BodyHandler<InputStream> handler = BodyHandlers.ofInputStream();
            CompletableFuture<HttpResponse<InputStream>> responseCF = client.sendAsync(req, handler);
            // This is dangerous. Blocking in the mapping function can wedge the
            // response. We do support it provided that there enough threads in
            // the executor.
            String lorem = responseCF.thenApply((r) -> {
                try {
                    return new String(r.body().readAllBytes(), UTF_8);
                } catch (IOException io) {
                    throw new UncheckedIOException(io);
                }
            }).join();
            if (!LOREM_IPSUM.equals(lorem)) {
                out.println("Response doesn't match");
                out.println("[" + LOREM_IPSUM + "] != [" + lorem + "]");
                assertEquals(LOREM_IPSUM, lorem);
            } else {
                out.println("Received expected response.");
            }
        }
    }

    @Test(dataProvider = "variants")
    public void testGZIPAsyncAsGZIPInputStreamBlocks(String uri, boolean sameClient) throws Exception {
        out.println("Uses sendAsync() and a mapping subscriber to create a GZIPInputStream. " +
                 "Registers a dependent action that calls readAllBytes()");
        out.println("Uses a fixed thread pool executor with two threads");
        HttpClient client = null;
        for (int i=0; i< ITERATION_COUNT; i++) {
            if (!sameClient || client == null)
                client = newHttpClient(); // needs at least 2 threads

            HttpRequest req = HttpRequest.newBuilder(URI.create(uri + "/gz/LoremIpsum.txt.gz"))
                    .build();
            BodyHandler<InputStream> handler = new GZIPBodyHandler();
            CompletableFuture<HttpResponse<InputStream>> responseCF = client.sendAsync(req, handler);
            // This is dangerous - we support this, but it can block
            // if there are not enough threads available.
            // Correct custom code should use thenApplyAsync instead.
            String lorem = responseCF.thenApply((r) -> {
                try {
                    return new String(r.body().readAllBytes(), UTF_8);
                } catch (IOException io) {
                    throw new UncheckedIOException(io);
                }
            }).join();
            if (!LOREM_IPSUM.equals(lorem)) {
                out.println("Response doesn't match");
                out.println("[" + LOREM_IPSUM + "] != [" + lorem + "]");
                assertEquals(LOREM_IPSUM, lorem);
            } else {
                out.println("Received expected response.");
            }
        }
    }

    @Test(dataProvider = "variants")
    public void testGZIPSyncAsGZIPInputStreamBlocks(String uri, boolean sameClient) throws Exception {
        out.println("Uses sendAsync() and a mapping subscriber to create a GZIPInputStream," +
                "which is mapped again using a mapping subscriber " +
                "to call readAllBytes() and map to String");
        out.println("Uses a fixed thread pool executor with two threads");
        HttpClient client = null;
        for (int i=0; i< ITERATION_COUNT; i++) {
            if (!sameClient || client == null)
                client = newHttpClient(); // needs at least 2 threads

            HttpRequest req = HttpRequest.newBuilder(URI.create(uri + "/gz/LoremIpsum.txt.gz"))
                    .build();
            // This is dangerous. Blocking in the mapping function can wedge the
            // response. We do support it provided that there enough thread in
            // the executor.
            BodyHandler<String> handler = new MappingBodyHandler<>(new GZIPBodyHandler(),
                    (InputStream is) ->  {
                        try {
                            return new String(is.readAllBytes(), UTF_8);
                        } catch(IOException io) {
                            throw new UncheckedIOException(io);
                        }
                    });
            HttpResponse<String> response = client.send(req, handler);
            String lorem = response.body();
            if (!LOREM_IPSUM.equals(lorem)) {
                out.println("Response doesn't match");
                out.println("[" + LOREM_IPSUM + "] != [" + lorem + "]");
                assertEquals(LOREM_IPSUM, lorem);
            } else {
                out.println("Received expected response.");
            }
        }
    }

    @Test(dataProvider = "variants")
    public void testGZIPSyncAsGZIPInputStreamSupplierInline(String uri, boolean sameClient) throws Exception {
        out.println("Uses plain send() and a mapping subscriber to "+
                "create a Supplier<GZIPInputStream>. Calls Supplier.get() " +
                "and readAllBytes() from main thread");
        out.println("Uses an inline executor (no threads)");
        HttpClient client = null;
        for (int i=0; i< ITERATION_COUNT; i++) {
            if (!sameClient || client == null)
                client = newInLineClient(); // should even work with no threads

            HttpRequest req = HttpRequest.newBuilder(URI.create(uri + "/gz/LoremIpsum.txt.gz"))
                    .build();
            // This is dangerous, because the finisher will block.
            // We support this, but the executor must have enough threads.
            BodyHandler<Supplier<InputStream>> handler = new BodyHandler<Supplier<InputStream>>() {
                public HttpResponse.BodySubscriber<Supplier<InputStream>> apply(
                        HttpResponse.ResponseInfo responseInfo)
                {
                    String contentType = responseInfo.headers().firstValue("Content-Encoding")
                            .orElse("identity");
                    out.println("Content-Encoding: " + contentType);
                    if (contentType.equalsIgnoreCase("gzip")) {
                        // This is dangerous. Blocking in the mapping function can wedge the
                        // response. We do support it provided that there enough thread in
                        // the executor.
                        return BodySubscribers.mapping(BodySubscribers.ofInputStream(),
                                (is) -> (() -> {
                                    try {
                                        return new GZIPInputStream(is);
                                    } catch (IOException io) {
                                        throw new UncheckedIOException(io);
                                    }
                                }));
                    } else return BodySubscribers.mapping(BodySubscribers.ofInputStream(),
                            (is) -> (() -> is));
                }
            };
            HttpResponse<Supplier<InputStream>> response = client.send(req, handler);
            String lorem = new String(response.body().get().readAllBytes(), UTF_8);
            if (!LOREM_IPSUM.equals(lorem)) {
                out.println("Response doesn't match");
                out.println("[" + LOREM_IPSUM + "] != [" + lorem + "]");
                assertEquals(LOREM_IPSUM, lorem);
            } else {
                out.println("Received expected response.");
            }
        }
    }

    static final class GZIPBodyHandler implements BodyHandler<InputStream> {
        @Override
        public HttpResponse.BodySubscriber<InputStream> apply(HttpResponse.ResponseInfo responseInfo) {
            String contentType = responseInfo.headers().firstValue("Content-Encoding")
                    .orElse("identity");
            out.println("Content-Encoding: " + contentType);
            if (contentType.equalsIgnoreCase("gzip")) {
                // This is dangerous. Blocking in the mapping function can wedge the
                // response. We do support it provided that there enough thread in
                // the executor.
                return BodySubscribers.mapping(BodySubscribers.ofInputStream(),
                        (is) -> {
                    try {
                        return new GZIPInputStream(is);
                    } catch (IOException io) {
                        throw new UncheckedIOException(io);
                    }
                });
            } else return BodySubscribers.ofInputStream();
        }
    }

    static final class MappingBodyHandler<T,U> implements BodyHandler<U> {
        final BodyHandler<T> upstream;
        final Function<? super T,? extends U> finisher;
        MappingBodyHandler(BodyHandler<T> upstream, Function<T,U> finisher) {
            this.upstream = upstream;
            this.finisher = finisher;
        }
        @Override
        public HttpResponse.BodySubscriber<U> apply(HttpResponse.ResponseInfo responseInfo) {
            return BodySubscribers.mapping(upstream.apply(responseInfo), finisher);
        }
    }

    static String serverAuthority(HttpServer server) {
        return InetAddress.getLoopbackAddress().getHostName() + ":"
                + server.getAddress().getPort();
    }

    @BeforeTest
    public void setup() throws Exception {
        sslContext = new SimpleSSLContext().get();
        if (sslContext == null)
            throw new AssertionError("Unexpected null sslContext");

        HttpTestHandler plainHandler = new LoremIpsumPlainHandler();
        HttpTestHandler gzipHandler  = new LoremIpsumGZIPHandler();

        // HTTP/1.1
        httpTestServer = HttpTestServer.create(HTTP_1_1);
        httpTestServer.addHandler(plainHandler, "/http1/chunk/txt");
        httpTestServer.addHandler(gzipHandler,  "/http1/chunk/gz");
        httpURI = "http://" + httpTestServer.serverAuthority() + "/http1/chunk";

        httpsTestServer = HttpTestServer.create(HTTP_1_1, sslContext);
        httpsTestServer.addHandler(plainHandler, "/https1/chunk/txt");
        httpsTestServer.addHandler(gzipHandler, "/https1/chunk/gz");
        httpsURI = "https://" + httpsTestServer.serverAuthority() + "/https1/chunk";

        // HTTP/2
        http2TestServer = HttpTestServer.create(HTTP_2);
        http2TestServer.addHandler(plainHandler, "/http2/chunk/txt");
        http2TestServer.addHandler(gzipHandler, "/http2/chunk/gz");
        http2URI = "http://" + http2TestServer.serverAuthority() + "/http2/chunk";

        https2TestServer = HttpTestServer.create(HTTP_2, sslContext);
        https2TestServer.addHandler(plainHandler, "/https2/chunk/txt");
        https2TestServer.addHandler(gzipHandler, "/https2/chunk/gz");
        https2URI = "https://" + https2TestServer.serverAuthority() + "/https2/chunk";

        httpTestServer.start();
        httpsTestServer.start();
        http2TestServer.start();
        https2TestServer.start();
    }

    @AfterTest
    public void teardown() throws Exception {
        Thread.sleep(100);
        AssertionError fail = TRACKER.check(500);
        try {
            httpTestServer.stop();
            httpsTestServer.stop();
            http2TestServer.stop();
            https2TestServer.stop();
        } finally {
            if (fail != null) {
                throw fail;
            }
        }
    }


    static class LoremIpsumPlainHandler implements HttpTestHandler {
        @Override
        public void handle(HttpTestExchange t) throws IOException {
            try {
                out.println("LoremIpsumPlainHandler received request to " + t.getRequestURI());
                t.getResponseHeaders().addHeader("Content-Encoding", "identity");
                t.sendResponseHeaders(200, -1);
                long size = 0;
                try (OutputStream os = t.getResponseBody()) {
                    for (String s : LOREM_IPSUM.split("\n")) {
                        byte[] buf = s.getBytes(StandardCharsets.UTF_8);
                        System.out.println("Writing " + (buf.length + 1) + " bytes...");
                        os.write(buf);
                        os.write('\n');
                        os.flush();
                        size += buf.length + 1;
                    }
                } finally {
                    System.out.println("Sent " + size + " bytes");
                }
            } catch (IOException | RuntimeException | Error e) {
                e.printStackTrace();
                throw e;
            }
        }
    }

    static class LoremIpsumGZIPHandler implements HttpTestHandler {
        @Override
        public void handle(HttpTestExchange t) throws IOException {
            try {
                out.println("LoremIpsumGZIPHandler received request to " + t.getRequestURI());
                t.getResponseHeaders().addHeader("Content-Encoding", "gzip");
                t.sendResponseHeaders(200, -1);
                long size = 0;
                try (GZIPOutputStream os =
                             new GZIPOutputStream(t.getResponseBody())) {
                    for (String s : LOREM_IPSUM.split("\n")) {
                        byte[] buf = s.getBytes(StandardCharsets.UTF_8);
                        System.out.println("Writing and compressing "
                                + (buf.length + 1) + " uncompressed bytes...");
                        os.write(buf);
                        os.write('\n');
                        os.flush();
                        size += buf.length + 1;
                    }
                } finally {
                    System.out.println("Sent and compressed " + size + " uncompressed bytes");
                }
            } catch (IOException | RuntimeException | Error e) {
                e.printStackTrace();
                throw e;
            }
        }
    }

}