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 }
public void ExpectContinueResponse3() { // request with header "Expect: 100-continue" must be replied with one "100 Continue" response // case 3: ContentDecoder is in chain and content is encoded var decoder = new HttpRequestDecoder(); var decompressor = new HttpContentDecompressor(); var aggregator = new HttpObjectAggregator(1024); var channel = new EmbeddedChannel(decoder, decompressor, aggregator); string req = "POST / HTTP/1.1\r\n" + "Content-Length: " + GzHelloWorld.Length + "\r\n" + "Expect: 100-continue\r\n" + "Content-Encoding: gzip\r\n" + "\r\n"; Assert.False(channel.WriteInbound(Unpooled.WrappedBuffer(Encoding.ASCII.GetBytes(req)))); var resp = channel.ReadOutbound <IFullHttpResponse>(); Assert.Equal(100, resp.Status.Code); resp.Release(); Assert.True(channel.WriteInbound(Unpooled.CopiedBuffer(GzHelloWorld))); AssertHasInboundMessages(channel, true); AssertHasOutboundMessages(channel, false); Assert.False(channel.Finish()); }
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()); }
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()); Queue <object> 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()); }
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)); Queue <object> 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()); }
public void RequestContentLength1() { // 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 HttpRequestDecoder(4096, 4096, 5); var decompressor = new HttpContentDecompressor(); var channel = new EmbeddedChannel(decoder, decompressor); string headers = "POST / HTTP/1.1\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)); Queue <object> req = channel.InboundMessages; Assert.True(req.Count >= 1); object o = req.Peek(); Assert.IsAssignableFrom <IHttpRequest>(o); var request = (IHttpRequest)o; if (request.Headers.TryGet(HttpHeaderNames.ContentLength, out ICharSequence v)) { Assert.Equal(HelloWorld.Length, long.Parse(v.ToString())); } AssertHasInboundMessages(channel, true); AssertHasOutboundMessages(channel, false); Assert.False(channel.Finish()); }
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 FullHttpRequest() { // test that ContentDecoder can be used after the ObjectAggregator var decoder = new HttpRequestDecoder(4096, 4096, 5); var aggregator = new HttpObjectAggregator(1024); var decompressor = new HttpContentDecompressor(); var channel = new EmbeddedChannel(decoder, aggregator, decompressor); string headers = "POST / HTTP/1.1\r\n" + "Content-Length: " + GzHelloWorld.Length + "\r\n" + "Content-Encoding: gzip\r\n" + "\r\n"; Assert.True(channel.WriteInbound(Unpooled.CopiedBuffer(Encoding.ASCII.GetBytes(headers), GzHelloWorld))); var req = channel.InboundMessages; Assert.True(req.Count > 1); int contentLength = 0; contentLength = CalculateContentLength(req, contentLength); byte[] receivedContent = ReadContent(req, contentLength, true); Assert.Equal(HelloWorld, Encoding.ASCII.GetString(receivedContent)); AssertHasInboundMessages(channel, true); AssertHasOutboundMessages(channel, false); Assert.False(channel.Finish()); }
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()); }
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(); }