Search in sources :

Example 56 with WebSocketFrame

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

the class FragmentExtensionTest method testFragmentExtension.

@Test
public void testFragmentExtension() throws Exception {
    int fragSize = 4;
    BlockheadClient client = new BlockheadClient(server.getServerUri());
    client.clearExtensions();
    client.addExtensions("fragment;maxLength=" + fragSize);
    client.setProtocols("onConnect");
    try {
        // Make sure the read times out if there are problems with the implementation
        client.setTimeout(1, TimeUnit.SECONDS);
        client.connect();
        client.sendStandardRequest();
        HttpResponse resp = client.expectUpgradeResponse();
        Assert.assertThat("Response", resp.getExtensionsHeader(), containsString("fragment"));
        String msg = "Sent as a long message that should be split";
        client.write(new TextFrame().setPayload(msg));
        String[] parts = split(msg, fragSize);
        EventQueue<WebSocketFrame> frames = client.readFrames(parts.length, 1000, TimeUnit.MILLISECONDS);
        for (int i = 0; i < parts.length; i++) {
            WebSocketFrame frame = frames.poll();
            Assert.assertThat("text[" + i + "].payload", frame.getPayloadAsUTF8(), is(parts[i]));
        }
    } finally {
        client.close();
    }
}
Also used : BlockheadClient(org.eclipse.jetty.websocket.common.test.BlockheadClient) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) HttpResponse(org.eclipse.jetty.websocket.common.test.HttpResponse) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 57 with WebSocketFrame

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

the class IdentityExtensionTest method testIdentityExtension.

@Test
public void testIdentityExtension() throws Exception {
    BlockheadClient client = new BlockheadClient(server.getServerUri());
    client.clearExtensions();
    client.addExtensions("identity;param=0");
    client.addExtensions("identity;param=1, identity ; param = '2' ; other = ' some = value '");
    client.setProtocols("onConnect");
    try {
        // Make sure the read times out if there are problems with the implementation
        client.setTimeout(1, TimeUnit.SECONDS);
        client.connect();
        client.sendStandardRequest();
        HttpResponse resp = client.expectUpgradeResponse();
        Assert.assertThat("Response", resp.getExtensionsHeader(), containsString("identity"));
        client.write(new TextFrame().setPayload("Hello"));
        EventQueue<WebSocketFrame> frames = client.readFrames(1, 1000, TimeUnit.MILLISECONDS);
        WebSocketFrame frame = frames.poll();
        Assert.assertThat("TEXT.payload", frame.getPayloadAsUTF8(), is("Hello"));
    } finally {
        client.close();
    }
}
Also used : BlockheadClient(org.eclipse.jetty.websocket.common.test.BlockheadClient) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) HttpResponse(org.eclipse.jetty.websocket.common.test.HttpResponse) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) Test(org.junit.Test)

Example 58 with WebSocketFrame

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

the class IncomingFramesCapture method incomingFrame.

@Override
public void incomingFrame(Frame frame) {
    WebSocketFrame copy = WebSocketFrame.copy(frame);
    // TODO: might need to make this optional (depending on use by client vs server tests)
    // Assert.assertThat("frame.masking must be set",frame.isMasked(),is(true));
    frames.add(copy);
}
Also used : WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame)

Example 59 with WebSocketFrame

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

the class BlockheadClient method onConnectionStateChange.

@Override
public void onConnectionStateChange(ConnectionState state) {
    LOG.debug("CLIENT onConnectionStateChange() - {}", state);
    switch(state) {
        case CLOSED:
            // this.disconnect();
            break;
        case CLOSING:
            CloseInfo close = ioState.getCloseInfo();
            WebSocketFrame frame = close.asFrame();
            LOG.debug("Issuing: {}", frame);
            try {
                write(frame);
            } catch (IOException e) {
                LOG.debug(e);
            }
            break;
        default:
            /* do nothing */
            break;
    }
}
Also used : WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) IOException(java.io.IOException) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo)

Example 60 with WebSocketFrame

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

the class Fuzzer method expect.

public void expect(List<WebSocketFrame> expect, int duration, TimeUnit unit) throws Exception {
    int expectedCount = expect.size();
    LOG.debug("expect() {} frame(s)", expect.size());
    // Read frames
    EventQueue<WebSocketFrame> frames = client.readFrames(expect.size(), duration, unit);
    String prefix = "";
    for (int i = 0; i < expectedCount; i++) {
        WebSocketFrame expected = expect.get(i);
        WebSocketFrame actual = frames.poll();
        prefix = "Frame[" + i + "]";
        LOG.debug("{} {}", prefix, actual);
        Assert.assertThat(prefix + ".opcode", OpCode.name(actual.getOpCode()), is(OpCode.name(expected.getOpCode())));
        prefix += "/" + actual.getOpCode();
        if (expected.getOpCode() == OpCode.CLOSE) {
            CloseInfo expectedClose = new CloseInfo(expected);
            CloseInfo actualClose = new CloseInfo(actual);
            Assert.assertThat(prefix + ".statusCode", actualClose.getStatusCode(), is(expectedClose.getStatusCode()));
        } else {
            Assert.assertThat(prefix + ".payloadLength", actual.getPayloadLength(), is(expected.getPayloadLength()));
            ByteBufferAssert.assertEquals(prefix + ".payload", expected.getPayload(), actual.getPayload());
        }
    }
}
Also used : WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) Matchers.containsString(org.hamcrest.Matchers.containsString) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo)

Aggregations

WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)185 Test (org.junit.Test)169 CloseInfo (org.eclipse.jetty.websocket.common.CloseInfo)118 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)112 ArrayList (java.util.ArrayList)111 Fuzzer (org.eclipse.jetty.websocket.common.test.Fuzzer)108 ByteBuffer (java.nio.ByteBuffer)79 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)51 BlockheadClient (org.eclipse.jetty.websocket.common.test.BlockheadClient)37 ContinuationFrame (org.eclipse.jetty.websocket.common.frames.ContinuationFrame)36 PingFrame (org.eclipse.jetty.websocket.common.frames.PingFrame)36 BinaryFrame (org.eclipse.jetty.websocket.common.frames.BinaryFrame)25 PongFrame (org.eclipse.jetty.websocket.common.frames.PongFrame)17 IBlockheadClient (org.eclipse.jetty.websocket.common.test.IBlockheadClient)17 Frame (org.eclipse.jetty.websocket.api.extensions.Frame)14 URI (java.net.URI)12 ExtensionConfig (org.eclipse.jetty.websocket.api.extensions.ExtensionConfig)9 Matchers.containsString (org.hamcrest.Matchers.containsString)9 IncomingFramesCapture (org.eclipse.jetty.websocket.common.test.IncomingFramesCapture)8 Stress (org.eclipse.jetty.toolchain.test.annotation.Stress)6