public void TestTransferCodingGZIP() { string requestStr = "POST / HTTP/1.1\r\n" + "Content-Length: " + GzHelloWorld.Length + "\r\n" + "Transfer-Encoding: gzip\r\n" + "\r\n"; HttpRequestDecoder decoder = new HttpRequestDecoder(); HttpContentDecoder decompressor = new HttpContentDecompressor(); EmbeddedChannel channel = new EmbeddedChannel(decoder, decompressor); channel.WriteInbound(Unpooled.CopiedBuffer(Encoding.ASCII.GetBytes(requestStr))); channel.WriteInbound(Unpooled.CopiedBuffer(GzHelloWorld)); IHttpRequest request = channel.ReadInbound <IHttpRequest>(); Assert.True(request.Result.IsSuccess); Assert.False(request.Headers.Contains(HttpHeaderNames.ContentLength)); IHttpContent content = channel.ReadInbound <IHttpContent>(); Assert.True(content.Result.IsSuccess); Assert.Equal(HelloWorld, content.Content.ToString(Encoding.ASCII)); content.Release(); ILastHttpContent lastHttpContent = channel.ReadInbound <ILastHttpContent>(); Assert.True(lastHttpContent.Result.IsSuccess); lastHttpContent.Release(); AssertHasInboundMessages(channel, false); AssertHasOutboundMessages(channel, false); Assert.False(channel.Finish()); channel.ReleaseInbound(); }
public void TestTransferCodingGZIPAndChunked() { string requestStr = "POST / HTTP/1.1\r\n" + "Host: example.com\r\n" + "Content-Type: application/x-www-form-urlencoded\r\n" + "Trailer: My-Trailer\r\n" + "Transfer-Encoding: gzip, chunked\r\n" + "\r\n"; HttpRequestDecoder decoder = new HttpRequestDecoder(); HttpContentDecoder decompressor = new HttpContentDecompressor(); EmbeddedChannel channel = new EmbeddedChannel(decoder, decompressor); Assert.True(channel.WriteInbound(Unpooled.CopiedBuffer(requestStr, Encoding.ASCII))); 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", Encoding.ASCII))); IHttpRequest request = channel.ReadInbound <IHttpRequest>(); Assert.True(request.Result.IsSuccess); Assert.True(request.Headers.ContainsValue(HttpHeaderNames.TransferEncoding, HttpHeaderValues.Chunked, true)); Assert.False(request.Headers.Contains(HttpHeaderNames.ContentLength)); IHttpContent chunk1 = channel.ReadInbound <IHttpContent>(); Assert.True(chunk1.Result.IsSuccess); Assert.Equal(HelloWorld, chunk1.Content.ToString(Encoding.ASCII)); chunk1.Release(); ILastHttpContent chunk2 = channel.ReadInbound <ILastHttpContent>(); Assert.True(chunk2.Result.IsSuccess); Assert.Equal("42", chunk2.TrailingHeaders.Get(AsciiString.Of("My-Trailer"), null)); chunk2.Release(); Assert.False(channel.Finish()); channel.ReleaseInbound(); }
public void TestDecodeEndDataAsClient() { EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(false)); IByteBuffer hello = Unpooled.CopiedBuffer("hello world", Encoding.UTF8); Assert.True(ch.WriteInbound(new DefaultHttp2DataFrame(hello, true))); ILastHttpContent content = ch.ReadInbound <ILastHttpContent>(); try { Assert.Equal("hello world", content.Content.ToString(Encoding.UTF8)); Assert.True(content.TrailingHeaders.IsEmpty); } finally { content.Release(); } Assert.Null(ch.ReadInbound <object>()); Assert.False(ch.Finish()); }
public void TestDecodeResponseTrailersAsClient() { EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(false)); IHttp2Headers headers = new DefaultHttp2Headers(); headers.Set((AsciiString)"key", (AsciiString)"value"); Assert.True(ch.WriteInbound(new DefaultHttp2HeadersFrame(headers, true))); ILastHttpContent trailers = ch.ReadInbound <ILastHttpContent>(); try { Assert.Equal(0, trailers.Content.ReadableBytes); Assert.Equal("value", trailers.TrailingHeaders.Get((AsciiString)"key", null)); Assert.False(trailers is IFullHttpRequest); } finally { trailers.Release(); } Assert.Null(ch.ReadInbound <object>()); Assert.False(ch.Finish()); }
public async Task ExecutorPreserveOrdering() { var sb = new ServerBootstrap(); sb.Group(new DefaultEventLoopGroup(1), new DefaultEventLoopGroup()); sb.Channel <LocalServerChannel>(); sb.ChildHandler(new ActionChannelInitializer <IChannel>(ch => { ch.Pipeline .AddLast(new HttpServerCodec()) .AddLast(new HttpObjectAggregator(1024)) .AddLast(/*compressorGroup,*/ new HttpContentCompressor()) .AddLast(new ChannelOutboundHandlerAdapter0()) .AddLast(new ChannelOutboundHandlerAdapter1()); })); var responses = new BlockingCollection <IHttpObject>(); var bs = new Bootstrap(); bs.Group(new DefaultEventLoopGroup()); bs.Channel <LocalChannel>(); bs.Handler(new ActionChannelInitializer <IChannel>(ch => { ch.Pipeline .AddLast(new HttpClientCodec()) .AddLast(new ChannelInboundHandlerAdapter0(responses)); })); IChannel serverChannel = null; IChannel clientChannel = null; try { serverChannel = await sb.BindAsync(new LocalAddress(Guid.NewGuid().ToString("N"))); clientChannel = await bs.ConnectAsync(serverChannel.LocalAddress); await clientChannel.WriteAndFlushAsync(NewRequest()); var result = responses.TryTake(out var item, TimeSpan.FromSeconds(1)); Assert.True(result); AssertEncodedResponse((IHttpResponse)item); result = responses.TryTake(out item, TimeSpan.FromSeconds(1)); Assert.True(result); IHttpContent c = (IHttpContent)item; Assert.NotNull(c); Assert.Equal($"1f8b08000000000000{Platform}f248cdc9c9d75108cf2fca4901000000ffff", ByteBufferUtil.HexDump(c.Content)); c.Release(); result = responses.TryTake(out item, TimeSpan.FromSeconds(1)); Assert.True(result); c = (IHttpContent)item; Assert.NotNull(c); Assert.Equal("0300c6865b260c000000", ByteBufferUtil.HexDump(c.Content)); c.Release(); result = responses.TryTake(out item, TimeSpan.FromSeconds(1)); Assert.True(result); ILastHttpContent last = (ILastHttpContent)item; Assert.NotNull(last); Assert.Equal(0, last.Content.ReadableBytes); last.Release(); Assert.False(responses.TryTake(out _, TimeSpan.FromSeconds(1))); } finally { if (clientChannel != null) { await clientChannel.CloseAsync(); } if (serverChannel != null) { await serverChannel.CloseAsync(); } await Task.WhenAll( sb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), sb.ChildGroup().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), bs.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5))); } }