|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.IO; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace HttpMultipartParser.UnitTests.ParserScenarios |
| 8 | +{ |
| 9 | + // This test attempts to reproduce the bug discussed here: |
| 10 | + // https://github.com/Http-Multipart-Data-Parser/Http-Multipart-Data-Parser/issues/40 |
| 11 | + public class FileLineSpansAccrossTwoChunks |
| 12 | + { |
| 13 | + private static readonly string part1 = "--boundary\r\nContent-Disposition: form-data; name=\"param1\"\r\n\r\nFirst value\r\n"; |
| 14 | + private static readonly string part2 = "--boundary\r\nContent-Disposition: form-data; name=\"param2\"\r\n\r\nSecond value\r\n--boundary--"; |
| 15 | + |
| 16 | + // Buffer size is calculated to split the '\r' and '\n' after "First value" |
| 17 | + private static readonly int _binaryBufferSize = part1.Length - 1; |
| 18 | + private static readonly string _testData = TestUtil.TrimAllLines($"{part1}{part2}"); |
| 19 | + |
| 20 | + private static readonly TestData _testCase = new TestData( |
| 21 | + _testData, |
| 22 | + new List<ParameterPart> |
| 23 | + { |
| 24 | + new ParameterPart("param1", "First value"), |
| 25 | + new ParameterPart("param2", "Second value"), |
| 26 | + }, |
| 27 | + Enumerable.Empty<FilePart>().ToList() |
| 28 | + ); |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Initializes the test data before each run, this primarily |
| 32 | + /// consists of resetting data stream positions. |
| 33 | + /// </summary> |
| 34 | + public FileLineSpansAccrossTwoChunks() |
| 35 | + { |
| 36 | + foreach (var filePart in _testCase.ExpectedFileData) |
| 37 | + { |
| 38 | + filePart.Data.Position = 0; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + [Fact] |
| 43 | + public void CanHandleNewLineAccrossTwoChunks() |
| 44 | + { |
| 45 | + var options = new ParserOptions |
| 46 | + { |
| 47 | + Boundary = "boundary", |
| 48 | + BinaryBufferSize = _binaryBufferSize, |
| 49 | + Encoding = Encoding.UTF8 |
| 50 | + }; |
| 51 | + |
| 52 | + using (Stream stream = TestUtil.StringToStream(_testCase.Request, options.Encoding)) |
| 53 | + { |
| 54 | + var parser = MultipartFormDataParser.Parse(stream, options); |
| 55 | + Assert.True(_testCase.Validate(parser)); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | +} |
0 commit comments