@Test
public void testCompressAndDecompressBigPayload() throws Exception {

    byte[] input = new byte[1024 * 1024];
    // Make them not compressible.
    new Random().nextBytes(input);
    int maxMessageSize = (1024 * 1024) + 8192;
    DeflateFrameExtension clientExtension = new DeflateFrameExtension();
    clientExtension.setPolicy(WebSocketPolicy.newClientPolicy());
    clientExtension.getPolicy().setMaxBinaryMessageSize(maxMessageSize);
    clientExtension.getPolicy().setMaxBinaryMessageBufferSize(maxMessageSize);
    clientExtension.setConfig(ExtensionConfig.parse("deflate-frame"));
    final DeflateFrameExtension serverExtension = new DeflateFrameExtension();
    serverExtension.setPolicy(WebSocketPolicy.newServerPolicy());
    serverExtension.getPolicy().setMaxBinaryMessageSize(maxMessageSize);
    serverExtension.getPolicy().setMaxBinaryMessageBufferSize(maxMessageSize);
    serverExtension.setConfig(ExtensionConfig.parse("deflate-frame"));
    // Chain the next element to decompress.
    clientExtension.setNextOutgoingFrames(new OutgoingFrames() {
        @Override
        public void outgoingFrame(Frame frame, Consumer<Result<Void>> result) {
            LOG.debug("outgoingFrame({})", frame);
            serverExtension.incomingFrame(frame);
            result.accept(Result.SUCCESS);
        }
    });
    final ByteArrayOutputStream result = new ByteArrayOutputStream(input.length);
    serverExtension.setNextIncomingFrames(new IncomingFrames() {
        @Override
        public void incomingFrame(Frame frame) {
            LOG.debug("incomingFrame({})", frame);
            try {
                result.write(BufferUtils.toArray(frame.getPayload()));
            } catch (IOException x) {
                throw new RuntimeException(x);
            }
        }
    });
    BinaryFrame frame = new BinaryFrame();
    frame.setPayload(input);
    frame.setFin(true);
    clientExtension.outgoingFrame(frame, null);
    assertArrayEquals(input, result.toByteArray());
}