@Test
  public void testParserAndGenerator() throws Exception {
    WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
    ByteBufferPool bufferPool = new MappedByteBufferPool();
    Generator gen = new Generator(policy, bufferPool);
    Parser parser = new Parser(policy, bufferPool);
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);

    String message = "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF";

    ByteBuffer out = bufferPool.acquire(8192, false);
    try {
      // Generate Buffer
      BufferUtil.flipToFill(out);
      WebSocketFrame frame = WebSocketFrame.text(message);
      out = gen.generate(frame);

      // Parse Buffer
      parser.parse(out);
    } finally {
      bufferPool.release(out);
    }

    // Validate
    capture.assertNoErrors();
    capture.assertHasFrame(OpCode.TEXT, 1);

    WebSocketFrame txt = capture.getFrames().get(0);
    Assert.assertThat("Text parsed", txt.getPayloadAsUTF8(), is(message));
  }
예제 #2
0
  /** Similar to the server side 5.6 testcase. pong, then text, then close frames. */
  @Test
  public void testParseCase5_6() {
    List<WebSocketFrame> send = new ArrayList<>();
    send.add(WebSocketFrame.pong().setPayload("ping"));
    send.add(WebSocketFrame.text("hello, world"));
    send.add(new CloseInfo(StatusCode.NORMAL).asFrame());

    ByteBuffer completeBuf = UnitGenerator.generate(send);
    UnitParser parser = new UnitParser();
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    parser.parse(completeBuf);

    capture.assertErrorCount(0);
    capture.assertHasFrame(OpCode.TEXT, 1);
    capture.assertHasFrame(OpCode.CLOSE, 1);
    capture.assertHasFrame(OpCode.PONG, 1);
  }