forked from jpoehls/gophermail
/
main_test.go
288 lines (230 loc) · 8.97 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
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
package gophermail
import (
"bufio"
"bytes"
"encoding/base64"
"fmt"
. "github.com/onsi/gomega"
"io"
"io/ioutil"
"mime"
"mime/multipart"
"net/mail"
"net/textproto"
"strings"
"testing"
"time"
)
// registerFailHandler registers a gomega fail handler that calls t.Fatal
// gomega.RegisterTestingT calls t.Error, which does not stop the test
func registerFailHandler(t *testing.T) {
RegisterFailHandler(func(message string, callerSkip ...int) {
t.Fatalf("\n%s", message)
})
}
// expectNoError fails the test if err is not nil
func expectNoError(err error) {
Expect(err).To(BeNil(), fmt.Sprintf("%v", err))
}
// getContentType gets the content type in a header, and parses it
func getContentType(header textproto.MIMEHeader) (string, map[string]string) {
contentType, ok := header["Content-Type"]
Expect(ok).To(BeTrue(), "Content-Type header not found")
Expect(contentType).NotTo(BeEmpty(), "Content-Type header is empty")
Expect(contentType).To(HaveLen(1), "More than one Content-Type header found")
mediaType, params, err := mime.ParseMediaType(contentType[0])
expectNoError(err)
return mediaType, params
}
func matchBase64(r io.Reader, expected string, msg string) {
base64contents, err := ioutil.ReadAll(r)
expectNoError(err)
contents, err := base64.StdEncoding.DecodeString(string(base64contents))
expectNoError(err)
Expect(string(contents)).To(Equal(expected), msg)
}
// testMail is the main testing function
func testMail(t *testing.T, plain, html, attachment bool) {
registerFailHandler(t)
// NOTE: QP decoding cuts off trailing whitespace
plainBody := "My Plain Text Body áűőú\n Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n Nunc et purus massa. Maecenas sed ex iaculis, feugiat elit ullamcorper, eleifend elit. Aliquam ultricies libero vitae interdum maximus. Nullam placerat purus dolor, a tempor magna efficitur in. Integer mattis, lacus tempus mattis rutrum, tellus velit ultricies nisl, a elementum dolor nisi sed diam."
htmlBody := "<p>My <b>HTML</b> Body</p>\n<p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc et purus massa.</p>"
filename := "test.txt"
fileContents := "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc et purus massa. Aenean sed enim turpis. Maecenas sed ex iaculis, feugiat elit ullamcorper, eleifend elit. Aliquam ultricies libero vitae interdum maximus. Nullam placerat purus dolor, a tempor magna efficitur in. Integer mattis, lacus tempus mattis rutrum, tellus velit ultricies nisl, a elementum dolor nisi sed diam. Nunc cursus arcu quis sapien dapibus suscipit. Aliquam in dolor ut enim faucibus volutpat vel id ipsum. Aenean blandit ipsum eu bibendum fermentum. Donec sagittis nunc dolor, in bibendum lorem pulvinar et. Nam elementum auctor tempor. Nunc et nisl diam. Pellentesque eget suscipit leo. Sed lacus urna, semper nec tellus a, finibus aliquet odio. Nulla in finibus justo, non congue dui."
m := &Message{}
m.SetFrom("Doman Sender <sender@domain.com>")
m.AddTo("First person <to_1@domain.com>")
m.Subject = "My Subject (abcdefghijklmnop qrstuvwxyz0123456789 abcdefghijklmnopqrstuvwxyz0123456789_567890)"
if plain {
m.Body = plainBody
}
if html {
m.HTMLBody = htmlBody
}
if attachment {
m.Attachments = []Attachment{Attachment{
Name: filename,
ContentType: "text/plain",
Data: strings.NewReader(fileContents),
}}
}
m.Headers = mail.Header{}
m.Headers["Date"] = []string{time.Now().UTC().Format(time.RFC822)}
b, err := m.Bytes()
expectNoError(err)
t.Logf("Bytes: \n%s", b)
byteReader := bytes.NewReader(b)
bufReader := bufio.NewReader(byteReader)
headerReader := textproto.NewReader(bufReader)
header, err := headerReader.ReadMIMEHeader()
expectNoError(err)
mediaType, params := getContentType(header)
if !attachment && !(plain && html) {
if html {
Expect(mediaType).To(Equal("text/html"), "Content-Type is not text/html")
} else {
Expect(mediaType).To(Equal("text/plain"), "Content-Type is not text/plain")
}
return
}
if attachment {
Expect(mediaType).To(Equal("multipart/mixed"), "Content-Type is not multipart/mixed")
} else {
Expect(mediaType).To(Equal("multipart/alternative"), "Content-Type is not multipart/alternative")
}
Expect(params).To(HaveKey("boundary"), "boundary is missing from Content-Type")
boundary := params["boundary"]
plainFound := false
htmlFound := false
attachmentFound := false
var readParts func(io.Reader, string, bool)
readParts = func(r io.Reader, boundary string, toplevel bool) {
multipartReader := multipart.NewReader(r, boundary)
for {
part, err := multipartReader.NextPart()
if err == io.EOF {
break
}
expectNoError(err)
t.Logf("part: %v", part)
mediaType, params := getContentType(part.Header)
dispositions, ok := part.Header["Content-Disposition"]
if ok && len(dispositions) == 1 {
attachmentMediaType, attachmentParams, err := mime.ParseMediaType(dispositions[0])
expectNoError(err)
Expect(attachmentMediaType).To(Equal("attachment"), "attachment media type is not \"attachment\"")
Expect(attachmentParams).To(HaveKey("filename"), "filename missing from attachment")
Expect(attachmentParams["filename"]).To(Equal(filename), "filename is incorrect")
Expect(toplevel).To(BeTrue(), "attachment found in multipart/alternative")
matchBase64(part, fileContents, "attachment does not match")
attachmentFound = true
} else {
switch mediaType {
case "text/plain":
rawContents, err := ioutil.ReadAll(part)
expectNoError(err)
contents := strings.Replace(string(rawContents), "\r\n", "\n", -1)
t.Logf("\n\n%#v\n\n%#v\n\n", contents, plainBody)
expectedBody := plainBody
if !plain {
expectedBody = ""
}
Expect(contents).To(Equal(expectedBody), "plain text body does not match")
plainFound = true
case "text/html":
matchBase64(part, htmlBody, "html body does not match")
htmlFound = true
case "multipart/alternative":
Expect(params).To(HaveKey("boundary"), "boundary is missing from Content-Type")
boundary := params["boundary"]
readParts(part, boundary, false)
default:
t.Logf("unexpected media type: %v", mediaType)
}
}
}
}
readParts(bufReader, boundary, true)
if plain || !plain && !html {
Expect(plainFound).To(BeTrue(), "plain text body not found")
} else {
Expect(plainFound).NotTo(BeTrue(), "plain text body found")
}
if html {
Expect(htmlFound).To(BeTrue(), "html text body not found")
} else {
Expect(htmlFound).NotTo(BeTrue(), "html text body found")
}
if attachment {
Expect(attachmentFound).To(BeTrue(), "attachment not found")
} else {
Expect(attachmentFound).NotTo(BeTrue(), "attachment found")
}
}
func TestPlainBody(t *testing.T) {
testMail(t, true, false, false)
}
func TestHtmlBody(t *testing.T) {
testMail(t, false, true, false)
}
func TestAlternativeBody(t *testing.T) {
testMail(t, true, true, false)
}
func TestPlainAttachment(t *testing.T) {
testMail(t, true, false, true)
}
func TestHtmlPlainAttachment(t *testing.T) {
testMail(t, true, true, true)
}
func TestNoBody(t *testing.T) {
testMail(t, false, false, false)
}
func TestNoBodyAttachment(t *testing.T) {
testMail(t, false, false, true)
}
func TestAutoDate(t *testing.T) {
startTime := time.Now()
m := &Message{}
m.SetFrom("Doman Sender <sender@domain.com>")
m.AddTo("First person <to_1@domain.com>")
m.Body = "Test message"
b, err := m.Bytes()
expectNoError(err)
t.Logf("Bytes: \n%s", b)
byteReader := bytes.NewReader(b)
bufReader := bufio.NewReader(byteReader)
headerReader := textproto.NewReader(bufReader)
header, err := headerReader.ReadMIMEHeader()
expectNoError(err)
dates, ok := header["Date"]
Expect(ok).To(BeTrue(), "Date header not found")
Expect(dates).NotTo(BeEmpty(), "Date header is empty")
Expect(dates).To(HaveLen(1), "More than one Date header found")
parsedTime, err := time.Parse(time.RFC822, dates[0])
t.Logf("%v", parsedTime)
Expect(parsedTime.Before(time.Now().Add(1*time.Minute))).To(BeTrue(), "Time in Date header is too low")
Expect(parsedTime.After(startTime.Add(-1*time.Minute))).To(BeTrue(), "Time in Date header is too high")
}
func TestManualDate(t *testing.T) {
msgTime := time.Now().Add(-30 * time.Minute)
m := &Message{}
m.SetFrom("Doman Sender <sender@domain.com>")
m.AddTo("First person <to_1@domain.com>")
m.Headers = mail.Header{}
m.Headers["Date"] = []string{msgTime.Format(time.RFC822)}
m.Body = "Test message"
b, err := m.Bytes()
expectNoError(err)
t.Logf("Bytes: \n%s", b)
byteReader := bytes.NewReader(b)
bufReader := bufio.NewReader(byteReader)
headerReader := textproto.NewReader(bufReader)
header, err := headerReader.ReadMIMEHeader()
expectNoError(err)
dates, ok := header["Date"]
Expect(ok).To(BeTrue(), "Date header not found")
Expect(dates).NotTo(BeEmpty(), "Date header is empty")
Expect(dates).To(HaveLen(1), "More than one Date header found")
parsedTime, err := time.Parse(time.RFC822, dates[0])
Expect(parsedTime.Equal(msgTime.Truncate(time.Minute))).To(BeTrue(), "Time in Date header is not what we specified")
}