Search in sources :

Example 71 with TextFrame

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));
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 72 with TextFrame

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));
}
Also used : TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 73 with TextFrame

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);
}
Also used : TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) Test(org.junit.Test)

Example 74 with TextFrame

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);
}
Also used : ArrayList(java.util.ArrayList) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 75 with TextFrame

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);
}
Also used : TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)142 Test (org.junit.Test)130 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)109 ArrayList (java.util.ArrayList)69 CloseInfo (org.eclipse.jetty.websocket.common.CloseInfo)68 Fuzzer (org.eclipse.jetty.websocket.common.test.Fuzzer)59 ByteBuffer (java.nio.ByteBuffer)54 ContinuationFrame (org.eclipse.jetty.websocket.common.frames.ContinuationFrame)38 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)35 BlockheadClient (org.eclipse.jetty.websocket.common.test.BlockheadClient)33 PingFrame (org.eclipse.jetty.websocket.common.frames.PingFrame)23 IBlockheadClient (org.eclipse.jetty.websocket.common.test.IBlockheadClient)14 URI (java.net.URI)13 IncomingFramesCapture (org.eclipse.jetty.websocket.common.test.IncomingFramesCapture)12 Matchers.containsString (org.hamcrest.Matchers.containsString)10 PongFrame (org.eclipse.jetty.websocket.common.frames.PongFrame)9 Frame (org.eclipse.jetty.websocket.api.extensions.Frame)8 ExtensionConfig (org.eclipse.jetty.websocket.api.extensions.ExtensionConfig)6 UnitParser (org.eclipse.jetty.websocket.common.test.UnitParser)6 Slow (org.eclipse.jetty.toolchain.test.annotation.Slow)5