Search in sources :

Example 96 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 97 with EmbeddedChannel

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

the class HttpClientCodecTest method testFailsOnMissingResponse.

@Test
public void testFailsOnMissingResponse() {
    HttpClientCodec codec = new HttpClientCodec(4096, 8192, 8192, true);
    EmbeddedChannel ch = new EmbeddedChannel(codec);
    assertTrue(ch.writeOutbound(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http://localhost/")));
    ByteBuf buffer = ch.readOutbound();
    assertNotNull(buffer);
    buffer.release();
    try {
        ch.finish();
        fail();
    } catch (CodecException e) {
        assertTrue(e instanceof PrematureChannelClosureException);
    }
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) CodecException(io.netty.handler.codec.CodecException) PrematureChannelClosureException(io.netty.handler.codec.PrematureChannelClosureException) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 98 with EmbeddedChannel

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

the class HttpClientCodecTest method testFailsOnIncompleteChunkedResponse.

@Test
public void testFailsOnIncompleteChunkedResponse() {
    HttpClientCodec codec = new HttpClientCodec(4096, 8192, 8192, true);
    EmbeddedChannel ch = new EmbeddedChannel(codec);
    ch.writeOutbound(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http://localhost/"));
    ByteBuf buffer = ch.readOutbound();
    assertNotNull(buffer);
    buffer.release();
    assertNull(ch.readInbound());
    ch.writeInbound(Unpooled.copiedBuffer(INCOMPLETE_CHUNKED_RESPONSE, CharsetUtil.ISO_8859_1));
    assertThat(ch.readInbound(), instanceOf(HttpResponse.class));
    // Chunk 'first'
    ((HttpContent) ch.readInbound()).release();
    // Chunk 'second'
    ((HttpContent) ch.readInbound()).release();
    assertNull(ch.readInbound());
    try {
        ch.finish();
        fail();
    } catch (CodecException e) {
        assertTrue(e instanceof PrematureChannelClosureException);
    }
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) CodecException(io.netty.handler.codec.CodecException) PrematureChannelClosureException(io.netty.handler.codec.PrematureChannelClosureException) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 99 with EmbeddedChannel

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

the class HttpClientCodecTest method testFailsNotOnRequestResponse.

@Test
public void testFailsNotOnRequestResponse() {
    HttpClientCodec codec = new HttpClientCodec(4096, 8192, 8192, true);
    EmbeddedChannel ch = new EmbeddedChannel(codec);
    ch.writeOutbound(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http://localhost/"));
    ch.writeInbound(Unpooled.copiedBuffer(RESPONSE, CharsetUtil.ISO_8859_1));
    ch.finish();
    for (; ; ) {
        Object msg = ch.readOutbound();
        if (msg == null) {
            break;
        }
        release(msg);
    }
    for (; ; ) {
        Object msg = ch.readInbound();
        if (msg == null) {
            break;
        }
        release(msg);
    }
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.Test)

Example 100 with EmbeddedChannel

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

the class HttpContentCompressorTest method testFullContent.

@Test
public void testFullContent() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new HttpContentCompressor());
    ch.writeInbound(newRequest());
    FullHttpResponse res = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.copiedBuffer("Hello, World", CharsetUtil.US_ASCII));
    res.headers().set(HttpHeaderNames.CONTENT_LENGTH, res.content().readableBytes());
    ch.writeOutbound(res);
    assertEncodedResponse(ch);
    HttpContent c = ch.readOutbound();
    assertThat(ByteBufUtil.hexDump(c.content()), is("1f8b0800000000000000f248cdc9c9d75108cf2fca4901000000ffff"));
    c.release();
    c = ch.readOutbound();
    assertThat(ByteBufUtil.hexDump(c.content()), is("0300c6865b260c000000"));
    c.release();
    LastHttpContent last = ch.readOutbound();
    assertThat(last.content().readableBytes(), is(0));
    last.release();
    assertThat(ch.readOutbound(), is(nullValue()));
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.Test)

Aggregations

EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)441 Test (org.junit.Test)360 ByteBuf (io.netty.buffer.ByteBuf)160 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 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 IOException (java.io.IOException)7 CountDownLatch (java.util.concurrent.CountDownLatch)6