Skip to content

Commit 8fa4974

Browse files
committed
Unit test for invalid part
1 parent 5d40e00 commit 8fa4974

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System.IO;
2+
using System.Linq;
3+
using System.Text;
4+
using System.Threading.Tasks;
5+
using Xunit;
6+
7+
namespace HttpMultipartParser.UnitTests.ParserScenarios
8+
{
9+
public class InvalidPart
10+
{
11+
// For details see: https://github.com/Http-Multipart-Data-Parser/Http-Multipart-Data-Parser/issues/110
12+
// This data is considered invalid because it contains nothing but empty lines
13+
private static readonly string _testData = @"--KoZIhvcNAQcB
14+
15+
--KoZIhvcNAQcB--";
16+
17+
private static readonly TestData _testCase = new TestData(
18+
_testData,
19+
Enumerable.Empty<ParameterPart>().ToList(),
20+
Enumerable.Empty<FilePart>().ToList()
21+
);
22+
23+
public InvalidPart()
24+
{
25+
foreach (var filePart in _testCase.ExpectedFileData)
26+
{
27+
filePart.Data.Position = 0;
28+
}
29+
}
30+
31+
[Fact]
32+
public void Exception_is_thrown_when_attempting_to_parse()
33+
{
34+
// The default behavior is to throw an exception when the form contains an invalid section.
35+
using (Stream stream = TestUtil.StringToStream(_testCase.Request, Encoding.UTF8))
36+
{
37+
Assert.Throws<MultipartParseException>(() => MultipartFormDataParser.Parse(stream));
38+
}
39+
}
40+
41+
[Fact]
42+
public async Task Exception_is_thrown_when_attempting_to_parse_async()
43+
{
44+
// The default behavior is to throw an exception when the form contains an invalid section.
45+
using (Stream stream = TestUtil.StringToStream(_testCase.Request, Encoding.UTF8))
46+
{
47+
await Assert.ThrowsAsync<MultipartParseException>(() => MultipartFormDataParser.ParseAsync(stream)).ConfigureAwait(false);
48+
}
49+
}
50+
51+
[Fact]
52+
public void Invalid_part_is_ignored()
53+
{
54+
using (Stream stream = TestUtil.StringToStream(_testCase.Request, Encoding.UTF8))
55+
{
56+
var parser = MultipartFormDataParser.Parse(stream, ignoreInvalidParts: true);
57+
Assert.Equal(0, parser.Files.Count);
58+
Assert.Equal(0, parser.Parameters.Count);
59+
}
60+
}
61+
62+
[Fact]
63+
public async Task Invalid_part_is_ignored_async()
64+
{
65+
using (Stream stream = TestUtil.StringToStream(_testCase.Request, Encoding.UTF8))
66+
{
67+
var parser = await MultipartFormDataParser.ParseAsync(stream, ignoreInvalidParts: true).ConfigureAwait(false);
68+
Assert.Equal(0, parser.Files.Count);
69+
Assert.Equal(0, parser.Parameters.Count);
70+
}
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)