-
Notifications
You must be signed in to change notification settings - Fork 6
/
message_test.go
360 lines (290 loc) · 8.57 KB
/
message_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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package smtpd_test
import (
"mime"
"strings"
"testing"
"net/mail"
"github.com/mailproto/smtpd"
)
const (
plainHTMLEmail = `From: Sender <[email protected]>
Date: Mon, 16 Jan 2017 16:59:33 -0500
Subject: Multipart Message
MIME-Version: 1.0
Content-Type: text/html
To: [email protected], "Recipient 2" <[email protected]>
Message-ID: <[email protected]>
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE html>
<html>
<body>
Sending bees<br><br>=F0=9F=90=9D
</body>
</html>`
alternativeEmail = `From: Sender <[email protected]>
Date: Mon, 16 Jan 2017 16:59:33 -0500
Subject: Multipart Message
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="_=test=_bbd1e98aa6c34ef59d8d102a0e795027"
To: [email protected], "Recipient 2" <[email protected]>
Message-ID: <[email protected]>
--_=test=_bbd1e98aa6c34ef59d8d102a0e795027
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Sending bees
=F0=9F=90=9D
--_=test=_bbd1e98aa6c34ef59d8d102a0e795027
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE html>
<html>
<body>
Sending bees<br><br>=F0=9F=90=9D
</body>
</html>
--_=test=_bbd1e98aa6c34ef59d8d102a0e795027--
`
emailWithAttachment = `From: Sender <[email protected]>
Date: Mon, 16 Jan 2017 16:59:33 -0500
Subject: Multipart Message
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="_=test=_bbd1e98aa6c34ef59d8d102a0e795027"
To: [email protected], "Recipient 2" <[email protected]>
Message-ID: <[email protected]>
--_=test=_bbd1e98aa6c34ef59d8d102a0e795027
Content-Type: multipart/alternative; boundary="_=ALT_=test=_bbd1e98aa6c34ef59d8d102a0e795027"
--_=ALT_=test=_bbd1e98aa6c34ef59d8d102a0e795027
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Sending bees
=F0=9F=90=9D
--_=ALT_=test=_bbd1e98aa6c34ef59d8d102a0e795027
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE html>
<html>
<body>
Sending bees<br><br>=F0=9F=90=9D
</body>
</html>
--_=ALT_=test=_bbd1e98aa6c34ef59d8d102a0e795027--
--_=test=_bbd1e98aa6c34ef59d8d102a0e795027
Content-Type: text/calendar; name="invite.ics"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="invite.ics"
QkVHSU46VkNBTEVOREFSClZFUlNJT046Mi4wClBST0RJRDotLy9tYWlscHJvdG8vL01haWxQcm90bwpDQUxTQ0FMRTpHUkVHT1JJQU4KQkVHSU46VkVWRU5UCkRUU1RBTVA6MjAxNzAxMTZUMTU0MDAwClVJRDpteWNvb2xldmVudEBtYWlscHJvdG8KCkRUU1RBUlQ7VFpJRD0iQW1lcmljYS9OZXdfWW9yayI6MjAxNzAxMThUMTEwMDAwCkRURU5EO1RaSUQ9IkFtZXJpY2EvTmV3X1lvcmsiOjIwMTcwMTE4VDEyMDAwMApTVU1NQVJZOlNlbmQgYW4gZW1haWwKTE9DQVRJT046VGVzdApFTkQ6VkVWRU5UCkVORDpWQ0FMRU5EQVI=
--_=test=_bbd1e98aa6c34ef59d8d102a0e795027--`
emailWithInvalidBody = `From: Sender <[email protected]>
Date: Mon, 16 Jan 2017 16:59:33 -0500
Subject: Invalid Body Message
MIME-Version: 1.0
Content-Type: text/html
To: [email protected], "Recipient 2" <[email protected]>
Message-ID: <[email protected]>
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE html>
<html>
<body>
Sending bees<br><br>=FG=XX==
</body>
</html>`
)
func TestPlainHTMLParsing(t *testing.T) {
msg, err := smtpd.NewMessage([]byte(plainHTMLEmail), nil, nil)
if err != nil {
t.Error("error creating message", err)
return
}
expectTo := []mail.Address{
{
Name: "",
Address: "[email protected]",
},
{
Name: "Recipient 2",
Address: "[email protected]",
},
}
if len(msg.To) < len(expectTo) {
t.Errorf("Not enough recipients, want: %v, got: %v", len(expectTo), len(msg.To))
}
for i, expect := range expectTo {
if i >= len(msg.To) {
break
}
if msg.To[i].Address != expect.Address || msg.To[i].Name != expect.Name {
t.Errorf("Wrong recipient %v want: %v, got: %v", i, expect, msg.To[i])
}
}
expectHTML := `<!DOCTYPE html>
<html>
<body>
Sending bees<br><br>🐝
</body>
</html>`
if html, err := msg.HTML(); err != nil {
t.Error(err)
} else if strings.TrimSpace(string(html)) != expectHTML {
t.Errorf("Wrong HTML content, want: %v, got: %v", expectHTML, strings.TrimSpace(string(html)))
}
if plain, err := msg.Plain(); err == nil {
t.Error("Expected plaintext version to be missing, got:", plain)
}
}
func TestAlternativeMessageParsing(t *testing.T) {
msg, err := smtpd.NewMessage([]byte(alternativeEmail), nil, nil)
if err != nil {
t.Error("error creating message", err)
return
}
expectTo := []mail.Address{
{
Name: "",
Address: "[email protected]",
},
{
Name: "Recipient 2",
Address: "[email protected]",
},
}
if len(msg.To) < len(expectTo) {
t.Errorf("Not enough recipients, want: %v, got: %v", len(expectTo), len(msg.To))
}
for i, expect := range expectTo {
if i >= len(msg.To) {
break
}
if msg.To[i].Address != expect.Address || msg.To[i].Name != expect.Name {
t.Errorf("Wrong recipient %v want: %v, got: %v", i, expect, msg.To[i])
}
}
expectHTML := `<!DOCTYPE html>
<html>
<body>
Sending bees<br><br>🐝
</body>
</html>`
if html, err := msg.HTML(); err != nil {
t.Error(err)
} else if strings.TrimSpace(string(html)) != expectHTML {
t.Errorf("Wrong HTML content, want: %v, got: %v", expectHTML, strings.TrimSpace(string(html)))
}
expectPlain := `Sending bees
🐝`
if plain, err := msg.Plain(); err != nil {
t.Error(err)
} else if strings.TrimSpace(string(plain)) != expectPlain {
t.Errorf("Wrong Plaintext content, want: %v, got: %v", expectPlain, strings.TrimSpace(string(plain)))
}
}
func TestMixedMessageParsing(t *testing.T) {
msg, err := smtpd.NewMessage([]byte(emailWithAttachment), nil, nil)
if err != nil {
t.Error("error creating message", err)
return
}
expectTo := []mail.Address{
{
Name: "",
Address: "[email protected]",
},
{
Name: "Recipient 2",
Address: "[email protected]",
},
}
if len(msg.To) < len(expectTo) {
t.Errorf("Not enough recipients, want: %v, got: %v", len(expectTo), len(msg.To))
}
for i, expect := range expectTo {
if i >= len(msg.To) {
break
}
if msg.To[i].Address != expect.Address || msg.To[i].Name != expect.Name {
t.Errorf("Wrong recipient %v want: %v, got: %v", i, expect, msg.To[i])
}
}
expectHTML := `<!DOCTYPE html>
<html>
<body>
Sending bees<br><br>🐝
</body>
</html>`
if html, err := msg.HTML(); err != nil {
t.Error(err)
} else if strings.TrimSpace(string(html)) != expectHTML {
t.Errorf("Wrong HTML content, want: %v, got: %v", expectHTML, strings.TrimSpace(string(html)))
}
expectPlain := `Sending bees
🐝`
if plain, err := msg.Plain(); err != nil {
t.Error(err)
} else if strings.TrimSpace(string(plain)) != expectPlain {
t.Errorf("Wrong Plaintext content, want: %v, got: %v", expectPlain, strings.TrimSpace(string(plain)))
}
// TODO: check rest of parse proceeded as expected
var attachments []*smtpd.Part
if attachments, err = msg.Attachments(); err != nil {
t.Error("couldn't load attachments", err)
}
if len(attachments) != 1 {
t.Errorf("want one attachment, got: %v", len(attachments))
}
mimeType, _, err := mime.ParseMediaType(attachments[0].Header.Get("Content-Type"))
if err != nil {
t.Error("Error parsing attachment MIME header:", err)
}
if mimeType != "text/calendar" {
t.Errorf("Expected text/calendar attachment, got: %v", mimeType)
}
expectVCal := `BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//mailproto//MailProto
CALSCALE:GREGORIAN
BEGIN:VEVENT
DTSTAMP:20170116T154000
UID:mycoolevent@mailproto
DTSTART;TZID="America/New_York":20170118T110000
DTEND;TZID="America/New_York":20170118T120000
SUMMARY:Send an email
LOCATION:Test
END:VEVENT
END:VCALENDAR`
if string(attachments[0].Body) != expectVCal {
t.Errorf("Wrong attachment, wanted: %v got: %v", expectVCal, string(attachments[0].Body))
}
}
func TestInvalidEmailBodyStillPassesToHandler(t *testing.T) {
msg, err := smtpd.NewMessage([]byte(emailWithInvalidBody), nil, nil)
if err != nil {
t.Error("error creating message", err)
return
}
expectTo := []mail.Address{
{
Name: "",
Address: "[email protected]",
},
{
Name: "Recipient 2",
Address: "[email protected]",
},
}
if len(msg.To) < len(expectTo) {
t.Errorf("Not enough recipients, want: %v, got: %v", len(expectTo), len(msg.To))
}
for i, expect := range expectTo {
if i >= len(msg.To) {
break
}
if msg.To[i].Address != expect.Address || msg.To[i].Name != expect.Name {
t.Errorf("Wrong recipient %v want: %v, got: %v", i, expect, msg.To[i])
}
}
_, err = msg.Parts()
if err == nil {
t.Error("Expected parts parsing to fail due to invalid body")
}
}