From 9607d08469194a53574f9b7b5b9f16fa0e45a897 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Tue, 31 Oct 2023 11:45:37 +0100 Subject: [PATCH] Add EML parsing from string to tests A new test `TestEMLToMsgFromString` was added to "eml_test.go". This test asserts the proper functionality of `EMLToMsgFromString` method that allows us to parse EMLs directly from a string input. This test is a necessary part of ensuring the functionality and reliability of our EML parsing process. --- eml_test.go | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 eml_test.go diff --git a/eml_test.go b/eml_test.go new file mode 100644 index 0000000..83e26ab --- /dev/null +++ b/eml_test.go @@ -0,0 +1,65 @@ +// SPDX-FileCopyrightText: 2022-2023 The go-mail Authors +// +// SPDX-License-Identifier: MIT + +package mail + +import ( + "strings" + "testing" +) + +const ( + exampleMailPlainNoEnc = `Date: Thu, 14 Sep 2023 14:35:28 +0200 +MIME-Version: 1.0 +Message-ID: + <1305604950.683004066175.AAAAAAAAaaaaaaaaB@test.com> +Subject: Example mail Plain text no Encoding +User-Agent: go-mail v0.3.9 // https://github.com/wneessen/go-mail +X-Mailer: go-mail v0.3.8 // https://github.com/wneessen/go-mail +From: "Toni Tester" +To: "Go Mail" +Cc: Second Recipient +Bcc: "Invisible User" +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +Dear Customer, + +This is a test mail. Please do not reply to this. + + +Thank your for your business! +The go-mail team + +-- +This is a signature` +) + +func TestEMLToMsgFromString(t *testing.T) { + tests := []struct { + name string + eml string + enc string + sub string + }{ + { + "Plain text no encoding", exampleMailPlainNoEnc, "8bit", + "Example mail Plain text no Encoding", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m, err := EMLToMsgFromString(tt.eml) + if err != nil { + t.Errorf("failed to parse EML: %s", err) + } + if m.Encoding() != tt.enc { + t.Errorf("EMLToMsgFromString failed: expected encoding: %s, but got: %s", tt.enc, m.Encoding()) + } + if s := m.GetGenHeader(HeaderSubject); len(s) > 0 && !strings.EqualFold(s[0], tt.sub) { + t.Errorf("EMLToMsgFromString failed: expected subject: %s, but got: %s", tt.sub, s[0]) + } + }) + } +}