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