@Test
  public void testGenerate125ByteBinaryCase1_2_2() {
    int length = 125;

    ByteBuffer bb = ByteBuffer.allocate(length);

    for (int i = 0; i < length; ++i) {
      bb.put("*".getBytes());
    }

    bb.flip();

    WebSocketFrame binaryFrame = WebSocketFrame.binary().setPayload(bb);

    Generator generator = new UnitGenerator();
    ByteBuffer actual = generator.generate(binaryFrame);

    ByteBuffer expected = ByteBuffer.allocate(length + 5);

    expected.put(new byte[] {(byte) 0x82});

    byte b = 0x00; // no masking
    b |= length & 0x7F;
    expected.put(b);

    for (int i = 0; i < length; ++i) {
      expected.put("*".getBytes());
    }

    BufferUtil.flipToFlush(expected, 0);

    ByteBufferAssert.assertEquals("buffers do not match", expected, actual);
  }
  @Test
  public void testGenerateEmptyBinaryCase1_2_1() {
    WebSocketFrame binaryFrame = WebSocketFrame.binary(new byte[] {});

    Generator generator = new UnitGenerator();
    ByteBuffer actual = generator.generate(binaryFrame);

    ByteBuffer expected = ByteBuffer.allocate(5);

    expected.put(new byte[] {(byte) 0x82, (byte) 0x00});

    BufferUtil.flipToFlush(expected, 0);

    ByteBufferAssert.assertEquals("buffers do not match", expected, actual);
  }
  @Test
  public void testGeneratedTwoFrames() throws IOException {
    WebSocketPolicy policy = WebSocketPolicy.newClientPolicy();

    DeflateFrameExtension ext = new DeflateFrameExtension();
    ext.setBufferPool(bufferPool);
    ext.setPolicy(policy);
    ext.setConfig(new ExtensionConfig(ext.getName()));

    boolean validating = true;
    Generator generator = new Generator(policy, bufferPool, validating);
    generator.configureFromExtensions(Collections.singletonList(ext));

    OutgoingNetworkBytesCapture capture = new OutgoingNetworkBytesCapture(generator);
    ext.setNextOutgoingFrames(capture);

    ext.outgoingFrame(new TextFrame().setPayload("Hello"), null);
    ext.outgoingFrame(new TextFrame().setPayload("There"), null);

    capture.assertBytes(0, "c107f248cdc9c90700");
  }
  private void assertOutgoing(String text, String expectedHex) throws IOException {
    WebSocketPolicy policy = WebSocketPolicy.newClientPolicy();

    DeflateFrameExtension ext = new DeflateFrameExtension();
    ext.setBufferPool(bufferPool);
    ext.setPolicy(policy);

    ExtensionConfig config = ExtensionConfig.parse("deflate-frame");
    ext.setConfig(config);

    boolean validating = true;
    Generator generator = new Generator(policy, bufferPool, validating);
    generator.configureFromExtensions(Collections.singletonList(ext));

    OutgoingNetworkBytesCapture capture = new OutgoingNetworkBytesCapture(generator);
    ext.setNextOutgoingFrames(capture);

    Frame frame = new TextFrame().setPayload(text);
    ext.outgoingFrame(frame, null);

    capture.assertBytes(0, expectedHex);
  }