use of org.eclipse.jetty.websocket.common.test.IncomingFramesCapture in project jetty.project by eclipse.
the class RFC6455ExamplesParserTest method testSingleMaskedPongRequest.
@Test
public void testSingleMaskedPongRequest() {
ByteBuffer buf = ByteBuffer.allocate(16);
// Raw bytes as found in RFC 6455, Section 5.7 - Examples
// Unmasked Pong request
buf.put(new byte[] { (byte) 0x8a, (byte) 0x85, 0x37, (byte) 0xfa, 0x21, 0x3d, 0x7f, (byte) 0x9f, 0x4d, 0x51, 0x58 });
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.PONG, 1);
WebSocketFrame pong = capture.getFrames().poll();
String actual = BufferUtil.toUTF8String(pong.getPayload());
Assert.assertThat("PongFrame.payload", actual, is("Hello"));
}
use of org.eclipse.jetty.websocket.common.test.IncomingFramesCapture in project jetty.project by eclipse.
the class TextPayloadParserTest method testFrameTooLargeDueToPolicy.
@Test
public void testFrameTooLargeDueToPolicy() throws Exception {
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
// Artificially small buffer/payload
// read buffer
policy.setInputBufferSize(1024);
// streaming buffer (not used in this test)
policy.setMaxTextMessageBufferSize(1024);
// actual maximum text message size policy
policy.setMaxTextMessageSize(1024);
byte[] utf = new byte[2048];
Arrays.fill(utf, (byte) 'a');
Assert.assertThat("Must be a medium length payload", utf.length, allOf(greaterThan(0x7E), lessThan(0xFFFF)));
ByteBuffer buf = ByteBuffer.allocate(utf.length + 8);
// text frame, fin = true
buf.put((byte) 0x81);
// 0x7E == 126 (a 2 byte payload length)
buf.put((byte) (0x80 | 0x7E));
buf.putShort((short) utf.length);
MaskedByteBuffer.putMask(buf);
MaskedByteBuffer.putPayload(buf, utf);
buf.flip();
UnitParser parser = new UnitParser(policy);
IncomingFramesCapture capture = new IncomingFramesCapture();
parser.setIncomingFramesHandler(capture);
parser.parseQuietly(buf);
capture.assertHasErrors(MessageTooLargeException.class, 1);
capture.assertHasNoFrames();
MessageTooLargeException err = (MessageTooLargeException) capture.getErrors().poll();
Assert.assertThat("Error.closeCode", err.getStatusCode(), is(StatusCode.MESSAGE_TOO_LARGE));
}
use of org.eclipse.jetty.websocket.common.test.IncomingFramesCapture in project jetty.project by eclipse.
the class TextPayloadParserTest method testMediumMaskedText.
@Test
public void testMediumMaskedText() throws Exception {
StringBuffer sb = new StringBuffer();
;
for (int i = 0; i < 14; i++) {
sb.append("Hello Medium World ");
}
sb.append(". The end.");
String expectedText = sb.toString();
byte[] utf = expectedText.getBytes(StandardCharsets.UTF_8);
Assert.assertThat("Must be a medium length payload", utf.length, allOf(greaterThan(0x7E), lessThan(0xFFFF)));
ByteBuffer buf = ByteBuffer.allocate(utf.length + 10);
buf.put((byte) 0x81);
// 0x7E == 126 (a 2 byte payload length)
buf.put((byte) (0x80 | 0x7E));
buf.putShort((short) 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 testShortMaskedUtf8Text.
@Test
public void testShortMaskedUtf8Text() 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 ClientCloseTest method confirmServerReceivedCloseFrame.
private void confirmServerReceivedCloseFrame(IBlockheadServerConnection serverConn, int expectedCloseCode, Matcher<String> closeReasonMatcher) throws IOException, TimeoutException {
IncomingFramesCapture serverCapture = serverConn.readFrames(1, 30, TimeUnit.SECONDS);
serverCapture.assertNoErrors();
serverCapture.assertFrameCount(1);
serverCapture.assertHasFrame(OpCode.CLOSE, 1);
WebSocketFrame frame = serverCapture.getFrames().poll();
assertThat("Server received close frame", frame.getOpCode(), is(OpCode.CLOSE));
CloseInfo closeInfo = new CloseInfo(frame);
assertThat("Server received close code", closeInfo.getStatusCode(), is(expectedCloseCode));
if (closeReasonMatcher == null) {
assertThat("Server received close reason", closeInfo.getReason(), nullValue());
} else {
assertThat("Server received close reason", closeInfo.getReason(), closeReasonMatcher);
}
}
Aggregations