Search in sources :

Example 41 with BlockheadClient

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

the class RequestHeadersTest method testAccessRequestCookies.

@Test
public void testAccessRequestCookies() throws Exception {
    BlockheadClient client = new BlockheadClient(server.getServerUri());
    client.setTimeout(1, TimeUnit.SECONDS);
    try {
        client.connect();
        client.addHeader("Cookie: fruit=Pear; type=Anjou\r\n");
        client.sendStandardRequest();
        client.expectUpgradeResponse();
        UpgradeRequest req = echoCreator.getLastRequest();
        Assert.assertThat("Last Request", req, notNullValue());
        List<HttpCookie> cookies = req.getCookies();
        Assert.assertThat("Request cookies", cookies, notNullValue());
        Assert.assertThat("Request cookies.size", cookies.size(), is(2));
        for (HttpCookie cookie : cookies) {
            Assert.assertThat("Cookie name", cookie.getName(), anyOf(is("fruit"), is("type")));
            Assert.assertThat("Cookie value", cookie.getValue(), anyOf(is("Pear"), is("Anjou")));
        }
    } finally {
        client.close();
    }
}
Also used : BlockheadClient(org.eclipse.jetty.websocket.common.test.BlockheadClient) ServletUpgradeRequest(org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest) UpgradeRequest(org.eclipse.jetty.websocket.api.UpgradeRequest) HttpCookie(java.net.HttpCookie) Test(org.junit.Test)

Example 42 with BlockheadClient

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

the class SubProtocolTest method testSubProtocol.

private void testSubProtocol(String requestProtocols, String acceptedSubProtocols) throws Exception {
    try (BlockheadClient client = new BlockheadClient(server.getServerUri())) {
        client.setTimeout(1, TimeUnit.SECONDS);
        client.connect();
        client.addHeader("Sec-WebSocket-Protocol: " + requestProtocols + "\r\n");
        client.sendStandardRequest();
        client.expectUpgradeResponse();
        client.write(new TextFrame().setPayload("showme"));
        EventQueue<WebSocketFrame> frames = client.readFrames(1, 30, TimeUnit.SECONDS);
        WebSocketFrame tf = frames.poll();
        assertThat(ProtocolEchoSocket.class.getSimpleName() + ".onMessage()", tf.getPayloadAsUTF8(), is("acceptedSubprotocol=" + acceptedSubProtocols));
    }
}
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)

Example 43 with BlockheadClient

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

the class SuspendResumeTest method testSuspendResume.

@Test
public void testSuspendResume() throws Exception {
    try (BlockheadClient client = new BlockheadClient(server.getServerUri())) {
        client.setTimeout(1, TimeUnit.SECONDS);
        client.connect();
        client.sendStandardRequest();
        client.expectUpgradeResponse();
        client.write(new TextFrame().setPayload("echo1"));
        client.write(new TextFrame().setPayload("echo2"));
        EventQueue<WebSocketFrame> frames = client.readFrames(2, 30, TimeUnit.SECONDS);
        WebSocketFrame tf = frames.poll();
        assertThat(EchoSocket.class.getSimpleName() + ".onMessage()", tf.getPayloadAsUTF8(), is("echo1"));
        tf = frames.poll();
        assertThat(EchoSocket.class.getSimpleName() + ".onMessage()", tf.getPayloadAsUTF8(), is("echo2"));
    }
}
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 44 with BlockheadClient

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

the class TooFastClientTest method testUpgradeWithLargeFrame.

/**
     * Test where were a client sends a HTTP Upgrade to websocket AND enough websocket frame(s)
     * to completely overfill the {@link org.eclipse.jetty.io.AbstractConnection#getInputBufferSize()}
     * to test a situation where the WebSocket connection opens with prefill that exceeds 
     * the normal input buffer sizes.
     * @throws Exception on test failure
     */
@Test
@Ignore("RELEASE")
public void testUpgradeWithLargeFrame() 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(100 * 1024);
        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 LeakTrackingByteBufferPool(new MappedByteBufferPool.Tagged()));
        byte[] bigMsgBytes = new byte[64 * 1024];
        Arrays.fill(bigMsgBytes, (byte) 'x');
        String bigMsg = new String(bigMsgBytes, StandardCharsets.UTF_8);
        // Need to set frame mask (as these are client frames)
        byte[] mask = new byte[] { 0x11, 0x22, 0x33, 0x44 };
        TextFrame frame = new TextFrame().setPayload(bigMsg);
        frame.setMask(mask);
        generator.generateWholeFrame(frame, 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(1, 1, TimeUnit.SECONDS);
        WebSocketFrame tf = frames.poll();
        Assert.assertThat("Text Frame/msg1", tf.getPayloadAsUTF8(), is(bigMsg));
    } finally {
        client.close();
    }
}
Also used : BlockheadClient(org.eclipse.jetty.websocket.common.test.BlockheadClient) LeakTrackingByteBufferPool(org.eclipse.jetty.io.LeakTrackingByteBufferPool) 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 45 with BlockheadClient

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

the class DecoratorsTest method testAccessRequestCookies.

@Test
public void testAccessRequestCookies() throws Exception {
    BlockheadClient client = new BlockheadClient(server.getServerUri());
    client.setTimeout(1, TimeUnit.SECONDS);
    try {
        client.connect();
        client.sendStandardRequest();
        client.expectUpgradeResponse();
        client.write(new TextFrame().setPayload("info"));
        EventQueue<WebSocketFrame> frames = client.readFrames(1, 1, TimeUnit.SECONDS);
        WebSocketFrame resp = frames.poll();
        String textMsg = resp.getPayloadAsUTF8();
        assertThat("DecoratedObjectFactory", textMsg, containsString("Object is a DecoratedObjectFactory"));
        assertThat("decorators.size", textMsg, containsString("Decorators.size = [1]"));
        assertThat("decorator type", textMsg, containsString("decorator[] = " + DummyUtilDecorator.class.getName()));
    } 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) Matchers.containsString(org.hamcrest.Matchers.containsString) 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