Search in sources :

Example 16 with BinaryWebSocketFrame

use of io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame in project netty by netty.

the class PerMessageDeflateDecoderTest method testNormalFrame.

@Test
public void testNormalFrame() {
    EmbeddedChannel decoderChannel = new EmbeddedChannel(new PerMessageDeflateDecoder(false));
    // initialize
    byte[] payload = new byte[300];
    random.nextBytes(payload);
    BinaryWebSocketFrame frame = new BinaryWebSocketFrame(true, WebSocketExtension.RSV3, Unpooled.wrappedBuffer(payload));
    // execute
    decoderChannel.writeInbound(frame);
    BinaryWebSocketFrame newFrame = decoderChannel.readInbound();
    // test
    assertNotNull(newFrame);
    assertNotNull(newFrame.content());
    assertTrue(newFrame instanceof BinaryWebSocketFrame);
    assertEquals(WebSocketExtension.RSV3, newFrame.rsv());
    assertEquals(300, newFrame.content().readableBytes());
    byte[] finalPayload = new byte[300];
    newFrame.content().readBytes(finalPayload);
    assertTrue(Arrays.equals(finalPayload, payload));
    newFrame.release();
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) Test(org.junit.Test)

Example 17 with BinaryWebSocketFrame

use of io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame in project netty by netty.

the class PerMessageDeflateEncoderTest method testAlreadyCompressedFrame.

@Test
public void testAlreadyCompressedFrame() {
    EmbeddedChannel encoderChannel = new EmbeddedChannel(new PerMessageDeflateEncoder(9, 15, false));
    // initialize
    byte[] payload = new byte[300];
    random.nextBytes(payload);
    BinaryWebSocketFrame frame = new BinaryWebSocketFrame(true, WebSocketExtension.RSV3 | WebSocketExtension.RSV1, Unpooled.wrappedBuffer(payload));
    // execute
    encoderChannel.writeOutbound(frame);
    BinaryWebSocketFrame newFrame = encoderChannel.readOutbound();
    // test
    assertNotNull(newFrame);
    assertNotNull(newFrame.content());
    assertTrue(newFrame instanceof BinaryWebSocketFrame);
    assertEquals(WebSocketExtension.RSV3 | WebSocketExtension.RSV1, newFrame.rsv());
    assertEquals(300, newFrame.content().readableBytes());
    byte[] finalPayload = new byte[300];
    newFrame.content().readBytes(finalPayload);
    assertTrue(Arrays.equals(finalPayload, payload));
    newFrame.release();
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) Test(org.junit.Test)

Example 18 with BinaryWebSocketFrame

use of io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame in project netty by netty.

the class PerMessageDeflateEncoderTest method testCompressedFrame.

@Test
public void testCompressedFrame() {
    EmbeddedChannel encoderChannel = new EmbeddedChannel(new PerMessageDeflateEncoder(9, 15, false));
    EmbeddedChannel decoderChannel = new EmbeddedChannel(ZlibCodecFactory.newZlibDecoder(ZlibWrapper.NONE));
    // initialize
    byte[] payload = new byte[300];
    random.nextBytes(payload);
    BinaryWebSocketFrame frame = new BinaryWebSocketFrame(true, WebSocketExtension.RSV3, Unpooled.wrappedBuffer(payload));
    // execute
    encoderChannel.writeOutbound(frame);
    BinaryWebSocketFrame compressedFrame = encoderChannel.readOutbound();
    // test
    assertNotNull(compressedFrame);
    assertNotNull(compressedFrame.content());
    assertTrue(compressedFrame instanceof BinaryWebSocketFrame);
    assertEquals(WebSocketExtension.RSV1 | WebSocketExtension.RSV3, compressedFrame.rsv());
    decoderChannel.writeInbound(compressedFrame.content());
    decoderChannel.writeInbound(DeflateDecoder.FRAME_TAIL);
    ByteBuf uncompressedPayload = decoderChannel.readInbound();
    assertEquals(300, uncompressedPayload.readableBytes());
    byte[] finalPayload = new byte[300];
    uncompressedPayload.readBytes(finalPayload);
    assertTrue(Arrays.equals(finalPayload, payload));
    uncompressedPayload.release();
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 19 with BinaryWebSocketFrame

use of io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame in project netty by netty.

the class PerMessageDeflateEncoderTest method testFramementedFrame.

@Test
public void testFramementedFrame() {
    EmbeddedChannel encoderChannel = new EmbeddedChannel(new PerMessageDeflateEncoder(9, 15, false));
    EmbeddedChannel decoderChannel = new EmbeddedChannel(ZlibCodecFactory.newZlibDecoder(ZlibWrapper.NONE));
    // initialize
    byte[] payload1 = new byte[100];
    random.nextBytes(payload1);
    byte[] payload2 = new byte[100];
    random.nextBytes(payload2);
    byte[] payload3 = new byte[100];
    random.nextBytes(payload3);
    BinaryWebSocketFrame frame1 = new BinaryWebSocketFrame(false, WebSocketExtension.RSV3, Unpooled.wrappedBuffer(payload1));
    ContinuationWebSocketFrame frame2 = new ContinuationWebSocketFrame(false, WebSocketExtension.RSV3, Unpooled.wrappedBuffer(payload2));
    ContinuationWebSocketFrame frame3 = new ContinuationWebSocketFrame(true, WebSocketExtension.RSV3, Unpooled.wrappedBuffer(payload3));
    // execute
    encoderChannel.writeOutbound(frame1);
    encoderChannel.writeOutbound(frame2);
    encoderChannel.writeOutbound(frame3);
    BinaryWebSocketFrame compressedFrame1 = encoderChannel.readOutbound();
    ContinuationWebSocketFrame compressedFrame2 = encoderChannel.readOutbound();
    ContinuationWebSocketFrame compressedFrame3 = encoderChannel.readOutbound();
    // test
    assertNotNull(compressedFrame1);
    assertNotNull(compressedFrame2);
    assertNotNull(compressedFrame3);
    assertEquals(WebSocketExtension.RSV1 | WebSocketExtension.RSV3, compressedFrame1.rsv());
    assertEquals(WebSocketExtension.RSV3, compressedFrame2.rsv());
    assertEquals(WebSocketExtension.RSV3, compressedFrame3.rsv());
    assertFalse(compressedFrame1.isFinalFragment());
    assertFalse(compressedFrame2.isFinalFragment());
    assertTrue(compressedFrame3.isFinalFragment());
    decoderChannel.writeInbound(compressedFrame1.content());
    ByteBuf uncompressedPayload1 = decoderChannel.readInbound();
    byte[] finalPayload1 = new byte[100];
    uncompressedPayload1.readBytes(finalPayload1);
    assertTrue(Arrays.equals(finalPayload1, payload1));
    uncompressedPayload1.release();
    decoderChannel.writeInbound(compressedFrame2.content());
    ByteBuf uncompressedPayload2 = decoderChannel.readInbound();
    byte[] finalPayload2 = new byte[100];
    uncompressedPayload2.readBytes(finalPayload2);
    assertTrue(Arrays.equals(finalPayload2, payload2));
    uncompressedPayload2.release();
    decoderChannel.writeInbound(compressedFrame3.content());
    decoderChannel.writeInbound(DeflateDecoder.FRAME_TAIL);
    ByteBuf uncompressedPayload3 = decoderChannel.readInbound();
    byte[] finalPayload3 = new byte[100];
    uncompressedPayload3.readBytes(finalPayload3);
    assertTrue(Arrays.equals(finalPayload3, payload3));
    uncompressedPayload3.release();
}
Also used : ContinuationWebSocketFrame(io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 20 with BinaryWebSocketFrame

use of io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame in project netty by netty.

the class PerFrameDeflateDecoderTest method testCompressedFrame.

@Test
public void testCompressedFrame() {
    EmbeddedChannel encoderChannel = new EmbeddedChannel(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.NONE, 9, 15, 8));
    EmbeddedChannel decoderChannel = new EmbeddedChannel(new PerFrameDeflateDecoder(false));
    // initialize
    byte[] payload = new byte[300];
    random.nextBytes(payload);
    encoderChannel.writeOutbound(Unpooled.wrappedBuffer(payload));
    ByteBuf compressedPayload = encoderChannel.readOutbound();
    BinaryWebSocketFrame compressedFrame = new BinaryWebSocketFrame(true, WebSocketExtension.RSV1 | WebSocketExtension.RSV3, compressedPayload.slice(0, compressedPayload.readableBytes() - 4));
    // execute
    decoderChannel.writeInbound(compressedFrame);
    BinaryWebSocketFrame uncompressedFrame = decoderChannel.readInbound();
    // test
    assertNotNull(uncompressedFrame);
    assertNotNull(uncompressedFrame.content());
    assertTrue(uncompressedFrame instanceof BinaryWebSocketFrame);
    assertEquals(WebSocketExtension.RSV3, uncompressedFrame.rsv());
    assertEquals(300, uncompressedFrame.content().readableBytes());
    byte[] finalPayload = new byte[300];
    uncompressedFrame.content().readBytes(finalPayload);
    assertTrue(Arrays.equals(finalPayload, payload));
    uncompressedFrame.release();
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Aggregations

BinaryWebSocketFrame (io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame)28 Test (org.junit.Test)20 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)15 ByteBuf (io.netty.buffer.ByteBuf)12 FrameChecker (io.undertow.websockets.utils.FrameChecker)7 WebSocketTestClient (io.undertow.websockets.utils.WebSocketTestClient)7 URI (java.net.URI)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 FutureResult (org.xnio.FutureResult)7 ServerWebSocketContainer (io.undertow.websockets.jsr.ServerWebSocketContainer)6 UndertowSession (io.undertow.websockets.jsr.UndertowSession)6 AnnotatedClientEndpoint (io.undertow.websockets.jsr.test.annotated.AnnotatedClientEndpoint)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 Endpoint (javax.websocket.Endpoint)6 EndpointConfig (javax.websocket.EndpointConfig)6 MessageHandler (javax.websocket.MessageHandler)6 Session (javax.websocket.Session)6 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)6 ContinuationWebSocketFrame (io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame)5 TextWebSocketFrame (io.netty.handler.codec.http.websocketx.TextWebSocketFrame)5