test/jdk/java/net/httpclient/LineBodyHandlerTest.java
branchhttp-client-branch
changeset 56009 cf8792f51dee
child 56035 2f3f5da13c4c
equal deleted inserted replaced
56008:bbd688c6fbbb 56009:cf8792f51dee
       
     1 /*
       
     2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 import java.io.BufferedReader;
       
    25 import java.io.ByteArrayOutputStream;
       
    26 import java.io.IOException;
       
    27 import java.io.InputStream;
       
    28 import java.io.OutputStream;
       
    29 import java.io.PrintStream;
       
    30 import java.io.StringReader;
       
    31 import java.io.UncheckedIOException;
       
    32 import java.math.BigInteger;
       
    33 import java.net.InetSocketAddress;
       
    34 import java.net.URI;
       
    35 import java.nio.charset.Charset;
       
    36 import java.nio.charset.StandardCharsets;
       
    37 import java.util.ArrayList;
       
    38 import java.util.List;
       
    39 import java.util.concurrent.CopyOnWriteArrayList;
       
    40 import java.util.concurrent.Flow;
       
    41 import java.util.function.Function;
       
    42 import java.util.function.Supplier;
       
    43 import java.util.stream.Collectors;
       
    44 import java.util.stream.Stream;
       
    45 
       
    46 import com.sun.net.httpserver.HttpExchange;
       
    47 import com.sun.net.httpserver.HttpHandler;
       
    48 import com.sun.net.httpserver.HttpServer;
       
    49 import com.sun.net.httpserver.HttpsConfigurator;
       
    50 import com.sun.net.httpserver.HttpsServer;
       
    51 import jdk.incubator.http.HttpClient;
       
    52 import jdk.incubator.http.HttpRequest;
       
    53 import jdk.incubator.http.HttpResponse;
       
    54 import jdk.incubator.http.HttpResponse.BodyHandler;
       
    55 import jdk.incubator.http.HttpResponse.BodySubscriber;
       
    56 import jdk.testlibrary.SimpleSSLContext;
       
    57 import org.testng.annotations.AfterTest;
       
    58 import org.testng.annotations.BeforeTest;
       
    59 import org.testng.annotations.DataProvider;
       
    60 import org.testng.annotations.Test;
       
    61 import javax.net.ssl.SSLContext;
       
    62 
       
    63 import static java.nio.charset.StandardCharsets.UTF_16;
       
    64 import static java.nio.charset.StandardCharsets.UTF_8;
       
    65 import static jdk.incubator.http.HttpRequest.BodyPublisher.fromString;
       
    66 import static org.testng.Assert.assertEquals;
       
    67 import static org.testng.Assert.assertThrows;
       
    68 import static org.testng.Assert.assertTrue;
       
    69 import static org.testng.Assert.assertNotNull;
       
    70 
       
    71 /*
       
    72  * @test
       
    73  * @summary Basic tests for line adapter subscribers as created by
       
    74  *          the BodyHandlers returned by BodyHandler::fromLineSubscriber
       
    75  *          and BodyHandler::asLines
       
    76  * @modules java.base/sun.net.www.http
       
    77  *          jdk.incubator.httpclient/jdk.incubator.http.internal.common
       
    78  *          jdk.incubator.httpclient/jdk.incubator.http.internal.frame
       
    79  *          jdk.incubator.httpclient/jdk.incubator.http.internal.hpack
       
    80  *          java.logging
       
    81  *          jdk.httpserver
       
    82  * @library /lib/testlibrary http2/server
       
    83  * @build Http2TestServer
       
    84  * @build jdk.testlibrary.SimpleSSLContext
       
    85  * @run testng/othervm LineBodyHandlerTest
       
    86  */
       
    87 
       
    88 public class LineBodyHandlerTest {
       
    89 
       
    90     SSLContext sslContext;
       
    91     HttpServer httpTestServer;         // HTTP/1.1    [ 4 servers ]
       
    92     HttpsServer httpsTestServer;       // HTTPS/1.1
       
    93     Http2TestServer http2TestServer;   // HTTP/2 ( h2c )
       
    94     Http2TestServer https2TestServer;  // HTTP/2 ( h2  )
       
    95     String httpURI;
       
    96     String httpsURI;
       
    97     String http2URI;
       
    98     String https2URI;
       
    99 
       
   100     @DataProvider(name = "uris")
       
   101     public Object[][] variants() {
       
   102         return new Object[][]{
       
   103                 { httpURI   },
       
   104                 { httpsURI  },
       
   105                 { http2URI  },
       
   106                 { https2URI },
       
   107         };
       
   108     }
       
   109 
       
   110     static final Class<NullPointerException> NPE = NullPointerException.class;
       
   111     static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class;
       
   112 
       
   113     @Test
       
   114     public void testNull() {
       
   115         assertThrows(NPE, () -> BodyHandler.fromLineSubscriber(null));
       
   116         assertNotNull(BodyHandler.fromLineSubscriber(new StringSubscriber()));
       
   117         assertThrows(NPE, () -> BodyHandler.fromLineSubscriber(null, Function.identity(), "\n"));
       
   118         assertThrows(NPE, () -> BodyHandler.fromLineSubscriber(new StringSubscriber(), null, "\n"));
       
   119         assertNotNull(BodyHandler.fromLineSubscriber(new StringSubscriber(), Function.identity(), null));
       
   120         assertThrows(NPE, () -> BodyHandler.fromLineSubscriber(null, null, "\n"));
       
   121         assertThrows(NPE, () -> BodyHandler.fromLineSubscriber(null, Function.identity(), null));
       
   122         assertThrows(NPE, () -> BodyHandler.fromLineSubscriber(new StringSubscriber(), null, null));
       
   123 
       
   124         assertThrows(NPE, () -> BodySubscriber.fromLineSubscriber(null));
       
   125         assertThrows(NPE, () -> BodySubscriber.fromLineSubscriber(null, Function.identity(),
       
   126                 Charset.defaultCharset(), System.lineSeparator()));
       
   127         assertThrows(NPE, () -> BodySubscriber.fromLineSubscriber(new StringSubscriber(), null,
       
   128                 Charset.defaultCharset(), System.lineSeparator()));
       
   129         assertThrows(NPE, () -> BodySubscriber.fromLineSubscriber(new StringSubscriber(), Function.identity(),
       
   130                 null, System.lineSeparator()));
       
   131         assertNotNull(BodySubscriber.fromLineSubscriber(new StringSubscriber(), Function.identity(),
       
   132                 Charset.defaultCharset(), null));
       
   133         assertThrows(NPE, () -> BodySubscriber.fromLineSubscriber(null, null,
       
   134                 Charset.defaultCharset(), System.lineSeparator()));
       
   135         assertThrows(NPE, () -> BodySubscriber.fromLineSubscriber(null, Function.identity(),
       
   136                 null, System.lineSeparator()));
       
   137         assertThrows(NPE, () -> BodySubscriber.fromLineSubscriber(null, Function.identity(),
       
   138                 Charset.defaultCharset(), null));
       
   139         assertThrows(NPE, () -> BodySubscriber.fromLineSubscriber(new StringSubscriber(), null,
       
   140                 null, System.lineSeparator()));
       
   141         assertThrows(NPE, () -> BodySubscriber.fromLineSubscriber(new StringSubscriber(), null,
       
   142                 Charset.defaultCharset(), null));
       
   143         assertThrows(NPE, () -> BodySubscriber.fromLineSubscriber(new StringSubscriber(), Function.identity(),
       
   144                 null, null));
       
   145         assertThrows(NPE, () -> BodySubscriber.fromLineSubscriber(new StringSubscriber(), null, null, null));
       
   146         assertThrows(NPE, () -> BodySubscriber.fromLineSubscriber(null, Function.identity(),
       
   147                 null, null));
       
   148         assertThrows(NPE, () -> BodySubscriber.fromLineSubscriber(null, null,
       
   149                 Charset.defaultCharset(), null));
       
   150         assertThrows(NPE, () -> BodySubscriber.fromLineSubscriber(null, null,
       
   151                 null, System.lineSeparator()));
       
   152         assertThrows(NPE, () -> BodySubscriber.fromLineSubscriber(null, null, null, null));
       
   153     }
       
   154 
       
   155     @Test
       
   156     public void testIAE() {
       
   157         assertThrows(IAE, () -> BodyHandler.fromLineSubscriber(new StringSubscriber(), Function.identity(),""));
       
   158         assertThrows(IAE, () -> BodyHandler.fromLineSubscriber(new CharSequenceSubscriber(), Function.identity(),""));
       
   159         assertThrows(IAE, () -> BodyHandler.fromLineSubscriber(new ObjectSubscriber(), Function.identity(), ""));
       
   160         assertThrows(IAE, () -> BodySubscriber.fromLineSubscriber(new StringSubscriber(), Function.identity(),
       
   161                     StandardCharsets.UTF_8, ""));
       
   162         assertThrows(IAE, () -> BodySubscriber.fromLineSubscriber(new CharSequenceSubscriber(), Function.identity(),
       
   163                     StandardCharsets.UTF_16, ""));
       
   164         assertThrows(IAE, () -> BodySubscriber.fromLineSubscriber(new ObjectSubscriber(), Function.identity(),
       
   165                     StandardCharsets.US_ASCII, ""));
       
   166     }
       
   167 
       
   168     private static final List<String> lines(String text, String eol) {
       
   169         if (eol == null) {
       
   170             return new BufferedReader(new StringReader(text)).lines().collect(Collectors.toList());
       
   171         } else {
       
   172             String replaced = text.replace(eol, "|");
       
   173             int i=0;
       
   174             while(replaced.endsWith("||")) {
       
   175                 replaced = replaced.substring(0,replaced.length()-1);
       
   176                 i++;
       
   177             }
       
   178             List<String> res = List.of(replaced.split("\\|"));
       
   179             if (i > 0) {
       
   180                 res = new ArrayList<>(res);
       
   181                 for (int j=0; j<i; j++) res.add("");
       
   182             }
       
   183             return res;
       
   184         }
       
   185     }
       
   186 
       
   187     @Test(dataProvider = "uris")
       
   188     void testStringWithFinisher(String url) {
       
   189         String body = "May the luck of the Irish be with you!";
       
   190         HttpClient client = HttpClient.newBuilder().sslContext(sslContext)
       
   191                 .build();
       
   192         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
       
   193                 .POST(fromString(body))
       
   194                 .build();
       
   195 
       
   196         StringSubscriber subscriber = new StringSubscriber();
       
   197         HttpResponse<String> response = client.sendAsync(request,
       
   198                 BodyHandler.fromLineSubscriber(subscriber, Supplier::get,"\n"))
       
   199                 .join();
       
   200         String text = response.body();
       
   201         System.out.println(text);
       
   202         assertEquals(response.statusCode(), 200);
       
   203         assertEquals(text, body);
       
   204         assertEquals(subscriber.list, lines(body, "\n"));
       
   205     }
       
   206 
       
   207     @Test(dataProvider = "uris")
       
   208     void testAsStream(String url) {
       
   209         String body = "May the luck of the Irish be with you!";
       
   210         HttpClient client = HttpClient.newBuilder().sslContext(sslContext)
       
   211                 .build();
       
   212         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
       
   213                 .POST(fromString(body))
       
   214                 .build();
       
   215 
       
   216         HttpResponse<Stream<String>> response = client.sendAsync(request,
       
   217                 BodyHandler.asLines()).join();
       
   218         Stream<String> stream = response.body();
       
   219         List<String> list = stream.collect(Collectors.toList());
       
   220         String text = list.stream().collect(Collectors.joining("|"));
       
   221         System.out.println(text);
       
   222         assertEquals(response.statusCode(), 200);
       
   223         assertEquals(text, body);
       
   224         assertEquals(list, List.of(body));
       
   225         assertEquals(list, lines(body, null));
       
   226     }
       
   227 
       
   228     @Test(dataProvider = "uris")
       
   229     void testStringWithFinisher2(String url) {
       
   230         String body = "May the luck\r\n\r\n of the Irish be with you!";
       
   231         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
       
   232         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
       
   233                 .POST(fromString(body))
       
   234                 .build();
       
   235 
       
   236         StringSubscriber subscriber = new StringSubscriber();
       
   237         HttpResponse<Void> response = client.sendAsync(request,
       
   238                 BodyHandler.fromLineSubscriber(subscriber)).join();
       
   239         String text = subscriber.get();
       
   240         System.out.println(text);
       
   241         assertEquals(response.statusCode(), 200);
       
   242         assertEquals(text, body.replace("\r\n", "\n"));
       
   243         assertEquals(subscriber.list, lines(body, null));
       
   244     }
       
   245 
       
   246     @Test(dataProvider = "uris")
       
   247     void testAsStreamWithCRLF(String url) {
       
   248         String body = "May the luck\r\n\r\n of the Irish be with you!";
       
   249         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
       
   250         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
       
   251                 .POST(fromString(body))
       
   252                 .build();
       
   253 
       
   254         HttpResponse<Stream<String>> response = client.sendAsync(request,
       
   255                 BodyHandler.asLines()).join();
       
   256         Stream<String> stream = response.body();
       
   257         List<String> list = stream.collect(Collectors.toList());
       
   258         String text = list.stream().collect(Collectors.joining("|"));
       
   259         System.out.println(text);
       
   260         assertEquals(response.statusCode(), 200);
       
   261         assertEquals(text, "May the luck|| of the Irish be with you!");
       
   262         assertEquals(list, List.of("May the luck",
       
   263                                    "",
       
   264                                    " of the Irish be with you!"));
       
   265         assertEquals(list, lines(body, null));
       
   266     }
       
   267 
       
   268     @Test(dataProvider = "uris")
       
   269     void testStringWithFinisherBlocking(String url) throws Exception {
       
   270         String body = "May the luck of the Irish be with you!";
       
   271         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
       
   272         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
       
   273                 .POST(fromString(body)).build();
       
   274 
       
   275         StringSubscriber subscriber = new StringSubscriber();
       
   276         HttpResponse<String> response = client.send(request,
       
   277                 BodyHandler.fromLineSubscriber(subscriber, Supplier::get, "\n"));
       
   278         String text = response.body();
       
   279         System.out.println(text);
       
   280         assertEquals(response.statusCode(), 200);
       
   281         assertEquals(text, "May the luck of the Irish be with you!");
       
   282         assertEquals(subscriber.list, lines(body, "\n"));
       
   283     }
       
   284 
       
   285     @Test(dataProvider = "uris")
       
   286     void testStringWithoutFinisherBlocking(String url) throws Exception {
       
   287         String body = "May the luck of the Irish be with you!";
       
   288         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
       
   289         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
       
   290                 .POST(fromString(body)).build();
       
   291 
       
   292         StringSubscriber subscriber = new StringSubscriber();
       
   293         HttpResponse<Void> response = client.send(request,
       
   294                 BodyHandler.fromLineSubscriber(subscriber));
       
   295         String text = subscriber.get();
       
   296         System.out.println(text);
       
   297         assertEquals(response.statusCode(), 200);
       
   298         assertEquals(text, "May the luck of the Irish be with you!");
       
   299         assertEquals(subscriber.list, lines(body, null));
       
   300     }
       
   301 
       
   302     // Subscriber<Object>
       
   303 
       
   304     @Test(dataProvider = "uris")
       
   305     void testAsStreamWithMixedCRLF(String url) {
       
   306         String body = "May\r\n the wind\r\n always be\rat your back.\r\r";
       
   307         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
       
   308         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
       
   309                 .POST(fromString(body))
       
   310                 .build();
       
   311 
       
   312         HttpResponse<Stream<String>> response = client.sendAsync(request,
       
   313                 BodyHandler.asLines()).join();
       
   314         Stream<String> stream = response.body();
       
   315         List<String> list = stream.collect(Collectors.toList());
       
   316         String text = list.stream().collect(Collectors.joining("|"));
       
   317         System.out.println(text);
       
   318         assertEquals(response.statusCode(), 200);
       
   319         assertTrue(text.length() != 0);  // what else can be asserted!
       
   320         assertEquals(text, "May| the wind| always be|at your back.|");
       
   321         assertEquals(list, List.of("May",
       
   322                                    " the wind",
       
   323                                    " always be",
       
   324                                    "at your back.",
       
   325                                    ""));
       
   326         assertEquals(list, lines(body, null));
       
   327     }
       
   328 
       
   329     @Test(dataProvider = "uris")
       
   330     void testAsStreamWithMixedCRLF_UTF8(String url) {
       
   331         String body = "May\r\n the wind\r\n always be\rat your back.\r\r";
       
   332         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
       
   333         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
       
   334                 .header("Content-type", "text/text; charset=UTF-8")
       
   335                 .POST(fromString(body, UTF_8)).build();
       
   336 
       
   337         HttpResponse<Stream<String>> response = client.sendAsync(request,
       
   338                 BodyHandler.asLines()).join();
       
   339         Stream<String> stream = response.body();
       
   340         List<String> list = stream.collect(Collectors.toList());
       
   341         String text = list.stream().collect(Collectors.joining("|"));
       
   342         System.out.println(text);
       
   343         assertEquals(response.statusCode(), 200);
       
   344         assertTrue(text.length() != 0);  // what else can be asserted!
       
   345         assertEquals(text, "May| the wind| always be|at your back.|");
       
   346         assertEquals(list, List.of("May",
       
   347                                    " the wind",
       
   348                                    " always be",
       
   349                                    "at your back.", ""));
       
   350         assertEquals(list, lines(body, null));
       
   351     }
       
   352 
       
   353     @Test(dataProvider = "uris")
       
   354     void testAsStreamWithMixedCRLF_UTF16(String url) {
       
   355         String body = "May\r\n the wind\r\n always be\rat your back.\r\r";
       
   356         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
       
   357         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
       
   358                 .header("Content-type", "text/text; charset=UTF-16")
       
   359                 .POST(fromString(body, UTF_16)).build();
       
   360 
       
   361         HttpResponse<Stream<String>> response = client.sendAsync(request,
       
   362                 BodyHandler.asLines()).join();
       
   363         Stream<String> stream = response.body();
       
   364         List<String> list = stream.collect(Collectors.toList());
       
   365         String text = list.stream().collect(Collectors.joining("|"));
       
   366         System.out.println(text);
       
   367         assertEquals(response.statusCode(), 200);
       
   368         assertTrue(text.length() != 0);  // what else can be asserted!
       
   369         assertEquals(text, "May| the wind| always be|at your back.|");
       
   370         assertEquals(list, List.of("May",
       
   371                                    " the wind",
       
   372                                    " always be",
       
   373                                    "at your back.",
       
   374                                    ""));
       
   375         assertEquals(list, lines(body, null));
       
   376     }
       
   377 
       
   378     @Test(dataProvider = "uris")
       
   379     void testObjectWithFinisher(String url) {
       
   380         String body = "May\r\n the wind\r\n always be\rat your back.";
       
   381         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
       
   382         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
       
   383                 .POST(fromString(body))
       
   384                 .build();
       
   385 
       
   386         ObjectSubscriber subscriber = new ObjectSubscriber();
       
   387         HttpResponse<String> response = client.sendAsync(request,
       
   388                 BodyHandler.fromLineSubscriber(subscriber, ObjectSubscriber::get, "\r\n"))
       
   389                 .join();
       
   390         String text = response.body();
       
   391         System.out.println(text);
       
   392         assertEquals(response.statusCode(), 200);
       
   393         assertTrue(text.length() != 0);  // what else can be asserted!
       
   394         assertEquals(text, "May\n the wind\n always be\rat your back.");
       
   395         assertEquals(subscriber.list, List.of("May",
       
   396                                               " the wind",
       
   397                                               " always be\rat your back."));
       
   398         assertEquals(subscriber.list, lines(body, "\r\n"));
       
   399     }
       
   400 
       
   401     @Test(dataProvider = "uris")
       
   402     void testObjectWithFinisher_UTF16(String url) {
       
   403         String body = "May\r\n the wind\r\n always be\rat your back.\r\r";
       
   404         HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
       
   405         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
       
   406                 .header("Content-type", "text/text; charset=UTF-16")
       
   407                 .POST(fromString(body, UTF_16)).build();
       
   408         ObjectSubscriber subscriber = new ObjectSubscriber();
       
   409         HttpResponse<String> response = client.sendAsync(request,
       
   410                 BodyHandler.fromLineSubscriber(subscriber,
       
   411                                                ObjectSubscriber::get,
       
   412                                    null)).join();
       
   413         String text = response.body();
       
   414         System.out.println(text);
       
   415         assertEquals(response.statusCode(), 200);
       
   416         assertTrue(text.length() != 0);  // what else can be asserted!
       
   417         assertEquals(text, "May\n the wind\n always be\nat your back.\n");
       
   418         assertEquals(subscriber.list, List.of("May",
       
   419                                               " the wind",
       
   420                                               " always be",
       
   421                                               "at your back.",
       
   422                                               ""));
       
   423         assertEquals(subscriber.list, lines(body, null));
       
   424     }
       
   425 
       
   426     @Test(dataProvider = "uris")
       
   427     void testObjectWithoutFinisher(String url) {
       
   428         String body = "May\r\n the wind\r\n always be\rat your back.";
       
   429         HttpClient client = HttpClient.newBuilder().sslContext(sslContext)
       
   430                 .build();
       
   431         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
       
   432                 .POST(fromString(body))
       
   433                 .build();
       
   434 
       
   435         ObjectSubscriber subscriber = new ObjectSubscriber();
       
   436         HttpResponse<Void> response = client.sendAsync(request,
       
   437                 BodyHandler.fromLineSubscriber(subscriber)).join();
       
   438         String text = subscriber.get();
       
   439         System.out.println(text);
       
   440         assertEquals(response.statusCode(), 200);
       
   441         assertTrue(text.length() != 0);  // what else can be asserted!
       
   442         assertEquals(text, "May\n the wind\n always be\nat your back.");
       
   443         assertEquals(subscriber.list, List.of("May",
       
   444                                               " the wind",
       
   445                                               " always be",
       
   446                                               "at your back."));
       
   447         assertEquals(subscriber.list, lines(body, null));
       
   448     }
       
   449 
       
   450     @Test(dataProvider = "uris")
       
   451     void testObjectWithFinisherBlocking(String url) throws Exception {
       
   452         String body = "May\r\n the wind\r\n always be\nat your back.";
       
   453         HttpClient client = HttpClient.newBuilder().sslContext(sslContext)
       
   454                 .build();
       
   455         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
       
   456                 .POST(fromString(body))
       
   457                 .build();
       
   458 
       
   459         ObjectSubscriber subscriber = new ObjectSubscriber();
       
   460         HttpResponse<String> response = client.send(request,
       
   461                 BodyHandler.fromLineSubscriber(subscriber,
       
   462                                                ObjectSubscriber::get,
       
   463                                    "\r\n"));
       
   464         String text = response.body();
       
   465         System.out.println(text);
       
   466         assertEquals(response.statusCode(), 200);
       
   467         assertTrue(text.length() != 0);  // what else can be asserted!
       
   468         assertEquals(text, "May\n the wind\n always be\nat your back.");
       
   469         assertEquals(subscriber.list, List.of("May",
       
   470                                               " the wind",
       
   471                                               " always be\nat your back."));
       
   472         assertEquals(subscriber.list, lines(body, "\r\n"));
       
   473     }
       
   474 
       
   475     @Test(dataProvider = "uris")
       
   476     void testObjectWithoutFinisherBlocking(String url) throws Exception {
       
   477         String body = "May\r\n the wind\r\n always be\nat your back.";
       
   478         HttpClient client = HttpClient.newBuilder().sslContext(sslContext)
       
   479                 .build();
       
   480         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
       
   481                 .POST(fromString(body))
       
   482                 .build();
       
   483 
       
   484         ObjectSubscriber subscriber = new ObjectSubscriber();
       
   485         HttpResponse<Void> response = client.send(request,
       
   486                 BodyHandler.fromLineSubscriber(subscriber));
       
   487         String text = subscriber.get();
       
   488         System.out.println(text);
       
   489         assertEquals(response.statusCode(), 200);
       
   490         assertTrue(text.length() != 0);  // what else can be asserted!
       
   491         assertEquals(text, "May\n the wind\n always be\nat your back.");
       
   492         assertEquals(subscriber.list, List.of("May",
       
   493                                               " the wind",
       
   494                                               " always be",
       
   495                                               "at your back."));
       
   496         assertEquals(subscriber.list, lines(body, null));
       
   497     }
       
   498 
       
   499     static private final String LINE = "Bient\u00f4t nous plongerons dans les" +
       
   500             " fr\u00f4\ud801\udc00des t\u00e9n\u00e8bres, ";
       
   501 
       
   502     static private final String bigtext() {
       
   503         StringBuilder res = new StringBuilder((LINE.length() + 1) * 50);
       
   504         for (int i = 0; i<50; i++) {
       
   505             res.append(LINE);
       
   506             if (i%2 == 0) res.append("\r\n");
       
   507         }
       
   508         return res.toString();
       
   509     }
       
   510 
       
   511     @Test(dataProvider = "uris")
       
   512     void testBigTextFromLineSubscriber(String url) {
       
   513         HttpClient client = HttpClient.newBuilder().sslContext(sslContext)
       
   514                 .build();
       
   515         String bigtext = bigtext();
       
   516         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
       
   517                 .POST(fromString(bigtext))
       
   518                 .build();
       
   519 
       
   520         StringSubscriber subscriber = new StringSubscriber();
       
   521         HttpResponse<String> response = client.sendAsync(request,
       
   522                 BodyHandler.fromLineSubscriber(subscriber, Supplier::get,"\r\n"))
       
   523                 .join();
       
   524         String text = response.body();
       
   525         System.out.println(text);
       
   526         assertEquals(response.statusCode(), 200);
       
   527         assertEquals(text, bigtext.replace("\r\n", "\n"));
       
   528         assertEquals(subscriber.list, lines(bigtext, "\r\n"));
       
   529     }
       
   530 
       
   531     @Test(dataProvider = "uris")
       
   532     void testBigTextAsStream(String url) {
       
   533         HttpClient client = HttpClient.newBuilder().sslContext(sslContext)
       
   534                 .build();
       
   535         String bigtext = bigtext();
       
   536         HttpRequest request = HttpRequest.newBuilder(URI.create(url))
       
   537                 .POST(fromString(bigtext))
       
   538                 .build();
       
   539 
       
   540         HttpResponse<Stream<String>> response = client.sendAsync(request,
       
   541                 BodyHandler.asLines()).join();
       
   542         Stream<String> stream = response.body();
       
   543         List<String> list = stream.collect(Collectors.toList());
       
   544         String text = list.stream().collect(Collectors.joining("|"));
       
   545         System.out.println(text);
       
   546         assertEquals(response.statusCode(), 200);
       
   547         assertEquals(text, bigtext.replace("\r\n", "|"));
       
   548         assertEquals(list, List.of(bigtext.split("\r\n")));
       
   549         assertEquals(list, lines(bigtext, null));
       
   550     }
       
   551 
       
   552     /** An abstract Subscriber that converts all received data into a String. */
       
   553     static abstract class AbstractSubscriber implements Supplier<String> {
       
   554         protected volatile Flow.Subscription subscription;
       
   555         protected final StringBuilder baos = new StringBuilder();
       
   556         protected volatile String text;
       
   557         protected volatile RuntimeException error;
       
   558         protected final List<Object> list = new CopyOnWriteArrayList<>();
       
   559 
       
   560         public void onSubscribe(Flow.Subscription subscription) {
       
   561             this.subscription = subscription;
       
   562             subscription.request(Long.MAX_VALUE);
       
   563         }
       
   564         public void onError(Throwable throwable) {
       
   565             System.out.println(this + " onError: " + throwable);
       
   566             error = new RuntimeException(throwable);
       
   567         }
       
   568         public void onComplete() {
       
   569             System.out.println(this + " onComplete");
       
   570             text = baos.toString();
       
   571         }
       
   572         @Override public String get() {
       
   573             if (error != null) throw error;
       
   574             return text;
       
   575         }
       
   576     }
       
   577 
       
   578     static class StringSubscriber extends AbstractSubscriber
       
   579             implements Flow.Subscriber<String>, Supplier<String>
       
   580     {
       
   581         @Override public void onNext(String item) {
       
   582             System.out.print(this + " onNext: \"" + item + "\"");
       
   583             if (baos.length() != 0) baos.append('\n');
       
   584             baos.append(item);
       
   585             list.add(item);
       
   586         }
       
   587     }
       
   588 
       
   589     static class CharSequenceSubscriber extends AbstractSubscriber
       
   590             implements Flow.Subscriber<CharSequence>, Supplier<String>
       
   591     {
       
   592         @Override public void onNext(CharSequence item) {
       
   593             System.out.print(this + " onNext: " + item);
       
   594             if (baos.length() != 0) baos.append('\n');
       
   595             baos.append(item);
       
   596             list.add(item);
       
   597         }
       
   598     }
       
   599 
       
   600     static class ObjectSubscriber extends AbstractSubscriber
       
   601             implements Flow.Subscriber<Object>, Supplier<String>
       
   602     {
       
   603         @Override public void onNext(Object item) {
       
   604             System.out.print(this + " onNext: " + item);
       
   605             if (baos.length() != 0) baos.append('\n');
       
   606             baos.append(item);
       
   607             list.add(item);
       
   608         }
       
   609     }
       
   610 
       
   611 
       
   612     static void uncheckedWrite(ByteArrayOutputStream baos, byte[] ba) {
       
   613         try {
       
   614             baos.write(ba);
       
   615         } catch (IOException e) {
       
   616             throw new UncheckedIOException(e);
       
   617         }
       
   618     }
       
   619 
       
   620     @BeforeTest
       
   621     public void setup() throws Exception {
       
   622         sslContext = new SimpleSSLContext().get();
       
   623         if (sslContext == null)
       
   624             throw new AssertionError("Unexpected null sslContext");
       
   625 
       
   626         InetSocketAddress sa = new InetSocketAddress("localhost", 0);
       
   627         httpTestServer = HttpServer.create(sa, 0);
       
   628         httpTestServer.createContext("/http1/echo", new Http1EchoHandler());
       
   629         httpURI = "http://127.0.0.1:" + httpTestServer.getAddress().getPort() + "/http1/echo";
       
   630 
       
   631         httpsTestServer = HttpsServer.create(sa, 0);
       
   632         httpsTestServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
       
   633         httpsTestServer.createContext("/https1/echo", new Http1EchoHandler());
       
   634         httpsURI = "https://127.0.0.1:" + httpsTestServer.getAddress().getPort() + "/https1/echo";
       
   635 
       
   636         http2TestServer = new Http2TestServer("127.0.0.1", false, 0);
       
   637         http2TestServer.addHandler(new Http2EchoHandler(), "/http2/echo");
       
   638         int port = http2TestServer.getAddress().getPort();
       
   639         http2URI = "http://127.0.0.1:" + port + "/http2/echo";
       
   640 
       
   641         https2TestServer = new Http2TestServer("127.0.0.1", true, 0);
       
   642         https2TestServer.addHandler(new Http2EchoHandler(), "/https2/echo");
       
   643         port = https2TestServer.getAddress().getPort();
       
   644         https2URI = "https://127.0.0.1:" + port + "/https2/echo";
       
   645 
       
   646         httpTestServer.start();
       
   647         httpsTestServer.start();
       
   648         http2TestServer.start();
       
   649         https2TestServer.start();
       
   650     }
       
   651 
       
   652     @AfterTest
       
   653     public void teardown() throws Exception {
       
   654         httpTestServer.stop(0);
       
   655         httpsTestServer.stop(0);
       
   656         http2TestServer.stop();
       
   657         https2TestServer.stop();
       
   658     }
       
   659 
       
   660     static void printBytes(PrintStream out, String prefix, byte[] bytes) {
       
   661         int padding = 4 + 4 - (bytes.length % 4);
       
   662         padding = padding > 4 ? padding - 4 : 4;
       
   663         byte[] bigbytes = new byte[bytes.length + padding];
       
   664         System.arraycopy(bytes, 0, bigbytes, padding, bytes.length);
       
   665         out.println(prefix + bytes.length + " "
       
   666                     + new BigInteger(bigbytes).toString(16));
       
   667     }
       
   668 
       
   669     static class Http1EchoHandler implements HttpHandler {
       
   670         @Override
       
   671         public void handle(HttpExchange t) throws IOException {
       
   672             try (InputStream is = t.getRequestBody();
       
   673                  OutputStream os = t.getResponseBody()) {
       
   674                 byte[] bytes = is.readAllBytes();
       
   675                 printBytes(System.out,"Bytes: ", bytes);
       
   676                 if (t.getRequestHeaders().containsKey("Content-type")) {
       
   677                     t.getResponseHeaders().add("Content-type",
       
   678                             t.getRequestHeaders().getFirst("Content-type"));
       
   679                 }
       
   680                 t.sendResponseHeaders(200, bytes.length);
       
   681                 os.write(bytes);
       
   682             }
       
   683         }
       
   684     }
       
   685 
       
   686     static class Http2EchoHandler implements Http2Handler {
       
   687         @Override
       
   688         public void handle(Http2TestExchange t) throws IOException {
       
   689             try (InputStream is = t.getRequestBody();
       
   690                  OutputStream os = t.getResponseBody()) {
       
   691                 byte[] bytes = is.readAllBytes();
       
   692                 printBytes(System.out,"Bytes: ", bytes);
       
   693                 if (t.getRequestHeaders().firstValue("Content-type").isPresent()) {
       
   694                     t.getResponseHeaders().addHeader("Content-type",
       
   695                             t.getRequestHeaders().firstValue("Content-type").get());
       
   696                 }
       
   697                 t.sendResponseHeaders(200, bytes.length);
       
   698                 os.write(bytes);
       
   699             }
       
   700         }
       
   701     }
       
   702 }