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 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
|
/*
* Copyright (c) 2018, 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.
*/
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.StringReader;
import java.io.UncheckedIOException;
import java.math.BigInteger;
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.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.net.http.HttpResponse.BodySubscribers;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Flow;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.net.ssl.SSLContext;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpsConfigurator;
import com.sun.net.httpserver.HttpsServer;
import jdk.testlibrary.SimpleSSLContext;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import static java.nio.charset.StandardCharsets.UTF_16;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.net.http.HttpRequest.BodyPublishers.ofString;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertThrows;
import static org.testng.Assert.assertTrue;
/*
* @test
* @summary Basic tests for line adapter subscribers as created by
* the BodyHandlers returned by BodyHandler::fromLineSubscriber
* and BodyHandler::asLines
* @modules java.base/sun.net.www.http
* java.net.http/jdk.internal.net.http.common
* java.net.http/jdk.internal.net.http.frame
* java.net.http/jdk.internal.net.http.hpack
* java.logging
* jdk.httpserver
* @library /lib/testlibrary http2/server
* @build Http2TestServer LineBodyHandlerTest HttpServerAdapters
* @build jdk.testlibrary.SimpleSSLContext
* @run testng/othervm LineBodyHandlerTest
*/
public class LineBodyHandlerTest 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;
@DataProvider(name = "uris")
public Object[][] variants() {
return new Object[][]{
{ httpURI },
{ httpsURI },
{ http2URI },
{ https2URI },
};
}
static final Class<NullPointerException> NPE = NullPointerException.class;
static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class;
@Test
public void testNull() {
assertThrows(NPE, () -> BodyHandlers.fromLineSubscriber(null));
assertNotNull(BodyHandlers.fromLineSubscriber(new StringSubscriber()));
assertThrows(NPE, () -> BodyHandlers.fromLineSubscriber(null, Function.identity(), "\n"));
assertThrows(NPE, () -> BodyHandlers.fromLineSubscriber(new StringSubscriber(), null, "\n"));
assertNotNull(BodyHandlers.fromLineSubscriber(new StringSubscriber(), Function.identity(), null));
assertThrows(NPE, () -> BodyHandlers.fromLineSubscriber(null, null, "\n"));
assertThrows(NPE, () -> BodyHandlers.fromLineSubscriber(null, Function.identity(), null));
assertThrows(NPE, () -> BodyHandlers.fromLineSubscriber(new StringSubscriber(), null, null));
assertThrows(NPE, () -> BodySubscribers.fromLineSubscriber(null));
assertThrows(NPE, () -> BodySubscribers.fromLineSubscriber(null, Function.identity(),
Charset.defaultCharset(), System.lineSeparator()));
assertThrows(NPE, () -> BodySubscribers.fromLineSubscriber(new StringSubscriber(), null,
Charset.defaultCharset(), System.lineSeparator()));
assertThrows(NPE, () -> BodySubscribers.fromLineSubscriber(new StringSubscriber(), Function.identity(),
null, System.lineSeparator()));
assertNotNull(BodySubscribers.fromLineSubscriber(new StringSubscriber(), Function.identity(),
Charset.defaultCharset(), null));
assertThrows(NPE, () -> BodySubscribers.fromLineSubscriber(null, null,
Charset.defaultCharset(), System.lineSeparator()));
assertThrows(NPE, () -> BodySubscribers.fromLineSubscriber(null, Function.identity(),
null, System.lineSeparator()));
assertThrows(NPE, () -> BodySubscribers.fromLineSubscriber(null, Function.identity(),
Charset.defaultCharset(), null));
assertThrows(NPE, () -> BodySubscribers.fromLineSubscriber(new StringSubscriber(), null,
null, System.lineSeparator()));
assertThrows(NPE, () -> BodySubscribers.fromLineSubscriber(new StringSubscriber(), null,
Charset.defaultCharset(), null));
assertThrows(NPE, () -> BodySubscribers.fromLineSubscriber(new StringSubscriber(), Function.identity(),
null, null));
assertThrows(NPE, () -> BodySubscribers.fromLineSubscriber(new StringSubscriber(), null, null, null));
assertThrows(NPE, () -> BodySubscribers.fromLineSubscriber(null, Function.identity(),
null, null));
assertThrows(NPE, () -> BodySubscribers.fromLineSubscriber(null, null,
Charset.defaultCharset(), null));
assertThrows(NPE, () -> BodySubscribers.fromLineSubscriber(null, null,
null, System.lineSeparator()));
assertThrows(NPE, () -> BodySubscribers.fromLineSubscriber(null, null, null, null));
}
@Test
public void testIAE() {
assertThrows(IAE, () -> BodyHandlers.fromLineSubscriber(new StringSubscriber(), Function.identity(),""));
assertThrows(IAE, () -> BodyHandlers.fromLineSubscriber(new CharSequenceSubscriber(), Function.identity(),""));
assertThrows(IAE, () -> BodyHandlers.fromLineSubscriber(new ObjectSubscriber(), Function.identity(), ""));
assertThrows(IAE, () -> BodySubscribers.fromLineSubscriber(new StringSubscriber(), Function.identity(),
StandardCharsets.UTF_8, ""));
assertThrows(IAE, () -> BodySubscribers.fromLineSubscriber(new CharSequenceSubscriber(), Function.identity(),
StandardCharsets.UTF_16, ""));
assertThrows(IAE, () -> BodySubscribers.fromLineSubscriber(new ObjectSubscriber(), Function.identity(),
StandardCharsets.US_ASCII, ""));
}
private static final List<String> lines(String text, String eol) {
if (eol == null) {
return new BufferedReader(new StringReader(text)).lines().collect(Collectors.toList());
} else {
String replaced = text.replace(eol, "|");
int i=0;
while(replaced.endsWith("||")) {
replaced = replaced.substring(0,replaced.length()-1);
i++;
}
List<String> res = List.of(replaced.split("\\|"));
if (i > 0) {
res = new ArrayList<>(res);
for (int j=0; j<i; j++) res.add("");
}
return res;
}
}
@Test(dataProvider = "uris")
void testStringWithFinisher(String url) {
String body = "May the luck of the Irish be with you!";
HttpClient client = HttpClient.newBuilder().sslContext(sslContext)
.build();
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
.POST(BodyPublishers.ofString(body))
.build();
StringSubscriber subscriber = new StringSubscriber();
CompletableFuture<HttpResponse<String>> cf
= client.sendAsync(request, BodyHandlers.fromLineSubscriber(
subscriber, Supplier::get, "\n"));
assertNoObtrusion(cf);
HttpResponse<String> response = cf.join();
String text = response.body();
System.out.println(text);
assertEquals(response.statusCode(), 200);
assertEquals(text, body);
assertEquals(subscriber.list, lines(body, "\n"));
}
@Test(dataProvider = "uris")
void testAsStream(String url) {
String body = "May the luck of the Irish be with you!";
HttpClient client = HttpClient.newBuilder().sslContext(sslContext)
.build();
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
.POST(BodyPublishers.ofString(body))
.build();
CompletableFuture<HttpResponse<Stream<String>>> cf
= client.sendAsync(request, BodyHandlers.ofLines());
assertNoObtrusion(cf);
HttpResponse<Stream<String>> response = cf.join();
Stream<String> stream = response.body();
List<String> list = stream.collect(Collectors.toList());
String text = list.stream().collect(Collectors.joining("|"));
System.out.println(text);
assertEquals(response.statusCode(), 200);
assertEquals(text, body);
assertEquals(list, List.of(body));
assertEquals(list, lines(body, null));
}
@Test(dataProvider = "uris")
void testStringWithFinisher2(String url) {
String body = "May the luck\r\n\r\n of the Irish be with you!";
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
.POST(BodyPublishers.ofString(body))
.build();
StringSubscriber subscriber = new StringSubscriber();
CompletableFuture<HttpResponse<Void>> cf
= client.sendAsync(request,
BodyHandlers.fromLineSubscriber(subscriber));
assertNoObtrusion(cf);
HttpResponse<Void> response = cf.join();
String text = subscriber.get();
System.out.println(text);
assertEquals(response.statusCode(), 200);
assertEquals(text, body.replace("\r\n", "\n"));
assertEquals(subscriber.list, lines(body, null));
}
@Test(dataProvider = "uris")
void testAsStreamWithCRLF(String url) {
String body = "May the luck\r\n\r\n of the Irish be with you!";
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
.POST(BodyPublishers.ofString(body))
.build();
CompletableFuture<HttpResponse<Stream<String>>> cf
= client.sendAsync(request, BodyHandlers.ofLines());
assertNoObtrusion(cf);
HttpResponse<Stream<String>> response = cf.join();
Stream<String> stream = response.body();
List<String> list = stream.collect(Collectors.toList());
String text = list.stream().collect(Collectors.joining("|"));
System.out.println(text);
assertEquals(response.statusCode(), 200);
assertEquals(text, "May the luck|| of the Irish be with you!");
assertEquals(list, List.of("May the luck",
"",
" of the Irish be with you!"));
assertEquals(list, lines(body, null));
}
@Test(dataProvider = "uris")
void testStringWithFinisherBlocking(String url) throws Exception {
String body = "May the luck of the Irish be with you!";
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
.POST(BodyPublishers.ofString(body)).build();
StringSubscriber subscriber = new StringSubscriber();
HttpResponse<String> response = client.send(request,
BodyHandlers.fromLineSubscriber(subscriber, Supplier::get, "\n"));
String text = response.body();
System.out.println(text);
assertEquals(response.statusCode(), 200);
assertEquals(text, "May the luck of the Irish be with you!");
assertEquals(subscriber.list, lines(body, "\n"));
}
@Test(dataProvider = "uris")
void testStringWithoutFinisherBlocking(String url) throws Exception {
String body = "May the luck of the Irish be with you!";
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
.POST(BodyPublishers.ofString(body)).build();
StringSubscriber subscriber = new StringSubscriber();
HttpResponse<Void> response = client.send(request,
BodyHandlers.fromLineSubscriber(subscriber));
String text = subscriber.get();
System.out.println(text);
assertEquals(response.statusCode(), 200);
assertEquals(text, "May the luck of the Irish be with you!");
assertEquals(subscriber.list, lines(body, null));
}
// Subscriber<Object>
@Test(dataProvider = "uris")
void testAsStreamWithMixedCRLF(String url) {
String body = "May\r\n the wind\r\n always be\rat your back.\r\r";
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
.POST(BodyPublishers.ofString(body))
.build();
CompletableFuture<HttpResponse<Stream<String>>> cf
= client.sendAsync(request, BodyHandlers.ofLines());
assertNoObtrusion(cf);
HttpResponse<Stream<String>> response = cf.join();
Stream<String> stream = response.body();
List<String> list = stream.collect(Collectors.toList());
String text = list.stream().collect(Collectors.joining("|"));
System.out.println(text);
assertEquals(response.statusCode(), 200);
assertTrue(text.length() != 0); // what else can be asserted!
assertEquals(text, "May| the wind| always be|at your back.|");
assertEquals(list, List.of("May",
" the wind",
" always be",
"at your back.",
""));
assertEquals(list, lines(body, null));
}
@Test(dataProvider = "uris")
void testAsStreamWithMixedCRLF_UTF8(String url) {
String body = "May\r\n the wind\r\n always be\rat your back.\r\r";
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
.header("Content-type", "text/text; charset=UTF-8")
.POST(BodyPublishers.ofString(body, UTF_8)).build();
CompletableFuture<HttpResponse<Stream<String>>> cf
= client.sendAsync(request, BodyHandlers.ofLines());
assertNoObtrusion(cf);
HttpResponse<Stream<String>> response = cf.join();
Stream<String> stream = response.body();
List<String> list = stream.collect(Collectors.toList());
String text = list.stream().collect(Collectors.joining("|"));
System.out.println(text);
assertEquals(response.statusCode(), 200);
assertTrue(text.length() != 0); // what else can be asserted!
assertEquals(text, "May| the wind| always be|at your back.|");
assertEquals(list, List.of("May",
" the wind",
" always be",
"at your back.", ""));
assertEquals(list, lines(body, null));
}
@Test(dataProvider = "uris")
void testAsStreamWithMixedCRLF_UTF16(String url) {
String body = "May\r\n the wind\r\n always be\rat your back.\r\r";
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
.header("Content-type", "text/text; charset=UTF-16")
.POST(BodyPublishers.ofString(body, UTF_16)).build();
CompletableFuture<HttpResponse<Stream<String>>> cf
= client.sendAsync(request, BodyHandlers.ofLines());
assertNoObtrusion(cf);
HttpResponse<Stream<String>> response = cf.join();
Stream<String> stream = response.body();
List<String> list = stream.collect(Collectors.toList());
String text = list.stream().collect(Collectors.joining("|"));
System.out.println(text);
assertEquals(response.statusCode(), 200);
assertTrue(text.length() != 0); // what else can be asserted!
assertEquals(text, "May| the wind| always be|at your back.|");
assertEquals(list, List.of("May",
" the wind",
" always be",
"at your back.",
""));
assertEquals(list, lines(body, null));
}
@Test(dataProvider = "uris")
void testObjectWithFinisher(String url) {
String body = "May\r\n the wind\r\n always be\rat your back.";
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
.POST(BodyPublishers.ofString(body))
.build();
ObjectSubscriber subscriber = new ObjectSubscriber();
CompletableFuture<HttpResponse<String>> cf
= client.sendAsync(request, BodyHandlers.fromLineSubscriber(
subscriber, ObjectSubscriber::get, "\r\n"));
assertNoObtrusion(cf);
HttpResponse<String> response = cf.join();
String text = response.body();
System.out.println(text);
assertEquals(response.statusCode(), 200);
assertTrue(text.length() != 0); // what else can be asserted!
assertEquals(text, "May\n the wind\n always be\rat your back.");
assertEquals(subscriber.list, List.of("May",
" the wind",
" always be\rat your back."));
assertEquals(subscriber.list, lines(body, "\r\n"));
}
@Test(dataProvider = "uris")
void testObjectWithFinisher_UTF16(String url) {
String body = "May\r\n the wind\r\n always be\rat your back.\r\r";
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
.header("Content-type", "text/text; charset=UTF-16")
.POST(BodyPublishers.ofString(body, UTF_16)).build();
ObjectSubscriber subscriber = new ObjectSubscriber();
CompletableFuture<HttpResponse<String>> cf
= client.sendAsync(request, BodyHandlers.fromLineSubscriber(
subscriber, ObjectSubscriber::get, null));
assertNoObtrusion(cf);
HttpResponse<String> response = cf.join();
String text = response.body();
System.out.println(text);
assertEquals(response.statusCode(), 200);
assertTrue(text.length() != 0); // what else can be asserted!
assertEquals(text, "May\n the wind\n always be\nat your back.\n");
assertEquals(subscriber.list, List.of("May",
" the wind",
" always be",
"at your back.",
""));
assertEquals(subscriber.list, lines(body, null));
}
@Test(dataProvider = "uris")
void testObjectWithoutFinisher(String url) {
String body = "May\r\n the wind\r\n always be\rat your back.";
HttpClient client = HttpClient.newBuilder().sslContext(sslContext)
.build();
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
.POST(BodyPublishers.ofString(body))
.build();
ObjectSubscriber subscriber = new ObjectSubscriber();
CompletableFuture<HttpResponse<Void>> cf
= client.sendAsync(request,
BodyHandlers.fromLineSubscriber(subscriber));
assertNoObtrusion(cf);
HttpResponse<Void> response = cf.join();
String text = subscriber.get();
System.out.println(text);
assertEquals(response.statusCode(), 200);
assertTrue(text.length() != 0); // what else can be asserted!
assertEquals(text, "May\n the wind\n always be\nat your back.");
assertEquals(subscriber.list, List.of("May",
" the wind",
" always be",
"at your back."));
assertEquals(subscriber.list, lines(body, null));
}
@Test(dataProvider = "uris")
void testObjectWithFinisherBlocking(String url) throws Exception {
String body = "May\r\n the wind\r\n always be\nat your back.";
HttpClient client = HttpClient.newBuilder().sslContext(sslContext)
.build();
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
.POST(BodyPublishers.ofString(body))
.build();
ObjectSubscriber subscriber = new ObjectSubscriber();
HttpResponse<String> response = client.send(request,
BodyHandlers.fromLineSubscriber(subscriber,
ObjectSubscriber::get,
"\r\n"));
String text = response.body();
System.out.println(text);
assertEquals(response.statusCode(), 200);
assertTrue(text.length() != 0); // what else can be asserted!
assertEquals(text, "May\n the wind\n always be\nat your back.");
assertEquals(subscriber.list, List.of("May",
" the wind",
" always be\nat your back."));
assertEquals(subscriber.list, lines(body, "\r\n"));
}
@Test(dataProvider = "uris")
void testObjectWithoutFinisherBlocking(String url) throws Exception {
String body = "May\r\n the wind\r\n always be\nat your back.";
HttpClient client = HttpClient.newBuilder().sslContext(sslContext)
.build();
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
.POST(BodyPublishers.ofString(body))
.build();
ObjectSubscriber subscriber = new ObjectSubscriber();
HttpResponse<Void> response = client.send(request,
BodyHandlers.fromLineSubscriber(subscriber));
String text = subscriber.get();
System.out.println(text);
assertEquals(response.statusCode(), 200);
assertTrue(text.length() != 0); // what else can be asserted!
assertEquals(text, "May\n the wind\n always be\nat your back.");
assertEquals(subscriber.list, List.of("May",
" the wind",
" always be",
"at your back."));
assertEquals(subscriber.list, lines(body, null));
}
static private final String LINE = "Bient\u00f4t nous plongerons dans les" +
" fr\u00f4\ud801\udc00des t\u00e9n\u00e8bres, ";
static private final String bigtext() {
StringBuilder res = new StringBuilder((LINE.length() + 1) * 50);
for (int i = 0; i<50; i++) {
res.append(LINE);
if (i%2 == 0) res.append("\r\n");
}
return res.toString();
}
@Test(dataProvider = "uris")
void testBigTextFromLineSubscriber(String url) {
HttpClient client = HttpClient.newBuilder().sslContext(sslContext)
.build();
String bigtext = bigtext();
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
.POST(BodyPublishers.ofString(bigtext))
.build();
StringSubscriber subscriber = new StringSubscriber();
CompletableFuture<HttpResponse<String>> cf
= client.sendAsync(request, BodyHandlers.fromLineSubscriber(
subscriber, Supplier::get, "\r\n"));
assertNoObtrusion(cf);
HttpResponse<String> response = cf.join();
String text = response.body();
System.out.println(text);
assertEquals(response.statusCode(), 200);
assertEquals(text, bigtext.replace("\r\n", "\n"));
assertEquals(subscriber.list, lines(bigtext, "\r\n"));
}
@Test(dataProvider = "uris")
void testBigTextAsStream(String url) {
HttpClient client = HttpClient.newBuilder().sslContext(sslContext)
.build();
String bigtext = bigtext();
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
.POST(BodyPublishers.ofString(bigtext))
.build();
CompletableFuture<HttpResponse<Stream<String>>> cf
= client.sendAsync(request, BodyHandlers.ofLines());
assertNoObtrusion(cf);
HttpResponse<Stream<String>> response = cf.join();
Stream<String> stream = response.body();
List<String> list = stream.collect(Collectors.toList());
String text = list.stream().collect(Collectors.joining("|"));
System.out.println(text);
assertEquals(response.statusCode(), 200);
assertEquals(text, bigtext.replace("\r\n", "|"));
assertEquals(list, List.of(bigtext.split("\r\n")));
assertEquals(list, lines(bigtext, null));
}
/** An abstract Subscriber that converts all received data into a String. */
static abstract class AbstractSubscriber implements Supplier<String> {
protected volatile Flow.Subscription subscription;
protected final StringBuilder baos = new StringBuilder();
protected volatile String text;
protected volatile RuntimeException error;
protected final List<Object> list = new CopyOnWriteArrayList<>();
public void onSubscribe(Flow.Subscription subscription) {
this.subscription = subscription;
subscription.request(Long.MAX_VALUE);
}
public void onError(Throwable throwable) {
System.out.println(this + " onError: " + throwable);
error = new RuntimeException(throwable);
}
public void onComplete() {
System.out.println(this + " onComplete");
text = baos.toString();
}
@Override public String get() {
if (error != null) throw error;
return text;
}
}
static class StringSubscriber extends AbstractSubscriber
implements Flow.Subscriber<String>, Supplier<String>
{
@Override public void onNext(String item) {
System.out.print(this + " onNext: \"" + item + "\"");
if (baos.length() != 0) baos.append('\n');
baos.append(item);
list.add(item);
}
}
static class CharSequenceSubscriber extends AbstractSubscriber
implements Flow.Subscriber<CharSequence>, Supplier<String>
{
@Override public void onNext(CharSequence item) {
System.out.print(this + " onNext: " + item);
if (baos.length() != 0) baos.append('\n');
baos.append(item);
list.add(item);
}
}
static class ObjectSubscriber extends AbstractSubscriber
implements Flow.Subscriber<Object>, Supplier<String>
{
@Override public void onNext(Object item) {
System.out.print(this + " onNext: " + item);
if (baos.length() != 0) baos.append('\n');
baos.append(item);
list.add(item);
}
}
static void uncheckedWrite(ByteArrayOutputStream baos, byte[] ba) {
try {
baos.write(ba);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
@BeforeTest
public void setup() throws Exception {
sslContext = new SimpleSSLContext().get();
if (sslContext == null)
throw new AssertionError("Unexpected null sslContext");
InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
httpTestServer = HttpTestServer.of(HttpServer.create(sa, 0));
httpTestServer.addHandler(new HttpTestEchoHandler(), "/http1/echo");
httpURI = "http://" + httpTestServer.serverAuthority() + "/http1/echo";
HttpsServer httpsServer = HttpsServer.create(sa, 0);
httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
httpsTestServer = HttpTestServer.of(httpsServer);
httpsTestServer.addHandler(new HttpTestEchoHandler(),"/https1/echo");
httpsURI = "https://" + httpsTestServer.serverAuthority() + "/https1/echo";
http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0));
http2TestServer.addHandler(new HttpTestEchoHandler(), "/http2/echo");
http2URI = "http://" + http2TestServer.serverAuthority() + "/http2/echo";
https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, sslContext));
https2TestServer.addHandler(new HttpTestEchoHandler(), "/https2/echo");
https2URI = "https://" + https2TestServer.serverAuthority() + "/https2/echo";
httpTestServer.start();
httpsTestServer.start();
http2TestServer.start();
https2TestServer.start();
}
@AfterTest
public void teardown() throws Exception {
httpTestServer.stop();
httpsTestServer.stop();
http2TestServer.stop();
https2TestServer.stop();
}
static void printBytes(PrintStream out, String prefix, byte[] bytes) {
int padding = 4 + 4 - (bytes.length % 4);
padding = padding > 4 ? padding - 4 : 4;
byte[] bigbytes = new byte[bytes.length + padding];
System.arraycopy(bytes, 0, bigbytes, padding, bytes.length);
out.println(prefix + bytes.length + " "
+ new BigInteger(bigbytes).toString(16));
}
private static void assertNoObtrusion(CompletableFuture<?> cf) {
assertThrows(UnsupportedOperationException.class,
() -> cf.obtrudeException(new RuntimeException()));
assertThrows(UnsupportedOperationException.class,
() -> cf.obtrudeValue(null));
}
}
|