-
Notifications
You must be signed in to change notification settings - Fork 75
/
mailyak_test.go
94 lines (77 loc) · 2.68 KB
/
mailyak_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package mailyak
import (
"fmt"
"net/smtp"
"strings"
"testing"
"time"
)
// TestMailYakStringer ensures MailYak struct conforms to the Stringer interface.
func TestMailYakStringer(t *testing.T) {
t.Parallel()
mail := New("mail.host.com:25", smtp.PlainAuth("", "user", "pass", "mail.host.com"))
mail.From("[email protected]")
mail.FromName("From Example")
mail.To("[email protected]")
mail.Bcc("[email protected]", "[email protected]")
mail.Subject("Test subject")
mail.ReplyTo("[email protected]")
mail.HTML().Set("HTML part: this is just a test.")
mail.Plain().Set("Plain text part: this is also just a test.")
mail.Attach("test.html", strings.NewReader("<html><head></head></html>"))
mail.Attach("test2.html", strings.NewReader("<html><head></head></html>"))
mail.AddHeader("Precedence", "bulk")
mail.date = "a date"
want := "&MailYak{date: \"a date\", from: \"[email protected]\", fromName: \"From Example\", html: 31 bytes, plain: 42 bytes, toAddrs: [[email protected]], bccAddrs: [[email protected] [email protected]], subject: \"Test subject\", Precedence: [\"bulk\"], host: \"mail.host.com:25\", attachments (2): [{filename: test.html} {filename: test2.html}], auth set: true, explicit tls: false}"
got := fmt.Sprintf("%+v", mail)
if got != want {
t.Errorf("MailYak.String() = %v, want %v", got, want)
}
}
// TestMailYakDate ensures two emails sent with the same MailYak instance use
// different (updated) date timestamps.
func TestMailYakDate(t *testing.T) {
t.Parallel()
mail := New("mail.host.com:25", smtp.PlainAuth("", "user", "pass", "mail.host.com"))
mail.From("[email protected]")
mail.To("[email protected]")
mail.Subject("Test subject")
// send two emails at different times (discarding any errors)
_, _ = mail.MimeBuf()
dateOne := mail.date
time.Sleep(1 * time.Second)
_, _ = mail.MimeBuf()
dateTwo := mail.date
if dateOne == dateTwo {
t.Errorf("MailYak.Send(): timestamp not updated: %v", dateOne)
}
}
// TestStripNames ensures that the stripNames() method correctly
// remove the name part of a list of RFC 5322 addresses.
func TestStripNames(t *testing.T) {
addresses := []string{
"invalid1",
"John Doe <[email protected]>",
"<[email protected]>",
"invalid2",
}
expected := map[string]struct{}{
"[email protected]": {},
"[email protected]": {},
"invalid1": {},
"[email protected]": {},
"[email protected]": {},
"invalid2": {},
}
result := stripNames(addresses)
if len(result) != len(expected) {
t.Fatalf("stripNames: Expected %d addresses, got %d: \n%v", len(expected), len(result), result)
}
for _, addr := range result {
if _, ok := expected[addr]; !ok {
t.Errorf("stripNames: Address %q was not expected", addr)
}
}
}