use of org.eclipse.jetty.websocket.common.test.IncomingFramesCapture in project jetty.project by eclipse.
the class ClientCloseTest method confirmConnection.
private void confirmConnection(CloseTrackingSocket clientSocket, Future<Session> clientFuture, IBlockheadServerConnection serverConns) throws Exception {
// Wait for client connect on via future
clientFuture.get(30, TimeUnit.SECONDS);
// Wait for client connect via client websocket
assertThat("Client WebSocket is Open", clientSocket.openLatch.await(30, TimeUnit.SECONDS), is(true));
try {
// Send message from client to server
final String echoMsg = "echo-test";
Future<Void> testFut = clientSocket.getRemote().sendStringByFuture(echoMsg);
// Wait for send future
testFut.get(30, TimeUnit.SECONDS);
// Read Frame on server side
IncomingFramesCapture serverCapture = serverConns.readFrames(1, 30, TimeUnit.SECONDS);
serverCapture.assertNoErrors();
serverCapture.assertFrameCount(1);
WebSocketFrame frame = serverCapture.getFrames().poll();
assertThat("Server received frame", frame.getOpCode(), is(OpCode.TEXT));
assertThat("Server received frame payload", frame.getPayloadAsUTF8(), is(echoMsg));
// Server send echo reply
serverConns.write(new TextFrame().setPayload(echoMsg));
// Wait for received echo
clientSocket.messageQueue.awaitEventCount(1, 1, TimeUnit.SECONDS);
// Verify received message
String recvMsg = clientSocket.messageQueue.poll();
assertThat("Received message", recvMsg, is(echoMsg));
// Verify that there are no errors
assertThat("Error events", clientSocket.error.get(), nullValue());
} finally {
clientSocket.clearQueues();
}
}
use of org.eclipse.jetty.websocket.common.test.IncomingFramesCapture 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.IncomingFramesCapture 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.IncomingFramesCapture 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);
}
use of org.eclipse.jetty.websocket.common.test.IncomingFramesCapture in project jetty.project by eclipse.
the class RFC6455ExamplesParserTest method testSingleUnmasked64KByteBinaryMessage.
@Test
public void testSingleUnmasked64KByteBinaryMessage() {
int dataSize = 1024 * 64;
ByteBuffer buf = ByteBuffer.allocate((dataSize + 10));
// Raw bytes as found in RFC 6455, Section 5.7 - Examples
// 64 Kbytes binary message in a single unmasked frame
buf.put(new byte[] { (byte) 0x82, 0x7F });
// 64bit size
buf.putLong(dataSize);
for (int i = 0; i < dataSize; i++) {
buf.put((byte) 0x77);
}
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) 0x77));
}
}
Aggregations