Search in sources :

Example 6 with WebSocketPolicy

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

the class PerMessageDeflateExtensionTest method testPerMessageDeflateDefault.

/**
     * Default configuration for permessage-deflate
     * @throws Exception on test failure
     */
@Test
public void testPerMessageDeflateDefault() throws Exception {
    Assume.assumeTrue("Server has permessage-deflate registered", server.getWebSocketServletFactory().getExtensionFactory().isAvailable("permessage-deflate"));
    Assert.assertThat("server scheme", server.getServerUri().getScheme(), is(scheme));
    int binBufferSize = (int) (msgSize * 1.5);
    WebSocketPolicy serverPolicy = server.getWebSocketServletFactory().getPolicy();
    // Ensure binBufferSize is sane (not smaller then other buffers)
    binBufferSize = Math.max(binBufferSize, serverPolicy.getMaxBinaryMessageSize());
    binBufferSize = Math.max(binBufferSize, serverPolicy.getMaxBinaryMessageBufferSize());
    binBufferSize = Math.max(binBufferSize, this.inputBufferSize);
    serverPolicy.setMaxBinaryMessageSize(binBufferSize);
    serverPolicy.setMaxBinaryMessageBufferSize(binBufferSize);
    WebSocketClient client = new WebSocketClient(server.getSslContextFactory());
    WebSocketPolicy clientPolicy = client.getPolicy();
    clientPolicy.setMaxBinaryMessageSize(binBufferSize);
    clientPolicy.setMaxBinaryMessageBufferSize(binBufferSize);
    if (inputBufferSize > 0) {
        clientPolicy.setInputBufferSize(inputBufferSize);
    }
    try {
        client.start();
        // Make sure the read times out if there are problems with the implementation
        client.setMaxIdleTimeout(TimeUnit.SECONDS.toMillis(15));
        CaptureSocket clientSocket = new CaptureSocket();
        ClientUpgradeRequest request = new ClientUpgradeRequest();
        request.addExtensions("permessage-deflate");
        request.setSubProtocols("echo");
        Future<Session> fut = client.connect(clientSocket, server.getServerUri(), request);
        // Wait for connect
        Session session = fut.get(30, TimeUnit.SECONDS);
        assertThat("Response.extensions", getNegotiatedExtensionList(session), containsString("permessage-deflate"));
        // Create message
        byte[] msg = new byte[msgSize];
        Random rand = new Random();
        rand.setSeed(8080);
        rand.nextBytes(msg);
        // Calculate sha1
        String sha1 = Sha1Sum.calculate(msg);
        // Client sends first message
        session.getRemote().sendBytes(ByteBuffer.wrap(msg));
        clientSocket.messages.awaitEventCount(1, 5, TimeUnit.SECONDS);
        String echoMsg = clientSocket.messages.poll();
        Assert.assertThat("Echo'd Message", echoMsg, is("binary[sha1=" + sha1 + "]"));
    } finally {
        client.stop();
    }
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) Random(java.util.Random) CaptureSocket(org.eclipse.jetty.websocket.server.helper.CaptureSocket) ClientUpgradeRequest(org.eclipse.jetty.websocket.client.ClientUpgradeRequest) Matchers.containsString(org.hamcrest.Matchers.containsString) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Example 7 with WebSocketPolicy

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

the class AbstractABCase method initGenerators.

@Before
public void initGenerators() {
    WebSocketPolicy policy = WebSocketPolicy.newClientPolicy();
    strictGenerator = new Generator(policy, bufferPool, true);
    laxGenerator = new Generator(policy, bufferPool, false);
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) Generator(org.eclipse.jetty.websocket.common.Generator) Before(org.junit.Before)

Example 8 with WebSocketPolicy

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

the class ClosePayloadParserTest method testGameOver.

@Test
public void testGameOver() {
    String expectedReason = "Game Over";
    byte[] utf = expectedReason.getBytes(StandardCharsets.UTF_8);
    ByteBuffer payload = ByteBuffer.allocate(utf.length + 2);
    payload.putChar((char) StatusCode.NORMAL);
    payload.put(utf, 0, utf.length);
    payload.flip();
    ByteBuffer buf = ByteBuffer.allocate(24);
    // fin + close
    buf.put((byte) (0x80 | OpCode.CLOSE));
    buf.put((byte) (0x80 | payload.remaining()));
    MaskedByteBuffer.putMask(buf);
    MaskedByteBuffer.putPayload(buf, payload);
    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.CLOSE, 1);
    CloseInfo close = new CloseInfo(capture.getFrames().poll());
    Assert.assertThat("CloseFrame.statusCode", close.getStatusCode(), is(StatusCode.NORMAL));
    Assert.assertThat("CloseFrame.data", close.getReason(), is(expectedReason));
}
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) MaskedByteBuffer(org.eclipse.jetty.websocket.common.util.MaskedByteBuffer) ByteBuffer(java.nio.ByteBuffer) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) Test(org.junit.Test)

Example 9 with WebSocketPolicy

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

the class RFC6455ExamplesParserTest method testSingleUnmasked64KByteBinaryMessage.

@Test
public void testSingleUnmasked64KByteBinaryMessage() {
    int dataSize = 1024 * 64;
    ByteBuffer buf = ByteBuffer.allocate((dataSize + 10));
    // Raw bytes as found in RFC 6455, Section 5.7 - Examples
    // 64 Kbytes binary message in a single unmasked frame
    buf.put(new byte[] { (byte) 0x82, 0x7F });
    // 64bit size
    buf.putLong(dataSize);
    for (int i = 0; i < dataSize; i++) {
        buf.put((byte) 0x77);
    }
    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) 0x77));
    }
}
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 10 with WebSocketPolicy

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

the class RFC6455ExamplesParserTest method testSingleMaskedPongRequest.

@Test
public void testSingleMaskedPongRequest() {
    ByteBuffer buf = ByteBuffer.allocate(16);
    // Raw bytes as found in RFC 6455, Section 5.7 - Examples
    // Unmasked Pong request
    buf.put(new byte[] { (byte) 0x8a, (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.PONG, 1);
    WebSocketFrame pong = capture.getFrames().poll();
    String actual = BufferUtil.toUTF8String(pong.getPayload());
    Assert.assertThat("PongFrame.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

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