Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoakim Erdfelt2015-02-12 16:59:52 +0000
committerJoakim Erdfelt2015-02-12 16:59:52 +0000
commit30dda86b201e96f9fa7bcdd59e335cbeb98b149a (patch)
tree1136c015581df38ed91ebec718a0c34af005cd1e /jetty-websocket
parent12ca82b5bd6db9ef1d7568c0e5b5457f92362d7e (diff)
parent23e9246115a82697c9ec3bdc634430eefc0fe244 (diff)
downloadorg.eclipse.jetty.project-30dda86b201e96f9fa7bcdd59e335cbeb98b149a.tar.gz
org.eclipse.jetty.project-30dda86b201e96f9fa7bcdd59e335cbeb98b149a.tar.xz
org.eclipse.jetty.project-30dda86b201e96f9fa7bcdd59e335cbeb98b149a.zip
Merge branch 'jetty-9.2.x'
Diffstat (limited to 'jetty-websocket')
-rw-r--r--jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/Generator.java4
-rw-r--r--jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/test/BlockheadClient.java8
-rw-r--r--jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/TooFastClientTest.java112
3 files changed, 122 insertions, 2 deletions
diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/Generator.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/Generator.java
index b2c3b010d6..fd3310cb82 100644
--- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/Generator.java
+++ b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/Generator.java
@@ -344,10 +344,12 @@ public class Generator
/**
* Generate the whole frame (header + payload copy) into a single ByteBuffer.
* <p>
- * Note: THIS IS SLOW. Only use this if you must.
+ * Note: This is slow, moves lots of memory around. Only use this if you must (such as in unit testing).
*
* @param frame
* the frame to generate
+ * @param buf
+ * the buffer to output the generated frame to
*/
public void generateWholeFrame(Frame frame, ByteBuffer buf)
{
diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/test/BlockheadClient.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/test/BlockheadClient.java
index 5de6db9898..65eea4c68e 100644
--- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/test/BlockheadClient.java
+++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/test/BlockheadClient.java
@@ -628,6 +628,12 @@ public class BlockheadClient implements OutgoingFrames, ConnectionStateListener,
public void sendStandardRequest() throws IOException
{
+ StringBuilder req = generateUpgradeRequest();
+ writeRaw(req.toString());
+ }
+
+ public StringBuilder generateUpgradeRequest()
+ {
StringBuilder req = new StringBuilder();
req.append("GET ").append(getRequestPath()).append(" HTTP/1.1\r\n");
req.append("Host: ").append(getRequestHost()).append("\r\n");
@@ -651,7 +657,7 @@ public class BlockheadClient implements OutgoingFrames, ConnectionStateListener,
}
req.append("Sec-WebSocket-Version: ").append(version).append("\r\n");
req.append("\r\n");
- writeRaw(req.toString());
+ return req;
}
public void setConnectionValue(String connectionValue)
diff --git a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/TooFastClientTest.java b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/TooFastClientTest.java
new file mode 100644
index 0000000000..354a617e20
--- /dev/null
+++ b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/TooFastClientTest.java
@@ -0,0 +1,112 @@
+//
+// ========================================================================
+// Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
+// ------------------------------------------------------------------------
+// All rights reserved. This program and the accompanying materials
+// are made available under the terms of the Eclipse Public License v1.0
+// and Apache License v2.0 which accompanies this distribution.
+//
+// The Eclipse Public License is available at
+// http://www.eclipse.org/legal/epl-v10.html
+//
+// The Apache License v2.0 is available at
+// http://www.opensource.org/licenses/apache2.0.php
+//
+// You may elect to redistribute this code under either of these licenses.
+// ========================================================================
+//
+
+package org.eclipse.jetty.websocket.server;
+
+import static org.hamcrest.Matchers.*;
+
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+import java.util.concurrent.TimeUnit;
+
+import org.eclipse.jetty.io.MappedByteBufferPool;
+import org.eclipse.jetty.toolchain.test.EventQueue;
+import org.eclipse.jetty.util.BufferUtil;
+import org.eclipse.jetty.websocket.api.WebSocketPolicy;
+import org.eclipse.jetty.websocket.common.Generator;
+import org.eclipse.jetty.websocket.common.WebSocketFrame;
+import org.eclipse.jetty.websocket.common.frames.TextFrame;
+import org.eclipse.jetty.websocket.common.test.BlockheadClient;
+import org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPool;
+import org.eclipse.jetty.websocket.server.examples.MyEchoServlet;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Ignore;
+import org.junit.Test;
+
+/**
+ * Test simulating a client that talks too quickly.
+ * <p>
+ * There is a class of client that will send the GET+Upgrade Request along with a few websocket frames in a single
+ * network packet. This test attempts to perform this behavior as close as possible.
+ */
+@Ignore
+public class TooFastClientTest
+{
+ private static SimpleServletServer server;
+
+ @BeforeClass
+ public static void startServer() throws Exception
+ {
+ server = new SimpleServletServer(new MyEchoServlet());
+ server.start();
+ }
+
+ @AfterClass
+ public static void stopServer()
+ {
+ server.stop();
+ }
+
+ @Test
+ public void testUpgradeWithWebkitDeflateExtension() 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(4096);
+ 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 LeakTrackingBufferPool("Generator",new MappedByteBufferPool()));
+ String msg1 = "Echo 1";
+ String msg2 = "This is also an echo ... cho ... ho ... o";
+
+ generator.generateWholeFrame(new TextFrame().setPayload(msg1),initialPacket);
+ generator.generateWholeFrame(new TextFrame().setPayload(msg2),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(2,1,TimeUnit.SECONDS);
+ WebSocketFrame tf = frames.poll();
+ Assert.assertThat("Text Frame/msg1",tf.getPayloadAsUTF8(),is(msg1));
+ tf = frames.poll();
+ Assert.assertThat("Text Frame/msg2",tf.getPayloadAsUTF8(),is(msg2));
+ }
+ finally
+ {
+ client.close();
+ }
+ }
+}

Back to the top