Skip to content

Commit

Permalink
Add unit test for multipart message with alternatives and attachments
Browse files Browse the repository at this point in the history
This test validates the `WriteTo` method for a message containing plain text, HTML alternative, an attachment, and an embedded file. It ensures the correct structure and content of the multipart message, enhancing test coverage and reliability.
  • Loading branch information
wneessen committed Jan 19, 2025
1 parent e8f96c9 commit c712edb
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6047,6 +6047,66 @@ func TestMsg_WriteTo(t *testing.T) {
}
checkMessageContent(t, buffer, wants)
})
t.Run("WriteTo Multipart plain body, alternative html, attachment and embed", func(t *testing.T) {
message := testMessage(t)
message.AddAlternativeString(TypeTextHTML, "<p>HTML alternative part</p>")
message.AttachFile("testdata/attachment.txt")
message.EmbedFile("testdata/embed.txt")
buffer := bytes.NewBuffer(nil)
if _, err := message.WriteTo(buffer); err != nil {
t.Fatalf("failed to write message to buffer: %s", err)
}
wants := []msgContentTest{
{0, "Date:", false, true, false},
{1, "MIME-Version: 1.0", true, true, false},
{2, "Message-ID: <", false, true, false},
{2, ">", false, false, true},
{6, "From: <[email protected]>", true, true, false},
{7, "To: <[email protected]>", true, true, false},
{8, `Content-Type: multipart/mixed;`, true, true, false},
{9, ` boundary=`, false, true, false},
{10, "", true, false, false},
{11, "--", false, true, false},
{12, `Content-Type: multipart/related;`, true, true, false},
{13, ` boundary=`, false, true, false},
{14, "", true, false, false},
{15, "--", false, true, false},
{16, `Content-Type: multipart/alternative;`, true, true, false},
{17, ` boundary=`, false, true, false},
{18, "", true, false, false},
{19, "--", false, true, false},
{20, "Content-Transfer-Encoding: quoted-printable", true, true, false},
{21, "Content-Type: text/plain; charset=UTF-8", true, true, false},
{22, "", true, false, false},
{23, "Testmail", true, true, false},
{24, "--", false, true, false},
{25, "Content-Transfer-Encoding: quoted-printable", true, true, false},
{26, "Content-Type: text/html; charset=UTF-8", true, true, false},
{27, "", true, false, false},
{28, `<p>HTML alternative part</p>`, true, true, false},
{29, "--", false, true, true},
{30, "", true, false, false},
{31, "--", false, true, false},
{32, `Content-Disposition: inline; filename="embed.txt"`, true, true, false},
{33, "Content-Id: <embed.txt>", true, true, false},
{34, "Content-Transfer-Encoding: base64", true, true, false},
{35, `Content-Type: text/plain; charset=utf-8; name="embed.txt"`, true, true, false},
{36, "", true, false, false},
{37, "VGhp", false, true, false},
{38, "", true, false, false},
{39, "--", false, true, true},
{40, "", true, false, false},
{41, "--", false, true, false},
{42, `Content-Disposition: attachment; filename="attachment.txt"`, true, true, false},
{43, "Content-Transfer-Encoding: base64", true, true, false},
{44, `Content-Type: text/plain; charset=utf-8; name="attachment.txt"`, true, true, false},
{45, "", true, false, false},
{46, "VGhp", false, true, false},
{47, "", true, false, false},
{48, "--", false, true, true},
}
checkMessageContent(t, buffer, wants)
})
}

func TestMsg_WriteToFile(t *testing.T) {
Expand Down

0 comments on commit c712edb

Please sign in to comment.