Skip to content

Commit d697fd1

Browse files
committed
Add a few missing async tests
1 parent f9c8c85 commit d697fd1

1 file changed

Lines changed: 107 additions & 0 deletions

File tree

  • Source/HttpMultipartParser.UnitTests/ParserScenarios

Source/HttpMultipartParser.UnitTests/ParserScenarios/TinyData.cs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.IO;
33
using System.Text;
44
using System.Text.RegularExpressions;
5+
using System.Threading.Tasks;
56
using Xunit;
67

78
namespace HttpMultipartParser.UnitTests.ParserScenarios
@@ -55,6 +56,19 @@ public void CanAutoDetectBoundary()
5556
}
5657
}
5758

59+
/// <summary>
60+
/// Tests for correct detection of the boundary in the input stream.
61+
/// </summary>
62+
[Fact]
63+
public async Task CanAutoDetectBoundaryAsync()
64+
{
65+
using (Stream stream = TestUtil.StringToStream(_testCase.Request, Encoding.UTF8))
66+
{
67+
var parser = await MultipartFormDataParser.ParseAsync(stream).ConfigureAwait(false);
68+
Assert.True(_testCase.Validate(parser));
69+
}
70+
}
71+
5872
/// <summary>
5973
/// Ensures that boundary detection works even when the boundary spans
6074
/// two different buffers.
@@ -69,6 +83,20 @@ public void CanDetectBoundariesCrossBuffer()
6983
}
7084
}
7185

86+
/// <summary>
87+
/// Ensures that boundary detection works even when the boundary spans
88+
/// two different buffers.
89+
/// </summary>
90+
[Fact]
91+
public async Task CanDetectBoundariesCrossBufferAsync()
92+
{
93+
using (Stream stream = TestUtil.StringToStream(_testCase.Request, Encoding.UTF8))
94+
{
95+
var parser = await MultipartFormDataParser.ParseAsync(stream, "boundry", Encoding.UTF8, 16).ConfigureAwait(false);
96+
Assert.True(_testCase.Validate(parser));
97+
}
98+
}
99+
72100
/// <summary>
73101
/// Ensure that mixed newline formats are correctly handled.
74102
/// </summary>
@@ -85,6 +113,22 @@ public void CorrectlyHandleMixedNewlineFormats()
85113
}
86114
}
87115

116+
/// <summary>
117+
/// Ensure that mixed newline formats are correctly handled.
118+
/// </summary>
119+
[Fact]
120+
public async Task CorrectlyHandleMixedNewlineFormatsAsync()
121+
{
122+
// Replace the first '\n' with '\r\n'
123+
var regex = new Regex(Regex.Escape("\n"));
124+
string request = regex.Replace(_testCase.Request, "\r\n", 1);
125+
using (Stream stream = TestUtil.StringToStream(request, Encoding.UTF8))
126+
{
127+
var parser = await MultipartFormDataParser.ParseAsync(stream, "boundry", Encoding.UTF8).ConfigureAwait(false);
128+
Assert.True(_testCase.Validate(parser));
129+
}
130+
}
131+
88132
/// <summary>
89133
/// Tests for correct handling of <c>crlf (\r\n)</c> in the input stream.
90134
/// </summary>
@@ -99,6 +143,20 @@ public void CorrectlyHandlesCRLF()
99143
}
100144
}
101145

146+
/// <summary>
147+
/// Tests for correct handling of <c>crlf (\r\n)</c> in the input stream.
148+
/// </summary>
149+
[Fact]
150+
public async Task CorrectlyHandlesCRLFAsync()
151+
{
152+
string request = _testCase.Request.Replace("\n", "\r\n");
153+
using (Stream stream = TestUtil.StringToStream(request, Encoding.UTF8))
154+
{
155+
var parser = await MultipartFormDataParser.ParseAsync(stream, "boundry", Encoding.UTF8).ConfigureAwait(false);
156+
Assert.True(_testCase.Validate(parser));
157+
}
158+
}
159+
102160
/// <summary>
103161
/// The tiny data test.
104162
/// </summary>
@@ -112,6 +170,19 @@ public void TinyDataTest()
112170
}
113171
}
114172

173+
/// <summary>
174+
/// The tiny data test.
175+
/// </summary>
176+
[Fact]
177+
public async Task TinyDataTestAsync()
178+
{
179+
using (Stream stream = TestUtil.StringToStream(_testCase.Request, Encoding.UTF8))
180+
{
181+
var parser = await MultipartFormDataParser.ParseAsync(stream, "boundry", Encoding.UTF8).ConfigureAwait(false);
182+
Assert.True(_testCase.Validate(parser));
183+
}
184+
}
185+
115186
[Fact]
116187
public void DoesNotCloseTheStream()
117188
{
@@ -125,6 +196,19 @@ public void DoesNotCloseTheStream()
125196
}
126197
}
127198

199+
[Fact]
200+
public async Task DoesNotCloseTheStreamAsync()
201+
{
202+
using (Stream stream = TestUtil.StringToStream(_testCase.Request, Encoding.UTF8))
203+
{
204+
var parser = await MultipartFormDataParser.ParseAsync(stream, "boundry", Encoding.UTF8).ConfigureAwait(false);
205+
Assert.True(_testCase.Validate(parser));
206+
207+
stream.Position = 0;
208+
Assert.True(true, "A closed stream would throw ObjectDisposedException");
209+
}
210+
}
211+
128212
[Fact]
129213
public void GetParameterValueReturnsNullIfNoParameterFound()
130214
{
@@ -135,6 +219,16 @@ public void GetParameterValueReturnsNullIfNoParameterFound()
135219
}
136220
}
137221

222+
[Fact]
223+
public async Task GetParameterValueReturnsNullIfNoParameterFoundAsync()
224+
{
225+
using (Stream stream = TestUtil.StringToStream(_testCase.Request, Encoding.UTF8))
226+
{
227+
var parser = await MultipartFormDataParser.ParseAsync(stream, "boundry", Encoding.UTF8).ConfigureAwait(false);
228+
Assert.Null(parser.GetParameterValue("does not exist"));
229+
}
230+
}
231+
138232
[Fact]
139233
public void CanDetectBoundriesWithNewLineInNextBuffer()
140234
{
@@ -147,5 +241,18 @@ public void CanDetectBoundriesWithNewLineInNextBuffer()
147241
}
148242
}
149243
}
244+
245+
[Fact]
246+
public async Task CanDetectBoundriesWithNewLineInNextBufferAsync()
247+
{
248+
for (int i = 16; i < _testCase.Request.Length; i++)
249+
{
250+
using (Stream stream = TestUtil.StringToStream(_testCase.Request, Encoding.UTF8))
251+
{
252+
var parser = await MultipartFormDataParser.ParseAsync(stream, "boundry", Encoding.UTF8, i).ConfigureAwait(false);
253+
Assert.True(_testCase.Validate(parser), $"Failure in buffer length {i}");
254+
}
255+
}
256+
}
150257
}
151258
}

0 commit comments

Comments
 (0)