Search in sources :

Example 31 with UnitParser

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

the class TestABCase1_1 method testParseEmptyTextCase1_1_1.

@Test
public void testParseEmptyTextCase1_1_1() {
    ByteBuffer expected = ByteBuffer.allocate(5);
    expected.put(new byte[] { (byte) 0x81, (byte) 0x00 });
    expected.flip();
    Parser parser = new UnitParser(policy);
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    parser.parse(expected);
    capture.assertNoErrors();
    capture.assertHasFrame(OpCode.TEXT, 1);
    Frame pActual = capture.getFrames().poll();
    Assert.assertThat("TextFrame.payloadLength", pActual.getPayloadLength(), is(0));
}
Also used : Frame(org.eclipse.jetty.websocket.api.extensions.Frame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) 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) Parser(org.eclipse.jetty.websocket.common.Parser) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) Test(org.junit.Test)

Example 32 with UnitParser

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

the class TestABCase1_1 method testParse126ByteTextCase1_1_3.

@Test
public void testParse126ByteTextCase1_1_3() {
    int length = 126;
    ByteBuffer expected = ByteBuffer.allocate(length + 5);
    expected.put(new byte[] { (byte) 0x81 });
    // no masking
    byte b = 0x00;
    b |= length & 0x7E;
    expected.put(b);
    expected.putShort((short) length);
    for (int i = 0; i < length; ++i) {
        expected.put("*".getBytes());
    }
    expected.flip();
    Parser parser = new UnitParser(policy);
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    parser.parse(expected);
    capture.assertNoErrors();
    capture.assertHasFrame(OpCode.TEXT, 1);
    Frame pActual = capture.getFrames().poll();
    Assert.assertThat("TextFrame.payloadLength", pActual.getPayloadLength(), is(length));
// Assert.assertEquals("TextFrame.payload",length,pActual.getPayloadData().length);
}
Also used : Frame(org.eclipse.jetty.websocket.api.extensions.Frame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) 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) Parser(org.eclipse.jetty.websocket.common.Parser) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) Test(org.junit.Test)

Example 33 with UnitParser

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

the class TestABCase1_1 method testParse127ByteTextCase1_1_4.

@Test
public void testParse127ByteTextCase1_1_4() {
    int length = 127;
    ByteBuffer expected = ByteBuffer.allocate(length + 5);
    expected.put(new byte[] { (byte) 0x81 });
    // no masking
    byte b = 0x00;
    b |= length & 0x7E;
    expected.put(b);
    expected.putShort((short) length);
    for (int i = 0; i < length; ++i) {
        expected.put("*".getBytes());
    }
    expected.flip();
    Parser parser = new UnitParser(policy);
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    parser.parse(expected);
    capture.assertNoErrors();
    capture.assertHasFrame(OpCode.TEXT, 1);
    Frame pActual = capture.getFrames().poll();
    Assert.assertThat("TextFrame.payloadLength", pActual.getPayloadLength(), is(length));
// Assert.assertEquals("TextFrame.payload",length,pActual.getPayloadData().length);
}
Also used : Frame(org.eclipse.jetty.websocket.api.extensions.Frame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) 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) Parser(org.eclipse.jetty.websocket.common.Parser) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) Test(org.junit.Test)

Example 34 with UnitParser

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

the class TestABCase1_2 method testParse128ByteBinaryCase1_2_5.

@Test
public void testParse128ByteBinaryCase1_2_5() {
    int length = 128;
    ByteBuffer expected = ByteBuffer.allocate(length + 5);
    expected.put(new byte[] { (byte) 0x82 });
    // no masking
    byte b = 0x00;
    b |= 0x7E;
    expected.put(b);
    expected.putShort((short) length);
    for (int i = 0; i < length; ++i) {
        expected.put("*".getBytes());
    }
    expected.flip();
    Parser parser = new UnitParser(policy);
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    parser.parse(expected);
    capture.assertNoErrors();
    capture.assertHasFrame(OpCode.BINARY, 1);
    Frame pActual = capture.getFrames().poll();
    Assert.assertThat("BinaryFrame.payloadLength", pActual.getPayloadLength(), is(length));
// Assert.assertEquals("BinaryFrame.payload",length,pActual.getPayloadData().length);
}
Also used : Frame(org.eclipse.jetty.websocket.api.extensions.Frame) BinaryFrame(org.eclipse.jetty.websocket.common.frames.BinaryFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.websocket.common.Parser) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) Test(org.junit.Test)

Example 35 with UnitParser

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

the class RFC6455ExamplesParserTest method testFragmentedUnmaskedTextMessage.

@Test
public void testFragmentedUnmaskedTextMessage() {
    WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.CLIENT);
    Parser parser = new UnitParser(policy);
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    ByteBuffer buf = ByteBuffer.allocate(16);
    BufferUtil.clearToFill(buf);
    // Raw bytes as found in RFC 6455, Section 5.7 - Examples
    // A fragmented unmasked text message (part 1 of 2 "Hel")
    buf.put(new byte[] { (byte) 0x01, (byte) 0x03, 0x48, (byte) 0x65, 0x6c });
    // Parse #1
    BufferUtil.flipToFlush(buf, 0);
    parser.parse(buf);
    // part 2 of 2 "lo" (A continuation frame of the prior text message)
    BufferUtil.flipToFill(buf);
    buf.put(new byte[] { (byte) 0x80, 0x02, 0x6c, 0x6f });
    // Parse #2
    BufferUtil.flipToFlush(buf, 0);
    parser.parse(buf);
    capture.assertNoErrors();
    capture.assertHasFrame(OpCode.TEXT, 1);
    capture.assertHasFrame(OpCode.CONTINUATION, 1);
    WebSocketFrame txt = capture.getFrames().poll();
    String actual = BufferUtil.toUTF8String(txt.getPayload());
    Assert.assertThat("TextFrame[0].data", actual, is("Hel"));
    txt = capture.getFrames().poll();
    actual = BufferUtil.toUTF8String(txt.getPayload());
    Assert.assertThat("TextFrame[1].data", actual, is("lo"));
}
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