Search in sources :

Example 91 with EmbeddedChannel

use of io.netty.channel.embedded.EmbeddedChannel in project netty by netty.

the class HttpRequestDecoderBenchmark method testDecodeWholeRequestInMultipleSteps.

private static void testDecodeWholeRequestInMultipleSteps(byte[] content, int fragmentSize) {
    final EmbeddedChannel channel = new EmbeddedChannel(new HttpRequestDecoder());
    final int headerLength = content.length - CONTENT_LENGTH;
    // split up the header
    for (int a = 0; a < headerLength; ) {
        int amount = fragmentSize;
        if (a + amount > headerLength) {
            amount = headerLength - a;
        }
        // if header is done it should produce a HttpRequest
        channel.writeInbound(Unpooled.wrappedBuffer(content, a, amount));
        a += amount;
    }
    for (int i = CONTENT_LENGTH; i > 0; i--) {
        // Should produce HttpContent
        channel.writeInbound(Unpooled.wrappedBuffer(content, content.length - i, 1));
    }
}
Also used : HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel)

Example 92 with EmbeddedChannel

use of io.netty.channel.embedded.EmbeddedChannel in project netty by netty.

the class WebSocketClientHandshakerTest method testHttpResponseAndFrameInSameBuffer.

private void testHttpResponseAndFrameInSameBuffer(boolean codec) {
    String url = "ws://localhost:9999/ws";
    final WebSocketClientHandshaker shaker = newHandshaker(URI.create(url));
    final WebSocketClientHandshaker handshaker = new WebSocketClientHandshaker(shaker.uri(), shaker.version(), null, EmptyHttpHeaders.INSTANCE, Integer.MAX_VALUE) {

        @Override
        protected FullHttpRequest newHandshakeRequest() {
            return shaker.newHandshakeRequest();
        }

        @Override
        protected void verify(FullHttpResponse response) {
        // Not do any verification, so we not need to care sending the correct headers etc in the test,
        // which would just make things more complicated.
        }

        @Override
        protected WebSocketFrameDecoder newWebsocketDecoder() {
            return shaker.newWebsocketDecoder();
        }

        @Override
        protected WebSocketFrameEncoder newWebSocketEncoder() {
            return shaker.newWebSocketEncoder();
        }
    };
    byte[] data = new byte[24];
    PlatformDependent.threadLocalRandom().nextBytes(data);
    // Create a EmbeddedChannel which we will use to encode a BinaryWebsocketFrame to bytes and so use these
    // to test the actual handshaker.
    WebSocketServerHandshakerFactory factory = new WebSocketServerHandshakerFactory(url, null, false);
    WebSocketServerHandshaker socketServerHandshaker = factory.newHandshaker(shaker.newHandshakeRequest());
    EmbeddedChannel websocketChannel = new EmbeddedChannel(socketServerHandshaker.newWebSocketEncoder(), socketServerHandshaker.newWebsocketDecoder());
    assertTrue(websocketChannel.writeOutbound(new BinaryWebSocketFrame(Unpooled.wrappedBuffer(data))));
    byte[] bytes = "HTTP/1.1 101 Switching Protocols\r\nContent-Length: 0\r\n\r\n".getBytes(CharsetUtil.US_ASCII);
    CompositeByteBuf compositeByteBuf = Unpooled.compositeBuffer();
    compositeByteBuf.addComponent(true, Unpooled.wrappedBuffer(bytes));
    for (; ; ) {
        ByteBuf frameBytes = websocketChannel.readOutbound();
        if (frameBytes == null) {
            break;
        }
        compositeByteBuf.addComponent(true, frameBytes);
    }
    EmbeddedChannel ch = new EmbeddedChannel(new HttpObjectAggregator(Integer.MAX_VALUE), new SimpleChannelInboundHandler<FullHttpResponse>() {

        @Override
        protected void channelRead0(ChannelHandlerContext ctx, FullHttpResponse msg) throws Exception {
            handshaker.finishHandshake(ctx.channel(), msg);
            ctx.pipeline().remove(this);
        }
    });
    if (codec) {
        ch.pipeline().addFirst(new HttpClientCodec());
    } else {
        ch.pipeline().addFirst(new HttpRequestEncoder(), new HttpResponseDecoder());
    }
    // We need to first write the request as HttpClientCodec will fail if we receive a response before a request
    // was written.
    shaker.handshake(ch).syncUninterruptibly();
    for (; ; ) {
        // Just consume the bytes, we are not interested in these.
        ByteBuf buf = ch.readOutbound();
        if (buf == null) {
            break;
        }
        buf.release();
    }
    assertTrue(ch.writeInbound(compositeByteBuf));
    assertTrue(ch.finish());
    BinaryWebSocketFrame frame = ch.readInbound();
    ByteBuf expect = Unpooled.wrappedBuffer(data);
    try {
        assertEquals(expect, frame.content());
        assertTrue(frame.isFinalFragment());
        assertEquals(0, frame.rsv());
    } finally {
        expect.release();
        frame.release();
    }
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) HttpRequestEncoder(io.netty.handler.codec.http.HttpRequestEncoder) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) HttpResponseDecoder(io.netty.handler.codec.http.HttpResponseDecoder)

Example 93 with EmbeddedChannel

use of io.netty.channel.embedded.EmbeddedChannel in project netty by netty.

the class WebSocketFrameAggregatorTest method textFrameTooBig.

@Test
public void textFrameTooBig() throws Exception {
    EmbeddedChannel channel = new EmbeddedChannel(new WebSocketFrameAggregator(8));
    channel.writeInbound(new BinaryWebSocketFrame(true, 1, Unpooled.wrappedBuffer(content1)));
    channel.writeInbound(new BinaryWebSocketFrame(false, 0, Unpooled.wrappedBuffer(content1)));
    try {
        channel.writeInbound(new ContinuationWebSocketFrame(false, 0, Unpooled.wrappedBuffer(content2)));
        Assert.fail();
    } catch (TooLongFrameException e) {
    // expected
    }
    channel.writeInbound(new ContinuationWebSocketFrame(false, 0, Unpooled.wrappedBuffer(content2)));
    channel.writeInbound(new ContinuationWebSocketFrame(true, 0, Unpooled.wrappedBuffer(content2)));
    channel.writeInbound(new BinaryWebSocketFrame(true, 1, Unpooled.wrappedBuffer(content1)));
    channel.writeInbound(new BinaryWebSocketFrame(false, 0, Unpooled.wrappedBuffer(content1)));
    try {
        channel.writeInbound(new ContinuationWebSocketFrame(false, 0, Unpooled.wrappedBuffer(content2)));
        Assert.fail();
    } catch (TooLongFrameException e) {
    // expected
    }
    channel.writeInbound(new ContinuationWebSocketFrame(false, 0, Unpooled.wrappedBuffer(content2)));
    channel.writeInbound(new ContinuationWebSocketFrame(true, 0, Unpooled.wrappedBuffer(content2)));
    for (; ; ) {
        Object msg = channel.readInbound();
        if (msg == null) {
            break;
        }
        ReferenceCountUtil.release(msg);
    }
    channel.finish();
}
Also used : TooLongFrameException(io.netty.handler.codec.TooLongFrameException) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.Test)

Example 94 with EmbeddedChannel

use of io.netty.channel.embedded.EmbeddedChannel in project netty by netty.

the class WebSocketFrameAggregatorTest method testAggregationText.

@Test
public void testAggregationText() {
    EmbeddedChannel channel = new EmbeddedChannel(new WebSocketFrameAggregator(Integer.MAX_VALUE));
    channel.writeInbound(new TextWebSocketFrame(true, 1, Unpooled.wrappedBuffer(content1)));
    channel.writeInbound(new TextWebSocketFrame(false, 0, Unpooled.wrappedBuffer(content1)));
    channel.writeInbound(new ContinuationWebSocketFrame(false, 0, Unpooled.wrappedBuffer(content2)));
    channel.writeInbound(new PingWebSocketFrame(Unpooled.wrappedBuffer(content1)));
    channel.writeInbound(new PongWebSocketFrame(Unpooled.wrappedBuffer(content1)));
    channel.writeInbound(new ContinuationWebSocketFrame(true, 0, Unpooled.wrappedBuffer(content3)));
    Assert.assertTrue(channel.finish());
    TextWebSocketFrame frame = channel.readInbound();
    Assert.assertTrue(frame.isFinalFragment());
    Assert.assertEquals(1, frame.rsv());
    Assert.assertArrayEquals(content1, toBytes(frame.content()));
    PingWebSocketFrame frame2 = channel.readInbound();
    Assert.assertTrue(frame2.isFinalFragment());
    Assert.assertEquals(0, frame2.rsv());
    Assert.assertArrayEquals(content1, toBytes(frame2.content()));
    PongWebSocketFrame frame3 = channel.readInbound();
    Assert.assertTrue(frame3.isFinalFragment());
    Assert.assertEquals(0, frame3.rsv());
    Assert.assertArrayEquals(content1, toBytes(frame3.content()));
    TextWebSocketFrame frame4 = channel.readInbound();
    Assert.assertTrue(frame4.isFinalFragment());
    Assert.assertEquals(0, frame4.rsv());
    Assert.assertArrayEquals(aggregatedContent, toBytes(frame4.content()));
    Assert.assertNull(channel.readInbound());
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.Test)

Example 95 with EmbeddedChannel

use of io.netty.channel.embedded.EmbeddedChannel in project netty by netty.

the class WebSocketFrameAggregatorTest method testAggregationBinary.

@Test
public void testAggregationBinary() {
    EmbeddedChannel channel = new EmbeddedChannel(new WebSocketFrameAggregator(Integer.MAX_VALUE));
    channel.writeInbound(new BinaryWebSocketFrame(true, 1, Unpooled.wrappedBuffer(content1)));
    channel.writeInbound(new BinaryWebSocketFrame(false, 0, Unpooled.wrappedBuffer(content1)));
    channel.writeInbound(new ContinuationWebSocketFrame(false, 0, Unpooled.wrappedBuffer(content2)));
    channel.writeInbound(new PingWebSocketFrame(Unpooled.wrappedBuffer(content1)));
    channel.writeInbound(new PongWebSocketFrame(Unpooled.wrappedBuffer(content1)));
    channel.writeInbound(new ContinuationWebSocketFrame(true, 0, Unpooled.wrappedBuffer(content3)));
    Assert.assertTrue(channel.finish());
    BinaryWebSocketFrame frame = channel.readInbound();
    Assert.assertTrue(frame.isFinalFragment());
    Assert.assertEquals(1, frame.rsv());
    Assert.assertArrayEquals(content1, toBytes(frame.content()));
    PingWebSocketFrame frame2 = channel.readInbound();
    Assert.assertTrue(frame2.isFinalFragment());
    Assert.assertEquals(0, frame2.rsv());
    Assert.assertArrayEquals(content1, toBytes(frame2.content()));
    PongWebSocketFrame frame3 = channel.readInbound();
    Assert.assertTrue(frame3.isFinalFragment());
    Assert.assertEquals(0, frame3.rsv());
    Assert.assertArrayEquals(content1, toBytes(frame3.content()));
    BinaryWebSocketFrame frame4 = channel.readInbound();
    Assert.assertTrue(frame4.isFinalFragment());
    Assert.assertEquals(0, frame4.rsv());
    Assert.assertArrayEquals(aggregatedContent, toBytes(frame4.content()));
    Assert.assertNull(channel.readInbound());
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.Test)

Aggregations

EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)446 Test (org.junit.Test)364 ByteBuf (io.netty.buffer.ByteBuf)162 HttpResponse (io.netty.handler.codec.http.HttpResponse)30 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)28 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)25 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)21 HttpRequest (io.netty.handler.codec.http.HttpRequest)20 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)17 InetSocketAddress (java.net.InetSocketAddress)17 BinaryWebSocketFrame (io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame)15 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)14 TooLongFrameException (io.netty.handler.codec.TooLongFrameException)11 ArrayList (java.util.ArrayList)11 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)10 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)10 KvStateRegistry (org.apache.flink.runtime.query.KvStateRegistry)10 UUID (java.util.UUID)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 IOException (java.io.IOException)7