|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.IO; |
| 3 | +using System.Text; |
| 4 | +using Xunit; |
| 5 | + |
| 6 | +namespace HttpMultipartParser.UnitTests.ParserScenarios |
| 7 | +{ |
| 8 | + // This test attempts to reproduce the bug discussed here: |
| 9 | + // https://github.com/Http-Multipart-Data-Parser/Http-Multipart-Data-Parser/issues/64 |
| 10 | + public class FileChunkStartsWithBOM |
| 11 | + { |
| 12 | + private static readonly int _binaryBufferSize = 100; |
| 13 | + private static readonly byte[] _utf8BOMBinary = new byte[] { 0xef, 0xbb, 0xbf }; |
| 14 | + private static readonly string _utf8BOMString = Encoding.UTF8.GetString(_utf8BOMBinary); |
| 15 | + |
| 16 | + private static readonly string _prefix = |
| 17 | + @"--boundary |
| 18 | +Content-Type: application/octet-stream |
| 19 | +
|
| 20 | +"; |
| 21 | + // This padding ensures that the BOM is positioned at the begining of the second chunk |
| 22 | + private static readonly string _padding = new string('+', _binaryBufferSize - _prefix.Length); |
| 23 | + private static readonly string _fileContent = $"{_padding}{_utf8BOMString}Hello world"; |
| 24 | + |
| 25 | + private static readonly string _testDataFormat = |
| 26 | + @"{0}{1} |
| 27 | +--boundary--"; |
| 28 | + private static readonly string _testData = TestUtil.TrimAllLines(string.Format(_testDataFormat, _prefix, _fileContent)); |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Test case for files with additional parameter. |
| 32 | + /// </summary> |
| 33 | + private static readonly TestData _testCase = new TestData( |
| 34 | + _testData, |
| 35 | + new List<ParameterPart> { }, |
| 36 | + new List<FilePart> { |
| 37 | + new FilePart(null, null, TestUtil.StringToStreamNoBom(_fileContent), null, "application/octet-stream", "form-data") |
| 38 | + } |
| 39 | + ); |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Initializes the test data before each run, this primarily |
| 43 | + /// consists of resetting data stream positions. |
| 44 | + /// </summary> |
| 45 | + public FileChunkStartsWithBOM() |
| 46 | + { |
| 47 | + foreach (var filePart in _testCase.ExpectedFileData) |
| 48 | + { |
| 49 | + filePart.Data.Position = 0; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + [Fact] |
| 54 | + public void CanHandleBOM() |
| 55 | + { |
| 56 | + var encoding = Encoding.UTF8; |
| 57 | + |
| 58 | + using (Stream stream = TestUtil.StringToStream(_testCase.Request, encoding)) |
| 59 | + { |
| 60 | + var parser = MultipartFormDataParser.Parse(stream, encoding, _binaryBufferSize); |
| 61 | + |
| 62 | + using (var file = parser.Files[0].Data) |
| 63 | + { |
| 64 | + file.Position = 0; |
| 65 | + |
| 66 | + // Read the padding and assert we get the expected value |
| 67 | + var paddingBuffer = new byte[_padding.Length]; |
| 68 | + var readCount = file.Read(paddingBuffer, 0, paddingBuffer.Length); |
| 69 | + |
| 70 | + Assert.Equal(_padding, encoding.GetString(paddingBuffer)); |
| 71 | + |
| 72 | + // Read the BOM and assert we get the expected value |
| 73 | + var bomBuffer = new byte[_utf8BOMBinary.Length]; |
| 74 | + readCount = file.Read(bomBuffer, 0, bomBuffer.Length); |
| 75 | + |
| 76 | + // If this assertion fails, it means that we have reproduced the problem described in GH-64 |
| 77 | + // If it succeeds, it means that the bug has been fixed. |
| 78 | + Assert.Equal(_utf8BOMString, encoding.GetString(bomBuffer)); |
| 79 | + |
| 80 | + // Read the rest of the content and assert we get the expected value |
| 81 | + var restOfContentBuffer = new byte[_fileContent.Length - _padding.Length - _utf8BOMString.Length]; |
| 82 | + readCount = file.Read(restOfContentBuffer, 0, restOfContentBuffer.Length); |
| 83 | + |
| 84 | + Assert.Equal("Hello world", encoding.GetString(restOfContentBuffer)); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments