Search in sources :

Example 31 with BlockheadClient

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

the class WebSocketServletRFCTest method testUppercaseUpgrade.

/**
     * Test http://tools.ietf.org/html/rfc6455#section-4.1 where server side upgrade handling is supposed to be case insensitive.
     * <p>
     * This test will simulate a client requesting upgrade with all uppercase headers.
     * @throws Exception on test failure
     */
@Test
public void testUppercaseUpgrade() throws Exception {
    BlockheadClient client = new BlockheadClient(server.getServerUri());
    try {
        client.connect();
        StringBuilder req = new StringBuilder();
        req.append("GET ").append(client.getRequestPath()).append(" HTTP/1.1\r\n");
        req.append("HOST: ").append(client.getRequestHost()).append("\r\n");
        req.append("UPGRADE: WEBSOCKET\r\n");
        req.append("CONNECTION: UPGRADE\r\n");
        req.append("SEC-WEBSOCKET-KEY: ").append(client.getRequestWebSocketKey()).append("\r\n");
        req.append("SEC-WEBSOCKET-ORIGIN: ").append(client.getRequestWebSocketOrigin()).append("\r\n");
        req.append("SEC-WEBSOCKET-PROTOCOL: ECHO\r\n");
        req.append("SEC-WEBSOCKET-VERSION: 13\r\n");
        req.append("\r\n");
        client.writeRaw(req.toString());
        client.expectUpgradeResponse();
        // 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) Utf8StringBuilder(org.eclipse.jetty.util.Utf8StringBuilder) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) Test(org.junit.Test)

Example 32 with BlockheadClient

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

the class WebSocketUpgradeFilterTest method testNormalConfiguration.

@Test
public void testNormalConfiguration() throws Exception {
    URI destUri = serverUri.resolve("/info/");
    try (BlockheadClient client = new BlockheadClient(destUri)) {
        client.connect();
        client.sendStandardRequest();
        client.expectUpgradeResponse();
        client.write(new TextFrame().setPayload("hello"));
        EventQueue<WebSocketFrame> frames = client.readFrames(1, 1000, TimeUnit.MILLISECONDS);
        String payload = frames.poll().getPayloadAsUTF8();
        // If we can connect and send a text message, we know that the endpoint was
        // added properly, and the response will help us verify the policy configuration too
        assertThat("payload", payload, containsString("session.maxTextMessageSize=" + (10 * 1024 * 1024)));
    }
}
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) URI(java.net.URI) Test(org.junit.Test)

Example 33 with BlockheadClient

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

the class WebSocketUpgradeFilterTest method testStopStartOfHandler.

@Test
public void testStopStartOfHandler() throws Exception {
    URI destUri = serverUri.resolve("/info/");
    try (BlockheadClient client = new BlockheadClient(destUri)) {
        client.connect();
        client.sendStandardRequest();
        client.expectUpgradeResponse();
        client.write(new TextFrame().setPayload("hello 1"));
        EventQueue<WebSocketFrame> frames = client.readFrames(1, 1000, TimeUnit.MILLISECONDS);
        String payload = frames.poll().getPayloadAsUTF8();
        // If we can connect and send a text message, we know that the endpoint was
        // added properly, and the response will help us verify the policy configuration too
        assertThat("payload", payload, containsString("session.maxTextMessageSize=" + (10 * 1024 * 1024)));
    }
    server.getHandler().stop();
    server.getHandler().start();
    try (BlockheadClient client = new BlockheadClient(destUri)) {
        client.connect();
        client.sendStandardRequest();
        client.expectUpgradeResponse();
        client.write(new TextFrame().setPayload("hello 2"));
        EventQueue<WebSocketFrame> frames = client.readFrames(1, 1000, TimeUnit.MILLISECONDS);
        String payload = frames.poll().getPayloadAsUTF8();
        // If we can connect and send a text message, we know that the endpoint was
        // added properly, and the response will help us verify the policy configuration too
        assertThat("payload", payload, containsString("session.maxTextMessageSize=" + (10 * 1024 * 1024)));
    }
}
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) URI(java.net.URI) Test(org.junit.Test)

Example 34 with BlockheadClient

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

the class WebSocketCloseTest method testFastFail.

/**
     * Test fast fail (bug #410537)
     * 
     * @throws Exception
     *             on test failure
     */
@Test
public void testFastFail() throws Exception {
    try (IBlockheadClient client = new BlockheadClient(server.getServerUri())) {
        client.setProtocols("fastfail");
        client.setTimeout(5, TimeUnit.SECONDS);
        try (StacklessLogging scope = new StacklessLogging(FastFailSocket.class, WebSocketSession.class)) {
            client.connect();
            client.sendStandardRequest();
            client.expectUpgradeResponse();
            EventQueue<WebSocketFrame> frames = client.readFrames(1, 5, TimeUnit.SECONDS);
            WebSocketFrame frame = frames.poll();
            assertThat("frames[0].opcode", frame.getOpCode(), is(OpCode.CLOSE));
            CloseInfo close = new CloseInfo(frame);
            assertThat("Close Status Code", close.getStatusCode(), is(StatusCode.SERVER_ERROR));
            // respond with close
            client.write(close.asFrame());
            // ensure server socket got close event
            assertThat("Fast Fail Latch", closeSocket.closeLatch.await(5, 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) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) IBlockheadClient(org.eclipse.jetty.websocket.common.test.IBlockheadClient) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo) Test(org.junit.Test)

Example 35 with BlockheadClient

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

the class WebSocketCloseTest method testOpenSessionCleanup.

/**
     * Test session open session cleanup (bug #474936)
     * 
     * @throws Exception
     *             on test failure
     */
@Test
public void testOpenSessionCleanup() throws Exception {
    fastFail();
    fastClose();
    dropConnection();
    try (IBlockheadClient client = new BlockheadClient(server.getServerUri())) {
        client.setProtocols("container");
        client.setTimeout(1, TimeUnit.SECONDS);
        client.connect();
        client.sendStandardRequest();
        client.expectUpgradeResponse();
        TextFrame text = new TextFrame();
        text.setPayload("openSessions");
        client.write(text);
        EventQueue<WebSocketFrame> frames = client.readFrames(2, 1, TimeUnit.SECONDS);
        WebSocketFrame frame = frames.poll();
        assertThat("frames[0].opcode", frame.getOpCode(), is(OpCode.TEXT));
        String resp = frame.getPayloadAsUTF8();
        assertThat("Should only have 1 open session", resp, containsString("openSessions.size=1\n"));
        frame = frames.poll();
        assertThat("frames[1].opcode", frame.getOpCode(), is(OpCode.CLOSE));
        CloseInfo close = new CloseInfo(frame);
        assertThat("Close Status Code", close.getStatusCode(), is(StatusCode.NORMAL));
        // respond with close
        client.write(close.asFrame());
        // ensure server socket got close event
        assertThat("Open Sessions Latch", closeSocket.closeLatch.await(1, TimeUnit.SECONDS), is(true));
        assertThat("Open Sessions.statusCode", closeSocket.closeStatusCode, is(StatusCode.NORMAL));
        assertThat("Open Sessions.errors", closeSocket.errors.size(), is(0));
    }
}
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) Matchers.containsString(org.hamcrest.Matchers.containsString) IBlockheadClient(org.eclipse.jetty.websocket.common.test.IBlockheadClient) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo) 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