use of org.eclipse.jetty.websocket.common.test.UnitParser in project jetty.project by eclipse.
the class TestABCase2 method testParse125OctetPingCase2_4.
@Test
public void testParse125OctetPingCase2_4() {
byte[] bytes = new byte[125];
for (int i = 0; i < bytes.length; ++i) {
bytes[i] = Integer.valueOf(Integer.toOctalString(i)).byteValue();
}
ByteBuffer expected = ByteBuffer.allocate(bytes.length + 32);
expected.put(new byte[] { (byte) 0x89 });
// no masking
byte b = 0x00;
b |= bytes.length & 0x7F;
expected.put(b);
expected.put(bytes);
expected.flip();
Parser parser = new UnitParser(policy);
IncomingFramesCapture capture = new IncomingFramesCapture();
parser.setIncomingFramesHandler(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(OpCode.PING, 1);
Frame pActual = capture.getFrames().poll();
Assert.assertThat("PingFrame.payloadLength", pActual.getPayloadLength(), is(bytes.length));
Assert.assertEquals("PingFrame.payload", bytes.length, pActual.getPayloadLength());
}
use of org.eclipse.jetty.websocket.common.test.UnitParser in project jetty.project by eclipse.
the class TestABCase2 method testParseEmptyPingCase2_1.
@Test
public void testParseEmptyPingCase2_1() {
ByteBuffer expected = ByteBuffer.allocate(5);
expected.put(new byte[] { (byte) 0x89, (byte) 0x00 });
expected.flip();
Parser parser = new UnitParser(policy);
IncomingFramesCapture capture = new IncomingFramesCapture();
parser.setIncomingFramesHandler(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(OpCode.PING, 1);
Frame pActual = capture.getFrames().poll();
Assert.assertThat("PingFrame.payloadLength", pActual.getPayloadLength(), is(0));
Assert.assertEquals("PingFrame.payload", 0, pActual.getPayloadLength());
}
use of org.eclipse.jetty.websocket.common.test.UnitParser in project jetty.project by eclipse.
the class ClosePayloadParserTest method testGameOver.
@Test
public void testGameOver() {
String expectedReason = "Game Over";
byte[] utf = expectedReason.getBytes(StandardCharsets.UTF_8);
ByteBuffer payload = ByteBuffer.allocate(utf.length + 2);
payload.putChar((char) StatusCode.NORMAL);
payload.put(utf, 0, utf.length);
payload.flip();
ByteBuffer buf = ByteBuffer.allocate(24);
// fin + close
buf.put((byte) (0x80 | OpCode.CLOSE));
buf.put((byte) (0x80 | payload.remaining()));
MaskedByteBuffer.putMask(buf);
MaskedByteBuffer.putPayload(buf, payload);
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.CLOSE, 1);
CloseInfo close = new CloseInfo(capture.getFrames().poll());
Assert.assertThat("CloseFrame.statusCode", close.getStatusCode(), is(StatusCode.NORMAL));
Assert.assertThat("CloseFrame.data", close.getReason(), is(expectedReason));
}
use of org.eclipse.jetty.websocket.common.test.UnitParser in project jetty.project by eclipse.
the class ParserTest method testParseCase5_15.
/**
* Similar to the server side 5.15 testcase. A normal 2 fragment text text message, followed by another continuation.
*/
@Test
public void testParseCase5_15() {
List<WebSocketFrame> send = new ArrayList<>();
send.add(new TextFrame().setPayload("fragment1").setFin(false));
send.add(new ContinuationFrame().setPayload("fragment2").setFin(true));
// bad frame
send.add(new ContinuationFrame().setPayload("fragment3").setFin(false));
send.add(new TextFrame().setPayload("fragment4").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(1);
capture.assertHasFrame(OpCode.TEXT, 1);
capture.assertHasFrame(OpCode.CONTINUATION, 1);
}
use of org.eclipse.jetty.websocket.common.test.UnitParser in project jetty.project by eclipse.
the class ParserTest method testParseCase6_2_3.
/**
* Similar to the server side 6.2.3 testcase. Lots of small 1 byte UTF8 Text frames, representing 1 overall text message.
*/
@Test
public void testParseCase6_2_3() {
String utf8 = "Hello-습@쎟쎤쎼쎠쎡-UTF-8!!";
byte[] msg = StringUtil.getUtf8Bytes(utf8);
List<WebSocketFrame> send = new ArrayList<>();
int textCount = 0;
int continuationCount = 0;
int len = msg.length;
boolean continuation = false;
byte[] mini;
for (int i = 0; i < len; i++) {
DataFrame frame = null;
if (continuation) {
frame = new ContinuationFrame();
continuationCount++;
} else {
frame = new TextFrame();
textCount++;
}
mini = new byte[1];
mini[0] = msg[i];
frame.setPayload(ByteBuffer.wrap(mini));
boolean isLast = (i >= (len - 1));
frame.setFin(isLast);
send.add(frame);
continuation = 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.parse(completeBuf);
capture.assertErrorCount(0);
capture.assertHasFrame(OpCode.TEXT, textCount);
capture.assertHasFrame(OpCode.CONTINUATION, continuationCount);
capture.assertHasFrame(OpCode.CLOSE, 1);
}
Aggregations