Search in sources :

Example 6 with PingFrame

use of org.eclipse.jetty.websocket.common.frames.PingFrame in project jetty.project by eclipse.

the class PerMessageDeflateExtensionTest method testOutgoingPing.

/**
     * Outgoing PING (Control Frame) should pass through extension unmodified
     * @throws IOException on test failure
     */
@Test
public void testOutgoingPing() throws IOException {
    PerMessageDeflateExtension ext = new PerMessageDeflateExtension();
    ext.setBufferPool(bufferPool);
    ext.setPolicy(WebSocketPolicy.newServerPolicy());
    ExtensionConfig config = ExtensionConfig.parse("permessage-deflate");
    ext.setConfig(config);
    // Setup capture of outgoing frames
    OutgoingFramesCapture capture = new OutgoingFramesCapture();
    // Wire up stack
    ext.setNextOutgoingFrames(capture);
    String payload = "Are you there?";
    Frame ping = new PingFrame().setPayload(payload);
    ext.outgoingFrame(ping, null, BatchMode.OFF);
    capture.assertFrameCount(1);
    capture.assertHasFrame(OpCode.PING, 1);
    WebSocketFrame actual = capture.getFrames().getFirst();
    Assert.assertThat("Frame.opcode", actual.getOpCode(), is(OpCode.PING));
    Assert.assertThat("Frame.fin", actual.isFin(), is(true));
    Assert.assertThat("Frame.rsv1", actual.isRsv1(), is(false));
    Assert.assertThat("Frame.rsv2", actual.isRsv2(), is(false));
    Assert.assertThat("Frame.rsv3", actual.isRsv3(), is(false));
    ByteBuffer expected = BufferUtil.toBuffer(payload, StandardCharsets.UTF_8);
    Assert.assertThat("Frame.payloadLength", actual.getPayloadLength(), is(expected.remaining()));
    ByteBufferAssert.assertEquals("Frame.payload", expected, actual.getPayload().slice());
}
Also used : WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) Frame(org.eclipse.jetty.websocket.api.extensions.Frame) ContinuationFrame(org.eclipse.jetty.websocket.common.frames.ContinuationFrame) PingFrame(org.eclipse.jetty.websocket.common.frames.PingFrame) PingFrame(org.eclipse.jetty.websocket.common.frames.PingFrame) ExtensionConfig(org.eclipse.jetty.websocket.api.extensions.ExtensionConfig) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) ByteBuffer(java.nio.ByteBuffer) OutgoingFramesCapture(org.eclipse.jetty.websocket.common.test.OutgoingFramesCapture) AbstractExtensionTest(org.eclipse.jetty.websocket.common.extensions.AbstractExtensionTest) Test(org.junit.Test)

Example 7 with PingFrame

use of org.eclipse.jetty.websocket.common.frames.PingFrame in project jetty.project by eclipse.

the class PerMessageDeflateExtensionTest method testIncomingPing.

/**
     * Incoming PING (Control Frame) should pass through extension unmodified
     */
@Test
public void testIncomingPing() {
    PerMessageDeflateExtension ext = new PerMessageDeflateExtension();
    ext.setBufferPool(bufferPool);
    ext.setPolicy(WebSocketPolicy.newServerPolicy());
    ExtensionConfig config = ExtensionConfig.parse("permessage-deflate");
    ext.setConfig(config);
    // Setup capture of incoming frames
    IncomingFramesCapture capture = new IncomingFramesCapture();
    // Wire up stack
    ext.setNextIncomingFrames(capture);
    String payload = "Are you there?";
    Frame ping = new PingFrame().setPayload(payload);
    ext.incomingFrame(ping);
    capture.assertFrameCount(1);
    capture.assertHasFrame(OpCode.PING, 1);
    WebSocketFrame actual = capture.getFrames().poll();
    Assert.assertThat("Frame.opcode", actual.getOpCode(), is(OpCode.PING));
    Assert.assertThat("Frame.fin", actual.isFin(), is(true));
    Assert.assertThat("Frame.rsv1", actual.isRsv1(), is(false));
    Assert.assertThat("Frame.rsv2", actual.isRsv2(), is(false));
    Assert.assertThat("Frame.rsv3", actual.isRsv3(), is(false));
    ByteBuffer expected = BufferUtil.toBuffer(payload, StandardCharsets.UTF_8);
    Assert.assertThat("Frame.payloadLength", actual.getPayloadLength(), is(expected.remaining()));
    ByteBufferAssert.assertEquals("Frame.payload", expected, actual.getPayload().slice());
}
Also used : WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) Frame(org.eclipse.jetty.websocket.api.extensions.Frame) ContinuationFrame(org.eclipse.jetty.websocket.common.frames.ContinuationFrame) PingFrame(org.eclipse.jetty.websocket.common.frames.PingFrame) PingFrame(org.eclipse.jetty.websocket.common.frames.PingFrame) ExtensionConfig(org.eclipse.jetty.websocket.api.extensions.ExtensionConfig) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) ByteBuffer(java.nio.ByteBuffer) AbstractExtensionTest(org.eclipse.jetty.websocket.common.extensions.AbstractExtensionTest) Test(org.junit.Test)

Example 8 with PingFrame

use of org.eclipse.jetty.websocket.common.frames.PingFrame in project jetty.project by eclipse.

the class EventDriverTest method testListenerPingPong.

@Test
public void testListenerPingPong() throws Exception {
    ListenerPingPongSocket socket = new ListenerPingPongSocket();
    EventDriver driver = wrap(socket);
    try (LocalWebSocketSession conn = new CloseableLocalWebSocketSession(container, testname, driver)) {
        conn.start();
        conn.open();
        driver.incomingFrame(new PingFrame().setPayload("PING"));
        driver.incomingFrame(new PongFrame().setPayload("PONG"));
        driver.incomingFrame(new CloseInfo(StatusCode.NORMAL).asFrame());
        socket.capture.assertEventCount(4);
        socket.capture.pop().assertEventStartsWith("onWebSocketConnect");
        socket.capture.pop().assertEventStartsWith("onWebSocketPing(");
        socket.capture.pop().assertEventStartsWith("onWebSocketPong(");
        socket.capture.pop().assertEventStartsWith("onWebSocketClose(1000,");
    }
}
Also used : PongFrame(org.eclipse.jetty.websocket.common.frames.PongFrame) LocalWebSocketSession(org.eclipse.jetty.websocket.common.io.LocalWebSocketSession) CloseableLocalWebSocketSession(org.eclipse.jetty.websocket.common.io.CloseableLocalWebSocketSession) PingFrame(org.eclipse.jetty.websocket.common.frames.PingFrame) CloseableLocalWebSocketSession(org.eclipse.jetty.websocket.common.io.CloseableLocalWebSocketSession) ListenerPingPongSocket(examples.ListenerPingPongSocket) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo) Test(org.junit.Test)

Example 9 with PingFrame

use of org.eclipse.jetty.websocket.common.frames.PingFrame in project jetty.project by eclipse.

the class TestABCase2 method testGenerateOversizedBinaryPingCase2_5_A.

@Test(expected = WebSocketException.class)
public void testGenerateOversizedBinaryPingCase2_5_A() {
    byte[] bytes = new byte[126];
    Arrays.fill(bytes, (byte) 0x00);
    PingFrame pingFrame = new PingFrame();
    // should throw exception
    pingFrame.setPayload(ByteBuffer.wrap(bytes));
}
Also used : PingFrame(org.eclipse.jetty.websocket.common.frames.PingFrame) Test(org.junit.Test)

Example 10 with PingFrame

use of org.eclipse.jetty.websocket.common.frames.PingFrame in project jetty.project by eclipse.

the class TestABCase2 method testGenerateEmptyPingCase2_1.

@Test
public void testGenerateEmptyPingCase2_1() {
    WebSocketFrame pingFrame = new PingFrame();
    ByteBuffer actual = UnitGenerator.generate(pingFrame);
    ByteBuffer expected = ByteBuffer.allocate(5);
    expected.put(new byte[] { (byte) 0x89, (byte) 0x00 });
    expected.flip();
    ByteBufferAssert.assertEquals("buffers do not match", expected, actual);
}
Also used : PingFrame(org.eclipse.jetty.websocket.common.frames.PingFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

PingFrame (org.eclipse.jetty.websocket.common.frames.PingFrame)48 Test (org.junit.Test)45 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)33 CloseInfo (org.eclipse.jetty.websocket.common.CloseInfo)30 ArrayList (java.util.ArrayList)28 Fuzzer (org.eclipse.jetty.websocket.common.test.Fuzzer)27 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)24 ByteBuffer (java.nio.ByteBuffer)19 PongFrame (org.eclipse.jetty.websocket.common.frames.PongFrame)19 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)17 ContinuationFrame (org.eclipse.jetty.websocket.common.frames.ContinuationFrame)14 ExtensionConfig (org.eclipse.jetty.websocket.api.extensions.ExtensionConfig)4 Frame (org.eclipse.jetty.websocket.api.extensions.Frame)4 IncomingFramesCapture (org.eclipse.jetty.websocket.common.test.IncomingFramesCapture)4 BinaryFrame (org.eclipse.jetty.websocket.common.frames.BinaryFrame)3 CloseableLocalWebSocketSession (org.eclipse.jetty.websocket.common.io.CloseableLocalWebSocketSession)3 LocalWebSocketSession (org.eclipse.jetty.websocket.common.io.LocalWebSocketSession)3 ListenerPingPongSocket (examples.ListenerPingPongSocket)2 AbstractExtensionTest (org.eclipse.jetty.websocket.common.extensions.AbstractExtensionTest)2 FragmentExtension (org.eclipse.jetty.websocket.common.extensions.fragment.FragmentExtension)2