use of org.eclipse.jetty.websocket.common.frames.TextFrame 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.common.frames.TextFrame in project jetty.project by eclipse.
the class GeneratorParserRoundtripTest method testParserAndGeneratorMasked.
@Test
public void testParserAndGeneratorMasked() throws Exception {
Generator gen = new Generator(WebSocketPolicy.newClientPolicy(), bufferPool);
Parser parser = new Parser(WebSocketPolicy.newServerPolicy(), bufferPool);
IncomingFramesCapture capture = new IncomingFramesCapture();
parser.setIncomingFramesHandler(capture);
String message = "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF";
ByteBuffer out = bufferPool.acquire(8192, false);
BufferUtil.flipToFill(out);
try {
// Setup Frame
WebSocketFrame frame = new TextFrame().setPayload(message);
// Add masking
byte[] mask = new byte[4];
Arrays.fill(mask, (byte) 0xFF);
frame.setMask(mask);
// Generate Buffer
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.assertTrue("Text.isMasked", txt.isMasked());
Assert.assertThat("Text parsed", txt.getPayloadAsUTF8(), is(message));
}
use of org.eclipse.jetty.websocket.common.frames.TextFrame in project jetty.project by eclipse.
the class GeneratorTest method testText_Hello.
@Test
public void testText_Hello() {
WebSocketFrame frame = new TextFrame().setPayload("Hello");
byte[] utf = StringUtil.getUtf8Bytes("Hello");
assertGeneratedBytes("8105" + Hex.asHex(utf), frame);
}
use of org.eclipse.jetty.websocket.common.frames.TextFrame in project jetty.project by eclipse.
the class ParserTest method testParseCase5_18.
/**
* Similar to the server side 5.18 testcase. Text message fragmented as 2 frames, both as opcode=TEXT
*/
@Test
public void testParseCase5_18() {
List<WebSocketFrame> send = new ArrayList<>();
send.add(new TextFrame().setPayload("fragment1").setFin(false));
// bad frame, must be continuation
send.add(new TextFrame().setPayload("fragment2").setFin(true));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
ByteBuffer completeBuf = UnitGenerator.generate(send);
UnitParser parser = new UnitParser();
IncomingFramesCapture capture = new IncomingFramesCapture();
parser.setIncomingFramesHandler(capture);
parser.parseQuietly(completeBuf);
capture.assertErrorCount(1);
// fragment 1
capture.assertHasFrame(OpCode.TEXT, 1);
}
use of org.eclipse.jetty.websocket.common.frames.TextFrame in project jetty.project by eclipse.
the class TestABCase1_1 method testGenerate65535ByteTextCase1_1_6.
@Test
public void testGenerate65535ByteTextCase1_1_6() {
int length = 65535;
StringBuilder builder = new StringBuilder();
for (int i = 0; i < length; ++i) {
builder.append("*");
}
WebSocketFrame textFrame = new TextFrame().setPayload(builder.toString());
ByteBuffer actual = UnitGenerator.generate(textFrame);
ByteBuffer expected = ByteBuffer.allocate(length + 5);
expected.put(new byte[] { (byte) 0x81 });
// no masking
byte b = 0x00;
b |= 0x7E;
expected.put(b);
expected.put(new byte[] { (byte) 0xff, (byte) 0xff });
for (int i = 0; i < length; ++i) {
expected.put("*".getBytes());
}
expected.flip();
ByteBufferAssert.assertEquals("buffers do not match", expected, actual);
}
Aggregations