Пример #1
0
        public void ParseMessageWithBinaryData()
        {
            string boundary = "-----Test";

            byte[] requestData;
            using (MemoryStream stream = new MemoryStream())
            {
                using (MultipartStreamWriter writer = new MultipartStreamWriter(
                    stream,
                    boundary,
                    Encoding.UTF8))
                {
                    writer.WritePart("text/plain", "my text", Encoding.UTF8);
                    writer.WritePart("application/xml", "<tag/>", Encoding.UTF8);

                    byte[] pdfData = File.ReadAllBytes(@"..\..\..\samples\pdf-test.pdf");

                    List<HeaderField> headers = new List<HeaderField>();
                    headers.Add(new HeaderField("Content-type", "application/pdf"));
                    headers.Add(new HeaderField("Content-id", "PDF"));
                    writer.WritePart(headers, pdfData);

                    writer.WriteCloseDelimiter();
                }

                requestData = stream.ToArray();
            }

            log.DebugFormat("Message body: {0}", Encoding.UTF8.GetString(requestData));

            string requestContentType = string.Format(
                CultureInfo.InvariantCulture,
                "multipart/mixed;boundary=\"{0}\"",
                boundary);

            MultipartMessage message = StartReading(requestContentType, requestData);
            Assert.IsTrue(message.IsMultipart);
            Assert.AreEqual(MultipartMessage.MediaTypeMultipartMixed, message.MediaType);

            MultipartMessagePart part;

            part = reader.ReadNextPart();
            part = reader.ReadNextPart();
            part = reader.ReadNextPart();

            Assert.AreEqual("application/pdf", part.GetHeader("content-type").FieldValue);
            File.WriteAllBytes("test.pdf", part.Data);

            Assert.IsNull(reader.ReadNextPart());
        }
Пример #2
0
        public void WriteManuallyPartByPart()
        {
            string boundary = "-----Test";

            using (MemoryStream stream = new MemoryStream())
            {
                using (MultipartStreamWriter writer = new MultipartStreamWriter(
                    stream,
                    boundary,
                    Encoding.UTF8))
                {
                    writer.WritePart("text/plain", "my text", Encoding.UTF8);
                    writer.WritePart("application/xml", "<tag/>", Encoding.UTF8);
                    writer.WriteCloseDelimiter();
                }

                byte[] bytes = stream.ToArray();
                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);
            }
        }
Пример #3
0
        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);
            }
        }