Exemplo n.º 1
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);
                }
            }
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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));
        }