Search in sources :

Example 41 with UnitParser

use of org.eclipse.jetty.websocket.common.test.UnitParser in project jetty.project by eclipse.

the class ParserTest method testParseCase5_6.

/**
     * Similar to the server side 5.6 testcase. pong, then text, then close frames.
     */
@Test
public void testParseCase5_6() {
    List<WebSocketFrame> send = new ArrayList<>();
    send.add(new PongFrame().setPayload("ping"));
    send.add(new TextFrame().setPayload("hello, world"));
    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, 1);
    capture.assertHasFrame(OpCode.CLOSE, 1);
    capture.assertHasFrame(OpCode.PONG, 1);
}
Also used : PongFrame(org.eclipse.jetty.websocket.common.frames.PongFrame) ArrayList(java.util.ArrayList) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 42 with UnitParser

use of org.eclipse.jetty.websocket.common.test.UnitParser in project jetty.project by eclipse.

the class PingPayloadParserTest method testBasicPingParsing.

@Test
public void testBasicPingParsing() {
    ByteBuffer buf = ByteBuffer.allocate(16);
    BufferUtil.clearToFill(buf);
    buf.put(new byte[] { (byte) 0x89, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f });
    BufferUtil.flipToFlush(buf, 0);
    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.PING, 1);
    PingFrame ping = (PingFrame) capture.getFrames().poll();
    String actual = BufferUtil.toUTF8String(ping.getPayload());
    Assert.assertThat("PingFrame.payload", actual, is("Hello"));
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) PingFrame(org.eclipse.jetty.websocket.common.frames.PingFrame) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) ByteBuffer(java.nio.ByteBuffer) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) Test(org.junit.Test)

Example 43 with UnitParser

use of org.eclipse.jetty.websocket.common.test.UnitParser in project jetty.project by eclipse.

the class RFC6455ExamplesParserTest method testSingleUnmasked256ByteBinaryMessage.

@Test
public void testSingleUnmasked256ByteBinaryMessage() {
    int dataSize = 256;
    ByteBuffer buf = ByteBuffer.allocate(dataSize + 10);
    // Raw bytes as found in RFC 6455, Section 5.7 - Examples
    // 256 bytes binary message in a single unmasked frame
    buf.put(new byte[] { (byte) 0x82, 0x7E });
    // 16 bit size
    buf.putShort((short) 0x01_00);
    for (int i = 0; i < dataSize; i++) {
        buf.put((byte) 0x44);
    }
    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) 0x44));
    }
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) Frame(org.eclipse.jetty.websocket.api.extensions.Frame) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) ByteBuffer(java.nio.ByteBuffer) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) Test(org.junit.Test)

Example 44 with UnitParser

use of org.eclipse.jetty.websocket.common.test.UnitParser in project jetty.project by eclipse.

the class RFC6455ExamplesParserTest method testSingleUnmaskedTextMessage.

@Test
public void testSingleUnmaskedTextMessage() {
    ByteBuffer buf = ByteBuffer.allocate(16);
    // Raw bytes as found in RFC 6455, Section 5.7 - Examples
    // A single-frame unmasked text message
    buf.put(new byte[] { (byte) 0x81, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f });
    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.TEXT, 1);
    WebSocketFrame txt = capture.getFrames().poll();
    String actual = BufferUtil.toUTF8String(txt.getPayload());
    Assert.assertThat("TextFrame.payload", actual, is("Hello"));
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) ByteBuffer(java.nio.ByteBuffer) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) Test(org.junit.Test)

Example 45 with UnitParser

use of org.eclipse.jetty.websocket.common.test.UnitParser in project jetty.project by eclipse.

the class RFC6455ExamplesParserTest method testSingleMaskedTextMessage.

@Test
public void testSingleMaskedTextMessage() {
    ByteBuffer buf = ByteBuffer.allocate(16);
    // Raw bytes as found in RFC 6455, Section 5.7 - Examples
    // A single-frame masked text message
    buf.put(new byte[] { (byte) 0x81, (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.TEXT, 1);
    WebSocketFrame txt = capture.getFrames().poll();
    String actual = BufferUtil.toUTF8String(txt.getPayload());
    Assert.assertThat("TextFrame.payload", actual, is("Hello"));
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) ByteBuffer(java.nio.ByteBuffer) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) Test(org.junit.Test)

Aggregations

ByteBuffer (java.nio.ByteBuffer)53 IncomingFramesCapture (org.eclipse.jetty.websocket.common.test.IncomingFramesCapture)53 UnitParser (org.eclipse.jetty.websocket.common.test.UnitParser)53 Test (org.junit.Test)52 Parser (org.eclipse.jetty.websocket.common.Parser)27 Frame (org.eclipse.jetty.websocket.api.extensions.Frame)24 WebSocketPolicy (org.eclipse.jetty.websocket.api.WebSocketPolicy)23 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)19 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)13 BinaryFrame (org.eclipse.jetty.websocket.common.frames.BinaryFrame)8 ProtocolException (org.eclipse.jetty.websocket.api.ProtocolException)7 MaskedByteBuffer (org.eclipse.jetty.websocket.common.util.MaskedByteBuffer)7 ArrayList (java.util.ArrayList)6 PingFrame (org.eclipse.jetty.websocket.common.frames.PingFrame)6 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)4 WebSocketException (org.eclipse.jetty.websocket.api.WebSocketException)4 CloseFrame (org.eclipse.jetty.websocket.common.frames.CloseFrame)4 ContinuationFrame (org.eclipse.jetty.websocket.common.frames.ContinuationFrame)3 MessageTooLargeException (org.eclipse.jetty.websocket.api.MessageTooLargeException)1 ExtensionConfig (org.eclipse.jetty.websocket.api.extensions.ExtensionConfig)1