@Test
public void testParserAndGeneratorMasked() throws Exception
{
Generator gen = new Generator(WebSocketPolicy.newClientPolicy(),bufferPool);
Parser parser = new Parser(WebSocketPolicy.newServerPolicy(),bufferPool);
IncomingFramesCapture capture = new IncomingFramesCapture();
parser.setIncomingFramesHandler(capture);
String message = "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF";
ByteBuffer out = bufferPool.acquire(8192,false);
BufferUtil.flipToFill(out);
try
{
// Setup Frame
WebSocketFrame frame = new TextFrame().setPayload(message);
// Add masking
byte mask[] = new byte[4];
Arrays.fill(mask,(byte)0xFF);
frame.setMask(mask);
// Generate Buffer
ByteBuffer header = gen.generateHeaderBytes(frame);
ByteBuffer payload = frame.getPayload();
out.put(header);
out.put(payload);
// Parse Buffer
BufferUtil.flipToFlush(out,0);
parser.parse(out);
}
finally
{
bufferPool.release(out);
}
// Validate
capture.assertNoErrors();
capture.assertHasFrame(OpCode.TEXT,1);
TextFrame txt = (TextFrame)capture.getFrames().poll();
Assert.assertTrue("Text.isMasked",txt.isMasked());
Assert.assertThat("Text parsed",txt.getPayloadAsUTF8(),is(message));
}