Adding UnitGenerator and migrating more tests cases to WebSocketFrame

This commit is contained in:
Joakim Erdfelt 2012-07-10 12:05:03 -07:00
parent f97d86894a
commit b46ba628cd
7 changed files with 63 additions and 102 deletions

View File

@ -67,6 +67,11 @@ public class CloseInfo
}
}
public CloseInfo(int statusCode)
{
this(statusCode, null);
}
public CloseInfo(int statusCode, String reason)
{
this.statusCode = statusCode;

View File

@ -376,7 +376,7 @@ public class WebSocketFrame implements Frame
return this;
}
private WebSocketFrame setPayload(String str)
public WebSocketFrame setPayload(String str)
{
setPayload(BufferUtil.toBuffer(str,StringUtil.__UTF8_CHARSET));
return this;

View File

@ -1,6 +1,6 @@
package org.eclipse.jetty.websocket;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.*;
import java.nio.ByteBuffer;
@ -12,7 +12,6 @@ import org.eclipse.jetty.websocket.masks.FixedMasker;
import org.eclipse.jetty.websocket.masks.RandomMasker;
import org.eclipse.jetty.websocket.parser.FrameParseCapture;
import org.eclipse.jetty.websocket.parser.Parser;
import org.eclipse.jetty.websocket.protocol.FrameBuilder;
import org.eclipse.jetty.websocket.protocol.OpCode;
import org.eclipse.jetty.websocket.protocol.WebSocketFrame;
import org.junit.Assert;
@ -37,7 +36,8 @@ public class GeneratorParserRoundtripTest
{
// Generate Buffer
BufferUtil.flipToFill(out);
out = gen.generate(FrameBuilder.text().payload(message.getBytes()).asFrame());
WebSocketFrame frame = WebSocketFrame.text(message);
out = gen.generate(frame);
// Parse Buffer
BufferUtil.flipToFlush(out,0);
@ -74,15 +74,15 @@ public class GeneratorParserRoundtripTest
try
{
// Setup Frame
WebSocketFrame txt = FrameBuilder.text().payload(message.getBytes()).asFrame();
WebSocketFrame frame = WebSocketFrame.text(message);
// Add masking
byte mask[] = new byte[4];
new FixedMasker().genMask(mask);
txt.setMask(mask);
frame.setMask(mask);
// Generate Buffer
out = gen.generate(policy.getBufferSize(),txt);
out = gen.generate(policy.getBufferSize(),frame);
// Parse Buffer
BufferUtil.flipToFlush(out,0);

View File

@ -2,7 +2,6 @@ package org.eclipse.jetty.websocket.driver;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.io.StandardByteBufferPool;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.websocket.api.StatusCode;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.examples.AdapterConnectCloseSocket;
@ -11,7 +10,8 @@ import org.eclipse.jetty.websocket.examples.AnnotatedBinaryStreamSocket;
import org.eclipse.jetty.websocket.examples.AnnotatedFramesSocket;
import org.eclipse.jetty.websocket.examples.ListenerBasicSocket;
import org.eclipse.jetty.websocket.io.LocalWebSocketConnection;
import org.eclipse.jetty.websocket.protocol.FrameBuilder;
import org.eclipse.jetty.websocket.protocol.CloseInfo;
import org.eclipse.jetty.websocket.protocol.OpCode;
import org.eclipse.jetty.websocket.protocol.WebSocketFrame;
import org.junit.Rule;
import org.junit.Test;
@ -24,7 +24,7 @@ public class WebSocketEventDriverTest
private WebSocketFrame makeBinaryFrame(String content, boolean fin)
{
return FrameBuilder.binary().fin(fin).payload((content.getBytes(StringUtil.__UTF8_CHARSET))).asFrame();
return WebSocketFrame.binary().setFin(fin).setPayload(content);
}
private WebSocketEventDriver newDriver(Object websocket)
@ -45,7 +45,7 @@ public class WebSocketEventDriverTest
LocalWebSocketConnection conn = new LocalWebSocketConnection(testname);
driver.setConnection(conn);
driver.onConnect();
driver.onFrame(FrameBuilder.close(StatusCode.NORMAL).asFrame());
driver.onFrame(new CloseInfo(StatusCode.NORMAL).asFrame());
socket.capture.assertEventCount(2);
socket.capture.assertEventStartsWith(0,"onWebSocketConnect");
@ -62,7 +62,7 @@ public class WebSocketEventDriverTest
driver.setConnection(conn);
driver.onConnect();
driver.onFrame(makeBinaryFrame("Hello World",true));
driver.onFrame(FrameBuilder.close(StatusCode.NORMAL).asFrame());
driver.onFrame(new CloseInfo(StatusCode.NORMAL).asFrame());
socket.capture.assertEventCount(3);
socket.capture.assertEventStartsWith(0,"onConnect");
@ -79,10 +79,10 @@ public class WebSocketEventDriverTest
LocalWebSocketConnection conn = new LocalWebSocketConnection(testname);
driver.setConnection(conn);
driver.onConnect();
driver.onFrame(FrameBuilder.ping("PING").asFrame());
driver.onFrame(FrameBuilder.text().payload("Text Me").asFrame());
driver.onFrame(FrameBuilder.binary().payload(StringUtil.getUtf8Bytes("Hello Bin")).asFrame());
driver.onFrame(FrameBuilder.close(StatusCode.SHUTDOWN).asFrame());
driver.onFrame(new WebSocketFrame(OpCode.PING).setPayload("PING"));
driver.onFrame(WebSocketFrame.text("Text Me"));
driver.onFrame(WebSocketFrame.binary().setPayload("Hello Bin"));
driver.onFrame(new CloseInfo(StatusCode.SHUTDOWN).asFrame());
socket.capture.assertEventCount(6);
socket.capture.assertEventStartsWith(0,"onConnect(");
@ -103,7 +103,7 @@ public class WebSocketEventDriverTest
driver.setConnection(conn);
driver.onConnect();
driver.onFrame(makeBinaryFrame("Hello World",true));
driver.onFrame(FrameBuilder.close(StatusCode.NORMAL).asFrame());
driver.onFrame(new CloseInfo(StatusCode.NORMAL).asFrame());
socket.capture.assertEventCount(3);
socket.capture.assertEventStartsWith(0,"onConnect");
@ -120,8 +120,8 @@ public class WebSocketEventDriverTest
LocalWebSocketConnection conn = new LocalWebSocketConnection(testname);
driver.setConnection(conn);
driver.onConnect();
driver.onFrame(FrameBuilder.text("Hello World").asFrame());
driver.onFrame(FrameBuilder.close(StatusCode.NORMAL).asFrame());
driver.onFrame(WebSocketFrame.text("Hello World"));
driver.onFrame(new CloseInfo(StatusCode.NORMAL).asFrame());
socket.capture.assertEventCount(3);
socket.capture.assertEventStartsWith(0,"onWebSocketConnect");

View File

@ -4,9 +4,8 @@ import java.nio.ByteBuffer;
import java.util.Arrays;
import org.eclipse.jetty.websocket.ByteBufferAssert;
import org.eclipse.jetty.websocket.api.WebSocketBehavior;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.protocol.FrameBuilder;
import org.eclipse.jetty.websocket.protocol.OpCode;
import org.eclipse.jetty.websocket.protocol.UnitGenerator;
import org.eclipse.jetty.websocket.protocol.WebSocketFrame;
import org.junit.Test;
@ -17,12 +16,10 @@ public class RFC6455ExamplesGeneratorTest
@Test
public void testFragmentedUnmaskedTextMessage()
{
WebSocketFrame text1 = FrameBuilder.text("Hel").fin(false).asFrame();
WebSocketFrame text2 = FrameBuilder.continuation("lo").asFrame();
WebSocketFrame text1 = WebSocketFrame.text("Hel").setFin(false);
WebSocketFrame text2 = new WebSocketFrame(OpCode.CONTINUATION).setPayload("lo");
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
Generator generator = new Generator(policy);
Generator generator = new UnitGenerator();
ByteBuffer actual1 = generator.generate(text1);
ByteBuffer actual2 = generator.generate(text2);
@ -49,11 +46,12 @@ public class RFC6455ExamplesGeneratorTest
@Test
public void testSingleMaskedPongRequest()
{
WebSocketFrame pong = FrameBuilder.pong("Hello").mask(new byte[]
{ 0x37, (byte)0xfa, 0x21, 0x3d }).asFrame();
WebSocketFrame pong = new WebSocketFrame(OpCode.PONG);
pong.setPayload("Hello");
pong.setMask(new byte[]
{ 0x37, (byte)0xfa, 0x21, 0x3d });
WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
Generator gen = new Generator(policy);
Generator gen = new UnitGenerator();
ByteBuffer actual = gen.generate(pong);
actual.flip(); // make readable
@ -71,12 +69,12 @@ public class RFC6455ExamplesGeneratorTest
@Test
public void testSingleMaskedTextMessage()
{
WebSocketFrame text = FrameBuilder.text("Hello").mask(new byte[]
{ 0x37, (byte)0xfa, 0x21, 0x3d }).asFrame();
WebSocketFrame text = WebSocketFrame.text("Hello");
text.setMask(new byte[]
{ 0x37, (byte)0xfa, 0x21, 0x3d });
WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
Generator gen = new Generator(policy);
Generator gen = new UnitGenerator();
;
ByteBuffer actual = gen.generate(text);
actual.flip(); // make readable
@ -96,13 +94,12 @@ public class RFC6455ExamplesGeneratorTest
{
int dataSize = 256;
WebSocketFrame binary = FrameBuilder.binary().asFrame();
WebSocketFrame binary = WebSocketFrame.binary();
byte payload[] = new byte[dataSize];
Arrays.fill(payload,(byte)0x44);
binary.setPayload(payload);
WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
Generator gen = new Generator(policy);
Generator gen = new UnitGenerator();
ByteBuffer actual = gen.generate(binary);
@ -129,13 +126,12 @@ public class RFC6455ExamplesGeneratorTest
{
int dataSize = 1024 * 64;
WebSocketFrame binary = FrameBuilder.binary().asFrame();
WebSocketFrame binary = WebSocketFrame.binary();
byte payload[] = new byte[dataSize];
Arrays.fill(payload,(byte)0x44);
binary.setPayload(payload);
WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
Generator gen = new Generator(policy);
Generator gen = new UnitGenerator();
ByteBuffer actual = gen.generate(binary);
@ -161,11 +157,9 @@ public class RFC6455ExamplesGeneratorTest
@Test
public void testSingleUnmaskedPingRequest() throws Exception
{
WebSocketFrame ping = FrameBuilder.ping("Hello").asFrame();
WebSocketFrame ping = new WebSocketFrame(OpCode.PING).setPayload("Hello");
WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
Generator gen = new Generator(policy);
Generator gen = new UnitGenerator();
ByteBuffer actual = gen.generate(ping);
actual.flip(); // make readable
@ -180,11 +174,9 @@ public class RFC6455ExamplesGeneratorTest
@Test
public void testSingleUnmaskedTextMessage()
{
WebSocketFrame text = FrameBuilder.text("Hello").asFrame();
WebSocketFrame text = WebSocketFrame.text("Hello");
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
Generator generator = new Generator(policy);
Generator generator = new UnitGenerator();
ByteBuffer actual = generator.generate(text);

View File

@ -1,52 +0,0 @@
package org.eclipse.jetty.websocket.protocol;
import org.junit.Assert;
import org.junit.Test;
public class FrameBuilderTest
{
@Test
public void testSimpleInvalidCloseFrameBuilder()
{
byte[] actual = FrameBuilder.close().fin(false).asByteArray();
byte[] expected = new byte[]
{ (byte)0x08, (byte)0x00 };
Assert.assertArrayEquals(expected,actual);
}
@Test
public void testSimpleInvalidPingFrameBuilder()
{
byte[] actual = FrameBuilder.ping().fin(false).asByteArray();
byte[] expected = new byte[]
{ (byte)0x09, (byte)0x00 };
Assert.assertArrayEquals(expected,actual);
}
@Test
public void testSimpleValidCloseFrame()
{
byte[] actual = FrameBuilder.close(1000).asByteArray();
byte[] expected = new byte[]
{ (byte)0x88, (byte)0x02, (byte)0x03, (byte)0xe8 };
Assert.assertArrayEquals(expected,actual);
}
@Test
public void testSimpleValidPingFrame()
{
byte[] actual = FrameBuilder.ping().asByteArray();
byte[] expected = new byte[]
{ (byte)0x89, (byte)0x00 };
Assert.assertArrayEquals(expected,actual);
}
}

View File

@ -0,0 +1,16 @@
package org.eclipse.jetty.websocket.protocol;
import org.eclipse.jetty.io.StandardByteBufferPool;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.generator.Generator;
/**
* Convenience Generator.
*/
public class UnitGenerator extends Generator
{
public UnitGenerator()
{
super(WebSocketPolicy.newServerPolicy(),new StandardByteBufferPool());
}
}