Beispiel #1
0
        public void KeepAlive(bool isKeepAliveResponseExpected, HttpVersion httpVersion, HttpResponseStatus responseStatus, string sendKeepAlive, int setSelfDefinedMessageLength, ICharSequence setResponseConnection)
        {
            var channel = new EmbeddedChannel(new HttpServerKeepAliveHandler());
            var request = new DefaultFullHttpRequest(httpVersion, HttpMethod.Get, "/v1/foo/bar");

            HttpUtil.SetKeepAlive(request, RequestKeepAlive.Equals(sendKeepAlive));
            var response = new DefaultFullHttpResponse(httpVersion, responseStatus);

            if (!CharUtil.IsNullOrEmpty(setResponseConnection))
            {
                response.Headers.Set(HttpHeaderNames.Connection, setResponseConnection);
            }
            SetupMessageLength(setSelfDefinedMessageLength, response);

            Assert.True(channel.WriteInbound(request));
            var requestForwarded = channel.ReadInbound <object>();

            Assert.Equal(request, requestForwarded);
            ReferenceCountUtil.Release(requestForwarded);
            channel.WriteAndFlushAsync(response).Wait(TimeSpan.FromSeconds(1));
            var writtenResponse = channel.ReadOutbound <IHttpResponse>();

            Assert.Equal(isKeepAliveResponseExpected, channel.Open);
            Assert.Equal(isKeepAliveResponseExpected, HttpUtil.IsKeepAlive(writtenResponse));
            ReferenceCountUtil.Release(writtenResponse);
            Assert.False(channel.FinishAndReleaseAll());
        }
Beispiel #2
0
        public void PipelineKeepAlive(bool isKeepAliveResponseExpected, HttpVersion httpVersion, HttpResponseStatus responseStatus, string sendKeepAlive, int setSelfDefinedMessageLength, ICharSequence setResponseConnection)
        {
            var channel      = new EmbeddedChannel(new HttpServerKeepAliveHandler());
            var firstRequest = new DefaultFullHttpRequest(httpVersion, HttpMethod.Get, "/v1/foo/bar");

            HttpUtil.SetKeepAlive(firstRequest, true);
            var secondRequest = new DefaultFullHttpRequest(httpVersion, HttpMethod.Get, "/v1/foo/bar");

            HttpUtil.SetKeepAlive(secondRequest, RequestKeepAlive.Equals(sendKeepAlive));
            var finalRequest = new DefaultFullHttpRequest(httpVersion, HttpMethod.Get, "/v1/foo/bar");

            HttpUtil.SetKeepAlive(finalRequest, false);
            var response          = new DefaultFullHttpResponse(httpVersion, responseStatus);
            var informationalResp = new DefaultFullHttpResponse(httpVersion, Processing);

            HttpUtil.SetKeepAlive(response, true);
            HttpUtil.SetContentLength(response, 0);
            HttpUtil.SetKeepAlive(informationalResp, true);

            Assert.True(channel.WriteInbound(firstRequest, secondRequest, finalRequest));

            var requestForwarded = channel.ReadInbound <object>();

            Assert.Equal(firstRequest, requestForwarded);
            ReferenceCountUtil.Release(requestForwarded);

            channel.WriteAndFlushAsync(response.Duplicate().Retain()).Wait(TimeSpan.FromSeconds(1));
            var firstResponse = channel.ReadOutbound <IHttpResponse>();

            Assert.True(channel.Open);
            Assert.True(HttpUtil.IsKeepAlive(firstResponse));
            ReferenceCountUtil.Release(firstResponse);

            requestForwarded = channel.ReadInbound <object>();
            Assert.Equal(secondRequest, requestForwarded);
            ReferenceCountUtil.Release(requestForwarded);

            channel.WriteAndFlushAsync(informationalResp).Wait(TimeSpan.FromSeconds(1));
            var writtenInfoResp = channel.ReadOutbound <IHttpResponse>();

            Assert.True(channel.Open);
            Assert.True(HttpUtil.IsKeepAlive(writtenInfoResp));
            ReferenceCountUtil.Release(writtenInfoResp);

            if (!CharUtil.IsNullOrEmpty(setResponseConnection))
            {
                response.Headers.Set(HttpHeaderNames.Connection, setResponseConnection);
            }
            else
            {
                response.Headers.Remove(HttpHeaderNames.Connection);
            }
            SetupMessageLength(setSelfDefinedMessageLength, response);
            channel.WriteAndFlushAsync(response.Duplicate().Retain()).Wait(TimeSpan.FromSeconds(1));
            var secondResponse = channel.ReadOutbound <IHttpResponse>();

            Assert.Equal(isKeepAliveResponseExpected, channel.Open);
            Assert.Equal(isKeepAliveResponseExpected, HttpUtil.IsKeepAlive(secondResponse));
            ReferenceCountUtil.Release(secondResponse);

            requestForwarded = channel.ReadInbound <object>();
            Assert.Equal(finalRequest, requestForwarded);
            ReferenceCountUtil.Release(requestForwarded);

            if (isKeepAliveResponseExpected)
            {
                channel.WriteAndFlushAsync(response).Wait(TimeSpan.FromSeconds(1));
                var finalResponse = channel.ReadOutbound <IHttpResponse>();
                Assert.False(channel.Open);
                Assert.False(HttpUtil.IsKeepAlive(finalResponse));
            }
            ReferenceCountUtil.Release(response);
            Assert.False(channel.FinishAndReleaseAll());
        }