Skip to content

Commit 62406d8

Browse files
committed
Unit tests for mjpeg stream
1 parent 06ef571 commit 62406d8

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 MjpegStream
10+
{
11+
private static readonly string _testData = TestUtil.TrimAllLines(
12+
@"--MOBOTIX_Fast_Serverpush
13+
Content-Type: image/jpeg
14+
15+
<jpeg bytes>
16+
--MOBOTIX_Fast_Serverpush
17+
Content-Type: image/jpeg
18+
19+
<jpeg bytes>
20+
--MOBOTIX_Fast_Serverpush
21+
Content-Type: image/jpeg
22+
23+
... and so on ...
24+
--MOBOTIX_Fast_Serverpush--"
25+
);
26+
27+
/// <summary>
28+
/// Test case for mjpeg stream.
29+
/// </summary>
30+
private static readonly TestData _testCase = new TestData(
31+
_testData,
32+
new List<ParameterPart> { },
33+
new List<FilePart> {
34+
new FilePart(null, null, TestUtil.StringToStreamNoBom("<jpeg bytes>"), "image/jpeg", "form-data"),
35+
new FilePart(null, null, TestUtil.StringToStreamNoBom("<jpeg bytes>"), "image/jpeg", "form-data"),
36+
new FilePart(null, null, TestUtil.StringToStreamNoBom("... and so on ..."), "image/jpeg", "form-data")
37+
}
38+
);
39+
40+
/// <summary>
41+
/// Initializes the test data before each run, this primarily
42+
/// consists of resetting data stream positions.
43+
/// </summary>
44+
public MjpegStream()
45+
{
46+
foreach (var filePart in _testCase.ExpectedFileData)
47+
{
48+
filePart.Data.Position = 0;
49+
}
50+
}
51+
52+
[Fact]
53+
public void MjpegStreamTest()
54+
{
55+
using (Stream stream = TestUtil.StringToStream(_testCase.Request, Encoding.UTF8))
56+
{
57+
var parser = MultipartFormDataParser.Parse(stream, "MOBOTIX_Fast_Serverpush", Encoding.UTF8, 32);
58+
Assert.True(_testCase.Validate(parser));
59+
}
60+
}
61+
62+
[Fact]
63+
public async Task MjpegStreamTest_Async()
64+
{
65+
using (Stream stream = TestUtil.StringToStream(_testCase.Request, Encoding.UTF8))
66+
{
67+
var parser = await MultipartFormDataParser.ParseAsync(stream, "MOBOTIX_Fast_Serverpush", Encoding.UTF8, 32).ConfigureAwait(false);
68+
Assert.True(_testCase.Validate(parser));
69+
}
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)