Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit d0c1b6a

Browse files
hughbestephentoub
authored andcommitted
Add tests for a HttpListenerRequest.ContentLength64 porting bug
1 parent 8bcf1ce commit d0c1b6a

File tree

1 file changed

+71
-13
lines changed

1 file changed

+71
-13
lines changed

src/System.Net.HttpListener/tests/HttpListenerRequestTests.cs

Lines changed: 71 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,23 +94,75 @@ public async Task ContentEncoding_NoBody_ReturnsDefault()
9494
}
9595

9696
[ConditionalTheory(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotOneCoreUAP))]
97-
[InlineData("POST", "Content-Length: 9223372036854775807", 9223372036854775807)] // long.MaxValue
98-
[InlineData("POST", "Content-Length: 9223372036854775808", 0)] // long.MaxValue + 1
99-
[InlineData("POST", "Content-Length: 18446744073709551615 ", 0)] // ulong.MaxValue
100-
[InlineData("POST", "Content-Length: 0", 0)]
101-
[InlineData("PUT", "Content-Length: 0", 0)]
102-
[InlineData("PUT", "Content-Length: 1", 1)]
103-
[InlineData("PUT", "Content-Length: 1\nContent-Length: 1", 1)]
104-
[InlineData("POST", "Transfer-Encoding: chunked", -1)]
105-
[InlineData("PUT", "Transfer-Encoding: chunked", -1)]
106-
[InlineData("PUT", "Transfer-Encoding: chunked", -1)]
107-
[InlineData("PUT", "Content-Length: 10\nTransfer-Encoding: chunked", -1)]
108-
[InlineData("PUT", "Transfer-Encoding: chunked\nContent-Length: 10", -1)]
109-
public async Task ContentLength_GetProperty_ReturnsExpected(string method, string contentLengthString, long expected)
97+
[InlineData("POST", "Content-Length: 9223372036854775807", 9223372036854775807, true)] // long.MaxValue
98+
[InlineData("POST", "Content-Length: 9223372036854775808", 0, false)] // long.MaxValue + 1
99+
[InlineData("POST", "Content-Length: 18446744073709551615 ", 0, false)] // ulong.MaxValue
100+
[InlineData("POST", "Content-Length: 0", 0, false)]
101+
[InlineData("PUT", "Content-Length: 0", 0, false)]
102+
[InlineData("PUT", "Content-Length: 1", 1, true)]
103+
[InlineData("PUT", "Content-Length: 1\nContent-Length: 1", 1, true)]
104+
[InlineData("POST", "Transfer-Encoding: chunked", -1, true)]
105+
[InlineData("PUT", "Transfer-Encoding: chunked", -1, true)]
106+
[InlineData("PUT", "Transfer-Encoding: chunked", -1, true)]
107+
[InlineData("PUT", "Content-Length: 10\nTransfer-Encoding: chunked", -1, true)]
108+
[InlineData("PUT", "Transfer-Encoding: chunked\nContent-Length: 10", -1, true)]
109+
public async Task ContentLength_GetProperty_ReturnsExpected(string method, string contentLengthString, long expected, bool hasEntityBody)
110110
{
111111
await GetRequest(method, "", contentLengthString.Split('\n'), (_, request) =>
112112
{
113113
Assert.Equal(expected, request.ContentLength64);
114+
Assert.Equal(hasEntityBody, request.HasEntityBody);
115+
}, content: "\r\n");
116+
}
117+
118+
[ConditionalTheory(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotOneCoreUAP))]
119+
[InlineData(100)]
120+
[InlineData("-100")]
121+
[InlineData("")]
122+
[InlineData("abc")]
123+
[InlineData("9223372036854775808")]
124+
[ActiveIssue(20294, TargetFrameworkMonikers.Netcoreapp)]
125+
public async Task ContentLength_ManuallySetInHeaders_ReturnsExpected(string newValue)
126+
{
127+
await GetRequest("POST", null, new string[] { "Content-Length: 1" }, (_, request) =>
128+
{
129+
Assert.Equal("1", request.Headers["Content-Length"]);
130+
131+
request.Headers.Set("Content-Length", newValue);
132+
Assert.Equal(newValue, request.Headers["Content-Length"]);
133+
Assert.Equal(1, request.ContentLength64);
134+
135+
Assert.True(request.HasEntityBody);
136+
}, content: "\r\n");
137+
}
138+
139+
[ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotOneCoreUAP))]
140+
[ActiveIssue(20294, TargetFrameworkMonikers.Netcoreapp)]
141+
public async Task ContentLength_ManuallyRemovedFromHeaders_DoesNotAffect()
142+
{
143+
await GetRequest("POST", null, new string[] { "Content-Length: 1" }, (_, request) =>
144+
{
145+
Assert.Equal("1", request.Headers["Content-Length"]);
146+
147+
request.Headers.Remove("Content-Length");
148+
Assert.Equal(1, request.ContentLength64);
149+
150+
Assert.True(request.HasEntityBody);
151+
}, content: "\r\n");
152+
}
153+
154+
[ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotOneCoreUAP))]
155+
public async Task ContentLength_SetInHeadersAfterAccessingProperty_DoesNothing()
156+
{
157+
await GetRequest("POST", null, new string[] { "Content-Length: 1" }, (_, request) =>
158+
{
159+
Assert.Equal("1", request.Headers["Content-Length"]);
160+
Assert.Equal(1, request.ContentLength64);
161+
162+
request.Headers.Set("Content-Length", "1000");
163+
Assert.Equal(1, request.ContentLength64);
164+
165+
Assert.True(request.HasEntityBody);
114166
}, content: "\r\n");
115167
}
116168

@@ -164,10 +216,16 @@ public async Task EndPointProperties_GetProperty_ReturnsExpected()
164216

165217
HttpListenerRequest request = context.Request;
166218
Assert.Equal(client.RemoteEndPoint.ToString(), request.UserHostAddress);
219+
167220
Assert.Equal(client.RemoteEndPoint, request.LocalEndPoint);
221+
Assert.Same(request.LocalEndPoint, request.LocalEndPoint);
222+
168223
Assert.Equal(client.LocalEndPoint, request.RemoteEndPoint);
224+
Assert.Same(request.RemoteEndPoint, request.RemoteEndPoint);
169225

170226
Assert.Equal(factory.ListeningUrl, request.Url.ToString());
227+
Assert.Same(request.Url, request.Url);
228+
171229
Assert.Equal($"/{factory.Path}/", request.RawUrl);
172230
}
173231
}

0 commit comments

Comments
 (0)