Пример #1
0
  private void testWSFrames(boolean binary, WebsocketVersion version) throws Exception {

    String path = "/some/path";
    String query = "foo=bar&wibble=eek";
    String uri = path + "?" + query;

    // version 0 doesn't support continuations so we just send 1 frame per message
    int frames = version == WebsocketVersion.V00 ? 1 : 10;

    server =
        vertx
            .createHttpServer(new HttpServerOptions().setPort(HttpTestBase.DEFAULT_HTTP_PORT))
            .websocketHandler(
                ws -> {
                  assertEquals(uri, ws.uri());
                  assertEquals(path, ws.path());
                  assertEquals(query, ws.query());
                  assertEquals("Upgrade", ws.headers().get("Connection"));
                  AtomicInteger count = new AtomicInteger();
                  ws.frameHandler(
                      frame -> {
                        if (count.get() == 0) {
                          if (binary) {
                            assertTrue(frame.isBinary());
                            assertFalse(frame.isText());
                          } else {
                            assertFalse(frame.isBinary());
                            assertTrue(frame.isText());
                          }
                          assertFalse(frame.isContinuation());
                        } else {
                          assertFalse(frame.isBinary());
                          assertFalse(frame.isText());
                          assertTrue(frame.isContinuation());
                        }
                        if (count.get() == frames - 1) {
                          assertTrue(frame.isFinal());
                        } else {
                          assertFalse(frame.isFinal());
                        }
                        ws.writeFrame(frame);
                        if (count.incrementAndGet() == frames) {
                          count.set(0);
                        }
                      });
                });

    server.listen(
        ar -> {
          assertTrue(ar.succeeded());
          int bsize = 100;

          int msgs = 10;

          client.connectWebsocket(
              HttpTestBase.DEFAULT_HTTP_PORT,
              HttpTestBase.DEFAULT_HTTP_HOST,
              path + "?" + query,
              null,
              version,
              ws -> {
                final List<Buffer> sent = new ArrayList<>();
                final List<Buffer> received = new ArrayList<>();

                AtomicReference<Buffer> currentReceived = new AtomicReference<>(Buffer.buffer());
                ws.frameHandler(
                    frame -> {
                      // received.appendBuffer(frame.binaryData());
                      currentReceived.get().appendBuffer(frame.binaryData());
                      if (frame.isFinal()) {
                        received.add(currentReceived.get());
                        currentReceived.set(Buffer.buffer());
                      }
                      if (received.size() == msgs) {
                        int pos = 0;
                        for (Buffer rec : received) {
                          assertEquals(rec, sent.get(pos++));
                        }
                        testComplete();
                      }
                    });

                AtomicReference<Buffer> currentSent = new AtomicReference<>(Buffer.buffer());
                for (int i = 0; i < msgs; i++) {
                  for (int j = 0; j < frames; j++) {
                    Buffer buff;
                    WebSocketFrame frame;
                    if (binary) {
                      buff = Buffer.buffer(TestUtils.randomByteArray(bsize));
                      if (j == 0) {
                        frame = WebSocketFrame.binaryFrame(buff, false);
                      } else {
                        frame = WebSocketFrame.continuationFrame(buff, j == frames - 1);
                      }
                    } else {
                      String str = TestUtils.randomAlphaString(bsize);
                      buff = Buffer.buffer(str);
                      if (j == 0) {
                        frame = WebSocketFrame.textFrame(str, false);
                      } else {
                        frame = WebSocketFrame.continuationFrame(buff, j == frames - 1);
                      }
                    }
                    currentSent.get().appendBuffer(buff);
                    ws.writeFrame(frame);
                    if (j == frames - 1) {
                      sent.add(currentSent.get());
                      currentSent.set(Buffer.buffer());
                    }
                  }
                }
              });
        });
    await();
  }
Пример #2
0
 @Test
 public void testWebsocketFrameFactoryArguments() throws Exception {
   assertNullPointerException(() -> WebSocketFrame.binaryFrame(null, true));
   assertNullPointerException(() -> WebSocketFrame.textFrame(null, true));
   assertNullPointerException(() -> WebSocketFrame.continuationFrame(null, true));
 }