Search in sources :

Example 51 with WebSocketFrame

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

the class TooFastClientTest method testUpgradeWithSmallFrames.

@Test
@Ignore("RELEASE")
public void testUpgradeWithSmallFrames() throws Exception {
    BlockheadClient client = new BlockheadClient(server.getServerUri());
    try {
        client.connect();
        // Create ByteBuffer representing the initial opening network packet from the client
        ByteBuffer initialPacket = ByteBuffer.allocate(4096);
        BufferUtil.clearToFill(initialPacket);
        // Add upgrade request to packet
        StringBuilder upgradeRequest = client.generateUpgradeRequest();
        ByteBuffer upgradeBuffer = BufferUtil.toBuffer(upgradeRequest.toString(), StandardCharsets.UTF_8);
        initialPacket.put(upgradeBuffer);
        // Add text frames
        Generator generator = new Generator(WebSocketPolicy.newClientPolicy(), new LeakTrackingBufferPoolRule("Generator"));
        String msg1 = "Echo 1";
        String msg2 = "This is also an echooooo!";
        TextFrame frame1 = new TextFrame().setPayload(msg1);
        TextFrame frame2 = new TextFrame().setPayload(msg2);
        // Need to set frame mask (as these are client frames)
        byte[] mask = new byte[] { 0x11, 0x22, 0x33, 0x44 };
        frame1.setMask(mask);
        frame2.setMask(mask);
        generator.generateWholeFrame(frame1, initialPacket);
        generator.generateWholeFrame(frame2, initialPacket);
        // Write packet to network
        BufferUtil.flipToFlush(initialPacket, 0);
        client.writeRaw(initialPacket);
        // Expect upgrade
        client.expectUpgradeResponse();
        // Read frames (hopefully text frames)
        EventQueue<WebSocketFrame> frames = client.readFrames(2, 1, TimeUnit.SECONDS);
        WebSocketFrame tf = frames.poll();
        Assert.assertThat("Text Frame/msg1", tf.getPayloadAsUTF8(), is(msg1));
        tf = frames.poll();
        Assert.assertThat("Text Frame/msg2", tf.getPayloadAsUTF8(), is(msg2));
    } finally {
        client.close();
    }
}
Also used : LeakTrackingBufferPoolRule(org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPoolRule) BlockheadClient(org.eclipse.jetty.websocket.common.test.BlockheadClient) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) ByteBuffer(java.nio.ByteBuffer) Generator(org.eclipse.jetty.websocket.common.Generator) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 52 with WebSocketFrame

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

the class WebSocketServerSessionTest method testUpgradeRequestResponse.

@Test
public void testUpgradeRequestResponse() throws Exception {
    URI uri = server.getServerUri().resolve("/test?snack=cashews&amount=handful&brand=off");
    try (IBlockheadClient client = new BlockheadClient(uri)) {
        client.connect();
        client.sendStandardRequest();
        client.expectUpgradeResponse();
        // Ask the server socket for specific parameter map info
        client.write(new TextFrame().setPayload("getParameterMap|snack"));
        client.write(new TextFrame().setPayload("getParameterMap|amount"));
        client.write(new TextFrame().setPayload("getParameterMap|brand"));
        // intentionally invalid
        client.write(new TextFrame().setPayload("getParameterMap|cost"));
        // Read frame (hopefully text frame)
        EventQueue<WebSocketFrame> frames = client.readFrames(4, 5, TimeUnit.SECONDS);
        WebSocketFrame tf = frames.poll();
        Assert.assertThat("Parameter Map[snack]", tf.getPayloadAsUTF8(), is("[cashews]"));
        tf = frames.poll();
        Assert.assertThat("Parameter Map[amount]", tf.getPayloadAsUTF8(), is("[handful]"));
        tf = frames.poll();
        Assert.assertThat("Parameter Map[brand]", tf.getPayloadAsUTF8(), is("[off]"));
        tf = frames.poll();
        Assert.assertThat("Parameter Map[cost]", tf.getPayloadAsUTF8(), is("<null>"));
    }
}
Also used : IBlockheadClient(org.eclipse.jetty.websocket.common.test.IBlockheadClient) BlockheadClient(org.eclipse.jetty.websocket.common.test.BlockheadClient) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) URI(java.net.URI) IBlockheadClient(org.eclipse.jetty.websocket.common.test.IBlockheadClient) Test(org.junit.Test)

Example 53 with WebSocketFrame

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

the class WebSocketServletRFCTest method testInternalError.

/**
     * Test the requirement of responding with server terminated close code 1011 when there is an unhandled (internal server error) being produced by the
     * WebSocket POJO.
     * @throws Exception on test failure
     */
@Test
public void testInternalError() throws Exception {
    try (BlockheadClient client = new BlockheadClient(server.getServerUri());
        StacklessLogging stackless = new StacklessLogging(EventDriver.class)) {
        client.connect();
        client.sendStandardRequest();
        client.expectUpgradeResponse();
        try (StacklessLogging context = new StacklessLogging(EventDriver.class)) {
            // Generate text frame
            client.write(new TextFrame().setPayload("CRASH"));
            // Read frame (hopefully close frame)
            EventQueue<WebSocketFrame> frames = client.readFrames(1, 30, TimeUnit.SECONDS);
            Frame cf = frames.poll();
            CloseInfo close = new CloseInfo(cf);
            Assert.assertThat("Close Frame.status code", close.getStatusCode(), is(StatusCode.SERVER_ERROR));
        }
    }
}
Also used : BlockheadClient(org.eclipse.jetty.websocket.common.test.BlockheadClient) BinaryFrame(org.eclipse.jetty.websocket.common.frames.BinaryFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) Frame(org.eclipse.jetty.websocket.api.extensions.Frame) ContinuationFrame(org.eclipse.jetty.websocket.common.frames.ContinuationFrame) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo) Test(org.junit.Test)

Example 54 with WebSocketFrame

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

the class ChromeTest method testUpgradeWithWebkitDeflateExtension.

@Test
public void testUpgradeWithWebkitDeflateExtension() throws Exception {
    Assume.assumeTrue("Server has x-webkit-deflate-frame registered", server.getWebSocketServletFactory().getExtensionFactory().isAvailable("x-webkit-deflate-frame"));
    BlockheadClient client = new BlockheadClient(server.getServerUri());
    try {
        client.addExtensions("x-webkit-deflate-frame");
        client.setProtocols("chat");
        client.connect();
        client.sendStandardRequest();
        HttpResponse response = client.expectUpgradeResponse();
        Assert.assertThat("Response", response.getExtensionsHeader(), containsString("x-webkit-deflate-frame"));
        // Generate text frame
        String msg = "this is an echo ... cho ... ho ... o";
        client.write(new TextFrame().setPayload(msg));
        // Read frame (hopefully text frame)
        EventQueue<WebSocketFrame> frames = client.readFrames(1, 30, TimeUnit.SECONDS);
        WebSocketFrame tf = frames.poll();
        Assert.assertThat("Text Frame.status code", tf.getPayloadAsUTF8(), is(msg));
    } 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 55 with WebSocketFrame

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

the class DecoratorsLegacyTest method testAccessRequestCookies.

@Test
public void testAccessRequestCookies() throws Exception {
    BlockheadClient client = new BlockheadClient(server.getServerUri());
    client.setTimeout(1, TimeUnit.SECONDS);
    try {
        client.connect();
        client.sendStandardRequest();
        client.expectUpgradeResponse();
        client.write(new TextFrame().setPayload("info"));
        EventQueue<WebSocketFrame> frames = client.readFrames(1, 1, TimeUnit.SECONDS);
        WebSocketFrame resp = frames.poll();
        String textMsg = resp.getPayloadAsUTF8();
        assertThat("DecoratedObjectFactory", textMsg, containsString("Object is a DecoratedObjectFactory"));
        assertThat("decorators.size", textMsg, containsString("Decorators.size = [1]"));
        assertThat("decorator type", textMsg, containsString("decorator[] = " + DummyLegacyDecorator.class.getName()));
    } finally {
        client.close();
    }
}
Also used : BlockheadClient(org.eclipse.jetty.websocket.common.test.BlockheadClient) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

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