| 1 |
package mailyak |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"io/ioutil" |
| 8 |
"mime" |
| 9 |
"mime/multipart" |
| 10 |
"net/mail" |
| 11 |
"regexp" |
| 12 |
"strings" |
| 13 |
"testing" |
| 14 |
"time" |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
func TestMailYakFromHeader(t *testing.T) { |
| 19 |
t.Parallel() |
| 20 |
|
| 21 |
tests := []struct { |
| 22 |
|
| 23 |
name string |
| 24 |
|
| 25 |
rfromAddr string |
| 26 |
rfromName string |
| 27 |
|
| 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.Run(tt.name, func(t *testing.T) { |
| 52 |
t.Parallel() |
| 53 |
|
| 54 |
m := MailYak{ |
| 55 |
fromAddr: tt.rfromAddr, |
| 56 |
fromName: tt.rfromName, |
| 57 |
} |
| 58 |
|
| 59 |
if got := m.fromHeader(); got != tt.want { |
| 60 |
t.Errorf("%q. MailYak.fromHeader() = %v, want %v", tt.name, got, tt.want) |
| 61 |
} |
| 62 |
}) |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
func TestMailYakWriteHeaders(t *testing.T) { |
| 69 |
t.Parallel() |
| 70 |
|
| 71 |
now := time.Now().Format(time.RFC1123Z) |
| 72 |
tests := []struct { |
| 73 |
|
| 74 |
name string |
| 75 |
|
| 76 |
rtoAddrs []string |
| 77 |
rccAddrs []string |
| 78 |
rbccAddrs []string |
| 79 |
rsubject string |
| 80 |
rreplyTo string |
| 81 |
rwriteBccHeader bool |
| 82 |
|
| 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.Run(tt.name, func(t *testing.T) { |
| 179 |
t.Parallel() |
| 180 |
|
| 181 |
m := MailYak{ |
| 182 |
toAddrs: tt.rtoAddrs, |
| 183 |
subject: tt.rsubject, |
| 184 |
fromAddr: "[email protected]", |
| 185 |
fromName: "Dom", |
| 186 |
replyTo: tt.rreplyTo, |
| 187 |
ccAddrs: tt.rccAddrs, |
| 188 |
bccAddrs: tt.rbccAddrs, |
| 189 |
writeBccHeader: tt.rwriteBccHeader, |
| 190 |
date: now, |
| 191 |
} |
| 192 |
|
| 193 |
buf := &bytes.Buffer{} |
| 194 |
if err := m.writeHeaders(buf); err != nil { |
| 195 |
t.Fatal(err) |
| 196 |
} |
| 197 |
|
| 198 |
if gotBuf := buf.String(); gotBuf != tt.wantBuf { |
| 199 |
t.Errorf("%q. MailYak.writeHeaders() = %v, want %v", tt.name, gotBuf, tt.wantBuf) |
| 200 |
} |
| 201 |
}) |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
func TestMailYakWriteBody(t *testing.T) { |
| 207 |
t.Parallel() |
| 208 |
|
| 209 |
tests := []struct { |
| 210 |
|
| 211 |
name string |
| 212 |
|
| 213 |
rHTML string |
| 214 |
rPlain string |
| 215 |
|
| 216 |
boundary string |
| 217 |
|
| 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.Run(tt.name, func(t *testing.T) { |
| 257 |
t.Parallel() |
| 258 |
|
| 259 |
m := MailYak{} |
| 260 |
m.HTML().WriteString(tt.rHTML) |
| 261 |
m.Plain().WriteString(tt.rPlain) |
| 262 |
|
| 263 |
w := &bytes.Buffer{} |
| 264 |
if err := m.writeBody(w, tt.boundary); (err != nil) != tt.wantErr { |
| 265 |
t.Fatalf("%q. MailYak.writeBody() error = %v, wantErr %v", tt.name, err, tt.wantErr) |
| 266 |
} |
| 267 |
|
| 268 |
if gotW := w.String(); gotW != tt.wantW { |
| 269 |
t.Errorf("%q. MailYak.writeBody() = %v, want %v", tt.name, gotW, tt.wantW) |
| 270 |
} |
| 271 |
}) |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
func TestMailYakBuildMime(t *testing.T) { |
| 277 |
t.Parallel() |
| 278 |
|
| 279 |
now := time.Now().Format(time.RFC1123Z) |
| 280 |
tests := []struct { |
| 281 |
|
| 282 |
name string |
| 283 |
|
| 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 |
|
| 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 := regexp.MustCompile("\r?\n") |
| 429 |
|
| 430 |
for _, tt := range tests { |
| 431 |
tt := tt |
| 432 |
t.Run(tt.name, func(t *testing.T) { |
| 433 |
t.Parallel() |
| 434 |
|
| 435 |
m := &MailYak{ |
| 436 |
toAddrs: tt.rtoAddrs, |
| 437 |
subject: tt.rsubject, |
| 438 |
fromAddr: tt.rfromAddr, |
| 439 |
fromName: tt.rfromName, |
| 440 |
replyTo: tt.rreplyTo, |
| 441 |
trimRegex: regex, |
| 442 |
date: now, |
| 443 |
} |
| 444 |
m.HTML().Write(tt.rHTML) |
| 445 |
m.Plain().Write(tt.rPlain) |
| 446 |
if tt.rAttachemnt != "" { |
| 447 |
m.Attach("testAttachment", strings.NewReader(tt.rAttachemnt)) |
| 448 |
} |
| 449 |
|
| 450 |
buf := &bytes.Buffer{} |
| 451 |
err := m.buildMimeWithBoundaries(buf, "mixed", "alt") |
| 452 |
if (err != nil) != tt.wantErr { |
| 453 |
t.Fatalf("%q. MailYak.buildMime() error = %v, wantErr %v", tt.name, err, tt.wantErr) |
| 454 |
} |
| 455 |
|
| 456 |
if buf.String() != tt.want { |
| 457 |
t.Errorf("%q. MailYak.buildMime() = %v, want %v", tt.name, buf.String(), tt.want) |
| 458 |
} |
| 459 |
}) |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
func TestMailYakBuildMime_withAttachments(t *testing.T) { |
| 465 |
t.Parallel() |
| 466 |
|
| 467 |
tests := []struct { |
| 468 |
|
| 469 |
name string |
| 470 |
|
| 471 |
rHTML []byte |
| 472 |
rPlain []byte |
| 473 |
rtoAddrs []string |
| 474 |
rsubject string |
| 475 |
rfromAddr string |
| 476 |
rfromName string |
| 477 |
rreplyTo string |
| 478 |
rattachments []attachment |
| 479 |
|
| 480 |
wantAttach []string |
| 481 |
wantErr bool |
| 482 |
}{ |
| 483 |
{ |
| 484 |
"No attachment", |
| 485 |
[]byte{}, |
| 486 |
[]byte{}, |
| 487 |
[]string{""}, |
| 488 |
"", |
| 489 |
"", |
| 490 |
"", |
| 491 |
"", |
| 492 |
[]attachment{}, |
| 493 |
[]string{}, |
| 494 |
false, |
| 495 |
}, |
| 496 |
{ |
| 497 |
"One attachment", |
| 498 |
[]byte{}, |
| 499 |
[]byte{}, |
| 500 |
[]string{""}, |
| 501 |
"", |
| 502 |
"", |
| 503 |
"", |
| 504 |
"", |
| 505 |
[]attachment{ |
| 506 |
{"test.txt", 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 |
[]attachment{ |
| 521 |
{"test.txt", 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 |
[]attachment{ |
| 536 |
{"test.txt", strings.NewReader("content"), false, ""}, |
| 537 |
{"another.txt", 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 |
[]attachment{ |
| 552 |
{"test.txt", strings.NewReader("content"), true, ""}, |
| 553 |
{"another.txt", strings.NewReader("another"), true, ""}, |
| 554 |
}, |
| 555 |
[]string{"Y29udGVudA==", "YW5vdGhlcg=="}, |
| 556 |
false, |
| 557 |
}, |
| 558 |
} |
| 559 |
|
| 560 |
regex := regexp.MustCompile("\r?\n") |
| 561 |
|
| 562 |
for _, tt := range tests { |
| 563 |
tt := tt |
| 564 |
t.Run(tt.name, func(t *testing.T) { |
| 565 |
t.Parallel() |
| 566 |
|
| 567 |
m := &MailYak{ |
| 568 |
toAddrs: tt.rtoAddrs, |
| 569 |
subject: tt.rsubject, |
| 570 |
fromAddr: tt.rfromAddr, |
| 571 |
fromName: tt.rfromName, |
| 572 |
replyTo: tt.rreplyTo, |
| 573 |
attachments: tt.rattachments, |
| 574 |
trimRegex: regex, |
| 575 |
} |
| 576 |
m.HTML().Write(tt.rHTML) |
| 577 |
m.Plain().Write(tt.rPlain) |
| 578 |
|
| 579 |
buf := &bytes.Buffer{} |
| 580 |
err := m.buildMimeWithBoundaries(buf, "mixed", "alt") |
| 581 |
if (err != nil) != tt.wantErr { |
| 582 |
t.Fatalf("%q. MailYak.buildMime() error = %v, wantErr %v", tt.name, err, tt.wantErr) |
| 583 |
} |
| 584 |
|
| 585 |
seen := 0 |
| 586 |
msg, err := mail.ReadMessage(buf) |
| 587 |
if err != nil { |
| 588 |
t.Fatalf("%q. MailYak.buildMime() error %v", tt.name, err) |
| 589 |
} |
| 590 |
contentTypeHeader := msg.Header.Get("Content-Type") |
| 591 |
if contentTypeHeader == "" { |
| 592 |
if len(tt.rattachments) != 0 { |
| 593 |
t.Errorf("%q. MailYak.buildMime() attachments were expected, but Content-Type header wasn't set", tt.name) |
| 594 |
} |
| 595 |
return |
| 596 |
} |
| 597 |
mediaType, mediaTypeParams, err := mime.ParseMediaType(contentTypeHeader) |
| 598 |
if err != nil { |
| 599 |
t.Fatalf("%q. MailYak.buildMime() error %v", tt.name, err) |
| 600 |
} |
| 601 |
if mediaType != "multipart/mixed" { |
| 602 |
t.Fatalf("%q. MailYak.buildMime() Content-Type media type multipart/mixed was expected, but got %q", tt.name, mediaType) |
| 603 |
} |
| 604 |
mr := multipart.NewReader(msg.Body, mediaTypeParams["boundary"]) |
| 605 |
|
| 606 |
|
| 607 |
for contentTypeHeader != "" { |
| 608 |
p, err := mr.NextPart() |
| 609 |
if err == io.EOF { |
| 610 |
break |
| 611 |
} |
| 612 |
if err != nil { |
| 613 |
t.Errorf("%q. MailYak.buildMime() error = %v, wantErr %v", tt.name, err, tt.wantErr) |
| 614 |
} |
| 615 |
|
| 616 |
|
| 617 |
slurp, err := ioutil.ReadAll(p) |
| 618 |
if err != nil { |
| 619 |
t.Errorf("%q. MailYak.buildMime() error = %v, wantErr %v", tt.name, err, tt.wantErr) |
| 620 |
} |
| 621 |
|
| 622 |
|
| 623 |
if p.Header.Get("Content-Disposition") == "" { |
| 624 |
continue |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
for i, attch := range tt.rattachments { |
| 629 |
|
| 630 |
var disp string |
| 631 |
if attch.inline { |
| 632 |
disp = "inline; filename=%q; name=%q" |
| 633 |
} else { |
| 634 |
disp = "attachment; filename=%q; name=%q" |
| 635 |
} |
| 636 |
if p.Header.Get("Content-Disposition") != fmt.Sprintf(disp, attch.filename, attch.filename) { |
| 637 |
continue |
| 638 |
} |
| 639 |
|
| 640 |
|
| 641 |
if !bytes.Equal(slurp, []byte(tt.wantAttach[i])) { |
| 642 |
fmt.Printf("Part %q: %q\n", p.Header.Get("Content-Disposition"), slurp) |
| 643 |
continue |
| 644 |
} |
| 645 |
|
| 646 |
seen++ |
| 647 |
} |
| 648 |
|
| 649 |
} |
| 650 |
|
| 651 |
|
| 652 |
if seen != len(tt.rattachments) { |
| 653 |
t.Errorf("%q. MailYak.buildMime() didn't find all attachments in mime body", tt.name) |
| 654 |
} |
| 655 |
}) |
| 656 |
} |
| 657 |
} |