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

Commit 1760734

Browse files
committed
Fix HttpListener Read_LargeLength tests
1 parent 4f81ce7 commit 1760734

File tree

1 file changed

+24
-34
lines changed

1 file changed

+24
-34
lines changed

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

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -123,37 +123,32 @@ public async Task Read_FullLengthSynchronous_Success(bool transferEncodingChunke
123123
[ConditionalTheory(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotOneCoreUAP))]
124124
[InlineData(true)]
125125
[InlineData(false)]
126-
[ActiveIssue(18128, platforms: TestPlatforms.AnyUnix)] // Different behaviour when not chunked, that needs investigation.
127126
public async Task Read_LargeLengthAsynchronous_Success(bool transferEncodingChunked)
128127
{
129-
string text = new string('a', 128 * 1024 + 1); // More than 128kb
130-
byte[] expected = Encoding.UTF8.GetBytes(text);
128+
var rand = new Random(42);
129+
byte[] expected = Enumerable
130+
.Range(0, 128*1024 + 1) // More than 128kb
131+
.Select(_ => (byte)('a' + rand.Next(0, 26)))
132+
.ToArray();
133+
131134
Task<HttpListenerContext> contextTask = _listener.GetContextAsync();
132135

133136
using (HttpClient client = new HttpClient())
134137
{
135138
client.DefaultRequestHeaders.TransferEncodingChunked = transferEncodingChunked;
136-
Task<HttpResponseMessage> clientTask = client.PostAsync(_factory.ListeningUrl, new StringContent(text));
139+
Task<HttpResponseMessage> clientTask = client.PostAsync(_factory.ListeningUrl, new ByteArrayContent(expected));
137140

138141
HttpListenerContext context = await contextTask;
139142

140143
// If the size is greater than 128K, then we limit the size, and have to do multiple reads on
141144
// Windows, which uses http.sys internally.
142145
byte[] buffer = new byte[expected.Length];
143-
int bytesRead = await context.Request.InputStream.ReadAsync(buffer, 0, buffer.Length);
144-
if (PlatformDetection.IsWindows)
145-
{
146-
Assert.Equal(expected.Length - 1, bytesRead);
147-
Assert.NotEqual(expected, buffer);
148-
149-
bytesRead = await context.Request.InputStream.ReadAsync(buffer, buffer.Length - 1, 1);
150-
Assert.Equal(1, bytesRead);
151-
Assert.Equal(expected, buffer);
152-
}
153-
else
146+
int totalRead = 0;
147+
while (totalRead < expected.Length)
154148
{
155-
Assert.Equal(expected.Length, bytesRead);
156-
Assert.Equal(expected, buffer);
149+
int bytesRead = await context.Request.InputStream.ReadAsync(buffer, totalRead, expected.Length - totalRead);
150+
Assert.InRange(bytesRead, 1, expected.Length - totalRead);
151+
totalRead += bytesRead;
157152
}
158153

159154
// Subsequent reads don't do anything.
@@ -167,37 +162,32 @@ public async Task Read_LargeLengthAsynchronous_Success(bool transferEncodingChun
167162
[ConditionalTheory(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotOneCoreUAP))]
168163
[InlineData(true)]
169164
[InlineData(false)]
170-
[ActiveIssue(18128, platforms: TestPlatforms.AnyUnix)] // Different behaviour when not chunked, that needs investigation.
171165
public async Task Read_LargeLengthSynchronous_Success(bool transferEncodingChunked)
172166
{
173-
string text = new string('a', 128 * 1024 + 1); // More than 128kb
174-
byte[] expected = Encoding.UTF8.GetBytes(text);
167+
var rand = new Random(42);
168+
byte[] expected = Enumerable
169+
.Range(0, 128 * 1024 + 1) // More than 128kb
170+
.Select(_ => (byte)('a' + rand.Next(0, 26)))
171+
.ToArray();
172+
175173
Task<HttpListenerContext> contextTask = _listener.GetContextAsync();
176174

177175
using (HttpClient client = new HttpClient())
178176
{
179177
client.DefaultRequestHeaders.TransferEncodingChunked = transferEncodingChunked;
180-
Task<HttpResponseMessage> clientTask = client.PostAsync(_factory.ListeningUrl, new StringContent(text));
178+
Task<HttpResponseMessage> clientTask = client.PostAsync(_factory.ListeningUrl, new ByteArrayContent(expected));
181179

182180
HttpListenerContext context = await contextTask;
183181

184182
// If the size is greater than 128K, then we limit the size, and have to do multiple reads on
185183
// Windows, which uses http.sys internally.
186184
byte[] buffer = new byte[expected.Length];
187-
int bytesRead = context.Request.InputStream.Read(buffer, 0, buffer.Length);
188-
if (PlatformDetection.IsWindows)
189-
{
190-
Assert.Equal(expected.Length - 1, bytesRead);
191-
Assert.NotEqual(expected, buffer);
192-
193-
bytesRead = context.Request.InputStream.Read(buffer, buffer.Length - 1, 1);
194-
Assert.Equal(1, bytesRead);
195-
Assert.Equal(expected, buffer);
196-
}
197-
else
185+
int totalRead = 0;
186+
while (totalRead < expected.Length)
198187
{
199-
Assert.Equal(expected.Length, bytesRead);
200-
Assert.Equal(expected, buffer);
188+
int bytesRead = context.Request.InputStream.Read(buffer, totalRead, expected.Length - totalRead);
189+
Assert.InRange(bytesRead, 1, expected.Length - totalRead);
190+
totalRead += bytesRead;
201191
}
202192

203193
// Subsequent reads don't do anything.

0 commit comments

Comments
 (0)