Search in sources :

Example 21 with WebSocketPolicy

use of org.eclipse.jetty.websocket.api.WebSocketPolicy in project jetty.project by eclipse.

the class ExtensionStackTest method createExtensionStack.

private ExtensionStack createExtensionStack() {
    WebSocketPolicy policy = WebSocketPolicy.newClientPolicy();
    WebSocketContainerScope container = new SimpleContainerScope(policy, bufferPool);
    WebSocketExtensionFactory factory = new WebSocketExtensionFactory(container);
    return new ExtensionStack(factory);
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) WebSocketContainerScope(org.eclipse.jetty.websocket.common.scopes.WebSocketContainerScope) SimpleContainerScope(org.eclipse.jetty.websocket.common.scopes.SimpleContainerScope)

Example 22 with WebSocketPolicy

use of org.eclipse.jetty.websocket.api.WebSocketPolicy in project jetty.project by eclipse.

the class GeneratorParserRoundtripTest method testParserAndGenerator.

@Test
public void testParserAndGenerator() throws Exception {
    WebSocketPolicy policy = WebSocketPolicy.newClientPolicy();
    Generator gen = new Generator(policy, bufferPool);
    Parser parser = new Parser(policy, bufferPool);
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    String message = "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF";
    ByteBuffer out = bufferPool.acquire(8192, false);
    try {
        // Generate Buffer
        BufferUtil.flipToFill(out);
        WebSocketFrame frame = new TextFrame().setPayload(message);
        ByteBuffer header = gen.generateHeaderBytes(frame);
        ByteBuffer payload = frame.getPayload();
        out.put(header);
        out.put(payload);
        // Parse Buffer
        BufferUtil.flipToFlush(out, 0);
        parser.parse(out);
    } finally {
        bufferPool.release(out);
    }
    // Validate
    capture.assertNoErrors();
    capture.assertHasFrame(OpCode.TEXT, 1);
    TextFrame txt = (TextFrame) capture.getFrames().poll();
    Assert.assertThat("Text parsed", txt.getPayloadAsUTF8(), is(message));
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 23 with WebSocketPolicy

use of org.eclipse.jetty.websocket.api.WebSocketPolicy in project jetty.project by eclipse.

the class GeneratorTest method testWindowedGenerateWithMasking.

@Test
public void testWindowedGenerateWithMasking() {
    // A decent sized frame, with masking
    byte[] payload = new byte[10240];
    Arrays.fill(payload, (byte) 0x55);
    byte[] mask = new byte[] { 0x2A, (byte) 0xF0, 0x0F, 0x00 };
    WebSocketFrame frame = new BinaryFrame().setPayload(payload);
    // masking!
    frame.setMask(mask);
    // Generate
    int windowSize = 2929;
    WindowHelper helper = new WindowHelper(windowSize);
    ByteBuffer completeBuffer = helper.generateWindowed(frame);
    // Validate
    int expectedHeaderSize = 8;
    int expectedSize = payload.length + expectedHeaderSize;
    int expectedParts = 1;
    helper.assertTotalParts(expectedParts);
    helper.assertTotalBytes(payload.length + expectedHeaderSize);
    Assert.assertThat("Generated Buffer", completeBuffer.remaining(), is(expectedSize));
    // Parse complete buffer.
    WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
    Parser parser = new UnitParser(policy);
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    parser.parse(completeBuffer);
    // Assert validity of frame
    WebSocketFrame actual = capture.getFrames().poll();
    Assert.assertThat("Frame.opcode", actual.getOpCode(), is(OpCode.BINARY));
    Assert.assertThat("Frame.payloadLength", actual.getPayloadLength(), is(payload.length));
    // Validate payload contents for proper masking
    ByteBuffer actualData = actual.getPayload().slice();
    Assert.assertThat("Frame.payload.remaining", actualData.remaining(), is(payload.length));
    while (actualData.remaining() > 0) {
        Assert.assertThat("Actual.payload[" + actualData.position() + "]", actualData.get(), is((byte) 0x55));
    }
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) BinaryFrame(org.eclipse.jetty.websocket.common.frames.BinaryFrame) 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 24 with WebSocketPolicy

use of org.eclipse.jetty.websocket.api.WebSocketPolicy in project jetty.project by eclipse.

the class ParserTest method testParseNothing.

@Test
public void testParseNothing() {
    ByteBuffer buf = ByteBuffer.allocate(16);
    // Put nothing in the buffer.
    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();
    Assert.assertThat("Frame Count", capture.getFrames().size(), is(0));
}
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 25 with WebSocketPolicy

use of org.eclipse.jetty.websocket.api.WebSocketPolicy 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

WebSocketPolicy (org.eclipse.jetty.websocket.api.WebSocketPolicy)36 Test (org.junit.Test)26 ByteBuffer (java.nio.ByteBuffer)24 IncomingFramesCapture (org.eclipse.jetty.websocket.common.test.IncomingFramesCapture)24 UnitParser (org.eclipse.jetty.websocket.common.test.UnitParser)23 Frame (org.eclipse.jetty.websocket.api.extensions.Frame)7 MaskedByteBuffer (org.eclipse.jetty.websocket.common.util.MaskedByteBuffer)7 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)6 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)6 Parser (org.eclipse.jetty.websocket.common.Parser)5 BinaryFrame (org.eclipse.jetty.websocket.common.frames.BinaryFrame)4 ExtensionConfig (org.eclipse.jetty.websocket.api.extensions.ExtensionConfig)3 Generator (org.eclipse.jetty.websocket.common.Generator)3 EventDriver (org.eclipse.jetty.websocket.common.events.EventDriver)3 SimpleContainerScope (org.eclipse.jetty.websocket.common.scopes.SimpleContainerScope)3 Before (org.junit.Before)3 URI (java.net.URI)2 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)2 WebSocketContainerScope (org.eclipse.jetty.websocket.common.scopes.WebSocketContainerScope)2 DummyConnection (org.eclipse.jetty.websocket.common.test.DummyConnection)2