Skip to content

Commit 9ad5bb6

Browse files
committed
(GH-123) Unit test to demonstrate bug
1 parent ddf2338 commit 9ad5bb6

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Xunit;
7+
8+
namespace HttpMultipartParser.UnitTests.ParserScenarios
9+
{
10+
public class BoundaryEndsWithDoubleDash
11+
{
12+
// The boundary in this scenario ends with '--'. This is an unusual scenario but perfectly legitimate.
13+
// For details, see: https://github.com/Http-Multipart-Data-Parser/Http-Multipart-Data-Parser/issues/123
14+
private static readonly string _testData = TestUtil.TrimAllLines(
15+
@"--boundary_text--
16+
Content-type: text/plain; charset=UTF-8
17+
Content-Disposition: form-data; name=""file1""; filename=""file1.txt""
18+
19+
file content here
20+
--boundary_text--
21+
Content-type: text/plain; charset=UTF-8
22+
Content-Disposition: form-data; name=""file2""; filename=""file2.txt""
23+
24+
file content here 2
25+
--boundary_text----"
26+
);
27+
28+
private static readonly TestData _testCase = new TestData(
29+
_testData,
30+
Enumerable.Empty<ParameterPart>().ToList(),
31+
new List<FilePart>() {
32+
new FilePart("file1", "file1.txt", TestUtil.StringToStreamNoBom("file content here")),
33+
new FilePart("file2", "file2.txt", TestUtil.StringToStreamNoBom("file content here 2")),
34+
}
35+
);
36+
37+
public BoundaryEndsWithDoubleDash()
38+
{
39+
foreach (var filePart in _testCase.ExpectedFileData)
40+
{
41+
filePart.Data.Position = 0;
42+
}
43+
}
44+
45+
/// <summary>
46+
/// Tests for correct detection of the boundary in the input stream.
47+
/// </summary>
48+
[Fact]
49+
public void CanAutoDetectBoundary()
50+
{
51+
using (Stream stream = TestUtil.StringToStream(_testCase.Request, Encoding.UTF8))
52+
{
53+
var parser = MultipartFormDataParser.Parse(stream);
54+
Assert.True(_testCase.Validate(parser));
55+
}
56+
}
57+
58+
/// <summary>
59+
/// Tests for correct detection of the boundary in the input stream.
60+
/// </summary>
61+
[Fact]
62+
public async Task CanAutoDetectBoundaryAsync()
63+
{
64+
using (Stream stream = TestUtil.StringToStream(_testCase.Request, Encoding.UTF8))
65+
{
66+
var parser = await MultipartFormDataParser.ParseAsync(stream, Encoding.UTF8);
67+
Assert.True(_testCase.Validate(parser));
68+
}
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)