Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/WebSocketFrameTest.java')
-rw-r--r--jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/WebSocketFrameTest.java103
1 files changed, 66 insertions, 37 deletions
diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/WebSocketFrameTest.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/WebSocketFrameTest.java
index bd12408a95..098b5caf55 100644
--- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/WebSocketFrameTest.java
+++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/WebSocketFrameTest.java
@@ -18,6 +18,8 @@
package org.eclipse.jetty.websocket.common;
+import static org.hamcrest.Matchers.*;
+
import java.nio.ByteBuffer;
import org.eclipse.jetty.io.ByteBufferPool;
@@ -25,10 +27,11 @@ import org.eclipse.jetty.io.MappedByteBufferPool;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.websocket.api.StatusCode;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
-import org.eclipse.jetty.websocket.common.CloseInfo;
-import org.eclipse.jetty.websocket.common.Generator;
-import org.eclipse.jetty.websocket.common.OpCode;
-import org.eclipse.jetty.websocket.common.WebSocketFrame;
+import org.eclipse.jetty.websocket.api.extensions.Frame;
+import org.eclipse.jetty.websocket.common.frames.CloseFrame;
+import org.eclipse.jetty.websocket.common.frames.PingFrame;
+import org.eclipse.jetty.websocket.common.frames.TextFrame;
+import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
@@ -37,6 +40,14 @@ public class WebSocketFrameTest
private static Generator strictGenerator;
private static Generator laxGenerator;
+ private ByteBuffer generateWholeFrame(Generator generator, Frame frame)
+ {
+ ByteBuffer buf = ByteBuffer.allocate(frame.getPayloadLength() + Generator.OVERHEAD);
+ generator.generateWholeFrame(frame,buf);
+ BufferUtil.flipToFlush(buf,0);
+ return buf;
+ }
+
@BeforeClass
public static void initGenerator()
{
@@ -46,60 +57,78 @@ public class WebSocketFrameTest
laxGenerator = new Generator(policy,bufferPool,false);
}
- private void assertEqual(String message, ByteBuffer expected, ByteBuffer actual)
+ private void assertFrameHex(String message, String expectedHex, ByteBuffer actual)
{
- BufferUtil.flipToFlush(expected,0);
-
- ByteBufferAssert.assertEquals(message,expected,actual);
+ String actualHex = Hex.asHex(actual);
+ Assert.assertThat("Generated Frame:" + message,actualHex,is(expectedHex));
}
@Test
public void testLaxInvalidClose()
{
- WebSocketFrame frame = new WebSocketFrame(OpCode.CLOSE).setFin(false);
- ByteBuffer actual = laxGenerator.generate(frame);
- ByteBuffer expected = ByteBuffer.allocate(2);
- expected.put((byte)0x08);
- expected.put((byte)0x00);
-
- assertEqual("Lax Invalid Close Frame",expected,actual);
+ WebSocketFrame frame = new CloseFrame().setFin(false);
+ ByteBuffer actual = generateWholeFrame(laxGenerator,frame);
+ String expected = "0800";
+ assertFrameHex("Lax Invalid Close Frame",expected,actual);
}
@Test
public void testLaxInvalidPing()
{
- WebSocketFrame frame = new WebSocketFrame(OpCode.PING).setFin(false);
- ByteBuffer actual = laxGenerator.generate(frame);
- ByteBuffer expected = ByteBuffer.allocate(2);
- expected.put((byte)0x09);
- expected.put((byte)0x00);
-
- assertEqual("Lax Invalid Ping Frame",expected,actual);
+ WebSocketFrame frame = new PingFrame().setFin(false);
+ ByteBuffer actual = generateWholeFrame(laxGenerator,frame);
+ String expected = "0900";
+ assertFrameHex("Lax Invalid Ping Frame",expected,actual);
}
@Test
public void testStrictValidClose()
{
CloseInfo close = new CloseInfo(StatusCode.NORMAL);
- ByteBuffer actual = strictGenerator.generate(close.asFrame());
- ByteBuffer expected = ByteBuffer.allocate(4);
- expected.put((byte)0x88);
- expected.put((byte)0x02);
- expected.put((byte)0x03);
- expected.put((byte)0xE8);
-
- assertEqual("Strict Valid Close Frame",expected,actual);
+ ByteBuffer actual = generateWholeFrame(strictGenerator,close.asFrame());
+ String expected = "880203E8";
+ assertFrameHex("Strict Valid Close Frame",expected,actual);
}
@Test
public void testStrictValidPing()
{
- WebSocketFrame frame = new WebSocketFrame(OpCode.PING);
- ByteBuffer actual = strictGenerator.generate(frame);
- ByteBuffer expected = ByteBuffer.allocate(2);
- expected.put((byte)0x89);
- expected.put((byte)0x00);
-
- assertEqual("Strict Valid Ping Frame",expected,actual);
+ WebSocketFrame frame = new PingFrame();
+ ByteBuffer actual = generateWholeFrame(strictGenerator,frame);
+ String expected = "8900";
+ assertFrameHex("Strict Valid Ping Frame",expected,actual);
+ }
+
+ @Test
+ public void testRsv1()
+ {
+ TextFrame frame = new TextFrame();
+ frame.setPayload("Hi");
+ frame.setRsv1(true);
+ ByteBuffer actual = generateWholeFrame(laxGenerator,frame);
+ String expected = "C1024869";
+ assertFrameHex("Lax Text Frame with RSV1",expected,actual);
+ }
+
+ @Test
+ public void testRsv2()
+ {
+ TextFrame frame = new TextFrame();
+ frame.setPayload("Hi");
+ frame.setRsv2(true);
+ ByteBuffer actual = generateWholeFrame(laxGenerator,frame);
+ String expected = "A1024869";
+ assertFrameHex("Lax Text Frame with RSV2",expected,actual);
+ }
+
+ @Test
+ public void testRsv3()
+ {
+ TextFrame frame = new TextFrame();
+ frame.setPayload("Hi");
+ frame.setRsv3(true);
+ ByteBuffer actual = generateWholeFrame(laxGenerator,frame);
+ String expected = "91024869";
+ assertFrameHex("Lax Text Frame with RSV3",expected,actual);
}
}

Back to the top