use of org.eclipse.jetty.websocket.api.WebSocketPolicy in project jetty.project by eclipse.
the class ExtensionStackTest method createExtensionStack.
private ExtensionStack createExtensionStack() {
WebSocketPolicy policy = WebSocketPolicy.newClientPolicy();
WebSocketContainerScope container = new SimpleContainerScope(policy, bufferPool);
WebSocketExtensionFactory factory = new WebSocketExtensionFactory(container);
return new ExtensionStack(factory);
}
use of org.eclipse.jetty.websocket.api.WebSocketPolicy in project jetty.project by eclipse.
the class GeneratorParserRoundtripTest method testParserAndGenerator.
@Test
public void testParserAndGenerator() throws Exception {
WebSocketPolicy policy = WebSocketPolicy.newClientPolicy();
Generator gen = new Generator(policy, bufferPool);
Parser parser = new Parser(policy, bufferPool);
IncomingFramesCapture capture = new IncomingFramesCapture();
parser.setIncomingFramesHandler(capture);
String message = "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF";
ByteBuffer out = bufferPool.acquire(8192, false);
try {
// Generate Buffer
BufferUtil.flipToFill(out);
WebSocketFrame frame = new TextFrame().setPayload(message);
ByteBuffer header = gen.generateHeaderBytes(frame);
ByteBuffer payload = frame.getPayload();
out.put(header);
out.put(payload);
// Parse Buffer
BufferUtil.flipToFlush(out, 0);
parser.parse(out);
} finally {
bufferPool.release(out);
}
// Validate
capture.assertNoErrors();
capture.assertHasFrame(OpCode.TEXT, 1);
TextFrame txt = (TextFrame) capture.getFrames().poll();
Assert.assertThat("Text parsed", txt.getPayloadAsUTF8(), is(message));
}
use of org.eclipse.jetty.websocket.api.WebSocketPolicy in project jetty.project by eclipse.
the class GeneratorTest method testWindowedGenerateWithMasking.
@Test
public void testWindowedGenerateWithMasking() {
// A decent sized frame, with masking
byte[] payload = new byte[10240];
Arrays.fill(payload, (byte) 0x55);
byte[] mask = new byte[] { 0x2A, (byte) 0xF0, 0x0F, 0x00 };
WebSocketFrame frame = new BinaryFrame().setPayload(payload);
// masking!
frame.setMask(mask);
// Generate
int windowSize = 2929;
WindowHelper helper = new WindowHelper(windowSize);
ByteBuffer completeBuffer = helper.generateWindowed(frame);
// Validate
int expectedHeaderSize = 8;
int expectedSize = payload.length + expectedHeaderSize;
int expectedParts = 1;
helper.assertTotalParts(expectedParts);
helper.assertTotalBytes(payload.length + expectedHeaderSize);
Assert.assertThat("Generated Buffer", completeBuffer.remaining(), is(expectedSize));
// Parse complete buffer.
WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
Parser parser = new UnitParser(policy);
IncomingFramesCapture capture = new IncomingFramesCapture();
parser.setIncomingFramesHandler(capture);
parser.parse(completeBuffer);
// Assert validity of frame
WebSocketFrame actual = capture.getFrames().poll();
Assert.assertThat("Frame.opcode", actual.getOpCode(), is(OpCode.BINARY));
Assert.assertThat("Frame.payloadLength", actual.getPayloadLength(), is(payload.length));
// Validate payload contents for proper masking
ByteBuffer actualData = actual.getPayload().slice();
Assert.assertThat("Frame.payload.remaining", actualData.remaining(), is(payload.length));
while (actualData.remaining() > 0) {
Assert.assertThat("Actual.payload[" + actualData.position() + "]", actualData.get(), is((byte) 0x55));
}
}
use of org.eclipse.jetty.websocket.api.WebSocketPolicy in project jetty.project by eclipse.
the class ParserTest method testParseNothing.
@Test
public void testParseNothing() {
ByteBuffer buf = ByteBuffer.allocate(16);
// Put nothing in the buffer.
buf.flip();
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
Parser parser = new UnitParser(policy);
IncomingFramesCapture capture = new IncomingFramesCapture();
parser.setIncomingFramesHandler(capture);
parser.parse(buf);
capture.assertNoErrors();
Assert.assertThat("Frame Count", capture.getFrames().size(), is(0));
}
use of org.eclipse.jetty.websocket.api.WebSocketPolicy in project jetty.project by eclipse.
the class RFC6455ExamplesParserTest method testFragmentedUnmaskedTextMessage.
@Test
public void testFragmentedUnmaskedTextMessage() {
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.CLIENT);
Parser parser = new UnitParser(policy);
IncomingFramesCapture capture = new IncomingFramesCapture();
parser.setIncomingFramesHandler(capture);
ByteBuffer buf = ByteBuffer.allocate(16);
BufferUtil.clearToFill(buf);
// Raw bytes as found in RFC 6455, Section 5.7 - Examples
// A fragmented unmasked text message (part 1 of 2 "Hel")
buf.put(new byte[] { (byte) 0x01, (byte) 0x03, 0x48, (byte) 0x65, 0x6c });
// Parse #1
BufferUtil.flipToFlush(buf, 0);
parser.parse(buf);
// part 2 of 2 "lo" (A continuation frame of the prior text message)
BufferUtil.flipToFill(buf);
buf.put(new byte[] { (byte) 0x80, 0x02, 0x6c, 0x6f });
// Parse #2
BufferUtil.flipToFlush(buf, 0);
parser.parse(buf);
capture.assertNoErrors();
capture.assertHasFrame(OpCode.TEXT, 1);
capture.assertHasFrame(OpCode.CONTINUATION, 1);
WebSocketFrame txt = capture.getFrames().poll();
String actual = BufferUtil.toUTF8String(txt.getPayload());
Assert.assertThat("TextFrame[0].data", actual, is("Hel"));
txt = capture.getFrames().poll();
actual = BufferUtil.toUTF8String(txt.getPayload());
Assert.assertThat("TextFrame[1].data", actual, is("lo"));
}
Aggregations