Пример #1
0
        public async void TestWriteToAsync(string text)
        {
            var builder = new BodyBuilder();

            builder.Attachments.Add("filename", new MemoryStream(Encoding.UTF8.GetBytes(text)));
            builder.TextBody = "This is the text body.";

            var body = builder.ToMessageBody();

            using (var stream = new MemoryStream()) {
                var options = FormatOptions.Default.Clone();
                options.NewLineFormat = NewLineFormat.Dos;

                await body.WriteToAsync(options, stream);

                stream.Position = 0;

                var multipart = (Multipart)await MimeEntity.LoadAsync(stream);

                using (var input = ((MimePart)multipart[1]).Content.Open()) {
                    var buffer = new byte[1024];
                    int n;

                    n = input.Read(buffer, 0, buffer.Length);

                    var content = Encoding.UTF8.GetString(buffer, 0, n);

                    Assert.AreEqual(text, content);
                }
            }
        }
Пример #2
0
        public async Task Test()
        {
            var entity = await MimeEntity.LoadAsync(Path.Combine(Directory.GetCurrentDirectory(), "signed.txt"));

            Assert.IsInstanceOf <MultipartSigned>(entity);

            var signed = (MultipartSigned)entity;

            Assert.AreEqual(2, signed.Count);
        }
Пример #3
0
        public async void TestLoadHttpWebResponseAsync()
        {
            var text        = "This is some text and stuff." + Environment.NewLine;
            var contentType = new ContentType("text", "plain");

            using (var content = new MemoryStream(Encoding.ASCII.GetBytes(text), false)) {
                var entity = await MimeEntity.LoadAsync(contentType, content);

                Assert.IsInstanceOf <TextPart> (entity);

                var part = (TextPart)entity;

                Assert.AreEqual(text, part.Text);
            }
        }
Пример #4
0
        public async void TestTranscodingAsync()
        {
            var path     = Path.Combine("..", "..", "TestData", "images", "girl.jpg");
            var expected = File.ReadAllBytes(path);

            var part = new MimePart("image", "jpeg")
            {
                Content = new MimeContent(new MemoryStream(expected, false)),
                ContentTransferEncoding = ContentEncoding.Base64,
                FileName = "girl.jpg"
            };

            // encode in base64
            using (var output = new MemoryStream()) {
                await part.WriteToAsync(output);

                output.Position = 0;

                part = (MimePart)await MimeEntity.LoadAsync(output);
            }

            // transcode to uuencode
            part.ContentTransferEncoding = ContentEncoding.UUEncode;
            using (var output = new MemoryStream()) {
                await part.WriteToAsync(output);

                output.Position = 0;

                part = (MimePart)await MimeEntity.LoadAsync(output);
            }

            // verify decoded content
            using (var output = new MemoryStream()) {
                await part.Content.DecodeToAsync(output);

                output.Position = 0;

                var actual = output.ToArray();

                Assert.AreEqual(expected.Length, actual.Length);
                for (int i = 0; i < expected.Length; i++)
                {
                    Assert.AreEqual(expected[i], actual[i], "Image content differs at index {0}", i);
                }
            }
        }
Пример #5
0
        public async Task GetStream_CreateMimePart_WriteMimePart_LoadMimePart_CompareMimeParts(string fileName)
        {
            var stream = await GetAssetStreamAsync(fileName);

            var m1 = new MimePart
            {
                Content = new MimeContent(stream)
            };

            await m1.WriteToAsync(GetAssetFilePath($"{fileName}__m1.txt"));

            var entity = await MimeEntity.LoadAsync(GetAssetFilePath($"{fileName}__m1.txt"));

            var m2 = (MimePart)entity;

            FileAssert.AreEqual(stream, m1.Content.Stream);
            FileAssert.AreEqual(m1.Content.Stream, m2.Content.Stream);
        }
Пример #6
0
        public async Task TestParsingOfCustomTypeAsync()
        {
            var options = ParserOptions.Default.Clone();

            options.RegisterMimeType("text/html", typeof(CustomTextHtmlPart));

            using (var stream = new MemoryStream()) {
                var text = new TextPart("html")
                {
                    Text = "<html>this is some html and stuff</html>"
                };

                text.WriteTo(stream);
                stream.Position = 0;

                var html = await MimeEntity.LoadAsync(options, stream);

                Assert.IsInstanceOf <CustomTextHtmlPart> (html, "Expected the text/html part to use our custom type.");
            }
        }
Пример #7
0
        public void TestArgumentExceptions()
        {
            var part = new MimePart();

            Assert.Throws <ArgumentNullException> (() => new MimePart((string)null));
            Assert.Throws <ArgumentNullException> (() => new MimePart((ContentType)null));
            Assert.Throws <ArgumentNullException> (() => new MimePart(null, "octet-stream"));
            Assert.Throws <ArgumentNullException> (() => new MimePart("application", null));

            Assert.Throws <ArgumentOutOfRangeException> (() => part.ContentDuration = -1);
            Assert.Throws <ArgumentOutOfRangeException> (() => part.Prepare(EncodingConstraint.SevenBit, 1));

            Assert.Throws <ArgumentNullException> (() => MimeEntity.Load((Stream)null));
            Assert.Throws <ArgumentNullException> (() => MimeEntity.Load((Stream)null, true));
            Assert.Throws <ArgumentNullException> (() => MimeEntity.Load((ParserOptions)null, Stream.Null));
            Assert.Throws <ArgumentNullException> (() => MimeEntity.Load(ParserOptions.Default, (Stream)null));
            Assert.Throws <ArgumentNullException> (() => MimeEntity.Load(null, Stream.Null, true));
            Assert.Throws <ArgumentNullException> (() => MimeEntity.Load(ParserOptions.Default, (Stream)null, true));

            Assert.Throws <ArgumentNullException> (() => MimeEntity.Load((ContentType)null, Stream.Null));
            Assert.Throws <ArgumentNullException> (() => MimeEntity.Load(new ContentType("application", "octet-stream"), null));
            Assert.Throws <ArgumentNullException> (() => MimeEntity.Load(null, new ContentType("application", "octet-stream"), Stream.Null));
            Assert.Throws <ArgumentNullException> (() => MimeEntity.Load(ParserOptions.Default, null, Stream.Null));
            Assert.Throws <ArgumentNullException> (() => MimeEntity.Load(ParserOptions.Default, new ContentType("application", "octet-stream"), null));

            Assert.Throws <ArgumentNullException> (() => MimeEntity.Load((string)null));
            Assert.Throws <ArgumentNullException> (() => MimeEntity.Load(null, "fileName"));
            Assert.Throws <ArgumentNullException> (() => MimeEntity.Load(ParserOptions.Default, (string)null));

            Assert.Throws <ArgumentNullException> (async() => await MimeEntity.LoadAsync((Stream)null));
            Assert.Throws <ArgumentNullException> (async() => await MimeEntity.LoadAsync((Stream)null, true));
            Assert.Throws <ArgumentNullException> (async() => await MimeEntity.LoadAsync((ParserOptions)null, Stream.Null));
            Assert.Throws <ArgumentNullException> (async() => await MimeEntity.LoadAsync(ParserOptions.Default, (Stream)null));
            Assert.Throws <ArgumentNullException> (async() => await MimeEntity.LoadAsync(null, Stream.Null, true));
            Assert.Throws <ArgumentNullException> (async() => await MimeEntity.LoadAsync(ParserOptions.Default, (Stream)null, true));

            Assert.Throws <ArgumentNullException> (async() => await MimeEntity.LoadAsync((ContentType)null, Stream.Null));
            Assert.Throws <ArgumentNullException> (async() => await MimeEntity.LoadAsync(new ContentType("application", "octet-stream"), null));
            Assert.Throws <ArgumentNullException> (async() => await MimeEntity.LoadAsync(null, new ContentType("application", "octet-stream"), Stream.Null));
            Assert.Throws <ArgumentNullException> (async() => await MimeEntity.LoadAsync(ParserOptions.Default, null, Stream.Null));
            Assert.Throws <ArgumentNullException> (async() => await MimeEntity.LoadAsync(ParserOptions.Default, new ContentType("application", "octet-stream"), null));

            Assert.Throws <ArgumentNullException> (async() => await MimeEntity.LoadAsync((string)null));
            Assert.Throws <ArgumentNullException> (async() => await MimeEntity.LoadAsync(null, "fileName"));
            Assert.Throws <ArgumentNullException> (async() => await MimeEntity.LoadAsync(ParserOptions.Default, (string)null));

            Assert.Throws <ArgumentNullException> (() => part.Accept(null));
            Assert.Throws <ArgumentNullException> (() => part.WriteTo((string)null));
            Assert.Throws <ArgumentNullException> (() => part.WriteTo((Stream)null));
            Assert.Throws <ArgumentNullException> (() => part.WriteTo((string)null, false));
            Assert.Throws <ArgumentNullException> (() => part.WriteTo((Stream)null, false));
            Assert.Throws <ArgumentNullException> (() => part.WriteTo(null, Stream.Null));
            Assert.Throws <ArgumentNullException> (() => part.WriteTo(FormatOptions.Default, (Stream)null));
            Assert.Throws <ArgumentNullException> (() => part.WriteTo(null, "fileName"));
            Assert.Throws <ArgumentNullException> (() => part.WriteTo(FormatOptions.Default, (string)null));
            Assert.Throws <ArgumentNullException> (() => part.WriteTo(null, Stream.Null, false));
            Assert.Throws <ArgumentNullException> (() => part.WriteTo(FormatOptions.Default, (Stream)null, false));
            Assert.Throws <ArgumentNullException> (() => part.WriteTo(null, "fileName", false));
            Assert.Throws <ArgumentNullException> (() => part.WriteTo(FormatOptions.Default, (string)null, false));
            Assert.Throws <ArgumentException> (() => part.ContentId = "this is some text and stuff");

            Assert.Throws <ArgumentNullException> (async() => await part.WriteToAsync((string)null));
            Assert.Throws <ArgumentNullException> (async() => await part.WriteToAsync((Stream)null));
            Assert.Throws <ArgumentNullException> (async() => await part.WriteToAsync((string)null, false));
            Assert.Throws <ArgumentNullException> (async() => await part.WriteToAsync((Stream)null, false));
            Assert.Throws <ArgumentNullException> (async() => await part.WriteToAsync(null, Stream.Null));
            Assert.Throws <ArgumentNullException> (async() => await part.WriteToAsync(FormatOptions.Default, (Stream)null));
            Assert.Throws <ArgumentNullException> (async() => await part.WriteToAsync(null, "fileName"));
            Assert.Throws <ArgumentNullException> (async() => await part.WriteToAsync(FormatOptions.Default, (string)null));
            Assert.Throws <ArgumentNullException> (async() => await part.WriteToAsync(null, Stream.Null, false));
            Assert.Throws <ArgumentNullException> (async() => await part.WriteToAsync(FormatOptions.Default, (Stream)null, false));
            Assert.Throws <ArgumentNullException> (async() => await part.WriteToAsync(null, "fileName", false));
            Assert.Throws <ArgumentNullException> (async() => await part.WriteToAsync(FormatOptions.Default, (string)null, false));
        }