This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
main_test.go
81 lines (68 loc) · 1.76 KB
/
main_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
package gophermail
import (
"bytes"
"net/mail"
"net/textproto"
"testing"
"time"
)
func simpleMessage() *Message {
m := &Message{}
m.SetFrom("Domain Sender <[email protected]>")
m.AddTo("First person <[email protected]>")
m.Subject = "My Subject"
m.Body = "My Plain Text Body"
return m
}
func Test_Bytes(t *testing.T) {
m := &Message{}
m.SetFrom("Domain Sender <[email protected]>")
m.SetReplyTo("Don't reply <[email protected]>")
m.AddTo("First person <[email protected]>")
m.AddTo("[email protected]")
m.AddCc("Less important person <[email protected]>")
m.AddBcc("Sneaky person <[email protected]>")
m.Subject = "My Subject (abcdefghijklmnop qrstuvwxyz0123456789 abcdefghijklmnopqrstuvwxyz0123456789_567890)"
m.Body = "My Plain Text Body"
m.HTMLBody = "<p>My <b>HTML</b> Body</p>"
m.Headers = mail.Header{}
m.Headers["Date"] = []string{time.Now().UTC().Format(time.RFC822)}
bytes, err := m.Bytes()
if err != nil {
t.Log(err)
t.Fail()
}
t.Logf("%s", bytes)
}
func TestSubjectHeaderWithSimpleQuoting(t *testing.T) {
m := simpleMessage()
m.Subject = "My Subject"
buf := new(bytes.Buffer)
header := textproto.MIMEHeader{}
_, err := m.bytes(buf, header)
if err != nil {
t.Log(err)
t.Fail()
}
expected := "My Subject"
if sub := header.Get("Subject"); sub != expected {
t.Logf(`Expected Subject to be "%s" but got "%s"`, expected, sub)
t.Fail()
}
}
func TestSubjectHeaderWithExistingQuotes(t *testing.T) {
m := simpleMessage()
m.Subject = `"Hi World"`
buf := new(bytes.Buffer)
header := textproto.MIMEHeader{}
_, err := m.bytes(buf, header)
if err != nil {
t.Log(err)
t.Fail()
}
expected := `\"Hi World\"`
if sub := header.Get("Subject"); sub != expected {
t.Logf(`Expected Subject to be "%s" but got "%s"`, expected, sub)
t.Fail()
}
}