25 * @test |
25 * @test |
26 * @build DummyWebSocketServer |
26 * @build DummyWebSocketServer |
27 * @run testng/othervm -Djdk.httpclient.HttpClient.log=trace WebSocketTest |
27 * @run testng/othervm -Djdk.httpclient.HttpClient.log=trace WebSocketTest |
28 */ |
28 */ |
29 |
29 |
|
30 import org.testng.annotations.Test; |
|
31 |
|
32 import java.io.IOException; |
30 import java.net.http.WebSocket; |
33 import java.net.http.WebSocket; |
31 import org.testng.annotations.Test; |
|
32 |
|
33 import java.io.IOException; |
|
34 import java.nio.ByteBuffer; |
34 import java.nio.ByteBuffer; |
35 import java.nio.CharBuffer; |
35 import java.nio.CharBuffer; |
36 import java.nio.channels.SocketChannel; |
36 import java.nio.channels.SocketChannel; |
37 import java.nio.charset.StandardCharsets; |
37 import java.nio.charset.StandardCharsets; |
38 import java.util.ArrayList; |
38 import java.util.ArrayList; |
527 assertCompletesExceptionally(ISE, ws.sendPong(ByteBuffer.allocate(0))); |
527 assertCompletesExceptionally(ISE, ws.sendPong(ByteBuffer.allocate(0))); |
528 } |
528 } |
529 } |
529 } |
530 |
530 |
531 @Test |
531 @Test |
|
532 public void simpleAggregatingMessages() throws IOException { |
|
533 |
|
534 List<String> expected = List.of("alpha", "beta", "gamma", "delta"); |
|
535 |
|
536 int[] binary = new int[]{ |
|
537 0x81, 0x05, 0x61, 0x6c, 0x70, 0x68, 0x61, // "alpha" |
|
538 0x01, 0x02, 0x62, 0x65, // "be |
|
539 0x80, 0x02, 0x74, 0x61, // ta" |
|
540 0x01, 0x01, 0x67, // "g |
|
541 0x00, 0x01, 0x61, // a |
|
542 0x00, 0x00, // |
|
543 0x00, 0x00, // |
|
544 0x00, 0x01, 0x6d, // m |
|
545 0x00, 0x01, 0x6d, // m |
|
546 0x80, 0x01, 0x61, // a" |
|
547 0x8a, 0x00, // <PONG> |
|
548 0x01, 0x04, 0x64, 0x65, 0x6c, 0x74, // "delt |
|
549 0x00, 0x01, 0x61, // a |
|
550 0x80, 0x00, // " |
|
551 0x88, 0x00 // <CLOSE> |
|
552 }; |
|
553 CompletableFuture<List<String>> actual = new CompletableFuture<>(); |
|
554 |
|
555 try (DummyWebSocketServer server = serverWithCannedData(binary)) { |
|
556 server.open(); |
|
557 |
|
558 WebSocket.Listener listener = new WebSocket.Listener() { |
|
559 |
|
560 StringBuilder text = new StringBuilder(); |
|
561 |
|
562 @Override |
|
563 public CompletionStage<?> onText(WebSocket webSocket, |
|
564 CharSequence message, |
|
565 WebSocket.MessagePart part) { |
|
566 webSocket.request(1); |
|
567 String str = null; |
|
568 switch (part) { |
|
569 case FIRST: |
|
570 case PART: |
|
571 text.append(message); |
|
572 return null; |
|
573 case LAST: |
|
574 text.append(message); |
|
575 str = text.toString(); |
|
576 text.setLength(0); |
|
577 break; |
|
578 case WHOLE: |
|
579 str = message.toString(); |
|
580 break; |
|
581 } |
|
582 processWholeText(str); |
|
583 return null; |
|
584 } |
|
585 |
|
586 private void processWholeText(String string) { |
|
587 // -- your code here -- |
|
588 } |
|
589 }; |
|
590 |
|
591 newHttpClient().newWebSocketBuilder() |
|
592 .buildAsync(server.getURI(), listener) |
|
593 .join(); |
|
594 |
|
595 List<String> a = actual.join(); |
|
596 assertEquals(a, expected); |
|
597 } |
|
598 } |
|
599 |
|
600 @Test |
532 public void aggregatingMessages() throws IOException { |
601 public void aggregatingMessages() throws IOException { |
533 |
602 |
534 List<String> expected = List.of("alpha", "beta", "gamma", "delta"); |
603 List<String> expected = List.of("alpha", "beta", "gamma", "delta"); |
535 |
604 |
536 int[] binary = new int[]{ |
605 int[] binary = new int[]{ |