Search in sources :

Example 26 with BlockheadClient

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

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

the class WebSocketServletRFCTest method testEcho.

/**
     * Test the requirement of issuing socket and receiving echo response
     * @throws Exception on test failure
     */
@Test
public void testEcho() throws Exception {
    BlockheadClient client = new BlockheadClient(server.getServerUri());
    try {
        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));
    } 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) Test(org.junit.Test)

Example 28 with BlockheadClient

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

the class WebSocketServletRFCTest method testLowercaseUpgrade.

/**
     * 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 lowercase headers.
     * @throws Exception on test failure
     */
@Test
public void testLowercaseUpgrade() 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 29 with BlockheadClient

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

the class WebSocketServletRFCTest method testTextNotUTF8.

@Test
public void testTextNotUTF8() throws Exception {
    try (StacklessLogging stackless = new StacklessLogging(Parser.class);
        BlockheadClient client = new BlockheadClient(server.getServerUri())) {
        client.setProtocols("other");
        client.connect();
        client.sendStandardRequest();
        client.expectUpgradeResponse();
        byte[] buf = new byte[] { (byte) 0xC2, (byte) 0xC3 };
        WebSocketFrame txt = new TextFrame().setPayload(ByteBuffer.wrap(buf));
        txt.setMask(Hex.asByteArray("11223344"));
        ByteBuffer bbHeader = generator.generateHeaderBytes(txt);
        client.writeRaw(bbHeader);
        client.writeRaw(txt.getPayload());
        EventQueue<WebSocketFrame> frames = client.readFrames(1, 30, TimeUnit.SECONDS);
        WebSocketFrame frame = frames.poll();
        Assert.assertThat("frames[0].opcode", frame.getOpCode(), is(OpCode.CLOSE));
        CloseInfo close = new CloseInfo(frame);
        Assert.assertThat("Close Status Code", close.getStatusCode(), is(StatusCode.BAD_PAYLOAD));
    }
}
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) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) ByteBuffer(java.nio.ByteBuffer) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo) Test(org.junit.Test)

Example 30 with BlockheadClient

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

the class WebSocketServletRFCTest method testBinaryAggregate.

/**
     * Test that aggregation of binary frames into a single message occurs
     * @throws Exception on test failure
     */
@Test
public void testBinaryAggregate() throws Exception {
    BlockheadClient client = new BlockheadClient(server.getServerUri());
    try {
        client.connect();
        client.sendStandardRequest();
        client.expectUpgradeResponse();
        // Generate binary frames
        byte[] buf1 = new byte[128];
        byte[] buf2 = new byte[128];
        byte[] buf3 = new byte[128];
        Arrays.fill(buf1, (byte) 0xAA);
        Arrays.fill(buf2, (byte) 0xBB);
        Arrays.fill(buf3, (byte) 0xCC);
        WebSocketFrame bin;
        bin = new BinaryFrame().setPayload(buf1).setFin(false);
        // write buf1 (fin=false)
        client.write(bin);
        bin = new ContinuationFrame().setPayload(buf2).setFin(false);
        // write buf2 (fin=false)
        client.write(bin);
        bin = new ContinuationFrame().setPayload(buf3).setFin(true);
        // write buf3 (fin=true)
        client.write(bin);
        // Read frame echo'd back (hopefully a single binary frame)
        EventQueue<WebSocketFrame> frames = client.readFrames(1, 30, TimeUnit.SECONDS);
        Frame binmsg = frames.poll();
        int expectedSize = buf1.length + buf2.length + buf3.length;
        Assert.assertThat("BinaryFrame.payloadLength", binmsg.getPayloadLength(), is(expectedSize));
        int aaCount = 0;
        int bbCount = 0;
        int ccCount = 0;
        ByteBuffer echod = binmsg.getPayload();
        while (echod.remaining() >= 1) {
            byte b = echod.get();
            switch(b) {
                case (byte) 0xAA:
                    aaCount++;
                    break;
                case (byte) 0xBB:
                    bbCount++;
                    break;
                case (byte) 0xCC:
                    ccCount++;
                    break;
                default:
                    Assert.fail(String.format("Encountered invalid byte 0x%02X", (byte) (0xFF & b)));
            }
        }
        Assert.assertThat("Echoed data count for 0xAA", aaCount, is(buf1.length));
        Assert.assertThat("Echoed data count for 0xBB", bbCount, is(buf2.length));
        Assert.assertThat("Echoed data count for 0xCC", ccCount, is(buf3.length));
    } finally {
        client.close();
    }
}
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) BinaryFrame(org.eclipse.jetty.websocket.common.frames.BinaryFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) ContinuationFrame(org.eclipse.jetty.websocket.common.frames.ContinuationFrame) ByteBuffer(java.nio.ByteBuffer) 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