Search in sources :

Example 16 with IBlockheadClient

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

the class ConfiguratorTest method testProtocol_AltHeaderCase.

/**
     * Test of Sec-WebSocket-Protocol, using non-spec case header
     * @throws Exception on test failure
     */
@Test
public void testProtocol_AltHeaderCase() throws Exception {
    URI uri = baseServerUri.resolve("/protocols");
    ProtocolsConfigurator.seenProtocols.set(null);
    try (IBlockheadClient client = new BlockheadClient(uri)) {
        client.addHeader("Sec-Websocket-Protocol: echo, chat, status\r\n");
        client.connect();
        client.sendStandardRequest();
        client.expectUpgradeResponse();
        client.write(new TextFrame().setPayload("getProtocols"));
        EventQueue<WebSocketFrame> frames = client.readFrames(1, 1, TimeUnit.SECONDS);
        WebSocketFrame frame = frames.poll();
        Assert.assertThat("Frame Response", frame.getPayloadAsUTF8(), is("Requested Protocols: [\"echo\",\"chat\",\"status\"]"));
    }
}
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 17 with IBlockheadClient

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

the class MisbehavingClassTest method testAnnotatedRuntimeOnConnect.

@Test
public void testAnnotatedRuntimeOnConnect() throws Exception {
    try (IBlockheadClient client = new BlockheadClient(server.getServerUri());
        StacklessLogging scope = new StacklessLogging(AnnotatedRuntimeOnConnectSocket.class, WebSocketSession.class)) {
        client.setProtocols("annotated-runtime-connect");
        client.setTimeout(1, TimeUnit.SECONDS);
        AnnotatedRuntimeOnConnectSocket socket = badSocketsServlet.annotatedRuntimeConnect;
        socket.reset();
        client.connect();
        client.sendStandardRequest();
        client.expectUpgradeResponse();
        EventQueue<WebSocketFrame> frames = client.readFrames(1, 1, 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("Close Latch", socket.closeLatch.await(1, TimeUnit.SECONDS), is(true));
        assertThat("closeStatusCode", socket.closeStatusCode, is(StatusCode.SERVER_ERROR));
        // Validate errors
        assertThat("socket.onErrors", socket.errors.size(), is(1));
        Throwable cause = socket.errors.pop();
        assertThat("Error type", cause, instanceOf(RuntimeException.class));
    }
}
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 18 with IBlockheadClient

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

the class WebSocketServerSessionTest method testDisconnect.

@Test
public void testDisconnect() throws Exception {
    URI uri = server.getServerUri().resolve("/test/disconnect");
    try (IBlockheadClient client = new BlockheadClient(uri)) {
        client.connect();
        client.sendStandardRequest();
        client.expectUpgradeResponse();
        client.write(new TextFrame().setPayload("harsh-disconnect"));
        client.awaitDisconnect(1, TimeUnit.SECONDS);
    }
}
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) URI(java.net.URI) IBlockheadClient(org.eclipse.jetty.websocket.common.test.IBlockheadClient) Test(org.junit.Test)

Example 19 with IBlockheadClient

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

use of org.eclipse.jetty.websocket.common.test.IBlockheadClient 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)24 IBlockheadClient (org.eclipse.jetty.websocket.common.test.IBlockheadClient)24 Test (org.junit.Test)18 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)16 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)13 URI (java.net.URI)11 CloseInfo (org.eclipse.jetty.websocket.common.CloseInfo)10 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)9 HttpResponse (org.eclipse.jetty.websocket.common.test.HttpResponse)2 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 InetSocketAddress (java.net.InetSocketAddress)1 StdErrLog (org.eclipse.jetty.util.log.StdErrLog)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1