use of org.eclipse.jetty.websocket.common.test.IncomingFramesCapture in project jetty.project by eclipse.
the class RFC6455ExamplesParserTest method testFragmentedUnmaskedTextMessage.
@Test
public void testFragmentedUnmaskedTextMessage() {
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.CLIENT);
Parser parser = new UnitParser(policy);
IncomingFramesCapture capture = new IncomingFramesCapture();
parser.setIncomingFramesHandler(capture);
ByteBuffer buf = ByteBuffer.allocate(16);
BufferUtil.clearToFill(buf);
// Raw bytes as found in RFC 6455, Section 5.7 - Examples
// A fragmented unmasked text message (part 1 of 2 "Hel")
buf.put(new byte[] { (byte) 0x01, (byte) 0x03, 0x48, (byte) 0x65, 0x6c });
// Parse #1
BufferUtil.flipToFlush(buf, 0);
parser.parse(buf);
// part 2 of 2 "lo" (A continuation frame of the prior text message)
BufferUtil.flipToFill(buf);
buf.put(new byte[] { (byte) 0x80, 0x02, 0x6c, 0x6f });
// Parse #2
BufferUtil.flipToFlush(buf, 0);
parser.parse(buf);
capture.assertNoErrors();
capture.assertHasFrame(OpCode.TEXT, 1);
capture.assertHasFrame(OpCode.CONTINUATION, 1);
WebSocketFrame txt = capture.getFrames().poll();
String actual = BufferUtil.toUTF8String(txt.getPayload());
Assert.assertThat("TextFrame[0].data", actual, is("Hel"));
txt = capture.getFrames().poll();
actual = BufferUtil.toUTF8String(txt.getPayload());
Assert.assertThat("TextFrame[1].data", actual, is("lo"));
}
use of org.eclipse.jetty.websocket.common.test.IncomingFramesCapture in project jetty.project by eclipse.
the class TextPayloadParserTest method testShortMaskedText.
@Test
public void testShortMaskedText() throws Exception {
String expectedText = "Hello World";
byte[] utf = expectedText.getBytes(StandardCharsets.UTF_8);
ByteBuffer buf = ByteBuffer.allocate(24);
buf.put((byte) 0x81);
buf.put((byte) (0x80 | utf.length));
MaskedByteBuffer.putMask(buf);
MaskedByteBuffer.putPayload(buf, utf);
buf.flip();
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
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();
Assert.assertThat("TextFrame.data", txt.getPayloadAsUTF8(), is(expectedText));
}
use of org.eclipse.jetty.websocket.common.test.IncomingFramesCapture in project jetty.project by eclipse.
the class TextPayloadParserTest method testShortMaskedFragmentedText.
@Test
public void testShortMaskedFragmentedText() throws Exception {
String part1 = "Hello ";
String part2 = "World";
byte[] b1 = part1.getBytes(StandardCharsets.UTF_8);
byte[] b2 = part2.getBytes(StandardCharsets.UTF_8);
ByteBuffer buf = ByteBuffer.allocate(32);
// part 1
// no fin + text
buf.put((byte) 0x01);
buf.put((byte) (0x80 | b1.length));
MaskedByteBuffer.putMask(buf);
MaskedByteBuffer.putPayload(buf, b1);
// part 2
// fin + continuation
buf.put((byte) 0x80);
buf.put((byte) (0x80 | b2.length));
MaskedByteBuffer.putMask(buf);
MaskedByteBuffer.putPayload(buf, b2);
buf.flip();
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
Parser parser = new UnitParser(policy);
IncomingFramesCapture capture = new IncomingFramesCapture();
parser.setIncomingFramesHandler(capture);
parser.parse(buf);
capture.assertNoErrors();
capture.assertHasFrame(OpCode.TEXT, 1);
capture.assertHasFrame(OpCode.CONTINUATION, 1);
WebSocketFrame txt = capture.getFrames().poll();
Assert.assertThat("TextFrame[0].data", txt.getPayloadAsUTF8(), is(part1));
txt = capture.getFrames().poll();
Assert.assertThat("TextFrame[1].data", txt.getPayloadAsUTF8(), is(part2));
}
use of org.eclipse.jetty.websocket.common.test.IncomingFramesCapture in project jetty.project by eclipse.
the class TextPayloadParserTest method testLongMaskedText.
@Test
public void testLongMaskedText() throws Exception {
StringBuffer sb = new StringBuffer();
;
for (int i = 0; i < 3500; i++) {
sb.append("Hello Big World ");
}
sb.append(". The end.");
String expectedText = sb.toString();
byte[] utf = expectedText.getBytes(StandardCharsets.UTF_8);
Assert.assertThat("Must be a long length payload", utf.length, greaterThan(0xFFFF));
ByteBuffer buf = ByteBuffer.allocate(utf.length + 32);
// text frame, fin = true
buf.put((byte) 0x81);
// 0x7F == 127 (a 8 byte payload length)
buf.put((byte) (0x80 | 0x7F));
buf.putLong(utf.length);
MaskedByteBuffer.putMask(buf);
MaskedByteBuffer.putPayload(buf, utf);
buf.flip();
WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
policy.setMaxTextMessageSize(100000);
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();
Assert.assertThat("TextFrame.data", txt.getPayloadAsUTF8(), is(expectedText));
}
use of org.eclipse.jetty.websocket.common.test.IncomingFramesCapture in project jetty.project by eclipse.
the class ParserTest method testParseCase5_19.
/**
* Similar to the server side 5.19 testcase. text message, send in 5 frames/fragments, with 2 pings in the mix.
*/
@Test
public void testParseCase5_19() {
List<WebSocketFrame> send = new ArrayList<>();
send.add(new TextFrame().setPayload("f1").setFin(false));
send.add(new ContinuationFrame().setPayload(",f2").setFin(false));
send.add(new PingFrame().setPayload("pong-1"));
send.add(new ContinuationFrame().setPayload(",f3").setFin(false));
send.add(new ContinuationFrame().setPayload(",f4").setFin(false));
send.add(new PingFrame().setPayload("pong-2"));
send.add(new ContinuationFrame().setPayload(",f5").setFin(true));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
ByteBuffer completeBuf = UnitGenerator.generate(send);
UnitParser parser = new UnitParser();
IncomingFramesCapture capture = new IncomingFramesCapture();
parser.setIncomingFramesHandler(capture);
parser.parseQuietly(completeBuf);
capture.assertErrorCount(0);
capture.assertHasFrame(OpCode.TEXT, 1);
capture.assertHasFrame(OpCode.CONTINUATION, 4);
capture.assertHasFrame(OpCode.CLOSE, 1);
capture.assertHasFrame(OpCode.PING, 2);
}
Aggregations