Search in sources :

Example 26 with IBlockheadServerConnection

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

the class SlowServerTest method testServerSlowToRead.

@Test
@Slow
public void testServerSlowToRead() throws Exception {
    JettyTrackingSocket tsocket = new JettyTrackingSocket();
    client.setMasker(new ZeroMasker());
    client.setMaxIdleTimeout(60000);
    URI wsUri = server.getWsUri();
    Future<Session> future = client.connect(tsocket, wsUri);
    IBlockheadServerConnection sconnection = server.accept();
    sconnection.setSoTimeout(60000);
    sconnection.upgrade();
    // Confirm connected
    future.get(30, TimeUnit.SECONDS);
    tsocket.waitForConnected(30, TimeUnit.SECONDS);
    int messageCount = 10;
    // Setup slow server read thread
    ServerReadThread reader = new ServerReadThread(sconnection, messageCount);
    // slow it down
    reader.setSlowness(100);
    reader.start();
    // Have client write as quickly as it can.
    ClientWriteThread writer = new ClientWriteThread(tsocket.getSession());
    writer.setMessageCount(messageCount);
    writer.setMessage("Hello");
    // disable slowness
    writer.setSlowness(-1);
    writer.start();
    writer.join();
    // Verify receive
    reader.waitForExpectedMessageCount(10, TimeUnit.SECONDS);
    Assert.assertThat("Frame Receive Count", reader.getFrameCount(), is(messageCount));
    // Close
    tsocket.getSession().close(StatusCode.NORMAL, "Done");
    Assert.assertTrue("Client Socket Closed", tsocket.closeLatch.await(10, TimeUnit.SECONDS));
    tsocket.assertCloseCode(StatusCode.NORMAL);
    // stop reading
    reader.cancel();
}
Also used : IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) ZeroMasker(org.eclipse.jetty.websocket.client.masks.ZeroMasker) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 27 with IBlockheadServerConnection

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

the class SlowServerTest method testServerSlowToSend.

@Test
@Slow
public void testServerSlowToSend() throws Exception {
    JettyTrackingSocket clientSocket = new JettyTrackingSocket();
    client.setMasker(new ZeroMasker());
    client.setMaxIdleTimeout(60000);
    URI wsUri = server.getWsUri();
    Future<Session> clientConnectFuture = client.connect(clientSocket, wsUri);
    IBlockheadServerConnection serverConn = server.accept();
    serverConn.setSoTimeout(60000);
    serverConn.upgrade();
    // Confirm connected
    clientConnectFuture.get(30, TimeUnit.SECONDS);
    clientSocket.waitForConnected(30, TimeUnit.SECONDS);
    // Have server write slowly.
    int messageCount = 1000;
    ServerWriteThread writer = new ServerWriteThread(serverConn);
    writer.setMessageCount(messageCount);
    writer.setMessage("Hello");
    writer.setSlowness(10);
    writer.start();
    writer.join();
    // Verify receive
    Assert.assertThat("Message Receive Count", clientSocket.messageQueue.size(), is(messageCount));
    // Close
    serverConn.close(StatusCode.NORMAL);
    Assert.assertTrue("Client Socket Closed", clientSocket.closeLatch.await(10, TimeUnit.SECONDS));
    clientSocket.assertCloseCode(StatusCode.NORMAL);
}
Also used : IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) ZeroMasker(org.eclipse.jetty.websocket.client.masks.ZeroMasker) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 28 with IBlockheadServerConnection

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

the class TomcatServerQuirksTest method testTomcat7_0_32_WithTransferEncoding.

/**
     * Test for when encountering a "Transfer-Encoding: chunked" on a Upgrade Response header.
     * <ul>
     * <li><a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=393075">Eclipse Jetty Bug #393075</a></li>
     * <li><a href="https://issues.apache.org/bugzilla/show_bug.cgi?id=54067">Apache Tomcat Bug #54067</a></li>
     * </ul>
     * 
     * @throws Exception on test failure
     */
@Test
public void testTomcat7_0_32_WithTransferEncoding() throws Exception {
    BlockheadServer server = new BlockheadServer();
    WebSocketClient client = new WebSocketClient();
    try {
        final int bufferSize = 512;
        server.start();
        // Setup Client Factory
        client.start();
        // Create End User WebSocket Class
        LatchedSocket websocket = new LatchedSocket();
        // Open connection
        URI wsURI = server.getWsUri();
        client.connect(websocket, wsURI);
        // Accept incoming connection
        IBlockheadServerConnection socket = server.accept();
        // timeout
        socket.setSoTimeout(2000);
        // Issue upgrade
        // Add the extra problematic header that triggers bug found in jetty-io
        socket.addResponseHeader("Transfer-Encoding", "chunked");
        socket.upgrade();
        // Wait for proper upgrade
        Assert.assertTrue("Timed out waiting for Client side WebSocket open event", websocket.openLatch.await(1, TimeUnit.SECONDS));
        // Have server write frame.
        byte[] payload = new byte[bufferSize / 2];
        Arrays.fill(payload, (byte) 'x');
        ByteBuffer serverFrame = BufferUtil.allocate(bufferSize);
        BufferUtil.flipToFill(serverFrame);
        // FIN + TEXT
        serverFrame.put((byte) (0x80 | 0x01));
        // No MASK and 2 bytes length
        serverFrame.put((byte) 0x7E);
        // first length byte
        serverFrame.put((byte) (payload.length >> 8));
        // second length byte
        serverFrame.put((byte) (payload.length & 0xFF));
        serverFrame.put(payload);
        BufferUtil.flipToFlush(serverFrame, 0);
        socket.write(serverFrame);
        socket.flush();
        Assert.assertTrue(websocket.dataLatch.await(1000, TimeUnit.SECONDS));
    } finally {
        client.stop();
        server.stop();
    }
}
Also used : IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) BlockheadServer(org.eclipse.jetty.websocket.common.test.BlockheadServer) URI(java.net.URI) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 29 with IBlockheadServerConnection

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

the class WebSocketClientTest method testMaxMessageSize.

/**
     * Ensure that <code>@WebSocket(maxTextMessageSize = 100*1024)</code> behaves as expected.
     * 
     * @throws Exception
     *             on test failure
     */
@Test
public void testMaxMessageSize() throws Exception {
    MaxMessageSocket wsocket = new MaxMessageSocket();
    URI wsUri = server.getWsUri();
    Future<Session> future = client.connect(wsocket, wsUri);
    IBlockheadServerConnection ssocket = server.accept();
    ssocket.upgrade();
    wsocket.awaitConnect(1, TimeUnit.SECONDS);
    Session sess = future.get(30, TimeUnit.SECONDS);
    Assert.assertThat("Session", sess, notNullValue());
    Assert.assertThat("Session.open", sess.isOpen(), is(true));
    // Create string that is larger than default size of 64k
    // but smaller than maxMessageSize of 100k
    byte[] buf = new byte[80 * 1024];
    Arrays.fill(buf, (byte) 'x');
    String msg = StringUtil.toUTF8String(buf, 0, buf.length);
    wsocket.getSession().getRemote().sendStringByFuture(msg);
    ssocket.echoMessage(1, 2, TimeUnit.SECONDS);
    // wait for response from server
    wsocket.waitForMessage(1, TimeUnit.SECONDS);
    wsocket.assertMessage(msg);
    Assert.assertTrue(wsocket.dataLatch.await(2, TimeUnit.SECONDS));
}
Also used : IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) WebSocketSession(org.eclipse.jetty.websocket.common.WebSocketSession) Test(org.junit.Test)

Example 30 with IBlockheadServerConnection

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

the class WebSocketClientTest method testParameterMap.

@Test
public void testParameterMap() throws Exception {
    JettyTrackingSocket wsocket = new JettyTrackingSocket();
    URI wsUri = server.getWsUri().resolve("/test?snack=cashews&amount=handful&brand=off");
    Future<Session> future = client.connect(wsocket, wsUri);
    IBlockheadServerConnection ssocket = server.accept();
    ssocket.upgrade();
    future.get(30, TimeUnit.SECONDS);
    Assert.assertTrue(wsocket.openLatch.await(1, TimeUnit.SECONDS));
    Session session = wsocket.getSession();
    UpgradeRequest req = session.getUpgradeRequest();
    Assert.assertThat("Upgrade Request", req, notNullValue());
    Map<String, List<String>> parameterMap = req.getParameterMap();
    Assert.assertThat("Parameter Map", parameterMap, notNullValue());
    Assert.assertThat("Parameter[snack]", parameterMap.get("snack"), is(Arrays.asList(new String[] { "cashews" })));
    Assert.assertThat("Parameter[amount]", parameterMap.get("amount"), is(Arrays.asList(new String[] { "handful" })));
    Assert.assertThat("Parameter[brand]", parameterMap.get("brand"), is(Arrays.asList(new String[] { "off" })));
    Assert.assertThat("Parameter[cost]", parameterMap.get("cost"), nullValue());
}
Also used : UpgradeRequest(org.eclipse.jetty.websocket.api.UpgradeRequest) IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) List(java.util.List) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) WebSocketSession(org.eclipse.jetty.websocket.common.WebSocketSession) Test(org.junit.Test)

Aggregations

IBlockheadServerConnection (org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection)33 Test (org.junit.Test)33 Session (org.eclipse.jetty.websocket.api.Session)32 URI (java.net.URI)23 WebSocketSession (org.eclipse.jetty.websocket.common.WebSocketSession)15 ExecutionException (java.util.concurrent.ExecutionException)7 EndPoint (org.eclipse.jetty.io.EndPoint)7 SocketChannelEndPoint (org.eclipse.jetty.io.SocketChannelEndPoint)7 UpgradeException (org.eclipse.jetty.websocket.api.UpgradeException)6 Matchers.containsString (org.hamcrest.Matchers.containsString)6 Slow (org.eclipse.jetty.toolchain.test.annotation.Slow)3 RemoteEndpoint (org.eclipse.jetty.websocket.api.RemoteEndpoint)3 HttpCookie (java.net.HttpCookie)2 SocketTimeoutException (java.net.SocketTimeoutException)2 ByteBuffer (java.nio.ByteBuffer)2 ZeroMasker (org.eclipse.jetty.websocket.client.masks.ZeroMasker)2 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)2 Ignore (org.junit.Ignore)2 ConnectException (java.net.ConnectException)1 CookieManager (java.net.CookieManager)1