public void ShouldHandlePreambleWithStartBoundary() { Encoding encoding = Encoding.UTF8; const string boundary = "ABC123abc456"; const string thisPreamble = "This is an invalid message containing only a preamble."; const string contents = thisPreamble + @" --" + boundary; byte[] binaryContent = encoding.GetBytes(contents); MemoryStream bodyStream = new MemoryStream(binaryContent); StreamingMultiPartParser parser = new StreamingMultiPartParser(bodyStream, encoding, boundary); parser.PreambleFound += (o, e) => Assert.AreEqual(thisPreamble, toString(encoding, e)); parser.SectionFound += (o, e) => Assert.Fail("No sections should be returned because the message ends prematurely."); parser.EpilogueFound += (o, e) => Assert.AreEqual(String.Empty, toString(encoding, e)); parser.Parse().Wait(); }
public void ShouldParseMultiPartFile() { Encoding encoding = Encoding.UTF8; byte[] binaryContent = encoding.GetBytes(getMultiPartContents()); MemoryStream bodyStream = new MemoryStream(binaryContent); List<MultiPartSection> sections = new List<MultiPartSection>(); StreamingMultiPartParser parser = new StreamingMultiPartParser(bodyStream, encoding, boundary); string parsedPreamble = null; string parsedEpilogue = null; parser.PreambleFound += (o, e) => parsedPreamble = toString(encoding, e); parser.SectionFound += (o, e) => sections.Add(e); parser.EpilogueFound += (o, e) => parsedEpilogue = toString(encoding, e); parser.Parse().Wait(); Assert.AreEqual(preamble, parsedPreamble, "The preample was not read correctly."); Assert.AreEqual(2, sections.Count, "Two sections should have been parsed from the body."); var section1 = sections[0]; Assert.AreEqual(formDataHeaderValue1, section1.Headers[formDataHeaderName1], "The first header was not parsed."); string parsedFormData = toString(encoding, section1.Content); Assert.AreEqual(formData, parsedFormData, "The form data was not read correctly."); var section2 = sections[1]; Assert.AreEqual(formDataHeaderValue2, section2.Headers[formDataHeaderName2], "The second header was not parsed."); Assert.AreEqual(formDataHeaderValue3, section2.Headers[formDataHeaderName3], "The third header was not parsed."); string parsedFileData = toString(encoding, section2.Content); Assert.AreEqual(fileData, parsedFileData, "The file data was not read correctly."); Assert.AreEqual(epilogue, parsedEpilogue, "The epilogue was not parsed."); }
public void ShouldHandlePreambleOnly() { Encoding encoding = Encoding.UTF8; const string preamble = "This is an invalid message containing only a preamble."; byte[] binaryContent = encoding.GetBytes(preamble); MemoryStream bodyStream = new MemoryStream(binaryContent); StreamingMultiPartParser parser = new StreamingMultiPartParser(bodyStream, encoding, "AaB03x"); parser.PreambleFound += (o, e) => Assert.AreEqual(preamble, toString(encoding, e)); parser.SectionFound += (o, e) => Assert.Fail("No sections should be found if the contents are empty."); parser.EpilogueFound += (o, e) => Assert.AreEqual(String.Empty, toString(encoding, e)); parser.Parse().Wait(); }
public void ShouldHandlePartialSection() { Encoding encoding = Encoding.UTF8; const string contents = "--" + boundary + @" " + formDataHeaderName1 + ": " + formDataHeaderValue1 + @" "; byte[] binaryContent = encoding.GetBytes(contents); MemoryStream bodyStream = new MemoryStream(binaryContent); StreamingMultiPartParser parser = new StreamingMultiPartParser(bodyStream, encoding, boundary); parser.PreambleFound += (o, e) => Assert.AreEqual(String.Empty, toString(encoding, e)); parser.SectionFound += (o, e) => Assert.Fail("No sections should be returned if the content is just the end boundary."); parser.EpilogueFound += (o, e) => Assert.AreEqual(String.Empty, toString(encoding, e)); parser.Parse().Wait(); }
public void ShouldHandleMultiPartMixedContent() { Encoding encoding = Encoding.UTF8; byte[] binaryContent = encoding.GetBytes(getMultiPartMixedContents()); MemoryStream bodyStream = new MemoryStream(binaryContent); List<MultiPartSection> sections = new List<MultiPartSection>(); StreamingMultiPartParser parser = new StreamingMultiPartParser(bodyStream, encoding, "AaB03x"); parser.PreambleFound += (o, e) => Assert.AreEqual(String.Empty, toString(encoding, e)); parser.SectionFound += (o, e) => sections.Add(e); parser.EpilogueFound += (o, e) => Assert.AreEqual(String.Empty, toString(encoding, e)); parser.Parse().Wait(); Assert.AreEqual(3, sections.Count, "The wrong number of sections were found."); Assert.AreEqual("Larry", toString(encoding, sections[0].Content), "The first section content was wrong."); Assert.AreEqual("... contents of file1.txt ...", toString(encoding, sections[1].Content), "The second section content was wrong."); Assert.AreEqual("...contents of file2.gif...", toString(encoding, sections[2].Content), "The third section content was wrong."); }
public void ShouldHandleEndBoundaryWithEpilogue() { Encoding encoding = Encoding.UTF8; const string boundary = "ABC123abc456"; const string epilogue = "This is an epilogue. It comes after the end-boundary."; const string contents = "--" + boundary + @"-- " + epilogue; byte[] binaryContent = encoding.GetBytes(contents); MemoryStream bodyStream = new MemoryStream(binaryContent); StreamingMultiPartParser parser = new StreamingMultiPartParser(bodyStream, encoding, boundary); parser.PreambleFound += (o, e) => Assert.AreEqual(String.Empty, toString(encoding, e)); parser.SectionFound += (o, e) => Assert.Fail("No sections should be returned if the content is just the end boundary."); parser.EpilogueFound += (o, e) => Assert.AreEqual(epilogue, toString(encoding, e)); parser.Parse().Wait(); }
public void ShouldHandleEmptyContent() { Encoding encoding = Encoding.UTF8; byte[] binaryContent = encoding.GetBytes(String.Empty); MemoryStream bodyStream = new MemoryStream(binaryContent); StreamingMultiPartParser parser = new StreamingMultiPartParser(bodyStream, encoding, "AaB03x"); parser.PreambleFound += (o, e) => Assert.AreEqual(String.Empty, toString(encoding, e)); parser.SectionFound += (o, e) => Assert.Fail("No sections should be found if the contents are empty."); parser.EpilogueFound += (o, e) => Assert.AreEqual(String.Empty, toString(encoding, e)); parser.Parse().Wait(); }