mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-22 05:40:50 +01:00
Add EML message parsing tests
Introduced a new test, `TestEMLToMsgFromFile`, to validate the functions responsible for EML message parsing. This complements the existing `EMLToMsgFromString` test, holding them accountable for subject and encoding accuracy. Also, a temporary directory is now created for testing File-related operations in isolation.
This commit is contained in:
parent
047d923b45
commit
04c41a1f10
1 changed files with 61 additions and 6 deletions
67
eml_test.go
67
eml_test.go
|
@ -5,6 +5,8 @@
|
||||||
package mail
|
package mail
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
@ -143,15 +145,68 @@ func TestEMLToMsgFromString(t *testing.T) {
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
m, err := EMLToMsgFromString(tt.eml)
|
msg, err := EMLToMsgFromString(tt.eml)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("failed to parse EML: %s", err)
|
t.Errorf("failed to parse EML: %subject", err)
|
||||||
}
|
}
|
||||||
if m.Encoding() != tt.enc {
|
if msg.Encoding() != tt.enc {
|
||||||
t.Errorf("EMLToMsgFromString failed: expected encoding: %s, but got: %s", tt.enc, m.Encoding())
|
t.Errorf("EMLToMsgFromString failed: expected encoding: %subject, but got: %subject", tt.enc, msg.Encoding())
|
||||||
}
|
}
|
||||||
if s := m.GetGenHeader(HeaderSubject); len(s) > 0 && !strings.EqualFold(s[0], tt.sub) {
|
if subject := msg.GetGenHeader(HeaderSubject); len(subject) > 0 && !strings.EqualFold(subject[0], tt.sub) {
|
||||||
t.Errorf("EMLToMsgFromString failed: expected subject: %s, but got: %s", tt.sub, s[0])
|
t.Errorf("EMLToMsgFromString failed: expected subject: %subject, but got: %subject",
|
||||||
|
tt.sub, subject[0])
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEMLToMsgFromFile(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
eml string
|
||||||
|
enc string
|
||||||
|
sub string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"Plain text no encoding", exampleMailPlainNoEnc, "8bit",
|
||||||
|
"Example mail // plain text without encoding",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Plain text quoted-printable", exampleMailPlainQP, "quoted-printable",
|
||||||
|
"Example mail // plain text quoted-printable",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Plain text base64", exampleMailPlainB64, "base64",
|
||||||
|
"Example mail // plain text base64",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
tempDir, err := os.MkdirTemp("", fmt.Sprintf("*-%s", tt.name))
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to create temp dir: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
if err = os.RemoveAll(tempDir); err != nil {
|
||||||
|
t.Error("failed to remove temp dir:", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
err = os.WriteFile(fmt.Sprintf("%s/%s.eml", tempDir, tt.name), []byte(tt.eml), 0666)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("failed to write mail to temp file:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
msg, err := EMLToMsgFromFile(fmt.Sprintf("%s/%s.eml", tempDir, tt.name))
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to parse EML: %subject", err)
|
||||||
|
}
|
||||||
|
if msg.Encoding() != tt.enc {
|
||||||
|
t.Errorf("EMLToMsgFromString failed: expected encoding: %subject, but got: %subject", tt.enc, msg.Encoding())
|
||||||
|
}
|
||||||
|
if subject := msg.GetGenHeader(HeaderSubject); len(subject) > 0 && !strings.EqualFold(subject[0], tt.sub) {
|
||||||
|
t.Errorf("EMLToMsgFromString failed: expected subject: %subject, but got: %subject",
|
||||||
|
tt.sub, subject[0])
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue