Debian Codegraph @

mime_test.go

raw symbols SCIP
1 package mailyakscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/
2
3 import (
4 "bytesscip-go gomod github.com/golang/go/src go1.12 bytes/"
5 "fmtscip-go gomod github.com/golang/go/src go1.12 fmt/"
6 "ioscip-go gomod github.com/golang/go/src go1.12 io/"
7 "io/ioutilscip-go gomod github.com/golang/go/src go1.12 `io/ioutil`/"
8 "mimescip-go gomod github.com/golang/go/src go1.12 mime/"
9 "mime/multipartscip-go gomod github.com/golang/go/src go1.12 `mime/multipart`/"
10 "net/mailscip-go gomod github.com/golang/go/src go1.12 `net/mail`/"
11 "regexpscip-go gomod github.com/golang/go/src go1.12 regexp/"
12 "stringsscip-go gomod github.com/golang/go/src go1.12 strings/"
13 "testingscip-go gomod github.com/golang/go/src go1.12 testing/"
14 "timescip-go gomod github.com/golang/go/src go1.12 time/"
15 )
16
17 // TestMailYakFromHeader ensures the fromHeader method returns valid headers
18 func TestMailYakFromHeaderscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/TestMailYakFromHeader().(t *testingscip-go gomod github.com/golang/go/src go1.12 testing/.Tscip-go gomod github.com/golang/go/src go1.12 testing/T#) {
19 t.Parallelscip-go gomod github.com/golang/go/src go1.12 testing/T#Parallel().()
20
21 tests := []struct {
22 // Test description.
23 name string
24 // Receiver fields.
25 rfromAddr string
26 rfromName string
27 // Expected results.
28 want string
29 }{
30 {
31 "With name",
32 "[email protected]",
33 "Dom",
34 "From: Dom <[email protected]>\r\n",
35 },
36 {
37 "Without name",
38 "[email protected]",
39 "",
40 "From: [email protected]\r\n",
41 },
42 {
43 "Without either",
44 "",
45 "",
46 "From: \r\n",
47 },
48 }
49 for _, tt := range tests {
50 tt := tt
51 t.Runscip-go gomod github.com/golang/go/src go1.12 testing/T#Run().(tt.name, func(t *testingscip-go gomod github.com/golang/go/src go1.12 testing/.Tscip-go gomod github.com/golang/go/src go1.12 testing/T#) {
52 t.Parallelscip-go gomod github.com/golang/go/src go1.12 testing/T#Parallel().()
53
54 m := MailYakscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#{
55 fromAddrscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#fromAddr.: tt.rfromAddr,
56 fromNamescip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#fromName.: tt.rfromName,
57 }
58
59 if got := m.fromHeaderscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#fromHeader().(); got != tt.want {
60 t.Errorfscip-go gomod github.com/golang/go/src go1.12 testing/common#Errorf().("%q. MailYak.fromHeader() = %v, want %v", tt.name, got, tt.want)
61 }
62 })
63 }
64 }
65
66 // TestMailYakWriteHeaders ensures the MIME-Version, Date, Reply-To, From, To and
67 // Subject headers are correctly wrote
68 func TestMailYakWriteHeadersscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/TestMailYakWriteHeaders().(t *testingscip-go gomod github.com/golang/go/src go1.12 testing/.Tscip-go gomod github.com/golang/go/src go1.12 testing/T#) {
69 t.Parallelscip-go gomod github.com/golang/go/src go1.12 testing/T#Parallel().()
70
71 now := timescip-go gomod github.com/golang/go/src go1.12 time/.Nowscip-go gomod github.com/golang/go/src go1.12 time/Now().().Formatscip-go gomod github.com/golang/go/src go1.12 time/Time#Format().(timescip-go gomod github.com/golang/go/src go1.12 time/.RFC1123Zscip-go gomod github.com/golang/go/src go1.12 time/RFC1123Z.)
72 tests := []struct {
73 // Test description.
74 name string
75 // Receiver fields.
76 rtoAddrs []string
77 rccAddrs []string
78 rbccAddrs []string
79 rsubject string
80 rreplyTo string
81 rwriteBccHeader bool
82 // Expected results.
83 wantBuf string
84 }{
85 {
86 "All fields",
87 []string{"[email protected]"},
88 []string{},
89 []string{},
90 "Test",
91 "[email protected]",
92 true,
93 "From: Dom <[email protected]>\r\nMIME-Version: 1.0\r\nDate: " + now + "\r\nReply-To: [email protected]\r\nSubject: Test\r\nTo: [email protected]\r\n",
94 },
95 {
96 "No reply-to",
97 []string{"[email protected]"},
98 []string{},
99 []string{},
100 "",
101 "",
102 true,
103 "From: Dom <[email protected]>\r\nMIME-Version: 1.0\r\nDate: " + now + "\r\nSubject: \r\nTo: [email protected]\r\n",
104 },
105 {
106 "Multiple To addresses",
107 []string{"[email protected]", "[email protected]"},
108 []string{},
109 []string{},
110 "",
111 "",
112 true,
113 "From: Dom <[email protected]>\r\nMIME-Version: 1.0\r\nDate: " + now + "\r\nSubject: \r\nTo: [email protected],[email protected]\r\n",
114 },
115 {
116 "Single Cc address, Multiple To addresses",
117 []string{"[email protected]", "[email protected]"},
118 []string{"[email protected]"},
119 []string{},
120 "",
121 "",
122 true,
123 "From: Dom <[email protected]>\r\nMIME-Version: 1.0\r\nDate: " + now + "\r\nSubject: \r\nTo: [email protected],[email protected]\r\nCC: [email protected]\r\n",
124 },
125 {
126 "Multiple Cc addresses, Multiple To addresses",
127 []string{"[email protected]", "[email protected]"},
128 []string{"[email protected]", "[email protected]"},
129 []string{},
130 "",
131 "",
132 true,
133 "From: Dom <[email protected]>\r\nMIME-Version: 1.0\r\nDate: " + now + "\r\nSubject: \r\nTo: [email protected],[email protected]\r\nCC: [email protected],[email protected]\r\n",
134 },
135 {
136 "Single Bcc address, Multiple To addresses",
137 []string{"[email protected]", "[email protected]"},
138 []string{},
139 []string{"[email protected]"},
140 "",
141 "",
142 true,
143 "From: Dom <[email protected]>\r\nMIME-Version: 1.0\r\nDate: " + now + "\r\nSubject: \r\nTo: [email protected],[email protected]\r\nBCC: [email protected]\r\n",
144 },
145 {
146 "Multiple Bcc addresses, Multiple To addresses",
147 []string{"[email protected]", "[email protected]"},
148 []string{},
149 []string{"[email protected]", "[email protected]"},
150 "",
151 "",
152 true,
153 "From: Dom <[email protected]>\r\nMIME-Version: 1.0\r\nDate: " + now + "\r\nSubject: \r\nTo: [email protected],[email protected]\r\nBCC: [email protected],[email protected]\r\n",
154 },
155 {
156 "Multiple Bcc addresses, Multiple To addresses",
157 []string{"[email protected]", "[email protected]"},
158 []string{},
159 []string{"[email protected]", "[email protected]"},
160 "",
161 "",
162 false,
163 "From: Dom <[email protected]>\r\nMIME-Version: 1.0\r\nDate: " + now + "\r\nSubject: \r\nTo: [email protected],[email protected]\r\n",
164 },
165 {
166 "All together now",
167 []string{"[email protected]", "[email protected]"},
168 []string{"[email protected]", "[email protected]"},
169 []string{"[email protected]", "[email protected]"},
170 "",
171 "",
172 true,
173 "From: Dom <[email protected]>\r\nMIME-Version: 1.0\r\nDate: " + now + "\r\nSubject: \r\nTo: [email protected],[email protected]\r\nCC: [email protected],[email protected]\r\nBCC: [email protected],[email protected]\r\n",
174 },
175 }
176 for _, tt := range tests {
177 tt := tt
178 t.Runscip-go gomod github.com/golang/go/src go1.12 testing/T#Run().(tt.name, func(t *testingscip-go gomod github.com/golang/go/src go1.12 testing/.Tscip-go gomod github.com/golang/go/src go1.12 testing/T#) {
179 t.Parallelscip-go gomod github.com/golang/go/src go1.12 testing/T#Parallel().()
180
181 m := MailYakscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#{
182 toAddrsscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#toAddrs.: tt.rtoAddrs,
183 subjectscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#subject.: tt.rsubject,
184 fromAddrscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#fromAddr.: "[email protected]",
185 fromNamescip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#fromName.: "Dom",
186 replyToscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#replyTo.: tt.rreplyTo,
187 ccAddrsscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#ccAddrs.: tt.rccAddrs,
188 bccAddrsscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#bccAddrs.: tt.rbccAddrs,
189 writeBccHeaderscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#writeBccHeader.: tt.rwriteBccHeader,
190 datescip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#date.: now,
191 }
192
193 buf := &bytesscip-go gomod github.com/golang/go/src go1.12 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.12 bytes/Buffer#{}
194 if err := m.writeHeadersscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#writeHeaders().(buf); err != nil {
195 t.Fatalscip-go gomod github.com/golang/go/src go1.12 testing/common#Fatal().(err)
196 }
197
198 if gotBuf := buf.Stringscip-go gomod github.com/golang/go/src go1.12 bytes/Buffer#String().(); gotBuf != tt.wantBuf {
199 t.Errorfscip-go gomod github.com/golang/go/src go1.12 testing/common#Errorf().("%q. MailYak.writeHeaders() = %v, want %v", tt.name, gotBuf, tt.wantBuf)
200 }
201 })
202 }
203 }
204
205 // TestMailYakWriteBody ensures the correct MIME parts are wrote for the body
206 func TestMailYakWriteBodyscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/TestMailYakWriteBody().(t *testingscip-go gomod github.com/golang/go/src go1.12 testing/.Tscip-go gomod github.com/golang/go/src go1.12 testing/T#) {
207 t.Parallelscip-go gomod github.com/golang/go/src go1.12 testing/T#Parallel().()
208
209 tests := []struct {
210 // Test description.
211 name string
212 // Receiver fields.
213 rHTML string
214 rPlain string
215 // Parameters.
216 boundary string
217 // Expected results.
218 wantW string
219 wantErr bool
220 }{
221 {
222 "HTML",
223 "HTML",
224 "",
225 "t",
226 "--t\r\nContent-Transfer-Encoding: quoted-printable\r\nContent-Type: text/html; charset=UTF-8\r\n\r\nHTML\r\n--t--\r\n",
227 false,
228 },
229 {
230 "Plain text",
231 "",
232 "Plain",
233 "t",
234 "--t\r\nContent-Transfer-Encoding: quoted-printable\r\nContent-Type: text/plain; charset=UTF-8\r\n\r\nPlain\r\n--t--\r\n",
235 false,
236 },
237 {
238 "Both",
239 "HTML",
240 "Plain",
241 "t",
242 "--t\r\nContent-Transfer-Encoding: quoted-printable\r\nContent-Type: text/plain; charset=UTF-8\r\n\r\nPlain\r\n--t\r\nContent-Transfer-Encoding: quoted-printable\r\nContent-Type: text/html; charset=UTF-8\r\n\r\nHTML\r\n--t--\r\n",
243 false,
244 },
245 {
246 "Both with long lines",
247 "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
248 "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
249 "t",
250 "--t\r\nContent-Transfer-Encoding: quoted-printable\r\nContent-Type: text/plain; charset=UTF-8\r\n\r\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tem=\r\npor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, q=\r\nuis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo cons=\r\nequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillu=\r\nm dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non pr=\r\noident, sunt in culpa qui officia deserunt mollit anim id est laborum.\r\n--t\r\nContent-Transfer-Encoding: quoted-printable\r\nContent-Type: text/html; charset=UTF-8\r\n\r\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tem=\r\npor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, q=\r\nuis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo cons=\r\nequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillu=\r\nm dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non pr=\r\noident, sunt in culpa qui officia deserunt mollit anim id est laborum.\r\n--t--\r\n",
251 false,
252 },
253 }
254 for _, tt := range tests {
255 tt := tt
256 t.Runscip-go gomod github.com/golang/go/src go1.12 testing/T#Run().(tt.name, func(t *testingscip-go gomod github.com/golang/go/src go1.12 testing/.Tscip-go gomod github.com/golang/go/src go1.12 testing/T#) {
257 t.Parallelscip-go gomod github.com/golang/go/src go1.12 testing/T#Parallel().()
258
259 m := MailYakscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#{}
260 m.HTMLscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#HTML().().WriteStringscip-go gomod github.com/golang/go/src go1.12 bytes/Buffer#WriteString().(tt.rHTML)
261 m.Plainscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#Plain().().WriteStringscip-go gomod github.com/golang/go/src go1.12 bytes/Buffer#WriteString().(tt.rPlain)
262
263 w := &bytesscip-go gomod github.com/golang/go/src go1.12 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.12 bytes/Buffer#{}
264 if err := m.writeBodyscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#writeBody().(w, tt.boundary); (err != nil) != tt.wantErr {
265 t.Fatalfscip-go gomod github.com/golang/go/src go1.12 testing/common#Fatalf().("%q. MailYak.writeBody() error = %v, wantErr %v", tt.name, err, tt.wantErr)
266 }
267
268 if gotW := w.Stringscip-go gomod github.com/golang/go/src go1.12 bytes/Buffer#String().(); gotW != tt.wantW {
269 t.Errorfscip-go gomod github.com/golang/go/src go1.12 testing/common#Errorf().("%q. MailYak.writeBody() = %v, want %v", tt.name, gotW, tt.wantW)
270 }
271 })
272 }
273 }
274
275 // TestMailYakBuildMime tests all the other mime-related bits combine in a sane way
276 func TestMailYakBuildMimescip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/TestMailYakBuildMime().(t *testingscip-go gomod github.com/golang/go/src go1.12 testing/.Tscip-go gomod github.com/golang/go/src go1.12 testing/T#) {
277 t.Parallelscip-go gomod github.com/golang/go/src go1.12 testing/T#Parallel().()
278
279 now := timescip-go gomod github.com/golang/go/src go1.12 time/.Nowscip-go gomod github.com/golang/go/src go1.12 time/Now().().Formatscip-go gomod github.com/golang/go/src go1.12 time/Time#Format().(timescip-go gomod github.com/golang/go/src go1.12 time/.RFC1123Zscip-go gomod github.com/golang/go/src go1.12 time/RFC1123Z.)
280 tests := []struct {
281 // Test description.
282 name string
283 // Receiver fields.
284 rHTML []byte
285 rPlain []byte
286 rtoAddrs []string
287 rsubject string
288 rfromAddr string
289 rfromName string
290 rreplyTo string
291 rAttachemnt string
292 // Expected results.
293 want string
294 wantErr bool
295 }{
296 {
297 "Empty",
298 []byte{},
299 []byte{},
300 []string{""},
301 "",
302 "",
303 "",
304 "",
305 "",
306 "From: \r\nMIME-Version: 1.0\r\nDate: " + now + "\r\nSubject: \r\nTo: \r\n\r\n",
307 false,
308 },
309 {
310 "HTML",
311 []byte("HTML"),
312 []byte{},
313 []string{""},
314 "",
315 "",
316 "",
317 "",
318 "",
319 "From: \r\nMIME-Version: 1.0\r\nDate: " + now + "\r\nSubject: \r\nTo: \r\nContent-Type: multipart/mixed;\r\n\tboundary=\"mixed\"; charset=UTF-8\r\n\r\n--mixed\r\nContent-Type: multipart/alternative;\r\n\tboundary=\"alt\"\r\n\r\n--alt\r\nContent-Transfer-Encoding: quoted-printable\r\nContent-Type: text/html; charset=UTF-8\r\n\r\nHTML\r\n--alt--\r\n\r\n--mixed--\r\n",
320 false,
321 },
322 {
323 "Plain",
324 []byte{},
325 []byte("Plain"),
326 []string{""},
327 "",
328 "",
329 "",
330 "",
331 "",
332 "From: \r\nMIME-Version: 1.0\r\nDate: " + now + "\r\nSubject: \r\nTo: \r\nContent-Type: multipart/mixed;\r\n\tboundary=\"mixed\"; charset=UTF-8\r\n\r\n--mixed\r\nContent-Type: multipart/alternative;\r\n\tboundary=\"alt\"\r\n\r\n--alt\r\nContent-Transfer-Encoding: quoted-printable\r\nContent-Type: text/plain; charset=UTF-8\r\n\r\nPlain\r\n--alt--\r\n\r\n--mixed--\r\n",
333 false,
334 },
335 {
336 "Attachemnt",
337 []byte{},
338 []byte{},
339 []string{""},
340 "",
341 "",
342 "",
343 "",
344 "attachment",
345 "From: \r\nMIME-Version: 1.0\r\nDate: " + now + "\r\nSubject: \r\nTo: \r\nContent-Type: multipart/mixed;\r\n\tboundary=\"mixed\"; charset=UTF-8\r\n\r\n--mixed\r\nContent-Disposition: attachment;\n\tfilename=\"testAttachment\"; name=\"testAttachment\"\r\nContent-ID: <testAttachment>\r\nContent-Transfer-Encoding: base64\r\nContent-Type: text/plain; charset=utf-8;\n\tfilename=\"testAttachment\"; name=\"testAttachment\"\r\n\r\nYXR0YWNobWVudA==\r\n--mixed--\r\n",
346 false,
347 },
348 {
349 "Reply-To",
350 []byte{},
351 []byte{},
352 []string{""},
353 "",
354 "",
355 "",
356 "reply",
357 "",
358 "From: \r\nMIME-Version: 1.0\r\nDate: " + now + "\r\nReply-To: reply\r\nSubject: \r\nTo: \r\n\r\n",
359 false,
360 },
361 {
362 "From name",
363 []byte{},
364 []byte{},
365 []string{""},
366 "",
367 "",
368 "name",
369 "",
370 "",
371 "From: name <>\r\nMIME-Version: 1.0\r\nDate: " + now + "\r\nSubject: \r\nTo: \r\n\r\n",
372 false,
373 },
374 {
375 "From name + address",
376 []byte{},
377 []byte{},
378 []string{""},
379 "",
380 "addr",
381 "name",
382 "",
383 "",
384 "From: name <addr>\r\nMIME-Version: 1.0\r\nDate: " + now + "\r\nSubject: \r\nTo: \r\n\r\n",
385 false,
386 },
387 {
388 "From",
389 []byte{},
390 []byte{},
391 []string{""},
392 "",
393 "from",
394 "",
395 "",
396 "",
397 "From: from\r\nMIME-Version: 1.0\r\nDate: " + now + "\r\nSubject: \r\nTo: \r\n\r\n",
398 false,
399 },
400 {
401 "Subject",
402 []byte{},
403 []byte{},
404 []string{""},
405 "subject",
406 "",
407 "",
408 "",
409 "",
410 "From: \r\nMIME-Version: 1.0\r\nDate: " + now + "\r\nSubject: subject\r\nTo: \r\n\r\n",
411 false,
412 },
413 {
414 "To addresses",
415 []byte{},
416 []byte{},
417 []string{"one", "two"},
418 "",
419 "",
420 "",
421 "",
422 "",
423 "From: \r\nMIME-Version: 1.0\r\nDate: " + now + "\r\nSubject: \r\nTo: one,two\r\n\r\n",
424 false,
425 },
426 }
427
428 regex := regexpscip-go gomod github.com/golang/go/src go1.12 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.12 regexp/MustCompile().("\r?\n")
429
430 for _, tt := range tests {
431 tt := tt
432 t.Runscip-go gomod github.com/golang/go/src go1.12 testing/T#Run().(tt.name, func(t *testingscip-go gomod github.com/golang/go/src go1.12 testing/.Tscip-go gomod github.com/golang/go/src go1.12 testing/T#) {
433 t.Parallelscip-go gomod github.com/golang/go/src go1.12 testing/T#Parallel().()
434
435 m := &MailYakscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#{
436 toAddrsscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#toAddrs.: tt.rtoAddrs,
437 subjectscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#subject.: tt.rsubject,
438 fromAddrscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#fromAddr.: tt.rfromAddr,
439 fromNamescip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#fromName.: tt.rfromName,
440 replyToscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#replyTo.: tt.rreplyTo,
441 trimRegexscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#trimRegex.: regex,
442 datescip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#date.: now,
443 }
444 m.HTMLscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#HTML().().Writescip-go gomod github.com/golang/go/src go1.12 bytes/Buffer#Write().(tt.rHTML)
445 m.Plainscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#Plain().().Writescip-go gomod github.com/golang/go/src go1.12 bytes/Buffer#Write().(tt.rPlain)
446 if tt.rAttachemnt != "" {
447 m.Attachscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#Attach().("testAttachment", stringsscip-go gomod github.com/golang/go/src go1.12 strings/.NewReaderscip-go gomod github.com/golang/go/src go1.12 strings/NewReader().(tt.rAttachemnt))
448 }
449
450 buf := &bytesscip-go gomod github.com/golang/go/src go1.12 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.12 bytes/Buffer#{}
451 err := m.buildMimeWithBoundariesscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#buildMimeWithBoundaries().(buf, "mixed", "alt")
452 if (err != nil) != tt.wantErr {
453 t.Fatalfscip-go gomod github.com/golang/go/src go1.12 testing/common#Fatalf().("%q. MailYak.buildMime() error = %v, wantErr %v", tt.name, err, tt.wantErr)
454 }
455
456 if buf.Stringscip-go gomod github.com/golang/go/src go1.12 bytes/Buffer#String().() != tt.want {
457 t.Errorfscip-go gomod github.com/golang/go/src go1.12 testing/common#Errorf().("%q. MailYak.buildMime() = %v, want %v", tt.name, buf.Stringscip-go gomod github.com/golang/go/src go1.12 bytes/Buffer#String().(), tt.want)
458 }
459 })
460 }
461 }
462
463 // TestMailYakBuildMime_withAttachments ensures attachments are correctly added to the MIME message
464 func TestMailYakBuildMime_withAttachmentsscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/TestMailYakBuildMime_withAttachments().(t *testingscip-go gomod github.com/golang/go/src go1.12 testing/.Tscip-go gomod github.com/golang/go/src go1.12 testing/T#) {
465 t.Parallelscip-go gomod github.com/golang/go/src go1.12 testing/T#Parallel().()
466
467 tests := []struct {
468 // Test description.
469 name string
470 // Receiver fields.
471 rHTML []byte
472 rPlain []byte
473 rtoAddrs []string
474 rsubject string
475 rfromAddr string
476 rfromName string
477 rreplyTo string
478 rattachments []attachmentscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/attachment#
479 // Expected results.
480 wantAttach []string
481 wantErr bool
482 }{
483 {
484 "No attachment",
485 []byte{},
486 []byte{},
487 []string{""},
488 "",
489 "",
490 "",
491 "",
492 []attachmentscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/attachment#{},
493 []string{},
494 false,
495 },
496 {
497 "One attachment",
498 []byte{},
499 []byte{},
500 []string{""},
501 "",
502 "",
503 "",
504 "",
505 []attachmentscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/attachment#{
506 {"test.txt", stringsscip-go gomod github.com/golang/go/src go1.12 strings/.NewReaderscip-go gomod github.com/golang/go/src go1.12 strings/NewReader().("content"), false, ""},
507 },
508 []string{"Y29udGVudA=="},
509 false,
510 },
511 {
512 "One inline attachment",
513 []byte{},
514 []byte{},
515 []string{""},
516 "",
517 "",
518 "",
519 "",
520 []attachmentscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/attachment#{
521 {"test.txt", stringsscip-go gomod github.com/golang/go/src go1.12 strings/.NewReaderscip-go gomod github.com/golang/go/src go1.12 strings/NewReader().("content"), true, ""},
522 },
523 []string{"Y29udGVudA=="},
524 false,
525 },
526 {
527 "Two attachments",
528 []byte{},
529 []byte{},
530 []string{""},
531 "",
532 "",
533 "",
534 "",
535 []attachmentscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/attachment#{
536 {"test.txt", stringsscip-go gomod github.com/golang/go/src go1.12 strings/.NewReaderscip-go gomod github.com/golang/go/src go1.12 strings/NewReader().("content"), false, ""},
537 {"another.txt", stringsscip-go gomod github.com/golang/go/src go1.12 strings/.NewReaderscip-go gomod github.com/golang/go/src go1.12 strings/NewReader().("another"), false, ""},
538 },
539 []string{"Y29udGVudA==", "YW5vdGhlcg=="},
540 false,
541 },
542 {
543 "Two inline attachments",
544 []byte{},
545 []byte{},
546 []string{""},
547 "",
548 "",
549 "",
550 "",
551 []attachmentscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/attachment#{
552 {"test.txt", stringsscip-go gomod github.com/golang/go/src go1.12 strings/.NewReaderscip-go gomod github.com/golang/go/src go1.12 strings/NewReader().("content"), true, ""},
553 {"another.txt", stringsscip-go gomod github.com/golang/go/src go1.12 strings/.NewReaderscip-go gomod github.com/golang/go/src go1.12 strings/NewReader().("another"), true, ""},
554 },
555 []string{"Y29udGVudA==", "YW5vdGhlcg=="},
556 false,
557 },
558 }
559
560 regex := regexpscip-go gomod github.com/golang/go/src go1.12 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.12 regexp/MustCompile().("\r?\n")
561
562 for _, tt := range tests {
563 tt := tt
564 t.Runscip-go gomod github.com/golang/go/src go1.12 testing/T#Run().(tt.name, func(t *testingscip-go gomod github.com/golang/go/src go1.12 testing/.Tscip-go gomod github.com/golang/go/src go1.12 testing/T#) {
565 t.Parallelscip-go gomod github.com/golang/go/src go1.12 testing/T#Parallel().()
566
567 m := &MailYakscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#{
568 toAddrsscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#toAddrs.: tt.rtoAddrs,
569 subjectscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#subject.: tt.rsubject,
570 fromAddrscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#fromAddr.: tt.rfromAddr,
571 fromNamescip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#fromName.: tt.rfromName,
572 replyToscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#replyTo.: tt.rreplyTo,
573 attachmentsscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#attachments.: tt.rattachments,
574 trimRegexscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#trimRegex.: regex,
575 }
576 m.HTMLscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#HTML().().Writescip-go gomod github.com/golang/go/src go1.12 bytes/Buffer#Write().(tt.rHTML)
577 m.Plainscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#Plain().().Writescip-go gomod github.com/golang/go/src go1.12 bytes/Buffer#Write().(tt.rPlain)
578
579 buf := &bytesscip-go gomod github.com/golang/go/src go1.12 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.12 bytes/Buffer#{}
580 err := m.buildMimeWithBoundariesscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#buildMimeWithBoundaries().(buf, "mixed", "alt")
581 if (err != nil) != tt.wantErr {
582 t.Fatalfscip-go gomod github.com/golang/go/src go1.12 testing/common#Fatalf().("%q. MailYak.buildMime() error = %v, wantErr %v", tt.name, err, tt.wantErr)
583 }
584
585 seen := 0
586 msg, err := mailscip-go gomod github.com/golang/go/src go1.12 `net/mail`/.ReadMessagescip-go gomod github.com/golang/go/src go1.12 `net/mail`/ReadMessage().(buf)
587 if err != nil {
588 t.Fatalfscip-go gomod github.com/golang/go/src go1.12 testing/common#Fatalf().("%q. MailYak.buildMime() error %v", tt.name, err)
589 }
590 contentTypeHeader := msg.Headerscip-go gomod github.com/golang/go/src go1.12 `net/mail`/Message#Header..Getscip-go gomod github.com/golang/go/src go1.12 `net/mail`/Header#Get().("Content-Type")
591 if contentTypeHeader == "" {
592 if len(tt.rattachments) != 0 {
593 t.Errorfscip-go gomod github.com/golang/go/src go1.12 testing/common#Errorf().("%q. MailYak.buildMime() attachments were expected, but Content-Type header wasn't set", tt.name)
594 }
595 return
596 }
597 mediaType, mediaTypeParams, err := mimescip-go gomod github.com/golang/go/src go1.12 mime/.ParseMediaTypescip-go gomod github.com/golang/go/src go1.12 mime/ParseMediaType().(contentTypeHeader)
598 if err != nil {
599 t.Fatalfscip-go gomod github.com/golang/go/src go1.12 testing/common#Fatalf().("%q. MailYak.buildMime() error %v", tt.name, err)
600 }
601 if mediaType != "multipart/mixed" {
602 t.Fatalfscip-go gomod github.com/golang/go/src go1.12 testing/common#Fatalf().("%q. MailYak.buildMime() Content-Type media type multipart/mixed was expected, but got %q", tt.name, mediaType)
603 }
604 mr := multipartscip-go gomod github.com/golang/go/src go1.12 `mime/multipart`/.NewReaderscip-go gomod github.com/golang/go/src go1.12 `mime/multipart`/NewReader().(msg.Bodyscip-go gomod github.com/golang/go/src go1.12 `net/mail`/Message#Body., mediaTypeParams["boundary"])
605
606 // Itterate over the mime parts, look for attachments
607 for contentTypeHeader != "" {
608 p, err := mr.NextPartscip-go gomod github.com/golang/go/src go1.12 `mime/multipart`/Reader#NextPart().()
609 if err == ioscip-go gomod github.com/golang/go/src go1.12 io/.EOFscip-go gomod github.com/golang/go/src go1.12 io/EOF. {
610 break
611 }
612 if err != nil {
613 t.Errorfscip-go gomod github.com/golang/go/src go1.12 testing/common#Errorf().("%q. MailYak.buildMime() error = %v, wantErr %v", tt.name, err, tt.wantErr)
614 }
615
616 // Read the attachment data
617 slurp, err := ioutilscip-go gomod github.com/golang/go/src go1.12 `io/ioutil`/.ReadAllscip-go gomod github.com/golang/go/src go1.12 `io/ioutil`/ReadAll().(p)
618 if err != nil {
619 t.Errorfscip-go gomod github.com/golang/go/src go1.12 testing/common#Errorf().("%q. MailYak.buildMime() error = %v, wantErr %v", tt.name, err, tt.wantErr)
620 }
621
622 // Skip non-attachments
623 if p.Headerscip-go gomod github.com/golang/go/src go1.12 `mime/multipart`/Part#Header..Getscip-go gomod github.com/golang/go/src go1.12 `net/textproto`/MIMEHeader#Get().("Content-Disposition") == "" {
624 continue
625 }
626
627 // Run through our attachments looking for a match
628 for i, attch := range tt.rattachments {
629 // Check Disposition header
630 var disp string
631 if attch.inlinescip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/attachment#inline. {
632 disp = "inline; filename=%q; name=%q"
633 } else {
634 disp = "attachment; filename=%q; name=%q"
635 }
636 if p.Headerscip-go gomod github.com/golang/go/src go1.12 `mime/multipart`/Part#Header..Getscip-go gomod github.com/golang/go/src go1.12 `net/textproto`/MIMEHeader#Get().("Content-Disposition") != fmtscip-go gomod github.com/golang/go/src go1.12 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.12 fmt/Sprintf().(disp, attch.filenamescip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/attachment#filename., attch.filenamescip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/attachment#filename.) {
637 continue
638 }
639
640 // Check data
641 if !bytesscip-go gomod github.com/golang/go/src go1.12 bytes/.Equalscip-go gomod github.com/golang/go/src go1.12 bytes/Equal().(slurp, []byte(tt.wantAttach[i])) {
642 fmtscip-go gomod github.com/golang/go/src go1.12 fmt/.Printfscip-go gomod github.com/golang/go/src go1.12 fmt/Printf().("Part %q: %q\n", p.Headerscip-go gomod github.com/golang/go/src go1.12 `mime/multipart`/Part#Header..Getscip-go gomod github.com/golang/go/src go1.12 `net/textproto`/MIMEHeader#Get().("Content-Disposition"), slurp)
643 continue
644 }
645
646 seen++
647 }
648
649 }
650
651 // Did we see all the expected attachments?
652 if seen != len(tt.rattachments) {
653 t.Errorfscip-go gomod github.com/golang/go/src go1.12 testing/common#Errorf().("%q. MailYak.buildMime() didn't find all attachments in mime body", tt.name)
654 }
655 })
656 }
657 }