Search in sources :

Example 46 with BlockheadClient

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

the class FirefoxTest method testConnectionKeepAlive.

@Test
public void testConnectionKeepAlive() throws Exception {
    try (IBlockheadClient client = new BlockheadClient(server.getServerUri())) {
        // Odd Connection Header value seen in Firefox
        client.setConnectionValue("keep-alive, Upgrade");
        client.connect();
        client.sendStandardRequest();
        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));
    }
}
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) IBlockheadClient(org.eclipse.jetty.websocket.common.test.IBlockheadClient) Test(org.junit.Test)

Example 47 with BlockheadClient

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

the class IdleTimeoutTest method testIdleTimeout.

/**
     * Test IdleTimeout on server.
     * @throws Exception on test failure
     */
@Test
public void testIdleTimeout() throws Exception {
    BlockheadClient client = new BlockheadClient(server.getServerUri());
    client.setProtocols("onConnect");
    client.setTimeout(2500, TimeUnit.MILLISECONDS);
    try {
        client.connect();
        client.sendStandardRequest();
        client.expectUpgradeResponse();
        // This wait should be shorter than client timeout above, but
        // longer than server timeout configured in TimeoutServlet
        client.sleep(TimeUnit.MILLISECONDS, 1000);
        // Write to server
        // This action is possible, but does nothing.
        // Server could be in a half-closed state at this point.
        // Where the server read is closed (due to timeout), but the server write is still open.
        // The server could not read this frame, if it is in this half closed state
        client.write(new TextFrame().setPayload("Hello"));
        // Expect server to have closed due to its own timeout
        EventQueue<WebSocketFrame> frames = client.readFrames(1, 30, TimeUnit.SECONDS);
        WebSocketFrame frame = frames.poll();
        Assert.assertThat("frame opcode", frame.getOpCode(), is(OpCode.CLOSE));
        CloseInfo close = new CloseInfo(frame);
        Assert.assertThat("close code", close.getStatusCode(), is(StatusCode.SHUTDOWN));
        Assert.assertThat("close reason", close.getReason(), containsString("Timeout"));
    } 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) 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