use of org.eclipse.jetty.websocket.common.frames.TextFrame in project jetty.project by eclipse.
the class ParserTest method testWindowedParseLargeFrame.
@Test
public void testWindowedParseLargeFrame() {
// Create frames
byte[] payload = new byte[65536];
Arrays.fill(payload, (byte) '*');
List<WebSocketFrame> frames = new ArrayList<>();
TextFrame text = new TextFrame();
text.setPayload(ByteBuffer.wrap(payload));
text.setMask(Hex.asByteArray("11223344"));
frames.add(text);
frames.add(new CloseInfo(StatusCode.NORMAL).asFrame());
// Build up raw (network bytes) buffer
ByteBuffer networkBytes = UnitGenerator.generate(frames);
// Parse, in 4096 sized windows
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
Parser parser = new UnitParser(policy);
IncomingFramesCapture capture = new IncomingFramesCapture();
parser.setIncomingFramesHandler(capture);
while (networkBytes.remaining() > 0) {
ByteBuffer window = networkBytes.slice();
int windowSize = Math.min(window.remaining(), 4096);
window.limit(windowSize);
parser.parse(window);
networkBytes.position(networkBytes.position() + windowSize);
}
capture.assertNoErrors();
Assert.assertThat("Frame Count", capture.getFrames().size(), is(2));
WebSocketFrame frame = capture.getFrames().poll();
Assert.assertThat("Frame[0].opcode", frame.getOpCode(), is(OpCode.TEXT));
ByteBuffer actualPayload = frame.getPayload();
Assert.assertThat("Frame[0].payload.length", actualPayload.remaining(), is(payload.length));
// Should be all '*' characters (if masking is correct)
for (int i = actualPayload.position(); i < actualPayload.remaining(); i++) {
Assert.assertThat("Frame[0].payload[i]", actualPayload.get(i), is((byte) '*'));
}
}
use of org.eclipse.jetty.websocket.common.frames.TextFrame in project jetty.project by eclipse.
the class ParserTest method testParseCase5_6.
/**
* 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(new PongFrame().setPayload("ping"));
send.add(new TextFrame().setPayload("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);
}
use of org.eclipse.jetty.websocket.common.frames.TextFrame in project jetty.project by eclipse.
the class RFC6455ExamplesGeneratorTest method testSingleUnmaskedTextMessage.
@Test
public void testSingleUnmaskedTextMessage() {
WebSocketFrame text = new TextFrame().setPayload("Hello");
ByteBuffer actual = UnitGenerator.generate(text);
ByteBuffer expected = ByteBuffer.allocate(10);
expected.put(new byte[] { (byte) 0x81, (byte) 0x05, (byte) 0x48, (byte) 0x65, (byte) 0x6c, (byte) 0x6c, (byte) 0x6f });
expected.flip();
ByteBufferAssert.assertEquals("t1 buffers are not equal", expected, actual);
}
use of org.eclipse.jetty.websocket.common.frames.TextFrame in project jetty.project by eclipse.
the class RFC6455ExamplesGeneratorTest method testSingleMaskedTextMessage.
@Test
public void testSingleMaskedTextMessage() {
WebSocketFrame text = new TextFrame().setPayload("Hello");
text.setMask(new byte[] { 0x37, (byte) 0xfa, 0x21, 0x3d });
ByteBuffer actual = UnitGenerator.generate(text);
ByteBuffer expected = ByteBuffer.allocate(11);
// Raw bytes as found in RFC 6455, Section 5.7 - Examples
// A single-frame masked text message
expected.put(new byte[] { (byte) 0x81, (byte) 0x85, 0x37, (byte) 0xfa, 0x21, 0x3d, 0x7f, (byte) 0x9f, 0x4d, 0x51, 0x58 });
// make readable
expected.flip();
ByteBufferAssert.assertEquals("masked text buffers are not equal", expected, actual);
}
use of org.eclipse.jetty.websocket.common.frames.TextFrame in project jetty.project by eclipse.
the class RFC6455ExamplesGeneratorTest method testFragmentedUnmaskedTextMessage.
@Test
public void testFragmentedUnmaskedTextMessage() {
WebSocketFrame text1 = new TextFrame().setPayload("Hel").setFin(false);
WebSocketFrame text2 = new ContinuationFrame().setPayload("lo");
ByteBuffer actual1 = UnitGenerator.generate(text1);
ByteBuffer actual2 = UnitGenerator.generate(text2);
ByteBuffer expected1 = ByteBuffer.allocate(5);
expected1.put(new byte[] { (byte) 0x01, (byte) 0x03, (byte) 0x48, (byte) 0x65, (byte) 0x6c });
ByteBuffer expected2 = ByteBuffer.allocate(4);
expected2.put(new byte[] { (byte) 0x80, (byte) 0x02, (byte) 0x6c, (byte) 0x6f });
expected1.flip();
expected2.flip();
ByteBufferAssert.assertEquals("t1 buffers are not equal", expected1, actual1);
ByteBufferAssert.assertEquals("t2 buffers are not equal", expected2, actual2);
}
Aggregations