use of org.eclipse.jetty.websocket.common.test.IncomingFramesCapture 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.test.IncomingFramesCapture 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.test.IncomingFramesCapture in project jetty.project by eclipse.
the class PingPayloadParserTest method testBasicPingParsing.
@Test
public void testBasicPingParsing() {
ByteBuffer buf = ByteBuffer.allocate(16);
BufferUtil.clearToFill(buf);
buf.put(new byte[] { (byte) 0x89, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f });
BufferUtil.flipToFlush(buf, 0);
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.CLIENT);
Parser parser = new UnitParser(policy);
IncomingFramesCapture capture = new IncomingFramesCapture();
parser.setIncomingFramesHandler(capture);
parser.parse(buf);
capture.assertNoErrors();
capture.assertHasFrame(OpCode.PING, 1);
PingFrame ping = (PingFrame) capture.getFrames().poll();
String actual = BufferUtil.toUTF8String(ping.getPayload());
Assert.assertThat("PingFrame.payload", actual, is("Hello"));
}
use of org.eclipse.jetty.websocket.common.test.IncomingFramesCapture in project jetty.project by eclipse.
the class RFC6455ExamplesParserTest method testSingleUnmasked256ByteBinaryMessage.
@Test
public void testSingleUnmasked256ByteBinaryMessage() {
int dataSize = 256;
ByteBuffer buf = ByteBuffer.allocate(dataSize + 10);
// Raw bytes as found in RFC 6455, Section 5.7 - Examples
// 256 bytes binary message in a single unmasked frame
buf.put(new byte[] { (byte) 0x82, 0x7E });
// 16 bit size
buf.putShort((short) 0x01_00);
for (int i = 0; i < dataSize; i++) {
buf.put((byte) 0x44);
}
buf.flip();
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.CLIENT);
Parser parser = new UnitParser(policy);
IncomingFramesCapture capture = new IncomingFramesCapture();
parser.setIncomingFramesHandler(capture);
parser.parse(buf);
capture.assertNoErrors();
capture.assertHasFrame(OpCode.BINARY, 1);
Frame bin = capture.getFrames().poll();
Assert.assertThat("BinaryFrame.payloadLength", bin.getPayloadLength(), is(dataSize));
ByteBuffer data = bin.getPayload();
Assert.assertThat("BinaryFrame.payload.length", data.remaining(), is(dataSize));
for (int i = 0; i < dataSize; i++) {
Assert.assertThat("BinaryFrame.payload[" + i + "]", data.get(i), is((byte) 0x44));
}
}
use of org.eclipse.jetty.websocket.common.test.IncomingFramesCapture in project jetty.project by eclipse.
the class RFC6455ExamplesParserTest method testSingleUnmaskedTextMessage.
@Test
public void testSingleUnmaskedTextMessage() {
ByteBuffer buf = ByteBuffer.allocate(16);
// Raw bytes as found in RFC 6455, Section 5.7 - Examples
// A single-frame unmasked text message
buf.put(new byte[] { (byte) 0x81, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f });
buf.flip();
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.CLIENT);
Parser parser = new UnitParser(policy);
IncomingFramesCapture capture = new IncomingFramesCapture();
parser.setIncomingFramesHandler(capture);
parser.parse(buf);
capture.assertNoErrors();
capture.assertHasFrame(OpCode.TEXT, 1);
WebSocketFrame txt = capture.getFrames().poll();
String actual = BufferUtil.toUTF8String(txt.getPayload());
Assert.assertThat("TextFrame.payload", actual, is("Hello"));
}
Aggregations