public void ParseMessageWithTwoParts() { string contentType = "multipart/mixed; boundary=\"abcd\""; string content = "\r\n--abcd\r\nContent-Type: application/xml;charset=UTF-8\r\n\r\n<movie>...</movie>\r\n--abcd\r\n\r\nplain text\r\n--abcd--"; MultipartMessage message = ParseMessage(contentType, content); Assert.IsTrue(message.IsMultipart); Assert.AreEqual(MultipartMessage.MediaTypeMultipartMixed, message.MediaType); Assert.AreEqual("abcd", message.Boundary); Assert.AreEqual(2, message.Parts.Count); MultipartMessagePart part = message.Parts[0]; Assert.IsTrue(part.HasHeader("content-type")); Assert.AreEqual("application/xml", part.GetHeader("content-type").FieldValue); Assert.AreEqual("<movie>...</movie>", Encoding.UTF8.GetString(part.Data)); }
public void ParseMessageWithBoundaryWithoutQuotes() { string contentType = "multipart/mixed; boundary=abcd"; string content = "\r\n--abcd\r\nContent-Type: application/xml;charset=UTF-8\r\n\r\n<movie>...</movie>\r\n--abcd--"; MultipartMessage message = StartReading(contentType, content); Assert.IsTrue(message.IsMultipart); Assert.AreEqual(MultipartMessage.MediaTypeMultipartMixed, message.MediaType); Assert.AreEqual("abcd", message.Boundary); MultipartMessagePart part = reader.ReadNextPart(); Assert.IsTrue(part.HasHeader("content-type")); Assert.AreEqual("application/xml", part.GetHeader("content-type").FieldValue); Assert.AreEqual("<movie>...</movie>", Encoding.UTF8.GetString(part.Data)); Assert.IsNull(reader.ReadNextPart()); }
public void WriteMessageAsAWhole() { string boundary = "-----Test"; MultipartMessage message = new MultipartMessage(HeaderField.Parse("Content-type: multipart/mixed; boundary=-----Test")); MultipartMessagePart part; part = new MultipartMessagePart(); part.AddHeader(new HeaderField("Content-Type", "text/plain")); part.Data = Encoding.UTF8.GetBytes("my text"); message.AddPart(part); part = new MultipartMessagePart(); part.AddHeader(new HeaderField("Content-Type", "application/xml")); part.Data = Encoding.UTF8.GetBytes("<tag/>"); message.AddPart(part); using (MemoryStream stream = new MemoryStream()) { long writtenBytes; using (MultipartStreamWriter writer = new MultipartStreamWriter( stream, boundary, Encoding.UTF8)) { writer.WriteWholeMessage(message); writtenBytes = writer.BytesWritten; } byte[] bytes = stream.ToArray(); Assert.AreEqual(bytes.Length, writtenBytes); string result = Encoding.UTF8.GetString(bytes); Assert.AreEqual("\r\n-------Test\r\nContent-Type: text/plain\r\n\r\nmy text\r\n-------Test\r\nContent-Type: application/xml\r\n\r\n<tag/>\r\n-------Test--", result); } }