Пример #1
0
        public void FullHttpResponseEOF()
        {
            // test that ContentDecoder can be used after the ObjectAggregator
            var    decoder      = new HttpResponseDecoder(4096, 4096, 5);
            var    decompressor = new HttpContentDecompressor();
            var    channel      = new EmbeddedChannel(decoder, decompressor);
            string headers      = "HTTP/1.1 200 OK\r\n" +
                                  "Content-Encoding: gzip\r\n" +
                                  "\r\n";

            Assert.True(channel.WriteInbound(Unpooled.CopiedBuffer(Encoding.ASCII.GetBytes(headers), GzHelloWorld)));
            // This should terminate it.
            Assert.True(channel.Finish());

            var resp = channel.InboundMessages;

            Assert.True(resp.Count > 1);
            int contentLength = 0;

            contentLength = CalculateContentLength(resp, contentLength);
            byte[] receivedContent = ReadContent(resp, contentLength, false);
            Assert.Equal(HelloWorld, Encoding.ASCII.GetString(receivedContent));

            AssertHasInboundMessages(channel, true);
            AssertHasOutboundMessages(channel, false);
            Assert.False(channel.Finish());
        }
Пример #2
0
        public void ResponseContentLength2()
        {
            // case 2: if HttpObjectAggregator is down the chain, then correct Content - Length header must be set

            // force content to be in more than one chunk (5 bytes/chunk)
            var    decoder      = new HttpResponseDecoder(4096, 4096, 5);
            var    decompressor = new HttpContentDecompressor();
            var    aggregator   = new HttpObjectAggregator(1024);
            var    channel      = new EmbeddedChannel(decoder, decompressor, aggregator);
            string headers      = "HTTP/1.1 200 OK\r\n" +
                                  "Content-Length: " + GzHelloWorld.Length + "\r\n" +
                                  "Content-Encoding: gzip\r\n" +
                                  "\r\n";
            IByteBuffer buf = Unpooled.CopiedBuffer(Encoding.ASCII.GetBytes(headers), GzHelloWorld);

            Assert.True(channel.WriteInbound(buf));

            var res = channel.ReadInbound <IFullHttpResponse>();

            Assert.NotNull(res);
            Assert.True(res.Headers.TryGet(HttpHeaderNames.ContentLength, out ICharSequence value));
            Assert.Equal(HelloWorld.Length, long.Parse(value.ToString()));
            res.Release();

            AssertHasInboundMessages(channel, false);
            AssertHasOutboundMessages(channel, false);
            Assert.False(channel.Finish());
        }
Пример #3
0
        public void ResponseContentLength1()
        {
            // case 1: test that ContentDecompressor either sets the correct Content-Length header
            // or removes it completely (handlers down the chain must rely on LastHttpContent object)

            // force content to be in more than one chunk (5 bytes/chunk)
            var    decoder      = new HttpResponseDecoder(4096, 4096, 5);
            var    decompressor = new HttpContentDecompressor();
            var    channel      = new EmbeddedChannel(decoder, decompressor);
            string headers      = "HTTP/1.1 200 OK\r\n" +
                                  "Content-Length: " + GzHelloWorld.Length + "\r\n" +
                                  "Content-Encoding: gzip\r\n" +
                                  "\r\n";
            IByteBuffer buf = Unpooled.CopiedBuffer(Encoding.ASCII.GetBytes(headers), GzHelloWorld);

            Assert.True(channel.WriteInbound(buf));

            var resp = channel.InboundMessages;

            Assert.True(resp.Count >= 1);
            object o = resp.Peek();

            Assert.IsAssignableFrom <IHttpResponse>(o);
            var r = (IHttpResponse)o;

            Assert.False(r.Headers.Contains(HttpHeaderNames.ContentLength));
            Assert.True(r.Headers.TryGet(HttpHeaderNames.TransferEncoding, out ICharSequence transferEncoding));
            Assert.NotNull(transferEncoding);
            Assert.Equal(HttpHeaderValues.Chunked, transferEncoding);

            AssertHasInboundMessages(channel, true);
            AssertHasOutboundMessages(channel, false);
            Assert.False(channel.Finish());
        }
Пример #4
0
        public void ResponseDecompression()
        {
            // baseline test: response decoder, content decompressor && request aggregator work as expected
            var decoder      = new HttpResponseDecoder();
            var decompressor = new HttpContentDecompressor();
            var aggregator   = new HttpObjectAggregator(1024);
            var channel      = new EmbeddedChannel(decoder, decompressor, aggregator);

            string headers = "HTTP/1.1 200 OK\r\n" +
                             "Content-Length: " + GzHelloWorld.Length + "\r\n" +
                             "Content-Encoding: gzip\r\n" +
                             "\r\n";
            IByteBuffer buf = Unpooled.CopiedBuffer(Encoding.ASCII.GetBytes(headers), GzHelloWorld);

            Assert.True(channel.WriteInbound(buf));

            var resp = channel.ReadInbound <IFullHttpResponse>();

            Assert.NotNull(resp);
            Assert.True(resp.Headers.TryGetInt(HttpHeaderNames.ContentLength, out int length));
            Assert.Equal(HelloWorld.Length, length);
            Assert.Equal(HelloWorld, resp.Content.ToString(Encoding.ASCII));
            resp.Release();

            AssertHasInboundMessages(channel, false);
            AssertHasOutboundMessages(channel, false);
            Assert.False(channel.Finish()); // assert that no messages are left in channel
        }
Пример #5
0
        public void TrailerWithEmptyLineInSeparateBuffer()
        {
            HttpResponseDecoder decoder = new HttpResponseDecoder();
            EmbeddedChannel     channel = new EmbeddedChannel(decoder);

            String headers = "HTTP/1.1 200 OK\r\n"
                             + "Transfer-Encoding: chunked\r\n"
                             + "Trailer: My-Trailer\r\n";

            Assert.False(channel.WriteInbound(Unpooled.CopiedBuffer(Encoding.ASCII.GetBytes(headers))));
            Assert.True(channel.WriteInbound(Unpooled.CopiedBuffer(Encoding.ASCII.GetBytes("\r\n"))));

            Assert.True(channel.WriteInbound(Unpooled.CopiedBuffer("0\r\n", Encoding.ASCII)));
            Assert.True(channel.WriteInbound(Unpooled.CopiedBuffer("My-Trailer: 42\r\n", Encoding.ASCII)));
            Assert.True(channel.WriteInbound(Unpooled.CopiedBuffer("\r\n", Encoding.ASCII)));

            var response = channel.ReadInbound <IHttpResponse>();

            Assert.Equal(2, response.Headers.Size);
            Assert.Equal("chunked", response.Headers.Get(HttpHeaderNames.TransferEncoding, null));
            Assert.Equal("My-Trailer", response.Headers.Get(HttpHeaderNames.Trailer, null));

            var lastContent = channel.ReadInbound <ILastHttpContent>();

            Assert.Equal(1, lastContent.TrailingHeaders.Size);
            Assert.Equal("42", lastContent.TrailingHeaders.Get(AsciiString.Of("My-Trailer"), null));
            Assert.Equal(0, lastContent.Content.ReadableBytes);
            lastContent.Release();

            Assert.False(channel.Finish());
        }
Пример #6
0
        public void ChunkedRequestDecompression()
        {
            HttpResponseDecoder decoder      = new HttpResponseDecoder();
            HttpContentDecoder  decompressor = new HttpContentDecompressor();

            EmbeddedChannel channel = new EmbeddedChannel(decoder, decompressor, null);

            string headers = "HTTP/1.1 200 OK\r\n"
                             + "Transfer-Encoding: chunked\r\n"
                             + "Trailer: My-Trailer\r\n"
                             + "Content-Encoding: gzip\r\n\r\n";

            channel.WriteInbound(Unpooled.CopiedBuffer(Encoding.ASCII.GetBytes(headers)));

            string chunkLength = GzHelloWorld.Length.ToString("x2");

            Assert.True(channel.WriteInbound(Unpooled.CopiedBuffer(chunkLength + "\r\n", Encoding.ASCII)));
            Assert.True(channel.WriteInbound(Unpooled.CopiedBuffer(GzHelloWorld)));
            Assert.True(channel.WriteInbound(Unpooled.CopiedBuffer(Encoding.ASCII.GetBytes("\r\n"))));
            Assert.True(channel.WriteInbound(Unpooled.CopiedBuffer("0\r\n", Encoding.ASCII)));
            Assert.True(channel.WriteInbound(Unpooled.CopiedBuffer("My-Trailer: 42\r\n\r\n\r\n", Encoding.ASCII)));

            object ob1 = channel.ReadInbound <object>();

            Assert.True(ob1 is DefaultHttpResponse);

            object ob2 = channel.ReadInbound <object>();

            Assert.True(ob1 is DefaultHttpResponse);
            IHttpContent content = (IHttpContent)ob2;

            Assert.Equal(HelloWorld, content.Content.ToString(Encoding.ASCII));
            content.Release();

            object ob3 = channel.ReadInbound <object>();

            Assert.True(ob1 is DefaultHttpResponse);
            ILastHttpContent lastContent = (ILastHttpContent)ob3;

            Assert.NotNull(lastContent.Result);
            Assert.True(lastContent.Result.IsSuccess);
            Assert.False(lastContent.TrailingHeaders.IsEmpty);
            Assert.Equal("42", lastContent.TrailingHeaders.Get((AsciiString)"My-Trailer", null));
            AssertHasInboundMessages(channel, false);
            AssertHasOutboundMessages(channel, false);
            Assert.False(channel.Finish());
        }