forked from sendgrid/sendgrid-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail_test.go
327 lines (291 loc) · 7.37 KB
/
mail_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
package sendgrid
import (
"fmt"
"net/http"
"net/http/httptest"
"net/mail"
"testing"
"time"
)
func TestNewMail(t *testing.T) {
m := NewMail()
if m == nil {
t.Errorf("NewMail() shouldn't return nil")
}
}
func TestAddTo(t *testing.T) {
m := NewMail()
m.AddTo("Email Name<email@email.com>")
switch {
case len(m.To) != 1:
t.Errorf("AddTo should append to SGMail.To")
case len(m.ToName) != 1:
t.Errorf("AddTo should append to SGMail.ToName on a valid email")
}
}
func TestAddToFail(t *testing.T) {
m := NewMail()
err := m.AddTo(".com")
if err == nil {
t.Errorf("AddTo should fail on invalid email addresses")
}
}
func TestAddTos(t *testing.T) {
m := NewMail()
m.AddTos([]string{"Email Name <email+1@email.com>", "email+2@email.com"})
switch {
case len(m.To) != 2:
t.Errorf("AddTos should append to SGMail.To")
case len(m.ToName) != 1:
t.Errorf("AddTos should append to SGMail.ToName if a valid email was supplied")
}
}
func TestAddTosFail(t *testing.T) {
m := NewMail()
err := m.AddTos([]string{".co", "email+2@email.com"})
if err == nil {
t.Errorf("AddTos should fail in invalid email address")
}
}
func TestAddRecipients(t *testing.T) {
m := NewMail()
emails, _ := mail.ParseAddressList("Joe <email+1@email.com>, Doe <email+2@email.com>")
m.AddRecipients(emails)
switch {
case len(m.To) != 2:
t.Errorf("AddRecipients should append to SGMail.To")
case len(m.ToName) != 2:
t.Errorf("AddRecipients should append to SGMail.ToName if a valid email is supplied")
}
}
func TestAddToName(t *testing.T) {
m := NewMail()
m.AddToName("Name")
if len(m.ToName) != 1 {
t.Errorf("AddToName should append to SG.ToName")
}
}
func TestAddToNames(t *testing.T) {
m := NewMail()
m.AddToNames([]string{"Name", "Name2"})
if len(m.ToName) != 2 {
t.Errorf("AddToNames should append to SG.ToName")
}
}
func TestAddCc(t *testing.T) {
m := NewMail()
m.AddCc("Email Name<email@email.com>")
if len(m.Cc) != 1 {
t.Errorf("AddCc should append to SGMail.Cc")
}
}
func TestAddCcFail(t *testing.T) {
m := NewMail()
err := m.AddCc(".com")
if err == nil {
t.Errorf("AddCc should fail on invalid email addresses")
}
}
func TestAddCcs(t *testing.T) {
m := NewMail()
m.AddCcs([]string{"Email Name <email+1@email.com>", "email+2@email.com"})
if len(m.Cc) != 2 {
t.Errorf("AddCcs should append to SGMail.Cc")
}
}
func TestAddCcsFail(t *testing.T) {
m := NewMail()
err := m.AddCcs([]string{".co", "email+2@email.com"})
if err == nil {
t.Errorf("AddCcs should fail in invalid email address")
}
}
func TestAddCcRecipients(t *testing.T) {
m := NewMail()
emails, _ := mail.ParseAddressList("Joe <email+1@email.com>, Doe <email+2@email.com>")
m.AddCcRecipients(emails)
if len(m.Cc) != 2 {
t.Errorf("AddCcRecipients should append to SGMail.Cc")
}
}
func TestSetSubject(t *testing.T) {
m := NewMail()
testSubject := "Subject"
m.SetSubject(testSubject)
if m.Subject != testSubject {
t.Errorf("SetSubject should modify SGMail.Subject")
}
}
func TestSetText(t *testing.T) {
m := NewMail()
testText := "Text"
m.SetText(testText)
if m.Text != testText {
t.Errorf("SetText should modify SGMail.Text")
}
}
func TestSetHTML(t *testing.T) {
m := NewMail()
testHTML := "<html></html>"
m.SetHTML(testHTML)
if m.HTML != testHTML {
t.Errorf("SetHTML should modify SGMail.HTML")
}
}
func TestSetFrom(t *testing.T) {
m := NewMail()
testFrom, _ := mail.ParseAddress("Joe <email@email.com>")
m.SetFrom(testFrom.String())
switch {
case m.From != testFrom.Address:
t.Errorf("SetFrom should modify SGMail.From")
case m.FromName != testFrom.Name:
t.Errorf("SetFromName should modify SGMail.FromName")
}
}
func TestSetFromFail(t *testing.T) {
m := NewMail()
testFrom := ".com"
err := m.SetFrom(testFrom)
if err == nil {
t.Errorf("SetFrom should fail if an invalid email address is provided")
}
}
func TestAddBcc(t *testing.T) {
m := NewMail()
m.AddBcc("Email Name<email@email.com>")
if len(m.Bcc) != 1 {
t.Errorf("AddBcc should append to SGMail.Bcc")
}
}
func TestAddBccFail(t *testing.T) {
m := NewMail()
err := m.AddBcc(".com")
if err == nil {
t.Errorf("AddBcc should fail on invalid email addresses")
}
}
func TestAddBccs(t *testing.T) {
m := NewMail()
m.AddBccs([]string{"Email Name <email+1@email.com>", "email+2@email.com"})
if len(m.Bcc) != 2 {
t.Errorf("AddBccs should append to SGMail.Bcc")
}
}
func TestAddBccsFail(t *testing.T) {
m := NewMail()
err := m.AddBccs([]string{".co", "email+2@email.com"})
if err == nil {
t.Errorf("AddBccs should fail in invalid email address")
}
}
func TestAddBccRecipients(t *testing.T) {
m := NewMail()
emails, _ := mail.ParseAddressList("Joe <email+1@email.com>, Doe <email+2@email.com>")
m.AddBccRecipients(emails)
if len(m.Bcc) != 2 {
t.Errorf("AddBccRecipients should append to SGMail.Bcc")
}
}
func TestSetFromName(t *testing.T) {
m := NewMail()
testFromName := "Joe"
m.SetFromName(testFromName)
if m.FromName != testFromName {
t.Errorf("SetFromName should modify SGMail.FromName")
}
}
func TestSetReplyTo(t *testing.T) {
m := NewMail()
testReplyTo := "email@email.com"
m.SetReplyTo(testReplyTo)
if m.ReplyTo != testReplyTo {
t.Errorf("SetReplyTo should modify SGMail.ReplyTo")
}
}
func TestSetReplyToFail(t *testing.T) {
m := NewMail()
testReplyTo := ".com"
err := m.SetReplyTo(testReplyTo)
if err == nil {
t.Errorf("SetReplyTo should fail with an invalid address")
}
}
func TestSetDate(t *testing.T) {
m := NewMail()
date := "Today"
m.SetDate(date)
if m.Date != date {
t.Errorf("SetDate should modify SGMail.Date")
}
}
func TestSetRFCDate(t *testing.T) {
m := NewMail()
date := time.Now()
m.SetRFCDate(date)
if m.Date == "" {
t.Errorf("SetDate should fail if date is invalid RFC822")
}
}
func TestAddAttachment(t *testing.T) {
m := NewMail()
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "THIS IS A TEST!")
}))
defer fakeServer.Close()
res, err := http.Get(fakeServer.URL)
if err != nil {
t.Errorf("Fake server could'nt be reached")
}
defer res.Body.Close()
err = m.AddAttachment("Test", res.Body)
if _, ok := m.Files["Test"]; !ok {
t.Errorf("Attachment not added")
}
}
func TestAddAttachmentFail(t *testing.T) {
m := NewMail()
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "THIS IS A TEST!")
}))
defer fakeServer.Close()
res, err := http.Get(fakeServer.URL)
res.Body.Close()
if err != nil {
t.Errorf("Fake server could'nt be reached")
}
err = m.AddAttachment("Test", res.Body)
if _, ok := m.Files["Test"]; ok {
t.Errorf("Attachment should not be added")
}
}
func TestAddContentIds(t *testing.T) {
m := NewMail()
id, value := "id", "im a value"
m.AddContentID(id, value)
if val, ok := m.Content[id]; !ok && val != value {
t.Errorf("ContentID failed to be added")
}
}
func TestAddHeaders(t *testing.T) {
m := NewMail()
header, value := "id", "im a value"
m.AddHeader(header, value)
if val, ok := m.Headers[header]; !ok && val != value {
t.Errorf("Header failed to be added")
}
}
func TestHeaderString(t *testing.T) {
m := NewMail()
m.AddHeader("Cc", "hello@test.com")
if _, err := m.HeadersString(); err != nil {
t.Errorf("Error parsing headers: %v", err)
}
}
func TestAddToSMTPAPI(t *testing.T) {
m := NewMail()
m.SMTPAPIHeader.AddTo("example@email.com")
if len(m.SMTPAPIHeader.To) != 1 {
t.Error("SMTPAPIHeader To should be len of 1")
}
}