Search in sources :

Example 6 with BlockheadClient

use of org.eclipse.jetty.websocket.common.test.BlockheadClient 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 7 with BlockheadClient

use of org.eclipse.jetty.websocket.common.test.BlockheadClient in project jetty.project by eclipse.

the class WebSocketCloseTest method fastClose.

@SuppressWarnings("Duplicates")
private void fastClose() throws Exception {
    try (IBlockheadClient client = new BlockheadClient(server.getServerUri())) {
        client.setProtocols("fastclose");
        client.setTimeout(1, TimeUnit.SECONDS);
        try (StacklessLogging scope = new StacklessLogging(WebSocketSession.class)) {
            client.connect();
            client.sendStandardRequest();
            client.expectUpgradeResponse();
            client.readFrames(1, 1, TimeUnit.SECONDS);
            CloseInfo close = new CloseInfo(StatusCode.NORMAL, "Normal");
            assertThat("Close Status Code", close.getStatusCode(), is(StatusCode.NORMAL));
            // Notify server of close handshake
            // respond with close
            client.write(close.asFrame());
            // ensure server socket got close event
            assertThat("Fast Close Latch", closeSocket.closeLatch.await(1, TimeUnit.SECONDS), is(true));
            assertThat("Fast Close.statusCode", closeSocket.closeStatusCode, is(StatusCode.NORMAL));
        }
    }
}
Also used : IBlockheadClient(org.eclipse.jetty.websocket.common.test.IBlockheadClient) BlockheadClient(org.eclipse.jetty.websocket.common.test.BlockheadClient) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) IBlockheadClient(org.eclipse.jetty.websocket.common.test.IBlockheadClient) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo)

Example 8 with BlockheadClient

use of org.eclipse.jetty.websocket.common.test.BlockheadClient in project jetty.project by eclipse.

the class WebSocketCloseTest method dropConnection.

@SuppressWarnings("Duplicates")
private void dropConnection() throws Exception {
    try (IBlockheadClient client = new BlockheadClient(server.getServerUri())) {
        client.setProtocols("container");
        client.setTimeout(1, TimeUnit.SECONDS);
        try (StacklessLogging scope = new StacklessLogging(WebSocketSession.class)) {
            client.connect();
            client.sendStandardRequest();
            client.expectUpgradeResponse();
            client.disconnect();
        }
    }
}
Also used : IBlockheadClient(org.eclipse.jetty.websocket.common.test.IBlockheadClient) BlockheadClient(org.eclipse.jetty.websocket.common.test.BlockheadClient) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) IBlockheadClient(org.eclipse.jetty.websocket.common.test.IBlockheadClient)

Example 9 with BlockheadClient

use of org.eclipse.jetty.websocket.common.test.BlockheadClient in project jetty.project by eclipse.

the class WebSocketCloseTest method fastFail.

private void fastFail() throws Exception {
    try (IBlockheadClient client = new BlockheadClient(server.getServerUri())) {
        client.setProtocols("fastfail");
        client.setTimeout(1, TimeUnit.SECONDS);
        try (StacklessLogging scope = new StacklessLogging(WebSocketSession.class)) {
            client.connect();
            client.sendStandardRequest();
            client.expectUpgradeResponse();
            client.readFrames(1, 1, TimeUnit.SECONDS);
            CloseInfo close = new CloseInfo(StatusCode.NORMAL, "Normal");
            // respond with close
            client.write(close.asFrame());
            // ensure server socket got close event
            assertThat("Fast Fail Latch", closeSocket.closeLatch.await(1, TimeUnit.SECONDS), is(true));
            assertThat("Fast Fail.statusCode", closeSocket.closeStatusCode, is(StatusCode.SERVER_ERROR));
            assertThat("Fast Fail.errors", closeSocket.errors.size(), is(1));
        }
    }
}
Also used : IBlockheadClient(org.eclipse.jetty.websocket.common.test.IBlockheadClient) BlockheadClient(org.eclipse.jetty.websocket.common.test.BlockheadClient) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) IBlockheadClient(org.eclipse.jetty.websocket.common.test.IBlockheadClient) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo)

Example 10 with BlockheadClient

use of org.eclipse.jetty.websocket.common.test.BlockheadClient in project jetty.project by eclipse.

the class WebSocketInvalidVersionTest method testRequestVersion29.

/**
     * Test the requirement of responding with an http 400 when using a Sec-WebSocket-Version that is unsupported.
     * @throws Exception on test failure
     */
@Test
public void testRequestVersion29() throws Exception {
    @SuppressWarnings("resource") BlockheadClient client = new BlockheadClient(server.getServerUri());
    // intentionally bad version
    client.setVersion(29);
    try {
        client.connect();
        client.sendStandardRequest();
        HttpResponse response = client.readResponseHeader();
        Assert.assertThat("Response Status Code", response.getStatusCode(), is(400));
        Assert.assertThat("Response Status Reason", response.getStatusReason(), containsString("Unsupported websocket version specification"));
        Assert.assertThat("Response Versions", response.getHeader("Sec-WebSocket-Version"), is("13"));
    } finally {
        client.disconnect();
    }
}
Also used : BlockheadClient(org.eclipse.jetty.websocket.common.test.BlockheadClient) HttpResponse(org.eclipse.jetty.websocket.common.test.HttpResponse) Test(org.junit.Test)

Aggregations

BlockheadClient (org.eclipse.jetty.websocket.common.test.BlockheadClient)47 Test (org.junit.Test)40 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)37 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)34 IBlockheadClient (org.eclipse.jetty.websocket.common.test.IBlockheadClient)25 URI (java.net.URI)14 CloseInfo (org.eclipse.jetty.websocket.common.CloseInfo)14 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)12 Matchers.containsString (org.hamcrest.Matchers.containsString)7 HttpResponse (org.eclipse.jetty.websocket.common.test.HttpResponse)6 ByteBuffer (java.nio.ByteBuffer)4 Utf8StringBuilder (org.eclipse.jetty.util.Utf8StringBuilder)2 Frame (org.eclipse.jetty.websocket.api.extensions.Frame)2 Generator (org.eclipse.jetty.websocket.common.Generator)2 BinaryFrame (org.eclipse.jetty.websocket.common.frames.BinaryFrame)2 ContinuationFrame (org.eclipse.jetty.websocket.common.frames.ContinuationFrame)2 Ignore (org.junit.Ignore)2 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 HttpCookie (java.net.HttpCookie)1