Search in sources :

Example 1 with Parser

use of org.eclipse.jetty.websocket.common.Parser in project jetty.project by eclipse.

the class TestABCase2 method testParseHelloPingCase2_2.

@Test
public void testParseHelloPingCase2_2() {
    String message = "Hello, world!";
    byte[] messageBytes = message.getBytes();
    ByteBuffer expected = ByteBuffer.allocate(32);
    expected.put(new byte[] { (byte) 0x89 });
    // no masking
    byte b = 0x00;
    b |= messageBytes.length & 0x7F;
    expected.put(b);
    expected.put(messageBytes);
    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(message.length()));
    Assert.assertEquals("PingFrame.payload", message.length(), pActual.getPayloadLength());
}
Also used : Frame(org.eclipse.jetty.websocket.api.extensions.Frame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) 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) Parser(org.eclipse.jetty.websocket.common.Parser) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) Test(org.junit.Test)

Example 2 with Parser

use of org.eclipse.jetty.websocket.common.Parser in project jetty.project by eclipse.

the class TestABCase2 method testParseBinaryPingCase2_3.

@Test
public void testParseBinaryPingCase2_3() {
    byte[] bytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
    ByteBuffer expected = ByteBuffer.allocate(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());
}
Also used : Frame(org.eclipse.jetty.websocket.api.extensions.Frame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) 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) Parser(org.eclipse.jetty.websocket.common.Parser) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) Test(org.junit.Test)

Example 3 with Parser

use of org.eclipse.jetty.websocket.common.Parser in project jetty.project by eclipse.

the class TestABCase4 method testParserNonControlOpCode3Case4_1_1.

@Test
public void testParserNonControlOpCode3Case4_1_1() throws Exception {
    ByteBuffer expected = ByteBuffer.allocate(32);
    expected.put(new byte[] { (byte) 0x83, 0x00 });
    expected.flip();
    IncomingFramesCapture capture = new IncomingFramesCapture();
    try (StacklessLogging logging = new StacklessLogging(Parser.class)) {
        Parser parser = new UnitParser(policy);
        parser.setIncomingFramesHandler(capture);
        try {
            parser.parse(expected);
        } catch (ProtocolException ignore) {
        // ignore
        }
    }
    Assert.assertEquals("error on undefined opcode", 1, capture.getErrorCount(WebSocketException.class));
    Throwable known = capture.getErrors().poll();
    Assert.assertTrue("undefined option should be in message", known.getMessage().contains("Unknown opcode: 3"));
}
Also used : ProtocolException(org.eclipse.jetty.websocket.api.ProtocolException) WebSocketException(org.eclipse.jetty.websocket.api.WebSocketException) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) 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 4 with Parser

use of org.eclipse.jetty.websocket.common.Parser in project jetty.project by eclipse.

the class TestABCase4 method testParserControlOpCode12WithPayloadCase4_2_2.

@Test
public void testParserControlOpCode12WithPayloadCase4_2_2() throws Exception {
    ByteBuffer expected = ByteBuffer.allocate(32);
    expected.put(new byte[] { (byte) 0x8c, 0x01, 0x00 });
    expected.flip();
    IncomingFramesCapture capture = new IncomingFramesCapture();
    try (StacklessLogging logging = new StacklessLogging(Parser.class)) {
        Parser parser = new UnitParser(policy);
        parser.setIncomingFramesHandler(capture);
        try {
            parser.parse(expected);
        } catch (ProtocolException ignore) {
        // ignore
        }
    }
    Assert.assertEquals("error on undefined opcode", 1, capture.getErrorCount(WebSocketException.class));
    Throwable known = capture.getErrors().poll();
    Assert.assertTrue("undefined option should be in message", known.getMessage().contains("Unknown opcode: 12"));
}
Also used : ProtocolException(org.eclipse.jetty.websocket.api.ProtocolException) WebSocketException(org.eclipse.jetty.websocket.api.WebSocketException) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) 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 5 with Parser

use of org.eclipse.jetty.websocket.common.Parser in project jetty.project by eclipse.

the class TestABCase4 method testParserControlOpCode11Case4_2_1.

@Test
public void testParserControlOpCode11Case4_2_1() throws Exception {
    ByteBuffer expected = ByteBuffer.allocate(32);
    expected.put(new byte[] { (byte) 0x8b, 0x00 });
    expected.flip();
    IncomingFramesCapture capture = new IncomingFramesCapture();
    try (StacklessLogging logging = new StacklessLogging(Parser.class)) {
        Parser parser = new UnitParser(policy);
        parser.setIncomingFramesHandler(capture);
        try {
            parser.parse(expected);
        } catch (ProtocolException ignore) {
        // ignore
        }
    }
    Assert.assertEquals("error on undefined opcode", 1, capture.getErrorCount(WebSocketException.class));
    Throwable known = capture.getErrors().poll();
    Assert.assertTrue("undefined option should be in message", known.getMessage().contains("Unknown opcode: 11"));
}
Also used : ProtocolException(org.eclipse.jetty.websocket.api.ProtocolException) WebSocketException(org.eclipse.jetty.websocket.api.WebSocketException) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) 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)

Aggregations

ByteBuffer (java.nio.ByteBuffer)27 Parser (org.eclipse.jetty.websocket.common.Parser)27 IncomingFramesCapture (org.eclipse.jetty.websocket.common.test.IncomingFramesCapture)27 UnitParser (org.eclipse.jetty.websocket.common.test.UnitParser)27 Test (org.junit.Test)26 Frame (org.eclipse.jetty.websocket.api.extensions.Frame)22 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)19 BinaryFrame (org.eclipse.jetty.websocket.common.frames.BinaryFrame)7 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)7 WebSocketPolicy (org.eclipse.jetty.websocket.api.WebSocketPolicy)5 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)4 ProtocolException (org.eclipse.jetty.websocket.api.ProtocolException)4 WebSocketException (org.eclipse.jetty.websocket.api.WebSocketException)4 CloseFrame (org.eclipse.jetty.websocket.common.frames.CloseFrame)4 PingFrame (org.eclipse.jetty.websocket.common.frames.PingFrame)4 ExtensionConfig (org.eclipse.jetty.websocket.api.extensions.ExtensionConfig)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1