|
| 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 BlankLinesBeforeFirstBoundary |
| 10 | + { |
| 11 | + private static readonly string _testData = TestUtil.TrimAllLines( |
| 12 | + @"--boundary |
| 13 | + Content-Disposition: form-data; name=""text"" |
| 14 | +
|
| 15 | + textdata |
| 16 | + --boundary--" |
| 17 | + ); |
| 18 | + |
| 19 | + // This test case has a few blank lines before the first boundary marker |
| 20 | + // This unusual scenario is described in GH-116 |
| 21 | + // https://github.com/Http-Multipart-Data-Parser/Http-Multipart-Data-Parser/issues/116 |
| 22 | + private static readonly TestData _testCase = new TestData( |
| 23 | + $"\n\n\n{_testData}", // Intentionally add a few blank lines before the data. These blank lines should be ignored by the parser when attempting to detect the boundary marker |
| 24 | + new List<ParameterPart> { |
| 25 | + new ParameterPart("text", "textdata"), |
| 26 | + }, |
| 27 | + new List<FilePart>() |
| 28 | + ); |
| 29 | + |
| 30 | + public BlankLinesBeforeFirstBoundary() |
| 31 | + { |
| 32 | + foreach (var filePart in _testCase.ExpectedFileData) |
| 33 | + { |
| 34 | + filePart.Data.Position = 0; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Tests for correct detection of the boundary in the input stream. |
| 40 | + /// </summary> |
| 41 | + [Fact] |
| 42 | + public void CanAutoDetectBoundary() |
| 43 | + { |
| 44 | + using (Stream stream = TestUtil.StringToStream(_testCase.Request, Encoding.UTF8)) |
| 45 | + { |
| 46 | + var parser = MultipartFormDataParser.Parse(stream); |
| 47 | + Assert.True(_testCase.Validate(parser)); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Tests for correct detection of the boundary in the input stream. |
| 53 | + /// </summary> |
| 54 | + [Fact] |
| 55 | + public async Task CanAutoDetectBoundaryAsync() |
| 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 | +} |
0 commit comments