Skip to content

Commit a9a86f2

Browse files
committed
Merge branch 'feature/break_up_parse_unit_tests' into develop
Resolves #69
2 parents 333243f + d697fd1 commit a9a86f2

13 files changed

Lines changed: 1144 additions & 688 deletions

Source/HttpMultipartParser.UnitTests/HttpMultipartFormParserUnitTests.cs

Lines changed: 48 additions & 688 deletions
Large diffs are not rendered by default.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Text;
4+
using System.Threading.Tasks;
5+
using Xunit;
6+
7+
namespace HttpMultipartParser.UnitTests.ParserScenarios
8+
{
9+
/// <summary>
10+
/// Tests that the final '--' ending up in a seperate chunk doesn't break everything.
11+
/// </summary>
12+
public class ExactBufferTruncate
13+
{
14+
private static readonly string _testData = TestUtil.TrimAllLines(@"--boundry
15+
Content-Disposition: form-data; name=""text""
16+
17+
textdata
18+
--boundry
19+
Content-Disposition: form-data; name=""file""; filename=""data.txt""
20+
Content-Type: text/plain
21+
22+
1234567890123456789012
23+
--boundry--");
24+
25+
/// <summary>
26+
/// This test has the buffer split such that the final '--' of the end boundary
27+
/// falls into the next buffer.
28+
/// </summary>
29+
private static readonly TestData _testCase = new TestData(
30+
_testData,
31+
new List<ParameterPart> {
32+
new ParameterPart("text", "textdata")
33+
},
34+
new List<FilePart> {
35+
new FilePart( "file", "data.txt", TestUtil.StringToStreamNoBom("1234567890123456789012"))
36+
}
37+
);
38+
39+
public ExactBufferTruncate()
40+
{
41+
foreach (var filePart in _testCase.ExpectedFileData)
42+
{
43+
filePart.Data.Position = 0;
44+
}
45+
}
46+
47+
48+
/// <summary>
49+
/// Tests that the final '--' ending up in a seperate chunk doesn't break everything.
50+
/// </summary>
51+
[Fact]
52+
public void CanHandleFinalDashesInSeperateBufferFromEndBinary()
53+
{
54+
using (Stream stream = TestUtil.StringToStream(_testCase.Request, Encoding.UTF8))
55+
{
56+
var parser = MultipartFormDataParser.Parse(stream, "boundry", Encoding.UTF8, 16);
57+
Assert.True(_testCase.Validate(parser));
58+
}
59+
}
60+
61+
62+
/// <summary>
63+
/// Tests that the final '--' ending up in a seperate chunk doesn't break everything.
64+
/// </summary>
65+
[Fact]
66+
public async Task CanHandleFinalDashesInSeperateBufferFromEndBinaryAsync()
67+
{
68+
using (Stream stream = TestUtil.StringToStream(_testCase.Request, Encoding.UTF8))
69+
{
70+
var parser = await MultipartFormDataParser.ParseAsync(stream, "boundry", Encoding.UTF8, 16).ConfigureAwait(false);
71+
Assert.True(_testCase.Validate(parser));
72+
}
73+
}
74+
}
75+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Text;
4+
using System.Threading.Tasks;
5+
using Xunit;
6+
7+
namespace HttpMultipartParser.UnitTests.ParserScenarios
8+
{
9+
/// <summary>
10+
/// Test for cases where the file is last with expected outcomes.
11+
/// </summary>
12+
public class FileIsLast
13+
{
14+
private static readonly string _testData = TestUtil.TrimAllLines(
15+
@"-----------------------------41952539122868
16+
Content-Disposition: form-data; name=""adID""
17+
18+
1425
19+
-----------------------------41952539122868
20+
Content-Disposition: form-data; name=""files[]""; filename=""Capture.JPG""
21+
Content-Type: image/jpeg
22+
23+
BinaryData
24+
-----------------------------41952539122868--");
25+
26+
private static readonly TestData _testCase = new TestData(
27+
_testData,
28+
new List<ParameterPart> {
29+
new ParameterPart("adID", "1425")
30+
},
31+
new List<FilePart> {
32+
new FilePart("files[]", "Capture.JPG", TestUtil.StringToStreamNoBom("BinaryData"), "image/jpeg", "form-data")
33+
}
34+
);
35+
36+
public FileIsLast()
37+
{
38+
foreach (var filePart in _testCase.ExpectedFileData)
39+
{
40+
filePart.Data.Position = 0;
41+
}
42+
}
43+
44+
[Fact]
45+
public void CanHandleFileAsLastSection()
46+
{
47+
using (Stream stream = TestUtil.StringToStream(_testCase.Request, Encoding.UTF8))
48+
{
49+
var parser = MultipartFormDataParser.Parse(stream, Encoding.UTF8);
50+
Assert.True(_testCase.Validate(parser));
51+
}
52+
}
53+
54+
[Fact]
55+
public async Task CanHandleFileAsLastSectionAsync()
56+
{
57+
using (Stream stream = TestUtil.StringToStream(_testCase.Request, Encoding.UTF8))
58+
{
59+
var parser = await MultipartFormDataParser.ParseAsync(stream, Encoding.UTF8).ConfigureAwait(false);
60+
Assert.True(_testCase.Validate(parser));
61+
}
62+
}
63+
}
64+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Text;
4+
using System.Threading.Tasks;
5+
using Xunit;
6+
7+
namespace HttpMultipartParser.UnitTests.ParserScenarios
8+
{
9+
public class FullPathAsFileName
10+
{
11+
private static readonly string _testData = TestUtil.TrimAllLines(
12+
@"-----------------------------7de6cc440a46
13+
Content-Disposition: form-data; name=""file""; filename=""C:\test\test;abc.txt""
14+
Content-Type: text/plain
15+
16+
test
17+
-----------------------------7de6cc440a46--"
18+
);
19+
20+
private static readonly TestData _testCase = new TestData(
21+
_testData,
22+
new List<ParameterPart>(),
23+
new List<FilePart> {
24+
new FilePart("file", "test;abc.txt", TestUtil.StringToStream("test"), "text/plain", "form-data")
25+
}
26+
);
27+
28+
public FullPathAsFileName()
29+
{
30+
foreach (var filePart in _testCase.ExpectedFileData)
31+
{
32+
filePart.Data.Position = 0;
33+
}
34+
}
35+
36+
[Fact]
37+
public void HandlesFullPathAsFileNameWithSemicolonCorrectly()
38+
{
39+
using (Stream stream = TestUtil.StringToStream(_testCase.Request, Encoding.UTF8))
40+
{
41+
var parser = MultipartFormDataParser.Parse(stream, Encoding.UTF8);
42+
Assert.True(_testCase.Validate(parser));
43+
}
44+
}
45+
46+
[Fact]
47+
public async Task HandlesFullPathAsFileNameWithSemicolonCorrectlyAsync()
48+
{
49+
using (Stream stream = TestUtil.StringToStream(_testCase.Request, Encoding.UTF8))
50+
{
51+
var parser = await MultipartFormDataParser.ParseAsync(stream, Encoding.UTF8).ConfigureAwait(false);
52+
Assert.True(_testCase.Validate(parser));
53+
}
54+
}
55+
}
56+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Text;
4+
using System.Threading.Tasks;
5+
using Xunit;
6+
7+
namespace HttpMultipartParser.UnitTests.ParserScenarios
8+
{
9+
public class MixedSingleByteAndMultiByteWidth
10+
{
11+
private static readonly string _testData = TestUtil.TrimAllLines(
12+
@"-----------------------------41952539122868
13+
Content-Disposition: form-data; name=""تت""
14+
15+
1425
16+
-----------------------------41952539122868
17+
Content-Disposition: form-data; name=""files[]""; filename=""تست.jpg""
18+
Content-Type: image/jpeg
19+
20+
BinaryData
21+
-----------------------------41952539122868--"
22+
);
23+
24+
private static readonly TestData _testCase = new TestData(
25+
_testData,
26+
new List<ParameterPart> {
27+
new ParameterPart("تت", "1425")
28+
},
29+
new List<FilePart> {
30+
new FilePart("files[]", "تست.jpg", TestUtil.StringToStreamNoBom("BinaryData"), "image/jpeg", "form-data")
31+
}
32+
);
33+
34+
public MixedSingleByteAndMultiByteWidth()
35+
{
36+
foreach (var filePart in _testCase.ExpectedFileData)
37+
{
38+
filePart.Data.Position = 0;
39+
}
40+
}
41+
42+
[Fact]
43+
public void CanHandleMixedSingleByteAndMultiByteWidthCharacters()
44+
{
45+
using (
46+
Stream stream = TestUtil.StringToStream(_testCase.Request, Encoding.UTF8)
47+
)
48+
{
49+
var parser = MultipartFormDataParser.Parse(stream, Encoding.UTF8);
50+
Assert.True(_testCase.Validate(parser));
51+
}
52+
}
53+
54+
[Fact]
55+
public async Task CanHandleMixedSingleByteAndMultiByteWidthCharactersAsync()
56+
{
57+
using (
58+
Stream stream = TestUtil.StringToStream(_testCase.Request, Encoding.UTF8)
59+
)
60+
{
61+
var parser = await MultipartFormDataParser.ParseAsync(stream, Encoding.UTF8).ConfigureAwait(false);
62+
Assert.True(_testCase.Validate(parser));
63+
}
64+
}
65+
}
66+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Text;
4+
using System.Threading.Tasks;
5+
using Xunit;
6+
7+
namespace HttpMultipartParser.UnitTests.ParserScenarios
8+
{
9+
public class MixedUnicodeWidthAndAsciiWidthCharacters
10+
{
11+
private static readonly string _testData = TestUtil.TrimAllLines(
12+
@"--boundary_.oOo._MjQ1NTU=OTk3Ng==MjcxODE=
13+
Content-Disposition: form-data; name=""psAdTitle""
14+
15+
Bonjour poignée
16+
--boundary_.oOo._MjQ1NTU=OTk3Ng==MjcxODE=--"
17+
);
18+
19+
private static readonly TestData _testCase = new TestData(
20+
_testData,
21+
new List<ParameterPart> {
22+
new ParameterPart("psAdTitle", "Bonjour poignée")
23+
},
24+
new List<FilePart>()
25+
);
26+
27+
public MixedUnicodeWidthAndAsciiWidthCharacters()
28+
{
29+
foreach (var filePart in _testCase.ExpectedFileData)
30+
{
31+
filePart.Data.Position = 0;
32+
}
33+
}
34+
35+
[Fact]
36+
public void CanHandleUnicodeWidthAndAsciiWidthCharacters()
37+
{
38+
using (
39+
Stream stream = TestUtil.StringToStream(_testCase.Request, Encoding.UTF8))
40+
{
41+
var parser = MultipartFormDataParser.Parse(stream, Encoding.UTF8);
42+
Assert.True(_testCase.Validate(parser));
43+
}
44+
}
45+
46+
[Fact]
47+
public async Task CanHandleUnicodeWidthAndAsciiWidthCharactersAsync()
48+
{
49+
using (
50+
Stream stream = TestUtil.StringToStream(_testCase.Request, Encoding.UTF8))
51+
{
52+
var parser = await MultipartFormDataParser.ParseAsync(stream, Encoding.UTF8).ConfigureAwait(false);
53+
Assert.True(_testCase.Validate(parser));
54+
}
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)