Skip to content

Commit

Permalink
Added Experimental/MimeParser test for content with mixed line endings
Browse files Browse the repository at this point in the history
  • Loading branch information
jstedfast committed Dec 22, 2024
1 parent d439af1 commit 77a9788
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
38 changes: 38 additions & 0 deletions UnitTests/ExperimentalMimeParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3411,6 +3411,44 @@ This is some raw data.
}
}

[Test]
public void TestMimePartContentWithMixedLineEndings ()
{
string text = "From: [email protected]\r\nTo: [email protected]\r\nSubject: content with mixed line endings\r\nDate: Sat, Dec 21 2024 09:12:42 -0500\r\nMIME-Version: 1.0\r\nContent-Type: text/plain; charset=utf-8\r\n\r\nThis is a normal line of text.\r\nThis line ends with a bare LF.\nAnd this line endds with CRLF.\r\n";

using (var stream = new MemoryStream (Encoding.ASCII.GetBytes (text), false)) {
var parser = new ExperimentalMimeParser (stream, MimeFormat.Entity);
var message = parser.ParseMessage ();

Assert.That (message.Body, Is.InstanceOf<TextPart> (), "Expected body of the message to be text/plain");
var body = (TextPart) message.Body;

Assert.That (body.Headers[HeaderId.ContentType], Is.EqualTo ("text/plain; charset=utf-8"));
Assert.That (body.ContentType.Charset, Is.EqualTo ("utf-8"));
Assert.That (body.Text, Is.EqualTo ("This is a normal line of text.\r\nThis line ends with a bare LF.\r\nAnd this line endds with CRLF.\r\n"));
Assert.That (body.Content.NewLineFormat, Is.EqualTo (NewLineFormat.Mixed));
}
}

[Test]
public async Task TestMimePartContentWithMixedLineEndingsAsync ()
{
string text = "From: [email protected]\r\nTo: [email protected]\r\nSubject: content with mixed line endings\r\nDate: Sat, Dec 21 2024 09:12:42 -0500\r\nMIME-Version: 1.0\r\nContent-Type: text/plain; charset=utf-8\r\n\r\nThis is a normal line of text.\r\nThis line ends with a bare LF.\nAnd this line endds with CRLF.\r\n";

using (var stream = new MemoryStream (Encoding.ASCII.GetBytes (text), false)) {
var parser = new ExperimentalMimeParser (stream, MimeFormat.Entity);
var message = await parser.ParseMessageAsync ();

Assert.That (message.Body, Is.InstanceOf<TextPart> (), "Expected body of the message to be text/plain");
var body = (TextPart) message.Body;

Assert.That (body.Headers[HeaderId.ContentType], Is.EqualTo ("text/plain; charset=utf-8"));
Assert.That (body.ContentType.Charset, Is.EqualTo ("utf-8"));
Assert.That (body.Text, Is.EqualTo ("This is a normal line of text.\r\nThis line ends with a bare LF.\r\nAnd this line endds with CRLF.\r\n"));
Assert.That (body.Content.NewLineFormat, Is.EqualTo (NewLineFormat.Mixed));
}
}

[Test]
public void TestGarbageBeforeMessageHeaders ()
{
Expand Down
38 changes: 38 additions & 0 deletions UnitTests/MimeParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3481,6 +3481,44 @@ This is some raw data.
}
}

[Test]
public void TestMimePartContentWithMixedLineEndings ()
{
string text = "From: [email protected]\r\nTo: [email protected]\r\nSubject: content with mixed line endings\r\nDate: Sat, Dec 21 2024 09:12:42 -0500\r\nMIME-Version: 1.0\r\nContent-Type: text/plain; charset=utf-8\r\n\r\nThis is a normal line of text.\r\nThis line ends with a bare LF.\nAnd this line endds with CRLF.\r\n";

using (var stream = new MemoryStream (Encoding.ASCII.GetBytes (text), false)) {
var parser = new MimeParser (stream, MimeFormat.Entity);
var message = parser.ParseMessage ();

Assert.That (message.Body, Is.InstanceOf<TextPart> (), "Expected body of the message to be text/plain");
var body = (TextPart) message.Body;

Assert.That (body.Headers[HeaderId.ContentType], Is.EqualTo ("text/plain; charset=utf-8"));
Assert.That (body.ContentType.Charset, Is.EqualTo ("utf-8"));
Assert.That (body.Text, Is.EqualTo ("This is a normal line of text.\r\nThis line ends with a bare LF.\r\nAnd this line endds with CRLF.\r\n"));
Assert.That (body.Content.NewLineFormat, Is.EqualTo (NewLineFormat.Mixed));
}
}

[Test]
public async Task TestMimePartContentWithMixedLineEndingsAsync ()
{
string text = "From: [email protected]\r\nTo: [email protected]\r\nSubject: content with mixed line endings\r\nDate: Sat, Dec 21 2024 09:12:42 -0500\r\nMIME-Version: 1.0\r\nContent-Type: text/plain; charset=utf-8\r\n\r\nThis is a normal line of text.\r\nThis line ends with a bare LF.\nAnd this line endds with CRLF.\r\n";

using (var stream = new MemoryStream (Encoding.ASCII.GetBytes (text), false)) {
var parser = new MimeParser (stream, MimeFormat.Entity);
var message = await parser.ParseMessageAsync ();

Assert.That (message.Body, Is.InstanceOf<TextPart> (), "Expected body of the message to be text/plain");
var body = (TextPart) message.Body;

Assert.That (body.Headers[HeaderId.ContentType], Is.EqualTo ("text/plain; charset=utf-8"));
Assert.That (body.ContentType.Charset, Is.EqualTo ("utf-8"));
Assert.That (body.Text, Is.EqualTo ("This is a normal line of text.\r\nThis line ends with a bare LF.\r\nAnd this line endds with CRLF.\r\n"));
Assert.That (body.Content.NewLineFormat, Is.EqualTo (NewLineFormat.Mixed));
}
}

[Test]
public void TestGarbageBeforeMessageHeaders ()
{
Expand Down

0 comments on commit 77a9788

Please sign in to comment.