@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
  @Test
  public void testParseNothing() {
    ByteBuffer buf = ByteBuffer.allocate(16);
    // Put nothing in the buffer.
    buf.flip();

    WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
    Parser parser = new UnitParser(policy);
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    parser.parse(buf);

    capture.assertNoErrors();
    Assert.assertThat("Frame Count", capture.getFrames().size(), is(0));
  }