Skip to content

Commit a79e961

Browse files
committed
Added unit tests for MimeEntity.WriteTo(fileName) and Load(fileName)
1 parent d07bd9a commit a79e961

File tree

1 file changed

+66
-8
lines changed

1 file changed

+66
-8
lines changed

UnitTests/MimePartTests.cs

+66-8
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
// THE SOFTWARE.
2525
//
2626

27+
using System.IO;
2728
using System.Text;
29+
using System.Buffers;
2830

2931
using MimeKit;
3032
using MimeKit.Utils;
@@ -523,16 +525,16 @@ public async Task TestTranscodingAsync ()
523525
}
524526
}
525527

526-
[TestCase ("content", TestName = "TestWriteToNoNewLine")]
527-
[TestCase ("content\r\n", TestName = "TestWriteToNewLine")]
528+
[TestCase ("content", TestName = "TestWriteTo_NoNewLine")]
529+
[TestCase ("content\r\n", TestName = "TestWriteTo_NewLine")]
528530
public void TestWriteTo (string text)
529531
{
530532
var builder = new BodyBuilder ();
531533

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

535-
var body = builder.ToMessageBody ();
537+
using var body = builder.ToMessageBody ();
536538

537539
using (var stream = new MemoryStream ()) {
538540
var options = FormatOptions.Default.Clone ();
@@ -541,7 +543,7 @@ public void TestWriteTo (string text)
541543
body.WriteTo (options, stream);
542544
stream.Position = 0;
543545

544-
var multipart = (Multipart) MimeEntity.Load (stream);
546+
using var multipart = (Multipart) MimeEntity.Load (stream);
545547
using (var input = ((MimePart) multipart[1]).Content.Open ()) {
546548
var buffer = new byte[1024];
547549
int n;
@@ -555,16 +557,16 @@ public void TestWriteTo (string text)
555557
}
556558
}
557559

558-
[TestCase ("content", TestName = "TestWriteToNoNewLine")]
559-
[TestCase ("content\r\n", TestName = "TestWriteToNewLine")]
560+
[TestCase ("content", TestName = "TestWriteToAsync_NoNewLine")]
561+
[TestCase ("content\r\n", TestName = "TestWriteToAsync_NewLine")]
560562
public async Task TestWriteToAsync (string text)
561563
{
562564
var builder = new BodyBuilder ();
563565

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

567-
var body = builder.ToMessageBody ();
569+
using var body = builder.ToMessageBody ();
568570

569571
using (var stream = new MemoryStream ()) {
570572
var options = FormatOptions.Default.Clone ();
@@ -573,7 +575,7 @@ public async Task TestWriteToAsync (string text)
573575
await body.WriteToAsync (options, stream);
574576
stream.Position = 0;
575577

576-
var multipart = (Multipart) await MimeEntity.LoadAsync (stream);
578+
using var multipart = (Multipart) await MimeEntity.LoadAsync (stream);
577579
using (var input = ((MimePart) multipart[1]).Content.Open ()) {
578580
var buffer = new byte[1024];
579581
int n;
@@ -587,6 +589,62 @@ public async Task TestWriteToAsync (string text)
587589
}
588590
}
589591

592+
[TestCase ("content", TestName = "TestWriteToFile_NoNewLine")]
593+
[TestCase ("content\r\n", TestName = "TestWriteToFile_NewLine")]
594+
public void TestWriteToFile (string text)
595+
{
596+
var fileName = Path.GetTempFileName ();
597+
using var body = new TextPart ("plain") {
598+
Text = text
599+
};
600+
601+
try {
602+
var options = FormatOptions.Default.Clone ();
603+
options.NewLineFormat = NewLineFormat.Dos;
604+
605+
body.WriteTo (options, fileName);
606+
607+
using (var loaded = (TextPart) MimeEntity.Load (fileName))
608+
Assert.That (loaded.Text, Is.EqualTo (text));
609+
610+
options.NewLineFormat = NewLineFormat.Unix;
611+
body.WriteTo (options, fileName);
612+
613+
using (var loaded = (TextPart) MimeEntity.Load (fileName))
614+
Assert.That (loaded.Text, Is.EqualTo (text));
615+
} finally {
616+
File.Delete (fileName);
617+
}
618+
}
619+
620+
[TestCase ("content", TestName = "TestWriteToFileAsync_NoNewLine")]
621+
[TestCase ("content\r\n", TestName = "TestWriteToFileAsync_NewLine")]
622+
public async Task TestWriteToFileAsync (string text)
623+
{
624+
var fileName = Path.GetTempFileName ();
625+
using var body = new TextPart ("plain") {
626+
Text = text
627+
};
628+
629+
try {
630+
var options = FormatOptions.Default.Clone ();
631+
options.NewLineFormat = NewLineFormat.Dos;
632+
633+
await body.WriteToAsync (options, fileName);
634+
635+
using (var loaded = (TextPart) await MimeEntity.LoadAsync (fileName))
636+
Assert.That (loaded.Text, Is.EqualTo (text));
637+
638+
options.NewLineFormat = NewLineFormat.Unix;
639+
await body.WriteToAsync (options, fileName);
640+
641+
using (var loaded = (TextPart) await MimeEntity.LoadAsync (fileName))
642+
Assert.That (loaded.Text, Is.EqualTo (text));
643+
} finally {
644+
File.Delete (fileName);
645+
}
646+
}
647+
590648
[Test]
591649
public void TestLoadHttpWebResponse ()
592650
{

0 commit comments

Comments
 (0)