private void ReaderOperationWithMime(byte[] mime, string part1ExpectedStr, string part2ExpectedStr, int recommendedChunkSize) { Encoding utf8 = Sharpen.Extensions.GetEncoding("UTF-8"); // if the caller passes in a special chunksize, which is not equal to mime.length, then // lets test the algorithm _only_ at that chunksize. otherwise, test it at every chunksize // between 1 and mime.length. (this is needed because when testing with a very large mime value, // the test takes too long to test at every single chunk size) int chunkSize = 1; if (recommendedChunkSize != mime.Length) { chunkSize = recommendedChunkSize; } for (; chunkSize <= recommendedChunkSize; ++chunkSize) { ByteArrayInputStream mimeInputStream = new ByteArrayInputStream(mime); MultipartReaderTest.TestMultipartReaderDelegate delegate_ = new MultipartReaderTest.TestMultipartReaderDelegate (this); string contentType = "multipart/related; boundary=\"BOUNDARY\""; MultipartReader reader = new MultipartReader(contentType, delegate_); NUnit.Framework.Assert.IsFalse(reader.Finished()); int location = 0; int length = 0; do { NUnit.Framework.Assert.IsTrue("Parser didn't stop at end", location < mime.Length ); length = Math.Min(chunkSize, (mime.Length - location)); byte[] bytesRead = new byte[length]; mimeInputStream.Read(bytesRead, 0, length); reader.AppendData(bytesRead); location += chunkSize; }while (!reader.Finished()); NUnit.Framework.Assert.AreEqual(delegate_.partList.Count, 2); NUnit.Framework.Assert.AreEqual(delegate_.headersList.Count, 2); byte[] part1Expected = Sharpen.Runtime.GetBytesForString(part1ExpectedStr, utf8); byte[] part2Expected = Sharpen.Runtime.GetBytesForString(part2ExpectedStr, utf8); ByteArrayBuffer part1 = delegate_.partList[0]; ByteArrayBuffer part2 = delegate_.partList[1]; NUnit.Framework.Assert.IsTrue(Arrays.Equals(part1.ToByteArray(), part1Expected)); NUnit.Framework.Assert.IsTrue(Arrays.Equals(part2.ToByteArray(), part2Expected)); IDictionary <string, string> headers1 = delegate_.headersList[0]; NUnit.Framework.Assert.IsTrue(headers1.ContainsKey("Foo")); NUnit.Framework.Assert.AreEqual(headers1.Get("Foo"), "Bar"); NUnit.Framework.Assert.IsTrue(headers1.ContainsKey("Header")); NUnit.Framework.Assert.AreEqual(headers1.Get("Header"), "Val ue"); } }
private void ReaderOperationWithMime(byte[] mime, string part1ExpectedStr, string part2ExpectedStr, int recommendedChunkSize) { Encoding utf8 = Sharpen.Extensions.GetEncoding("UTF-8"); // if the caller passes in a special chunksize, which is not equal to mime.length, then // lets test the algorithm _only_ at that chunksize. otherwise, test it at every chunksize // between 1 and mime.length. (this is needed because when testing with a very large mime value, // the test takes too long to test at every single chunk size) int chunkSize = 1; if (recommendedChunkSize != mime.Length) { chunkSize = recommendedChunkSize; } for (; chunkSize <= recommendedChunkSize; ++chunkSize) { ByteArrayInputStream mimeInputStream = new ByteArrayInputStream(mime); MultipartReaderTest.TestMultipartReaderDelegate delegate_ = new MultipartReaderTest.TestMultipartReaderDelegate (this); string contentType = "multipart/related; boundary=\"BOUNDARY\""; MultipartReader reader = new MultipartReader(contentType, delegate_); NUnit.Framework.Assert.IsFalse(reader.Finished()); int location = 0; int length = 0; do { NUnit.Framework.Assert.IsTrue("Parser didn't stop at end", location < mime.Length ); length = Math.Min(chunkSize, (mime.Length - location)); byte[] bytesRead = new byte[length]; mimeInputStream.Read(bytesRead, 0, length); reader.AppendData(bytesRead); location += chunkSize; } while (!reader.Finished()); NUnit.Framework.Assert.AreEqual(delegate_.partList.Count, 2); NUnit.Framework.Assert.AreEqual(delegate_.headersList.Count, 2); byte[] part1Expected = Sharpen.Runtime.GetBytesForString(part1ExpectedStr, utf8); byte[] part2Expected = Sharpen.Runtime.GetBytesForString(part2ExpectedStr, utf8); ByteArrayBuffer part1 = delegate_.partList[0]; ByteArrayBuffer part2 = delegate_.partList[1]; NUnit.Framework.Assert.IsTrue(Arrays.Equals(part1.ToByteArray(), part1Expected)); NUnit.Framework.Assert.IsTrue(Arrays.Equals(part2.ToByteArray(), part2Expected)); IDictionary<string, string> headers1 = delegate_.headersList[0]; NUnit.Framework.Assert.IsTrue(headers1.ContainsKey("Foo")); NUnit.Framework.Assert.AreEqual(headers1.Get("Foo"), "Bar"); NUnit.Framework.Assert.IsTrue(headers1.ContainsKey("Header")); NUnit.Framework.Assert.AreEqual(headers1.Get("Header"), "Val ue"); } }