| 1 | package mailyak |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/base64" |
| 6 | "io" |
| 7 | "net/textproto" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | type testAttachment struct { |
| 13 | contentType string |
| 14 | disposition string |
| 15 | data bytes.Buffer |
| 16 | } |
| 17 | |
| 18 | // Satisfy the partCreator interface and keep track of wrote attachments |
| 19 | type testPartCreator struct { |
| 20 | attachments []*testAttachment |
| 21 | } |
| 22 | |
| 23 | func (t *testPartCreator) CreatePart(header textproto.MIMEHeader) (io.Writer, error) { |
| 24 | a := &testAttachment{ |
| 25 | contentType: header.Get("Content-Type"), |
| 26 | disposition: header.Get("Content-Disposition"), |
| 27 | } |
| 28 | |
| 29 | t.attachments = append(t.attachments, a) |
| 30 | return &a.data, nil |
| 31 | } |
| 32 | |
| 33 | // nopSplitter - it does nothing! |
| 34 | type nopSplitter struct { |
| 35 | w io.Writer |
| 36 | } |
| 37 | |
| 38 | func (t nopSplitter) Write(p []byte) (int, error) { |
| 39 | return t.w.Write(p) |
| 40 | } |
| 41 | |
| 42 | // nopBuilder satisfies the writeWrapper interface |
| 43 | type nopBuilder struct{} |
| 44 | |
| 45 | func (b nopBuilder) new(w io.Writer) io.Writer { |
| 46 | return &nopSplitter{w: w} |
| 47 | } |
| 48 | |
| 49 | // TestMailYakAttach calls Attach() and ensures the attachment slice is the |
| 50 | // correct length |
| 51 | func TestMailYakAttach(t *testing.T) { |
| 52 | t.Parallel() |
| 53 | |
| 54 | tests := []struct { |
| 55 | // Test description. |
| 56 | name string |
| 57 | // Receiver fields. |
| 58 | rattachments []attachment |
| 59 | // Parameters. |
| 60 | pname string |
| 61 | r io.Reader |
| 62 | // Expect |
| 63 | count int |
| 64 | }{ |
| 65 | { |
| 66 | "From empty", |
| 67 | []attachment{}, |
| 68 | "test", |
| 69 | &bytes.Buffer{}, |
| 70 | 1, |
| 71 | }, |
| 72 | { |
| 73 | "From one", |
| 74 | []attachment{{"Existing", &bytes.Buffer{}, false, ""}}, |
| 75 | "test", |
| 76 | &bytes.Buffer{}, |
| 77 | 2, |
| 78 | }, |
| 79 | } |
| 80 | for _, tt := range tests { |
| 81 | tt := tt |
| 82 | t.Run(tt.name, func(t *testing.T) { |
| 83 | t.Parallel() |
| 84 | |
| 85 | m := &MailYak{attachments: tt.rattachments} |
| 86 | m.Attach(tt.pname, tt.r) |
| 87 | |
| 88 | if tt.count != len(m.attachments) { |
| 89 | t.Errorf("%q. MailYak.Attach() len = %v, wantLen %v", tt.name, len(m.attachments), tt.count) |
| 90 | } |
| 91 | }) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // TestMailYakAttach calls AttachInline() and ensures the attachment slice is the |
| 96 | // correct length |
| 97 | func TestMailYakAttachInline(t *testing.T) { |
| 98 | t.Parallel() |
| 99 | |
| 100 | tests := []struct { |
| 101 | // Test description. |
| 102 | name string |
| 103 | // Receiver fields. |
| 104 | rattachments []attachment |
| 105 | // Parameters. |
| 106 | pname string |
| 107 | r io.Reader |
| 108 | // Expect |
| 109 | count int |
| 110 | }{ |
| 111 | { |
| 112 | "From empty", |
| 113 | []attachment{}, |
| 114 | "test", |
| 115 | &bytes.Buffer{}, |
| 116 | 1, |
| 117 | }, |
| 118 | { |
| 119 | "From one", |
| 120 | []attachment{{"Existing", &bytes.Buffer{}, false, ""}}, |
| 121 | "test", |
| 122 | &bytes.Buffer{}, |
| 123 | 2, |
| 124 | }, |
| 125 | } |
| 126 | for _, tt := range tests { |
| 127 | tt := tt |
| 128 | t.Run(tt.name, func(t *testing.T) { |
| 129 | t.Parallel() |
| 130 | |
| 131 | m := &MailYak{attachments: tt.rattachments} |
| 132 | m.AttachInline(tt.pname, tt.r) |
| 133 | |
| 134 | if tt.count != len(m.attachments) { |
| 135 | t.Errorf("%q. MailYak.Attach() len = %v, wantLen %v", tt.name, len(m.attachments), tt.count) |
| 136 | } |
| 137 | }) |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // TestMailYakAttachWithMimeType calls AttachWithMimeType() and ensures the attachment slice is the |
| 142 | // correct length |
| 143 | func TestMailYakAttachWithMimeType(t *testing.T) { |
| 144 | t.Parallel() |
| 145 | |
| 146 | tests := []struct { |
| 147 | // Test description. |
| 148 | name string |
| 149 | // Receiver fields. |
| 150 | rattachments []attachment |
| 151 | // Parameters. |
| 152 | pname string |
| 153 | r io.Reader |
| 154 | mime string |
| 155 | // Expect |
| 156 | count int |
| 157 | }{ |
| 158 | { |
| 159 | "From empty", |
| 160 | []attachment{}, |
| 161 | "test", |
| 162 | &bytes.Buffer{}, |
| 163 | "text/csv; charset=utf-8", |
| 164 | 1, |
| 165 | }, |
| 166 | { |
| 167 | "From one", |
| 168 | []attachment{{"Existing", &bytes.Buffer{}, false, "text/csv; charset=utf-8"}}, |
| 169 | "test", |
| 170 | &bytes.Buffer{}, |
| 171 | "text/csv; charset=utf-8", |
| 172 | 2, |
| 173 | }, |
| 174 | } |
| 175 | for _, tt := range tests { |
| 176 | tt := tt |
| 177 | t.Run(tt.name, func(t *testing.T) { |
| 178 | t.Parallel() |
| 179 | |
| 180 | m := &MailYak{attachments: tt.rattachments} |
| 181 | m.AttachWithMimeType(tt.pname, tt.r, tt.mime) |
| 182 | |
| 183 | if tt.count != len(m.attachments) { |
| 184 | t.Errorf("%q. MailYak.Attach() len = %v, wantLen %v", tt.name, len(m.attachments), tt.count) |
| 185 | } |
| 186 | }) |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | // TestMailYakAttachWithMimeType calls AttachInlineWithMimeType() and ensures the attachment slice is the |
| 191 | // correct length |
| 192 | func TestMailYakAttachInlineWithMimeType(t *testing.T) { |
| 193 | t.Parallel() |
| 194 | |
| 195 | tests := []struct { |
| 196 | // Test description. |
| 197 | name string |
| 198 | // Receiver fields. |
| 199 | rattachments []attachment |
| 200 | // Parameters. |
| 201 | pname string |
| 202 | r io.Reader |
| 203 | mime string |
| 204 | // Expect |
| 205 | count int |
| 206 | }{ |
| 207 | { |
| 208 | "From empty", |
| 209 | []attachment{}, |
| 210 | "test", |
| 211 | &bytes.Buffer{}, |
| 212 | "text/csv; charset=utf-8", |
| 213 | 1, |
| 214 | }, |
| 215 | { |
| 216 | "From one", |
| 217 | []attachment{{"Existing", &bytes.Buffer{}, false, "text/csv; charset=utf-8"}}, |
| 218 | "test", |
| 219 | &bytes.Buffer{}, |
| 220 | "text/csv; charset=utf-8", |
| 221 | 2, |
| 222 | }, |
| 223 | } |
| 224 | for _, tt := range tests { |
| 225 | tt := tt |
| 226 | t.Run(tt.name, func(t *testing.T) { |
| 227 | t.Parallel() |
| 228 | |
| 229 | m := &MailYak{attachments: tt.rattachments} |
| 230 | m.AttachInlineWithMimeType(tt.pname, tt.r, tt.mime) |
| 231 | |
| 232 | if tt.count != len(m.attachments) { |
| 233 | t.Errorf("%q. MailYak.Attach() len = %v, wantLen %v", tt.name, len(m.attachments), tt.count) |
| 234 | } |
| 235 | }) |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | // TestMailYakWriteAttachments ensures the correct headers are wrote, and the |
| 240 | // data is base64 encoded correctly |
| 241 | func TestMailYakWriteAttachments(t *testing.T) { |
| 242 | t.Parallel() |
| 243 | |
| 244 | tests := []struct { |
| 245 | // Test description. |
| 246 | name string |
| 247 | // Receiver fields. |
| 248 | rattachments []attachment |
| 249 | // Expected results. |
| 250 | ctype string |
| 251 | disp string |
| 252 | data string |
| 253 | wantErr bool |
| 254 | }{ |
| 255 | { |
| 256 | "Empty", |
| 257 | []attachment{{"Empty", &bytes.Buffer{}, false, ""}}, |
| 258 | "text/plain; charset=utf-8;\n\tfilename=\"Empty\"; name=\"Empty\"", |
| 259 | "attachment;\n\tfilename=\"Empty\"; name=\"Empty\"", |
| 260 | "", |
| 261 | false, |
| 262 | }, |
| 263 | { |
| 264 | "Short string", |
| 265 | []attachment{{"advice", strings.NewReader("Don't Panic"), false, ""}}, |
| 266 | "text/plain; charset=utf-8;\n\tfilename=\"advice\"; name=\"advice\"", |
| 267 | "attachment;\n\tfilename=\"advice\"; name=\"advice\"", |
| 268 | "RG9uJ3QgUGFuaWM=", |
| 269 | false, |
| 270 | }, |
| 271 | { |
| 272 | "Space in filename", |
| 273 | []attachment{{"Empty with spaces", &bytes.Buffer{}, false, ""}}, |
| 274 | "text/plain; charset=utf-8;\n\tfilename=\"Empty with spaces\"; name=\"Empty with spaces\"", |
| 275 | "attachment;\n\tfilename=\"Empty with spaces\"; name=\"Empty with spaces\"", |
| 276 | "", |
| 277 | false, |
| 278 | }, |
| 279 | { |
| 280 | "With specified MIME type", |
| 281 | []attachment{{"Empty with spaces", &bytes.Buffer{}, false, "text/csv; charset=utf-8"}}, |
| 282 | "text/csv; charset=utf-8;\n\tfilename=\"Empty with spaces\"; name=\"Empty with spaces\"", |
| 283 | "attachment;\n\tfilename=\"Empty with spaces\"; name=\"Empty with spaces\"", |
| 284 | "", |
| 285 | false, |
| 286 | }, |
| 287 | { |
| 288 | "Longer string", |
| 289 | []attachment{ |
| 290 | { |
| 291 | "partyinvite.txt", |
| 292 | strings.NewReader( |
| 293 | "If Baldrick served a meal at HQ he would be arrested for the biggest " + |
| 294 | "mass poisoning since Lucretia Borgia invited 500 friends for a Wine and Anthrax Party.", |
| 295 | ), |
| 296 | false, |
| 297 | "", |
| 298 | }, |
| 299 | }, |
| 300 | "text/plain; charset=utf-8;\n\tfilename=\"partyinvite.txt\"; name=\"partyinvite.txt\"", |
| 301 | "attachment;\n\tfilename=\"partyinvite.txt\"; name=\"partyinvite.txt\"", |
| 302 | "SWYgQmFsZHJpY2sgc2VydmVkIGEgbWVhbCBhdCBIUSBoZSB3b3VsZCBiZSBhcnJlc3Rl" + |
| 303 | "ZCBmb3IgdGhlIGJpZ2dlc3QgbWFzcyBwb2lzb25pbmcgc2luY2UgTHVjcmV0aWEgQm9y" + |
| 304 | "Z2lhIGludml0ZWQgNTAwIGZyaWVuZHMgZm9yIGEgV2luZSBhbmQgQW50aHJheCBQYXJ0eS4=", |
| 305 | false, |
| 306 | }, |
| 307 | { |
| 308 | "String >512 characters (content type sniff)", |
| 309 | []attachment{ |
| 310 | { |
| 311 | "qed.txt", |
| 312 | strings.NewReader( |
| 313 | `Now it is such a bizarrely improbable coincidence that anything so mind-bogglingly ` + |
| 314 | `useful could have evolved purely by chance that some thinkers have chosen to see it ` + |
| 315 | `as the final and clinching proof of the non-existence of God. The argument goes something ` + |
| 316 | `like this: "I refuse to prove that I exist," says God, "for proof denies faith, and ` + |
| 317 | `without faith I am nothing." "But," says Man, "The Babel fish is a dead giveaway, ` + |
| 318 | `isn't it? It could not have evolved by chance. It proves you exist, and so therefore, ` + |
| 319 | `by your own arguments, you don't. QED." "Oh dear," says God, "I hadn't thought of ` + |
| 320 | `that," and promptly vanishes in a puff of logic. "Oh, that was easy," says Man, and ` + |
| 321 | `for an encore goes on to prove that black is white and gets himself killed on the next ` + |
| 322 | `zebra crossing.`, |
| 323 | ), |
| 324 | false, |
| 325 | "", |
| 326 | }, |
| 327 | }, |
| 328 | "text/plain; charset=utf-8;\n\tfilename=\"qed.txt\"; name=\"qed.txt\"", |
| 329 | "attachment;\n\tfilename=\"qed.txt\"; name=\"qed.txt\"", |
| 330 | "Tm93IGl0IGlzIHN1Y2ggYSBiaXphcnJlbHkgaW1wcm9iYWJsZSBjb2luY2lkZW5jZSB0a" + |
| 331 | "GF0IGFueXRoaW5nIHNvIG1pbmQtYm9nZ2xpbmdseSB1c2VmdWwgY291bGQgaGF2ZSBldm" + |
| 332 | "9sdmVkIHB1cmVseSBieSBjaGFuY2UgdGhhdCBzb21lIHRoaW5rZXJzIGhhdmUgY2hvc2V" + |
| 333 | "uIHRvIHNlZSBpdCBhcyB0aGUgZmluYWwgYW5kIGNsaW5jaGluZyBwcm9vZiBvZiB0aGUg" + |
| 334 | "bm9uLWV4aXN0ZW5jZSBvZiBHb2QuIFRoZSBhcmd1bWVudCBnb2VzIHNvbWV0aGluZyBsa" + |
| 335 | "WtlIHRoaXM6ICJJIHJlZnVzZSB0byBwcm92ZSB0aGF0IEkgZXhpc3QsIiBzYXlzIEdvZC" + |
| 336 | "wgImZvciBwcm9vZiBkZW5pZXMgZmFpdGgsIGFuZCB3aXRob3V0IGZhaXRoIEkgYW0gbm9" + |
| 337 | "0aGluZy4iICJCdXQsIiBzYXlzIE1hbiwgIlRoZSBCYWJlbCBmaXNoIGlzIGEgZGVhZCBn" + |
| 338 | "aXZlYXdheSwgaXNuJ3QgaXQ/IEl0IGNvdWxkIG5vdCBoYXZlIGV2b2x2ZWQgYnkgY2hhb" + |
| 339 | "mNlLiBJdCBwcm92ZXMgeW91IGV4aXN0LCBhbmQgc28gdGhlcmVmb3JlLCBieSB5b3VyIG" + |
| 340 | "93biBhcmd1bWVudHMsIHlvdSBkb24ndC4gUUVELiIgIk9oIGRlYXIsIiBzYXlzIEdvZCw" + |
| 341 | "gIkkgaGFkbid0IHRob3VnaHQgb2YgdGhhdCwiIGFuZCBwcm9tcHRseSB2YW5pc2hlcyBp" + |
| 342 | "biBhIHB1ZmYgb2YgbG9naWMuICJPaCwgdGhhdCB3YXMgZWFzeSwiIHNheXMgTWFuLCBhb" + |
| 343 | "mQgZm9yIGFuIGVuY29yZSBnb2VzIG9uIHRvIHByb3ZlIHRoYXQgYmxhY2sgaXMgd2hpdG" + |
| 344 | "UgYW5kIGdldHMgaGltc2VsZiBraWxsZWQgb24gdGhlIG5leHQgemVicmEgY3Jvc3Npbmcu", |
| 345 | false, |
| 346 | }, |
| 347 | { |
| 348 | "HTML", |
| 349 | []attachment{{"name.html", strings.NewReader("<html><head></head></html>"), false, ""}}, |
| 350 | "text/html; charset=utf-8;\n\tfilename=\"name.html\"; name=\"name.html\"", |
| 351 | "attachment;\n\tfilename=\"name.html\"; name=\"name.html\"", |
| 352 | "PGh0bWw+PGhlYWQ+PC9oZWFkPjwvaHRtbD4=", |
| 353 | false, |
| 354 | }, |
| 355 | { |
| 356 | "HTML - wrong extension", |
| 357 | []attachment{{"name.png", strings.NewReader("<html><head></head></html>"), false, ""}}, |
| 358 | "text/html; charset=utf-8;\n\tfilename=\"name.png\"; name=\"name.png\"", |
| 359 | "attachment;\n\tfilename=\"name.png\"; name=\"name.png\"", |
| 360 | "PGh0bWw+PGhlYWQ+PC9oZWFkPjwvaHRtbD4=", |
| 361 | false, |
| 362 | }, |
| 363 | |
| 364 | // inline attachments |
| 365 | { |
| 366 | "Empty inline", |
| 367 | []attachment{{"Empty", &bytes.Buffer{}, true, ""}}, |
| 368 | "text/plain; charset=utf-8;\n\tfilename=\"Empty\"; name=\"Empty\"", |
| 369 | "inline;\n\tfilename=\"Empty\"; name=\"Empty\"", |
| 370 | "", |
| 371 | false, |
| 372 | }, |
| 373 | { |
| 374 | "Short string inline", |
| 375 | []attachment{{"advice", strings.NewReader("Don't Panic"), true, ""}}, |
| 376 | "text/plain; charset=utf-8;\n\tfilename=\"advice\"; name=\"advice\"", |
| 377 | "inline;\n\tfilename=\"advice\"; name=\"advice\"", |
| 378 | "RG9uJ3QgUGFuaWM=", |
| 379 | false, |
| 380 | }, |
| 381 | { |
| 382 | "Longer string inline", |
| 383 | []attachment{ |
| 384 | { |
| 385 | "partyinvite.txt", |
| 386 | strings.NewReader( |
| 387 | "If Baldrick served a meal at HQ he would be arrested for the biggest " + |
| 388 | "mass poisoning since Lucretia Borgia invited 500 friends for a Wine and Anthrax Party.", |
| 389 | ), |
| 390 | true, |
| 391 | "", |
| 392 | }, |
| 393 | }, |
| 394 | "text/plain; charset=utf-8;\n\tfilename=\"partyinvite.txt\"; name=\"partyinvite.txt\"", |
| 395 | "inline;\n\tfilename=\"partyinvite.txt\"; name=\"partyinvite.txt\"", |
| 396 | "SWYgQmFsZHJpY2sgc2VydmVkIGEgbWVhbCBhdCBIUSBoZSB3b3VsZCBiZSBhcnJlc3Rl" + |
| 397 | "ZCBmb3IgdGhlIGJpZ2dlc3QgbWFzcyBwb2lzb25pbmcgc2luY2UgTHVjcmV0aWEgQm9y" + |
| 398 | "Z2lhIGludml0ZWQgNTAwIGZyaWVuZHMgZm9yIGEgV2luZSBhbmQgQW50aHJheCBQYXJ0eS4=", |
| 399 | false, |
| 400 | }, |
| 401 | { |
| 402 | "String >512 characters (content type sniff) inline", |
| 403 | []attachment{ |
| 404 | { |
| 405 | "qed.txt", |
| 406 | strings.NewReader( |
| 407 | `Now it is such a bizarrely improbable coincidence that anything so mind-bogglingly ` + |
| 408 | `useful could have evolved purely by chance that some thinkers have chosen to see it ` + |
| 409 | `as the final and clinching proof of the non-existence of God. The argument goes something ` + |
| 410 | `like this: "I refuse to prove that I exist," says God, "for proof denies faith, and ` + |
| 411 | `without faith I am nothing." "But," says Man, "The Babel fish is a dead giveaway, ` + |
| 412 | `isn't it? It could not have evolved by chance. It proves you exist, and so therefore, ` + |
| 413 | `by your own arguments, you don't. QED." "Oh dear," says God, "I hadn't thought of ` + |
| 414 | `that," and promptly vanishes in a puff of logic. "Oh, that was easy," says Man, and ` + |
| 415 | `for an encore goes on to prove that black is white and gets himself killed on the next ` + |
| 416 | `zebra crossing.`, |
| 417 | ), |
| 418 | true, |
| 419 | "", |
| 420 | }, |
| 421 | }, |
| 422 | "text/plain; charset=utf-8;\n\tfilename=\"qed.txt\"; name=\"qed.txt\"", |
| 423 | "inline;\n\tfilename=\"qed.txt\"; name=\"qed.txt\"", |
| 424 | "Tm93IGl0IGlzIHN1Y2ggYSBiaXphcnJlbHkgaW1wcm9iYWJsZSBjb2luY2lkZW5jZSB0a" + |
| 425 | "GF0IGFueXRoaW5nIHNvIG1pbmQtYm9nZ2xpbmdseSB1c2VmdWwgY291bGQgaGF2ZSBldm" + |
| 426 | "9sdmVkIHB1cmVseSBieSBjaGFuY2UgdGhhdCBzb21lIHRoaW5rZXJzIGhhdmUgY2hvc2V" + |
| 427 | "uIHRvIHNlZSBpdCBhcyB0aGUgZmluYWwgYW5kIGNsaW5jaGluZyBwcm9vZiBvZiB0aGUg" + |
| 428 | "bm9uLWV4aXN0ZW5jZSBvZiBHb2QuIFRoZSBhcmd1bWVudCBnb2VzIHNvbWV0aGluZyBsa" + |
| 429 | "WtlIHRoaXM6ICJJIHJlZnVzZSB0byBwcm92ZSB0aGF0IEkgZXhpc3QsIiBzYXlzIEdvZC" + |
| 430 | "wgImZvciBwcm9vZiBkZW5pZXMgZmFpdGgsIGFuZCB3aXRob3V0IGZhaXRoIEkgYW0gbm9" + |
| 431 | "0aGluZy4iICJCdXQsIiBzYXlzIE1hbiwgIlRoZSBCYWJlbCBmaXNoIGlzIGEgZGVhZCBn" + |
| 432 | "aXZlYXdheSwgaXNuJ3QgaXQ/IEl0IGNvdWxkIG5vdCBoYXZlIGV2b2x2ZWQgYnkgY2hhb" + |
| 433 | "mNlLiBJdCBwcm92ZXMgeW91IGV4aXN0LCBhbmQgc28gdGhlcmVmb3JlLCBieSB5b3VyIG" + |
| 434 | "93biBhcmd1bWVudHMsIHlvdSBkb24ndC4gUUVELiIgIk9oIGRlYXIsIiBzYXlzIEdvZCw" + |
| 435 | "gIkkgaGFkbid0IHRob3VnaHQgb2YgdGhhdCwiIGFuZCBwcm9tcHRseSB2YW5pc2hlcyBp" + |
| 436 | "biBhIHB1ZmYgb2YgbG9naWMuICJPaCwgdGhhdCB3YXMgZWFzeSwiIHNheXMgTWFuLCBhb" + |
| 437 | "mQgZm9yIGFuIGVuY29yZSBnb2VzIG9uIHRvIHByb3ZlIHRoYXQgYmxhY2sgaXMgd2hpdG" + |
| 438 | "UgYW5kIGdldHMgaGltc2VsZiBraWxsZWQgb24gdGhlIG5leHQgemVicmEgY3Jvc3Npbmcu", |
| 439 | false, |
| 440 | }, |
| 441 | { |
| 442 | "HTML inline", |
| 443 | []attachment{{"name.html", strings.NewReader("<html><head></head></html>"), true, ""}}, |
| 444 | "text/html; charset=utf-8;\n\tfilename=\"name.html\"; name=\"name.html\"", |
| 445 | "inline;\n\tfilename=\"name.html\"; name=\"name.html\"", |
| 446 | "PGh0bWw+PGhlYWQ+PC9oZWFkPjwvaHRtbD4=", |
| 447 | false, |
| 448 | }, |
| 449 | { |
| 450 | "HTML - wrong extension inline", |
| 451 | []attachment{{"name.png", strings.NewReader("<html><head></head></html>"), true, ""}}, |
| 452 | "text/html; charset=utf-8;\n\tfilename=\"name.png\"; name=\"name.png\"", |
| 453 | "inline;\n\tfilename=\"name.png\"; name=\"name.png\"", |
| 454 | "PGh0bWw+PGhlYWQ+PC9oZWFkPjwvaHRtbD4=", |
| 455 | false, |
| 456 | }, |
| 457 | { |
| 458 | "String >512 characters (read full buffer)", |
| 459 | []attachment{ |
| 460 | { |
| 461 | "qed.txt", |
| 462 | base64.NewDecoder(base64.StdEncoding, strings.NewReader( |
| 463 | "Tm93IGl0IGlzIHN1Y2ggYSBiaXphcnJlbHkgaW1wcm9iYWJsZSBjb2luY2lkZW5jZSB0a"+ |
| 464 | "GF0IGFueXRoaW5nIHNvIG1pbmQtYm9nZ2xpbmdseSB1c2VmdWwgY291bGQgaGF2ZSBldm"+ |
| 465 | "9sdmVkIHB1cmVseSBieSBjaGFuY2UgdGhhdCBzb21lIHRoaW5rZXJzIGhhdmUgY2hvc2V"+ |
| 466 | "uIHRvIHNlZSBpdCBhcyB0aGUgZmluYWwgYW5kIGNsaW5jaGluZyBwcm9vZiBvZiB0aGUg"+ |
| 467 | "bm9uLWV4aXN0ZW5jZSBvZiBHb2QuIFRoZSBhcmd1bWVudCBnb2VzIHNvbWV0aGluZyBsa"+ |
| 468 | "WtlIHRoaXM6ICJJIHJlZnVzZSB0byBwcm92ZSB0aGF0IEkgZXhpc3QsIiBzYXlzIEdvZC"+ |
| 469 | "wgImZvciBwcm9vZiBkZW5pZXMgZmFpdGgsIGFuZCB3aXRob3V0IGZhaXRoIEkgYW0gbm9"+ |
| 470 | "0aGluZy4iICJCdXQsIiBzYXlzIE1hbiwgIlRoZSBCYWJlbCBmaXNoIGlzIGEgZGVhZCBn"+ |
| 471 | "aXZlYXdheSwgaXNuJ3QgaXQ/IEl0IGNvdWxkIG5vdCBoYXZlIGV2b2x2ZWQgYnkgY2hhb"+ |
| 472 | "mNlLiBJdCBwcm92ZXMgeW91IGV4aXN0LCBhbmQgc28gdGhlcmVmb3JlLCBieSB5b3VyIG"+ |
| 473 | "93biBhcmd1bWVudHMsIHlvdSBkb24ndC4gUUVELiIgIk9oIGRlYXIsIiBzYXlzIEdvZCw"+ |
| 474 | "gIkkgaGFkbid0IHRob3VnaHQgb2YgdGhhdCwiIGFuZCBwcm9tcHRseSB2YW5pc2hlcyBp"+ |
| 475 | "biBhIHB1ZmYgb2YgbG9naWMuICJPaCwgdGhhdCB3YXMgZWFzeSwiIHNheXMgTWFuLCBhb"+ |
| 476 | "mQgZm9yIGFuIGVuY29yZSBnb2VzIG9uIHRvIHByb3ZlIHRoYXQgYmxhY2sgaXMgd2hpdG"+ |
| 477 | "UgYW5kIGdldHMgaGltc2VsZiBraWxsZWQgb24gdGhlIG5leHQgemVicmEgY3Jvc3Npbmcu", |
| 478 | )), |
| 479 | false, |
| 480 | "", |
| 481 | }, |
| 482 | }, |
| 483 | "text/plain; charset=utf-8;\n\tfilename=\"qed.txt\"; name=\"qed.txt\"", |
| 484 | "attachment;\n\tfilename=\"qed.txt\"; name=\"qed.txt\"", |
| 485 | "Tm93IGl0IGlzIHN1Y2ggYSBiaXphcnJlbHkgaW1wcm9iYWJsZSBjb2luY2lkZW5jZSB0a" + |
| 486 | "GF0IGFueXRoaW5nIHNvIG1pbmQtYm9nZ2xpbmdseSB1c2VmdWwgY291bGQgaGF2ZSBldm" + |
| 487 | "9sdmVkIHB1cmVseSBieSBjaGFuY2UgdGhhdCBzb21lIHRoaW5rZXJzIGhhdmUgY2hvc2V" + |
| 488 | "uIHRvIHNlZSBpdCBhcyB0aGUgZmluYWwgYW5kIGNsaW5jaGluZyBwcm9vZiBvZiB0aGUg" + |
| 489 | "bm9uLWV4aXN0ZW5jZSBvZiBHb2QuIFRoZSBhcmd1bWVudCBnb2VzIHNvbWV0aGluZyBsa" + |
| 490 | "WtlIHRoaXM6ICJJIHJlZnVzZSB0byBwcm92ZSB0aGF0IEkgZXhpc3QsIiBzYXlzIEdvZC" + |
| 491 | "wgImZvciBwcm9vZiBkZW5pZXMgZmFpdGgsIGFuZCB3aXRob3V0IGZhaXRoIEkgYW0gbm9" + |
| 492 | "0aGluZy4iICJCdXQsIiBzYXlzIE1hbiwgIlRoZSBCYWJlbCBmaXNoIGlzIGEgZGVhZCBn" + |
| 493 | "aXZlYXdheSwgaXNuJ3QgaXQ/IEl0IGNvdWxkIG5vdCBoYXZlIGV2b2x2ZWQgYnkgY2hhb" + |
| 494 | "mNlLiBJdCBwcm92ZXMgeW91IGV4aXN0LCBhbmQgc28gdGhlcmVmb3JlLCBieSB5b3VyIG" + |
| 495 | "93biBhcmd1bWVudHMsIHlvdSBkb24ndC4gUUVELiIgIk9oIGRlYXIsIiBzYXlzIEdvZCw" + |
| 496 | "gIkkgaGFkbid0IHRob3VnaHQgb2YgdGhhdCwiIGFuZCBwcm9tcHRseSB2YW5pc2hlcyBp" + |
| 497 | "biBhIHB1ZmYgb2YgbG9naWMuICJPaCwgdGhhdCB3YXMgZWFzeSwiIHNheXMgTWFuLCBhb" + |
| 498 | "mQgZm9yIGFuIGVuY29yZSBnb2VzIG9uIHRvIHByb3ZlIHRoYXQgYmxhY2sgaXMgd2hpdG" + |
| 499 | "UgYW5kIGdldHMgaGltc2VsZiBraWxsZWQgb24gdGhlIG5leHQgemVicmEgY3Jvc3Npbmcu", |
| 500 | false, |
| 501 | }, |
| 502 | } |
| 503 | for _, tt := range tests { |
| 504 | tt := tt |
| 505 | t.Run(tt.name, func(t *testing.T) { |
| 506 | t.Parallel() |
| 507 | |
| 508 | m := MailYak{attachments: tt.rattachments} |
| 509 | pc := testPartCreator{} |
| 510 | |
| 511 | if err := m.writeAttachments(&pc, nopBuilder{}); (err != nil) != tt.wantErr { |
| 512 | t.Errorf("%q. MailYak.writeAttachments() error = %v, wantErr %v", tt.name, err, tt.wantErr) |
| 513 | } |
| 514 | |
| 515 | // Ensure there's an attachment |
| 516 | if len(pc.attachments) != 1 { |
| 517 | t.Fatalf("%q. MailYak.writeAttachments() unexpected number of attachments = %v, want 1", tt.name, len(pc.attachments)) |
| 518 | } |
| 519 | |
| 520 | if pc.attachments[0].contentType != tt.ctype { |
| 521 | t.Errorf("%q. MailYak.writeAttachments() content type = %v, want %v", tt.name, pc.attachments[0].contentType, tt.ctype) |
| 522 | } |
| 523 | |
| 524 | if pc.attachments[0].disposition != tt.disp { |
| 525 | t.Errorf("%q. MailYak.writeAttachments() disposition = %v, want %v", tt.name, pc.attachments[0].disposition, tt.disp) |
| 526 | } |
| 527 | |
| 528 | if pc.attachments[0].data.String() != tt.data { |
| 529 | t.Errorf("%q. MailYak.writeAttachments() data = %v, want %v", tt.name, pc.attachments[0].data.String(), tt.data) |
| 530 | } |
| 531 | }) |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | // TestMailYakWriteAttachments_multipleAttachments ensures multiple attachments |
| 536 | // are correctly handled |
| 537 | func TestMailYakWriteAttachments_multipleAttachments(t *testing.T) { |
| 538 | t.Parallel() |
| 539 | |
| 540 | tests := []struct { |
| 541 | // Test description. |
| 542 | name string |
| 543 | // Receiver fields. |
| 544 | rattachments []attachment |
| 545 | // Expected results. |
| 546 | want []testAttachment |
| 547 | wantErr bool |
| 548 | }{ |
| 549 | { |
| 550 | "Single Attachment", |
| 551 | []attachment{{"name.txt", strings.NewReader("test"), false, ""}}, |
| 552 | []testAttachment{ |
| 553 | { |
| 554 | contentType: "text/plain; charset=utf-8;\n\tfilename=\"name.txt\"; name=\"name.txt\"", |
| 555 | disposition: "attachment;\n\tfilename=\"name.txt\"; name=\"name.txt\"", |
| 556 | data: *bytes.NewBufferString("dGVzdA=="), |
| 557 | }, |
| 558 | }, |
| 559 | false, |
| 560 | }, |
| 561 | { |
| 562 | "Single Attachment with specified MIME type", |
| 563 | []attachment{{"name.txt", strings.NewReader("test"), false, "text/csv; charset=utf-8"}}, |
| 564 | []testAttachment{ |
| 565 | { |
| 566 | contentType: "text/csv; charset=utf-8;\n\tfilename=\"name.txt\"; name=\"name.txt\"", |
| 567 | disposition: "attachment;\n\tfilename=\"name.txt\"; name=\"name.txt\"", |
| 568 | data: *bytes.NewBufferString("dGVzdA=="), |
| 569 | }, |
| 570 | }, |
| 571 | false, |
| 572 | }, |
| 573 | { |
| 574 | "Multiple Attachment - same types", |
| 575 | []attachment{ |
| 576 | {"name.txt", strings.NewReader("test"), false, ""}, |
| 577 | {"different.txt", strings.NewReader("another"), false, ""}, |
| 578 | }, |
| 579 | []testAttachment{ |
| 580 | { |
| 581 | contentType: "text/plain; charset=utf-8;\n\tfilename=\"name.txt\"; name=\"name.txt\"", |
| 582 | disposition: "attachment;\n\tfilename=\"name.txt\"; name=\"name.txt\"", |
| 583 | data: *bytes.NewBufferString("dGVzdA=="), |
| 584 | }, |
| 585 | { |
| 586 | contentType: "text/plain; charset=utf-8;\n\tfilename=\"different.txt\"; name=\"different.txt\"", |
| 587 | disposition: "attachment;\n\tfilename=\"different.txt\"; name=\"different.txt\"", |
| 588 | data: *bytes.NewBufferString("YW5vdGhlcg=="), |
| 589 | }, |
| 590 | }, |
| 591 | false, |
| 592 | }, |
| 593 | { |
| 594 | "Multiple Attachment - different types", |
| 595 | []attachment{ |
| 596 | {"name.txt", strings.NewReader("test"), false, ""}, |
| 597 | {"html.txt", strings.NewReader("<html><head></head></html>"), false, ""}, |
| 598 | }, |
| 599 | []testAttachment{ |
| 600 | { |
| 601 | contentType: "text/plain; charset=utf-8;\n\tfilename=\"name.txt\"; name=\"name.txt\"", |
| 602 | disposition: "attachment;\n\tfilename=\"name.txt\"; name=\"name.txt\"", |
| 603 | data: *bytes.NewBufferString("dGVzdA=="), |
| 604 | }, |
| 605 | { |
| 606 | contentType: "text/html; charset=utf-8;\n\tfilename=\"html.txt\"; name=\"html.txt\"", |
| 607 | disposition: "attachment;\n\tfilename=\"html.txt\"; name=\"html.txt\"", |
| 608 | data: *bytes.NewBufferString("PGh0bWw+PGhlYWQ+PC9oZWFkPjwvaHRtbD4="), |
| 609 | }, |
| 610 | }, |
| 611 | false, |
| 612 | }, |
| 613 | { |
| 614 | "Multiple Attachment - different specified MIME types", |
| 615 | []attachment{ |
| 616 | {"name.txt", strings.NewReader("test"), false, "text/csv; charset=utf-8"}, |
| 617 | {"html.txt", strings.NewReader("<html><head></head></html>"), false, "application/xml"}, |
| 618 | }, |
| 619 | []testAttachment{ |
| 620 | { |
| 621 | contentType: "text/csv; charset=utf-8;\n\tfilename=\"name.txt\"; name=\"name.txt\"", |
| 622 | disposition: "attachment;\n\tfilename=\"name.txt\"; name=\"name.txt\"", |
| 623 | data: *bytes.NewBufferString("dGVzdA=="), |
| 624 | }, |
| 625 | { |
| 626 | contentType: "application/xml;\n\tfilename=\"html.txt\"; name=\"html.txt\"", |
| 627 | disposition: "attachment;\n\tfilename=\"html.txt\"; name=\"html.txt\"", |
| 628 | data: *bytes.NewBufferString("PGh0bWw+PGhlYWQ+PC9oZWFkPjwvaHRtbD4="), |
| 629 | }, |
| 630 | }, |
| 631 | false, |
| 632 | }, |
| 633 | { |
| 634 | "Multiple Attachments - >512 bytes, longer first", |
| 635 | []attachment{ |
| 636 | { |
| 637 | "550.txt", |
| 638 | strings.NewReader( |
| 639 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris ut nisl felis. " + |
| 640 | "Aenean felis justo, gravida eget leo aliquet, molestie aliquam risus. Vestibulum " + |
| 641 | "et nibh rhoncus, malesuada tellus eget, pellentesque diam. Sed venenatis vitae " + |
| 642 | "erat vel ullamcorper. Aenean rutrum pulvinar purus eget cursus. Integer at iaculis " + |
| 643 | "arcu. Maecenas mollis nulla dolor, et ultricies massa posuere quis. Nulla facilisi. " + |
| 644 | "Proin luctus nec nisl at imperdiet. Nulla dapibus purus ut lorem faucibus, at gravida " + |
| 645 | "tellus euismod. Curabitur ex risus, egestas in porta amet.", |
| 646 | ), |
| 647 | false, |
| 648 | "", |
| 649 | }, |
| 650 | { |
| 651 | "520.txt", strings.NewReader( |
| 652 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec eu vestibulum dolor. " + |
| 653 | "Nunc ac posuere felis, a mattis leo. Duis elementum tempor leo, sed efficitur nunc. " + |
| 654 | "Cras ornare feugiat vulputate. Maecenas sit amet felis lobortis ipsum dignissim euismod. " + |
| 655 | "Vestibulum id ullamcorper nulla, tincidunt hendrerit justo. Donec vitae eros quam. Nulla " + |
| 656 | "accumsan porta sapien, in consequat mauris fermentum ac. In at sem lobortis, auctor metus " + |
| 657 | "rutrum, blandit ipsum. Praesent commodo porta semper. Etiam dignissim libero nullam.", |
| 658 | ), |
| 659 | false, |
| 660 | "", |
| 661 | }, |
| 662 | }, |
| 663 | []testAttachment{ |
| 664 | { |
| 665 | contentType: "text/plain; charset=utf-8;\n\tfilename=\"550.txt\"; name=\"550.txt\"", |
| 666 | disposition: "attachment;\n\tfilename=\"550.txt\"; name=\"550.txt\"", |
| 667 | data: *bytes.NewBufferString( |
| 668 | "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gTWF1cmlzIHV0IG5pc" + |
| 669 | "2wgZmVsaXMuIEFlbmVhbiBmZWxpcyBqdXN0bywgZ3JhdmlkYSBlZ2V0IGxlbyBhbGlxdWV0LCBtb2xlc3RpZSBhbGlxdW" + |
| 670 | "FtIHJpc3VzLiBWZXN0aWJ1bHVtIGV0IG5pYmggcmhvbmN1cywgbWFsZXN1YWRhIHRlbGx1cyBlZ2V0LCBwZWxsZW50ZXN" + |
| 671 | "xdWUgZGlhbS4gU2VkIHZlbmVuYXRpcyB2aXRhZSBlcmF0IHZlbCB1bGxhbWNvcnBlci4gQWVuZWFuIHJ1dHJ1bSBwdWx2" + |
| 672 | "aW5hciBwdXJ1cyBlZ2V0IGN1cnN1cy4gSW50ZWdlciBhdCBpYWN1bGlzIGFyY3UuIE1hZWNlbmFzIG1vbGxpcyBudWxsY" + |
| 673 | "SBkb2xvciwgZXQgdWx0cmljaWVzIG1hc3NhIHBvc3VlcmUgcXVpcy4gTnVsbGEgZmFjaWxpc2kuIFByb2luIGx1Y3R1cy" + |
| 674 | "BuZWMgbmlzbCBhdCBpbXBlcmRpZXQuIE51bGxhIGRhcGlidXMgcHVydXMgdXQgbG9yZW0gZmF1Y2lidXMsIGF0IGdyYXZ" + |
| 675 | "pZGEgdGVsbHVzIGV1aXNtb2QuIEN1cmFiaXR1ciBleCByaXN1cywgZWdlc3RhcyBpbiBwb3J0YSBhbWV0Lg==", |
| 676 | ), |
| 677 | }, |
| 678 | { |
| 679 | contentType: "text/plain; charset=utf-8;\n\tfilename=\"520.txt\"; name=\"520.txt\"", |
| 680 | disposition: "attachment;\n\tfilename=\"520.txt\"; name=\"520.txt\"", |
| 681 | data: *bytes.NewBufferString( |
| 682 | "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gRG9uZWMgZXUgdmVz" + |
| 683 | "dGlidWx1bSBkb2xvci4gTnVuYyBhYyBwb3N1ZXJlIGZlbGlzLCBhIG1hdHRpcyBsZW8uIER1aXMgZWxlbWVudHVtIHRl" + |
| 684 | "bXBvciBsZW8sIHNlZCBlZmZpY2l0dXIgbnVuYy4gQ3JhcyBvcm5hcmUgZmV1Z2lhdCB2dWxwdXRhdGUuIE1hZWNlbmFz" + |
| 685 | "IHNpdCBhbWV0IGZlbGlzIGxvYm9ydGlzIGlwc3VtIGRpZ25pc3NpbSBldWlzbW9kLiBWZXN0aWJ1bHVtIGlkIHVsbGFt" + |
| 686 | "Y29ycGVyIG51bGxhLCB0aW5jaWR1bnQgaGVuZHJlcml0IGp1c3RvLiBEb25lYyB2aXRhZSBlcm9zIHF1YW0uIE51bGxh" + |
| 687 | "IGFjY3Vtc2FuIHBvcnRhIHNhcGllbiwgaW4gY29uc2VxdWF0IG1hdXJpcyBmZXJtZW50dW0gYWMuIEluIGF0IHNlbSBs" + |
| 688 | "b2JvcnRpcywgYXVjdG9yIG1ldHVzIHJ1dHJ1bSwgYmxhbmRpdCBpcHN1bS4gUHJhZXNlbnQgY29tbW9kbyBwb3J0YSBz" + |
| 689 | "ZW1wZXIuIEV0aWFtIGRpZ25pc3NpbSBsaWJlcm8gbnVsbGFtLg==", |
| 690 | ), |
| 691 | }, |
| 692 | }, |
| 693 | false, |
| 694 | }, |
| 695 | { |
| 696 | "Multiple Attachments - >512 bytes, shorter first", |
| 697 | []attachment{ |
| 698 | { |
| 699 | "520.txt", |
| 700 | strings.NewReader( |
| 701 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec eu vestibulum dolor. Nunc ac " + |
| 702 | "posuere felis, a mattis leo. Duis elementum tempor leo, sed efficitur nunc. Cras ornare " + |
| 703 | "feugiat vulputate. Maecenas sit amet felis lobortis ipsum dignissim euismod. Vestibulum " + |
| 704 | "id ullamcorper nulla, tincidunt hendrerit justo. Donec vitae eros quam. Nulla accumsan " + |
| 705 | "porta sapien, in consequat mauris fermentum ac. In at sem lobortis, auctor metus rutrum, " + |
| 706 | "blandit ipsum. Praesent commodo porta semper. Etiam dignissim libero nullam.", |
| 707 | ), |
| 708 | false, |
| 709 | "", |
| 710 | }, |
| 711 | { |
| 712 | "550.txt", |
| 713 | strings.NewReader( |
| 714 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris ut nisl felis. Aenean felis " + |
| 715 | "justo, gravida eget leo aliquet, molestie aliquam risus. Vestibulum et nibh rhoncus, " + |
| 716 | "malesuada tellus eget, pellentesque diam. Sed venenatis vitae erat vel ullamcorper. " + |
| 717 | "Aenean rutrum pulvinar purus eget cursus. Integer at iaculis arcu. Maecenas mollis " + |
| 718 | "nulla dolor, et ultricies massa posuere quis. Nulla facilisi. Proin luctus nec nisl " + |
| 719 | "at imperdiet. Nulla dapibus purus ut lorem faucibus, at gravida tellus euismod. Curabitur " + |
| 720 | "ex risus, egestas in porta amet.", |
| 721 | ), |
| 722 | false, |
| 723 | "", |
| 724 | }, |
| 725 | }, |
| 726 | []testAttachment{ |
| 727 | { |
| 728 | contentType: "text/plain; charset=utf-8;\n\tfilename=\"520.txt\"; name=\"520.txt\"", |
| 729 | disposition: "attachment;\n\tfilename=\"520.txt\"; name=\"520.txt\"", |
| 730 | data: *bytes.NewBufferString( |
| 731 | "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gRG9uZWMgZXUgdmVz" + |
| 732 | "dGlidWx1bSBkb2xvci4gTnVuYyBhYyBwb3N1ZXJlIGZlbGlzLCBhIG1hdHRpcyBsZW8uIER1aXMgZWxlbWVudHVtIHRl" + |
| 733 | "bXBvciBsZW8sIHNlZCBlZmZpY2l0dXIgbnVuYy4gQ3JhcyBvcm5hcmUgZmV1Z2lhdCB2dWxwdXRhdGUuIE1hZWNlbmFz" + |
| 734 | "IHNpdCBhbWV0IGZlbGlzIGxvYm9ydGlzIGlwc3VtIGRpZ25pc3NpbSBldWlzbW9kLiBWZXN0aWJ1bHVtIGlkIHVsbGFt" + |
| 735 | "Y29ycGVyIG51bGxhLCB0aW5jaWR1bnQgaGVuZHJlcml0IGp1c3RvLiBEb25lYyB2aXRhZSBlcm9zIHF1YW0uIE51bGxh" + |
| 736 | "IGFjY3Vtc2FuIHBvcnRhIHNhcGllbiwgaW4gY29uc2VxdWF0IG1hdXJpcyBmZXJtZW50dW0gYWMuIEluIGF0IHNlbSBs" + |
| 737 | "b2JvcnRpcywgYXVjdG9yIG1ldHVzIHJ1dHJ1bSwgYmxhbmRpdCBpcHN1bS4gUHJhZXNlbnQgY29tbW9kbyBwb3J0YSBz" + |
| 738 | "ZW1wZXIuIEV0aWFtIGRpZ25pc3NpbSBsaWJlcm8gbnVsbGFtLg==", |
| 739 | ), |
| 740 | }, |
| 741 | { |
| 742 | contentType: "text/plain; charset=utf-8;\n\tfilename=\"550.txt\"; name=\"550.txt\"", |
| 743 | disposition: "attachment;\n\tfilename=\"550.txt\"; name=\"550.txt\"", |
| 744 | data: *bytes.NewBufferString( |
| 745 | "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gTWF1cmlzIHV0IG5p" + |
| 746 | "c2wgZmVsaXMuIEFlbmVhbiBmZWxpcyBqdXN0bywgZ3JhdmlkYSBlZ2V0IGxlbyBhbGlxdWV0LCBtb2xlc3RpZSBhbGlx" + |
| 747 | "dWFtIHJpc3VzLiBWZXN0aWJ1bHVtIGV0IG5pYmggcmhvbmN1cywgbWFsZXN1YWRhIHRlbGx1cyBlZ2V0LCBwZWxsZW50" + |
| 748 | "ZXNxdWUgZGlhbS4gU2VkIHZlbmVuYXRpcyB2aXRhZSBlcmF0IHZlbCB1bGxhbWNvcnBlci4gQWVuZWFuIHJ1dHJ1bSBw" + |
| 749 | "dWx2aW5hciBwdXJ1cyBlZ2V0IGN1cnN1cy4gSW50ZWdlciBhdCBpYWN1bGlzIGFyY3UuIE1hZWNlbmFzIG1vbGxpcyBu" + |
| 750 | "dWxsYSBkb2xvciwgZXQgdWx0cmljaWVzIG1hc3NhIHBvc3VlcmUgcXVpcy4gTnVsbGEgZmFjaWxpc2kuIFByb2luIGx1" + |
| 751 | "Y3R1cyBuZWMgbmlzbCBhdCBpbXBlcmRpZXQuIE51bGxhIGRhcGlidXMgcHVydXMgdXQgbG9yZW0gZmF1Y2lidXMsIGF0" + |
| 752 | "IGdyYXZpZGEgdGVsbHVzIGV1aXNtb2QuIEN1cmFiaXR1ciBleCByaXN1cywgZWdlc3RhcyBpbiBwb3J0YSBhbWV0Lg==", |
| 753 | ), |
| 754 | }, |
| 755 | }, |
| 756 | false, |
| 757 | }, |
| 758 | |
| 759 | // inline attachments |
| 760 | { |
| 761 | "Single Inline Attachment", |
| 762 | []attachment{{"name.txt", strings.NewReader("test"), true, ""}}, |
| 763 | []testAttachment{ |
| 764 | { |
| 765 | contentType: "text/plain; charset=utf-8;\n\tfilename=\"name.txt\"; name=\"name.txt\"", |
| 766 | disposition: "inline;\n\tfilename=\"name.txt\"; name=\"name.txt\"", |
| 767 | data: *bytes.NewBufferString("dGVzdA=="), |
| 768 | }, |
| 769 | }, |
| 770 | false, |
| 771 | }, |
| 772 | { |
| 773 | "Single Inline Attachment with specified MIME type", |
| 774 | []attachment{{"name.txt", strings.NewReader("test"), true, "text/csv; charset=utf-8"}}, |
| 775 | []testAttachment{ |
| 776 | { |
| 777 | contentType: "text/csv; charset=utf-8;\n\tfilename=\"name.txt\"; name=\"name.txt\"", |
| 778 | disposition: "inline;\n\tfilename=\"name.txt\"; name=\"name.txt\"", |
| 779 | data: *bytes.NewBufferString("dGVzdA=="), |
| 780 | }, |
| 781 | }, |
| 782 | false, |
| 783 | }, |
| 784 | { |
| 785 | "Multiple Inline Attachments - same types", |
| 786 | []attachment{ |
| 787 | {"name.txt", strings.NewReader("test"), true, ""}, |
| 788 | {"different.txt", strings.NewReader("another"), true, ""}, |
| 789 | }, |
| 790 | []testAttachment{ |
| 791 | { |
| 792 | contentType: "text/plain; charset=utf-8;\n\tfilename=\"name.txt\"; name=\"name.txt\"", |
| 793 | disposition: "inline;\n\tfilename=\"name.txt\"; name=\"name.txt\"", |
| 794 | data: *bytes.NewBufferString("dGVzdA=="), |
| 795 | }, |
| 796 | { |
| 797 | contentType: "text/plain; charset=utf-8;\n\tfilename=\"different.txt\"; name=\"different.txt\"", |
| 798 | disposition: "inline;\n\tfilename=\"different.txt\"; name=\"different.txt\"", |
| 799 | data: *bytes.NewBufferString("YW5vdGhlcg=="), |
| 800 | }, |
| 801 | }, |
| 802 | false, |
| 803 | }, |
| 804 | { |
| 805 | "Multiple Attachments - One Inline, One not", |
| 806 | []attachment{ |
| 807 | {"name.txt", strings.NewReader("test"), false, ""}, |
| 808 | {"different.txt", strings.NewReader("another"), true, ""}, |
| 809 | }, |
| 810 | []testAttachment{ |
| 811 | { |
| 812 | contentType: "text/plain; charset=utf-8;\n\tfilename=\"name.txt\"; name=\"name.txt\"", |
| 813 | disposition: "attachment;\n\tfilename=\"name.txt\"; name=\"name.txt\"", |
| 814 | data: *bytes.NewBufferString("dGVzdA=="), |
| 815 | }, |
| 816 | { |
| 817 | contentType: "text/plain; charset=utf-8;\n\tfilename=\"different.txt\"; name=\"different.txt\"", |
| 818 | disposition: "inline;\n\tfilename=\"different.txt\"; name=\"different.txt\"", |
| 819 | data: *bytes.NewBufferString("YW5vdGhlcg=="), |
| 820 | }, |
| 821 | }, |
| 822 | false, |
| 823 | }, |
| 824 | { |
| 825 | "Multiple Inline Attachments - different types", |
| 826 | []attachment{ |
| 827 | {"name.txt", strings.NewReader("test"), true, ""}, |
| 828 | {"html.txt", strings.NewReader("<html><head></head></html>"), true, ""}, |
| 829 | }, |
| 830 | []testAttachment{ |
| 831 | { |
| 832 | contentType: "text/plain; charset=utf-8;\n\tfilename=\"name.txt\"; name=\"name.txt\"", |
| 833 | disposition: "inline;\n\tfilename=\"name.txt\"; name=\"name.txt\"", |
| 834 | data: *bytes.NewBufferString("dGVzdA=="), |
| 835 | }, |
| 836 | { |
| 837 | contentType: "text/html; charset=utf-8;\n\tfilename=\"html.txt\"; name=\"html.txt\"", |
| 838 | disposition: "inline;\n\tfilename=\"html.txt\"; name=\"html.txt\"", |
| 839 | data: *bytes.NewBufferString("PGh0bWw+PGhlYWQ+PC9oZWFkPjwvaHRtbD4="), |
| 840 | }, |
| 841 | }, |
| 842 | false, |
| 843 | }, |
| 844 | { |
| 845 | "Multiple Inline Attachments - specified MIME types", |
| 846 | []attachment{ |
| 847 | {"name.txt", strings.NewReader("test"), true, "text/csv; charset=utf-8"}, |
| 848 | {"different.txt", strings.NewReader("<html><head></head></html>"), true, "application/xml"}, |
| 849 | }, |
| 850 | []testAttachment{ |
| 851 | { |
| 852 | contentType: "text/csv; charset=utf-8;\n\tfilename=\"name.txt\"; name=\"name.txt\"", |
| 853 | disposition: "inline;\n\tfilename=\"name.txt\"; name=\"name.txt\"", |
| 854 | data: *bytes.NewBufferString("dGVzdA=="), |
| 855 | }, |
| 856 | { |
| 857 | contentType: "application/xml;\n\tfilename=\"different.txt\"; name=\"different.txt\"", |
| 858 | disposition: "inline;\n\tfilename=\"different.txt\"; name=\"different.txt\"", |
| 859 | data: *bytes.NewBufferString("PGh0bWw+PGhlYWQ+PC9oZWFkPjwvaHRtbD4="), |
| 860 | }, |
| 861 | }, |
| 862 | false, |
| 863 | }, |
| 864 | { |
| 865 | "Multiple Inline Attachments - >512 bytes, longer first", |
| 866 | []attachment{ |
| 867 | { |
| 868 | "550.txt", |
| 869 | strings.NewReader( |
| 870 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris ut nisl felis. " + |
| 871 | "Aenean felis justo, gravida eget leo aliquet, molestie aliquam risus. Vestibulum " + |
| 872 | "et nibh rhoncus, malesuada tellus eget, pellentesque diam. Sed venenatis vitae " + |
| 873 | "erat vel ullamcorper. Aenean rutrum pulvinar purus eget cursus. Integer at iaculis " + |
| 874 | "arcu. Maecenas mollis nulla dolor, et ultricies massa posuere quis. Nulla facilisi. " + |
| 875 | "Proin luctus nec nisl at imperdiet. Nulla dapibus purus ut lorem faucibus, at gravida " + |
| 876 | "tellus euismod. Curabitur ex risus, egestas in porta amet.", |
| 877 | ), |
| 878 | true, |
| 879 | "", |
| 880 | }, |
| 881 | { |
| 882 | "520.txt", strings.NewReader( |
| 883 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec eu vestibulum dolor. " + |
| 884 | "Nunc ac posuere felis, a mattis leo. Duis elementum tempor leo, sed efficitur nunc. " + |
| 885 | "Cras ornare feugiat vulputate. Maecenas sit amet felis lobortis ipsum dignissim euismod. " + |
| 886 | "Vestibulum id ullamcorper nulla, tincidunt hendrerit justo. Donec vitae eros quam. Nulla " + |
| 887 | "accumsan porta sapien, in consequat mauris fermentum ac. In at sem lobortis, auctor metus " + |
| 888 | "rutrum, blandit ipsum. Praesent commodo porta semper. Etiam dignissim libero nullam.", |
| 889 | ), |
| 890 | true, |
| 891 | "", |
| 892 | }, |
| 893 | }, |
| 894 | []testAttachment{ |
| 895 | { |
| 896 | contentType: "text/plain; charset=utf-8;\n\tfilename=\"550.txt\"; name=\"550.txt\"", |
| 897 | disposition: "inline;\n\tfilename=\"550.txt\"; name=\"550.txt\"", |
| 898 | data: *bytes.NewBufferString( |
| 899 | "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gTWF1cmlzIHV0IG5pc" + |
| 900 | "2wgZmVsaXMuIEFlbmVhbiBmZWxpcyBqdXN0bywgZ3JhdmlkYSBlZ2V0IGxlbyBhbGlxdWV0LCBtb2xlc3RpZSBhbGlxdW" + |
| 901 | "FtIHJpc3VzLiBWZXN0aWJ1bHVtIGV0IG5pYmggcmhvbmN1cywgbWFsZXN1YWRhIHRlbGx1cyBlZ2V0LCBwZWxsZW50ZXN" + |
| 902 | "xdWUgZGlhbS4gU2VkIHZlbmVuYXRpcyB2aXRhZSBlcmF0IHZlbCB1bGxhbWNvcnBlci4gQWVuZWFuIHJ1dHJ1bSBwdWx2" + |
| 903 | "aW5hciBwdXJ1cyBlZ2V0IGN1cnN1cy4gSW50ZWdlciBhdCBpYWN1bGlzIGFyY3UuIE1hZWNlbmFzIG1vbGxpcyBudWxsY" + |
| 904 | "SBkb2xvciwgZXQgdWx0cmljaWVzIG1hc3NhIHBvc3VlcmUgcXVpcy4gTnVsbGEgZmFjaWxpc2kuIFByb2luIGx1Y3R1cy" + |
| 905 | "BuZWMgbmlzbCBhdCBpbXBlcmRpZXQuIE51bGxhIGRhcGlidXMgcHVydXMgdXQgbG9yZW0gZmF1Y2lidXMsIGF0IGdyYXZ" + |
| 906 | "pZGEgdGVsbHVzIGV1aXNtb2QuIEN1cmFiaXR1ciBleCByaXN1cywgZWdlc3RhcyBpbiBwb3J0YSBhbWV0Lg==", |
| 907 | ), |
| 908 | }, |
| 909 | { |
| 910 | contentType: "text/plain; charset=utf-8;\n\tfilename=\"520.txt\"; name=\"520.txt\"", |
| 911 | disposition: "inline;\n\tfilename=\"520.txt\"; name=\"520.txt\"", |
| 912 | data: *bytes.NewBufferString( |
| 913 | "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gRG9uZWMgZXUgdmVz" + |
| 914 | "dGlidWx1bSBkb2xvci4gTnVuYyBhYyBwb3N1ZXJlIGZlbGlzLCBhIG1hdHRpcyBsZW8uIER1aXMgZWxlbWVudHVtIHRl" + |
| 915 | "bXBvciBsZW8sIHNlZCBlZmZpY2l0dXIgbnVuYy4gQ3JhcyBvcm5hcmUgZmV1Z2lhdCB2dWxwdXRhdGUuIE1hZWNlbmFz" + |
| 916 | "IHNpdCBhbWV0IGZlbGlzIGxvYm9ydGlzIGlwc3VtIGRpZ25pc3NpbSBldWlzbW9kLiBWZXN0aWJ1bHVtIGlkIHVsbGFt" + |
| 917 | "Y29ycGVyIG51bGxhLCB0aW5jaWR1bnQgaGVuZHJlcml0IGp1c3RvLiBEb25lYyB2aXRhZSBlcm9zIHF1YW0uIE51bGxh" + |
| 918 | "IGFjY3Vtc2FuIHBvcnRhIHNhcGllbiwgaW4gY29uc2VxdWF0IG1hdXJpcyBmZXJtZW50dW0gYWMuIEluIGF0IHNlbSBs" + |
| 919 | "b2JvcnRpcywgYXVjdG9yIG1ldHVzIHJ1dHJ1bSwgYmxhbmRpdCBpcHN1bS4gUHJhZXNlbnQgY29tbW9kbyBwb3J0YSBz" + |
| 920 | "ZW1wZXIuIEV0aWFtIGRpZ25pc3NpbSBsaWJlcm8gbnVsbGFtLg==", |
| 921 | ), |
| 922 | }, |
| 923 | }, |
| 924 | false, |
| 925 | }, |
| 926 | { |
| 927 | "Multiple Inline Attachments - >512 bytes, shorter first", |
| 928 | []attachment{ |
| 929 | { |
| 930 | "520.txt", |
| 931 | strings.NewReader( |
| 932 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec eu vestibulum dolor. Nunc ac " + |
| 933 | "posuere felis, a mattis leo. Duis elementum tempor leo, sed efficitur nunc. Cras ornare " + |
| 934 | "feugiat vulputate. Maecenas sit amet felis lobortis ipsum dignissim euismod. Vestibulum " + |
| 935 | "id ullamcorper nulla, tincidunt hendrerit justo. Donec vitae eros quam. Nulla accumsan " + |
| 936 | "porta sapien, in consequat mauris fermentum ac. In at sem lobortis, auctor metus rutrum, " + |
| 937 | "blandit ipsum. Praesent commodo porta semper. Etiam dignissim libero nullam.", |
| 938 | ), |
| 939 | true, |
| 940 | "", |
| 941 | }, |
| 942 | { |
| 943 | "550.txt", |
| 944 | strings.NewReader( |
| 945 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris ut nisl felis. Aenean felis " + |
| 946 | "justo, gravida eget leo aliquet, molestie aliquam risus. Vestibulum et nibh rhoncus, " + |
| 947 | "malesuada tellus eget, pellentesque diam. Sed venenatis vitae erat vel ullamcorper. " + |
| 948 | "Aenean rutrum pulvinar purus eget cursus. Integer at iaculis arcu. Maecenas mollis " + |
| 949 | "nulla dolor, et ultricies massa posuere quis. Nulla facilisi. Proin luctus nec nisl " + |
| 950 | "at imperdiet. Nulla dapibus purus ut lorem faucibus, at gravida tellus euismod. Curabitur " + |
| 951 | "ex risus, egestas in porta amet.", |
| 952 | ), |
| 953 | true, |
| 954 | "", |
| 955 | }, |
| 956 | }, |
| 957 | []testAttachment{ |
| 958 | { |
| 959 | contentType: "text/plain; charset=utf-8;\n\tfilename=\"520.txt\"; name=\"520.txt\"", |
| 960 | disposition: "inline;\n\tfilename=\"520.txt\"; name=\"520.txt\"", |
| 961 | data: *bytes.NewBufferString( |
| 962 | "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gRG9uZWMgZXUgdmVz" + |
| 963 | "dGlidWx1bSBkb2xvci4gTnVuYyBhYyBwb3N1ZXJlIGZlbGlzLCBhIG1hdHRpcyBsZW8uIER1aXMgZWxlbWVudHVtIHRl" + |
| 964 | "bXBvciBsZW8sIHNlZCBlZmZpY2l0dXIgbnVuYy4gQ3JhcyBvcm5hcmUgZmV1Z2lhdCB2dWxwdXRhdGUuIE1hZWNlbmFz" + |
| 965 | "IHNpdCBhbWV0IGZlbGlzIGxvYm9ydGlzIGlwc3VtIGRpZ25pc3NpbSBldWlzbW9kLiBWZXN0aWJ1bHVtIGlkIHVsbGFt" + |
| 966 | "Y29ycGVyIG51bGxhLCB0aW5jaWR1bnQgaGVuZHJlcml0IGp1c3RvLiBEb25lYyB2aXRhZSBlcm9zIHF1YW0uIE51bGxh" + |
| 967 | "IGFjY3Vtc2FuIHBvcnRhIHNhcGllbiwgaW4gY29uc2VxdWF0IG1hdXJpcyBmZXJtZW50dW0gYWMuIEluIGF0IHNlbSBs" + |
| 968 | "b2JvcnRpcywgYXVjdG9yIG1ldHVzIHJ1dHJ1bSwgYmxhbmRpdCBpcHN1bS4gUHJhZXNlbnQgY29tbW9kbyBwb3J0YSBz" + |
| 969 | "ZW1wZXIuIEV0aWFtIGRpZ25pc3NpbSBsaWJlcm8gbnVsbGFtLg==", |
| 970 | ), |
| 971 | }, |
| 972 | { |
| 973 | contentType: "text/plain; charset=utf-8;\n\tfilename=\"550.txt\"; name=\"550.txt\"", |
| 974 | disposition: "inline;\n\tfilename=\"550.txt\"; name=\"550.txt\"", |
| 975 | data: *bytes.NewBufferString( |
| 976 | "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gTWF1cmlzIHV0IG5p" + |
| 977 | "c2wgZmVsaXMuIEFlbmVhbiBmZWxpcyBqdXN0bywgZ3JhdmlkYSBlZ2V0IGxlbyBhbGlxdWV0LCBtb2xlc3RpZSBhbGlx" + |
| 978 | "dWFtIHJpc3VzLiBWZXN0aWJ1bHVtIGV0IG5pYmggcmhvbmN1cywgbWFsZXN1YWRhIHRlbGx1cyBlZ2V0LCBwZWxsZW50" + |
| 979 | "ZXNxdWUgZGlhbS4gU2VkIHZlbmVuYXRpcyB2aXRhZSBlcmF0IHZlbCB1bGxhbWNvcnBlci4gQWVuZWFuIHJ1dHJ1bSBw" + |
| 980 | "dWx2aW5hciBwdXJ1cyBlZ2V0IGN1cnN1cy4gSW50ZWdlciBhdCBpYWN1bGlzIGFyY3UuIE1hZWNlbmFzIG1vbGxpcyBu" + |
| 981 | "dWxsYSBkb2xvciwgZXQgdWx0cmljaWVzIG1hc3NhIHBvc3VlcmUgcXVpcy4gTnVsbGEgZmFjaWxpc2kuIFByb2luIGx1" + |
| 982 | "Y3R1cyBuZWMgbmlzbCBhdCBpbXBlcmRpZXQuIE51bGxhIGRhcGlidXMgcHVydXMgdXQgbG9yZW0gZmF1Y2lidXMsIGF0" + |
| 983 | "IGdyYXZpZGEgdGVsbHVzIGV1aXNtb2QuIEN1cmFiaXR1ciBleCByaXN1cywgZWdlc3RhcyBpbiBwb3J0YSBhbWV0Lg==", |
| 984 | ), |
| 985 | }, |
| 986 | }, |
| 987 | false, |
| 988 | }, |
| 989 | } |
| 990 | |
| 991 | for _, tt := range tests { |
| 992 | tt := tt |
| 993 | t.Run(tt.name, func(t *testing.T) { |
| 994 | t.Parallel() |
| 995 | |
| 996 | m := MailYak{attachments: tt.rattachments} |
| 997 | pc := testPartCreator{} |
| 998 | |
| 999 | if err := m.writeAttachments(&pc, nopBuilder{}); (err != nil) != tt.wantErr { |
| 1000 | t.Errorf("%q. MailYak.writeAttachments() error = %v, wantErr %v", tt.name, err, tt.wantErr) |
| 1001 | } |
| 1002 | |
| 1003 | // Did we get enough attachments? |
| 1004 | if len(tt.want) != len(pc.attachments) { |
| 1005 | t.Fatalf("%q. MailYak.writeAttachments() unexpected number of attachments = %v, want %v", tt.name, len(pc.attachments), len(tt.want)) |
| 1006 | } |
| 1007 | |
| 1008 | for i, want := range tt.want { |
| 1009 | got := pc.attachments[i] |
| 1010 | |
| 1011 | if want.contentType != got.contentType { |
| 1012 | t.Errorf("%q. MailYak.writeAttachments() content type = %v, want %v", tt.name, want.contentType, got.contentType) |
| 1013 | } |
| 1014 | |
| 1015 | if want.disposition != got.disposition { |
| 1016 | t.Errorf("%q. MailYak.writeAttachments() disposition = %v, want %v", tt.name, want.disposition, got.disposition) |
| 1017 | } |
| 1018 | |
| 1019 | if !bytes.Equal(want.data.Bytes(), got.data.Bytes()) { |
| 1020 | t.Errorf("%q. MailYak.writeAttachments() data = %v, want %v", tt.name, want.data.String(), got.data.String()) |
| 1021 | } |
| 1022 | } |
| 1023 | }) |
| 1024 | } |
| 1025 | } |