This repository was archived by the owner on Mar 14, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathHttpServerKeepAliveHandlerTest.cs
More file actions
180 lines (160 loc) · 10 KB
/
HttpServerKeepAliveHandlerTest.cs
File metadata and controls
180 lines (160 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace DotNetty.Codecs.Http.Tests
{
using System;
using System.Collections.Generic;
using DotNetty.Common.Utilities;
using DotNetty.Transport.Channels.Embedded;
using Xunit;
using static HttpResponseStatus;
public sealed class HttpServerKeepAliveHandlerTest
{
const string RequestKeepAlive = "REQUEST_KEEP_ALIVE";
const int NotSelfDefinedMsgLength = 0;
const int SetResponseLength = 1;
const int SetMultipart = 2;
const int SetChunked = 4;
public static IEnumerable<object[]> GetKeepAliveCases() => new[]
{
new object[] { true, HttpVersion.Http10, OK, RequestKeepAlive, SetResponseLength, HttpHeaderValues.KeepAlive }, // 0
new object[] { true, HttpVersion.Http10, OK, RequestKeepAlive, SetMultipart, HttpHeaderValues.KeepAlive }, // 1
new object[] { false, HttpVersion.Http10, OK, null, SetResponseLength, null }, // 2
new object[] { true, HttpVersion.Http11, OK, RequestKeepAlive, SetResponseLength, null }, // 3
new object[] { false, HttpVersion.Http11, OK, RequestKeepAlive, SetResponseLength, HttpHeaderValues.Close }, // 4
new object[] { true, HttpVersion.Http11, OK, RequestKeepAlive, SetMultipart, null }, // 5
new object[] { true, HttpVersion.Http11, OK, RequestKeepAlive, SetChunked, null }, // 6
new object[] { false, HttpVersion.Http11, OK, null, SetResponseLength, null }, // 7
new object[] { false, HttpVersion.Http10, OK, RequestKeepAlive, NotSelfDefinedMsgLength, null }, // 8
new object[] { false, HttpVersion.Http10, OK, null, NotSelfDefinedMsgLength, null }, // 9
new object[] { false, HttpVersion.Http11, OK, RequestKeepAlive, NotSelfDefinedMsgLength, null }, // 10
new object[] { false, HttpVersion.Http11, OK, null, NotSelfDefinedMsgLength, null }, // 11
new object[] { false, HttpVersion.Http10, OK, RequestKeepAlive, SetResponseLength, null }, // 12
new object[] { true, HttpVersion.Http11, NoContent, RequestKeepAlive, NotSelfDefinedMsgLength, null}, // 13
new object[] { false, HttpVersion.Http10, NoContent, null, NotSelfDefinedMsgLength, null} // 14
};
[Theory]
[MemberData(nameof(GetKeepAliveCases), DisableDiscoveryEnumeration = true)]
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();
Assert.Equal(request, requestForwarded);
ReferenceCountUtil.Release(requestForwarded);
channel.WriteAndFlushAsync(response).Wait(TimeSpan.FromSeconds(1));
var writtenResponse = channel.ReadOutbound<IHttpResponse>();
Assert.Equal(isKeepAliveResponseExpected, channel.IsOpen);
Assert.Equal(isKeepAliveResponseExpected, HttpUtil.IsKeepAlive(writtenResponse));
ReferenceCountUtil.Release(writtenResponse);
Assert.False(channel.FinishAndReleaseAll());
}
[Theory]
[MemberData(nameof(GetKeepAliveCases), DisableDiscoveryEnumeration = true)]
#pragma warning disable xUnit1026 // Theory methods should use all of their parameters
public void ConnectionCloseHeaderHandledCorrectly(bool isKeepAliveResponseExpected, HttpVersion httpVersion, HttpResponseStatus responseStatus, string sendKeepAlive, int setSelfDefinedMessageLength, ICharSequence setResponseConnection)
#pragma warning restore xUnit1026 // Theory methods should use all of their parameters
{
var channel = new EmbeddedChannel(new HttpServerKeepAliveHandler());
var response = new DefaultFullHttpResponse(httpVersion, responseStatus);
response.Headers.Set(HttpHeaderNames.Connection, HttpHeaderValues.Close);
SetupMessageLength(setSelfDefinedMessageLength, response);
channel.WriteAndFlushAsync(response).Wait(TimeSpan.FromSeconds(1));
var writtenResponse = channel.ReadOutbound<IHttpResponse>();
Assert.False(channel.IsOpen);
ReferenceCountUtil.Release(writtenResponse);
Assert.False(channel.FinishAndReleaseAll());
}
[Theory]
[MemberData(nameof(GetKeepAliveCases), DisableDiscoveryEnumeration = true)]
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();
Assert.Equal(firstRequest, requestForwarded);
ReferenceCountUtil.Release(requestForwarded);
channel.WriteAndFlushAsync(response.Duplicate().Retain()).Wait(TimeSpan.FromSeconds(1));
var firstResponse = channel.ReadOutbound<IHttpResponse>();
Assert.True(channel.IsOpen);
Assert.True(HttpUtil.IsKeepAlive(firstResponse));
ReferenceCountUtil.Release(firstResponse);
requestForwarded = channel.ReadInbound();
Assert.Equal(secondRequest, requestForwarded);
ReferenceCountUtil.Release(requestForwarded);
channel.WriteAndFlushAsync(informationalResp).Wait(TimeSpan.FromSeconds(1));
var writtenInfoResp = channel.ReadOutbound<IHttpResponse>();
Assert.True(channel.IsOpen);
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.IsOpen);
Assert.Equal(isKeepAliveResponseExpected, HttpUtil.IsKeepAlive(secondResponse));
ReferenceCountUtil.Release(secondResponse);
requestForwarded = channel.ReadInbound();
Assert.Equal(finalRequest, requestForwarded);
ReferenceCountUtil.Release(requestForwarded);
if (isKeepAliveResponseExpected)
{
channel.WriteAndFlushAsync(response).Wait(TimeSpan.FromSeconds(1));
var finalResponse = channel.ReadOutbound<IHttpResponse>();
Assert.False(channel.IsOpen);
Assert.False(HttpUtil.IsKeepAlive(finalResponse));
}
ReferenceCountUtil.Release(response);
Assert.False(channel.FinishAndReleaseAll());
}
static void SetupMessageLength(int setSelfDefinedMessageLength, IHttpResponse response)
{
switch (setSelfDefinedMessageLength)
{
case NotSelfDefinedMsgLength:
if (HttpUtil.IsContentLengthSet(response))
{
response.Headers.Remove(HttpHeaderNames.ContentLength);
}
break;
case SetResponseLength:
HttpUtil.SetContentLength(response, 0);
break;
case SetChunked:
HttpUtil.SetTransferEncodingChunked(response, true);
break;
case SetMultipart:
response.Headers.Set(HttpHeaderNames.ContentType, HttpHeaderValues.MultipartMixed);
break;
default:
throw new ArgumentException($"Unknown selfDefinedMessageLength: {setSelfDefinedMessageLength}");
}
}
}
}