forked from jordan-wright/email
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail_test.go
186 lines (162 loc) · 5.73 KB
/
email_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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package email
import (
"testing"
"bytes"
"crypto/rand"
"io"
"io/ioutil"
"mime"
"mime/multipart"
"net/mail"
"net/smtp"
)
func TestEmailTextHtmlAttachment(t *testing.T) {
e := NewEmail()
e.From = "Jordan Wright <[email protected]>"
e.To = []string{"[email protected]"}
e.Bcc = []string{"[email protected]"}
e.Cc = []string{"[email protected]"}
e.Subject = "Awesome Subject"
e.Text = []byte("Text Body is, of course, supported!\n")
e.HTML = []byte("<h1>Fancy Html is supported, too!</h1>\n")
e.Attach(bytes.NewBufferString("Rad attachement"), "rad.txt", "text/plain; charset=utf-8")
raw, err := e.Bytes()
if err != nil {
t.Fatal("Failed to render message: ", e)
}
msg, err := mail.ReadMessage(bytes.NewBuffer(raw))
if err != nil {
t.Fatal("Could not parse rendered message: ", err)
}
expectedHeaders := map[string]string{
"To": "[email protected]",
"From": "Jordan Wright <[email protected]>",
"Cc": "[email protected]",
"Subject": "Awesome Subject",
}
for header, expected := range expectedHeaders {
if val := msg.Header.Get(header); val != expected {
t.Errorf("Wrong value for message header %s: %v != %v", header, expected, val)
}
}
// Were the right headers set?
ct := msg.Header.Get("Content-type")
mt, params, err := mime.ParseMediaType(ct)
if err != nil {
t.Fatal("Content-type header is invalid: ", ct)
} else if mt != "multipart/mixed" {
t.Fatalf("Content-type expected \"multipart/mixed\", not %v", mt)
}
b := params["boundary"]
if b == "" {
t.Fatalf("Invalid or missing boundary parameter: ", b)
}
if len(params) != 1 {
t.Fatal("Unexpected content-type parameters")
}
// Is the generated message parsable?
mixed := multipart.NewReader(msg.Body, params["boundary"])
text, err := mixed.NextPart()
if err != nil {
t.Fatalf("Could not find text component of email: ", err)
}
// Does the text portion match what we expect?
mt, params, err = mime.ParseMediaType(text.Header.Get("Content-type"))
if err != nil {
t.Fatal("Could not parse message's Content-Type")
} else if mt != "multipart/alternative" {
t.Fatal("Message missing multipart/alternative")
}
mpReader := multipart.NewReader(text, params["boundary"])
part, err := mpReader.NextPart()
if err != nil {
t.Fatal("Could not read plain text component of message: ", err)
}
plainText, err := ioutil.ReadAll(part)
if err != nil {
t.Fatal("Could not read plain text component of message: ", err)
}
if !bytes.Equal(plainText, []byte("Text Body is, of course, supported!\r\n")) {
t.Fatalf("Plain text is broken: %#q", plainText)
}
// Check attachments.
_, err = mixed.NextPart()
if err != nil {
t.Fatalf("Could not find attachemnt compoenent of email: ", err)
}
if _, err = mixed.NextPart(); err != io.EOF {
t.Error("Expected only text and one attachement!")
}
}
func ExampleGmail() {
e := NewEmail()
e.From = "Jordan Wright <[email protected]>"
e.To = []string{"[email protected]"}
e.Bcc = []string{"[email protected]"}
e.Cc = []string{"[email protected]"}
e.Subject = "Awesome Subject"
e.Text = []byte("Text Body is, of course, supported!\n")
e.HTML = []byte("<h1>Fancy Html is supported, too!</h1>\n")
e.Send("smtp.gmail.com:587", smtp.PlainAuth("", e.From, "password123", "smtp.gmail.com"))
}
func ExampleAttach() {
e := NewEmail()
e.AttachFile("test.txt")
}
func Test_base64Wrap(t *testing.T) {
file := "I'm a file long enough to force the function to wrap a\n" +
"couple of lines, but I stop short of the end of one line and\n" +
"have some padding dangling at the end."
encoded := "SSdtIGEgZmlsZSBsb25nIGVub3VnaCB0byBmb3JjZSB0aGUgZnVuY3Rpb24gdG8gd3JhcCBhCmNv\r\n" +
"dXBsZSBvZiBsaW5lcywgYnV0IEkgc3RvcCBzaG9ydCBvZiB0aGUgZW5kIG9mIG9uZSBsaW5lIGFu\r\n" +
"ZApoYXZlIHNvbWUgcGFkZGluZyBkYW5nbGluZyBhdCB0aGUgZW5kLg==\r\n"
var buf bytes.Buffer
base64Wrap(&buf, []byte(file))
if !bytes.Equal(buf.Bytes(), []byte(encoded)) {
t.Fatalf("Encoded file does not match expected: %#q != %#q", string(buf.Bytes()), encoded)
}
}
func Test_quotedPrintEncode(t *testing.T) {
var buf bytes.Buffer
text := []byte("Dear reader!\n\n" +
"This is a test email to try and capture some of the corner cases that exist within\n" +
"the quoted-printable encoding.\n" +
"There are some wacky parts like =, and this input assumes UNIX line breaks so\r\n" +
"it can come out a little weird. Also, we need to support unicode so here's a fish: 🐟\n")
expected := []byte("Dear reader!\r\n\r\n" +
"This is a test email to try and capture some of the corner cases that exist=\r\n" +
" within\r\n" +
"the quoted-printable encoding.\r\n" +
"There are some wacky parts like =3D, and this input assumes UNIX line break=\r\n" +
"s so=0D\r\n" +
"it can come out a little weird. Also, we need to support unicode so here's=\r\n" +
" a fish: =F0=9F=90=9F\r\n")
if err := quotePrintEncode(&buf, text); err != nil {
t.Fatal("quotePrintEncode: ", err)
}
if b := buf.Bytes(); !bytes.Equal(b, expected) {
t.Errorf("quotedPrintEncode generated incorrect results: %#q != %#q", b, expected)
}
}
func Benchmark_quotedPrintEncode(b *testing.B) {
text := []byte("Dear reader!\n\n" +
"This is a test email to try and capture some of the corner cases that exist within\n" +
"the quoted-printable encoding.\n" +
"There are some wacky parts like =, and this input assumes UNIX line breaks so\r\n" +
"it can come out a little weird. Also, we need to support unicode so here's a fish: 🐟\n")
for i := 0; i <= b.N; i++ {
if err := quotePrintEncode(ioutil.Discard, text); err != nil {
panic(err)
}
}
}
func Benchmark_base64Wrap(b *testing.B) {
// Reasonable base case; 128K random bytes
file := make([]byte, 128*1024)
if _, err := rand.Read(file); err != nil {
panic(err)
}
for i := 0; i <= b.N; i++ {
base64Wrap(ioutil.Discard, file)
}
}