How to use mailParser method in mountebank

Best JavaScript code snippet using mountebank

mailparser.js

Source:mailparser.js Github

copy

Full Screen

1"use strict";2var MailParser = require("../lib/mailparser").MailParser,3 encodinglib = require("encoding"),4 fs = require("fs");5exports["General tests"] = {6 "Many chunks": function(test) {7 var encodedText = "Content-Type: text/plain; charset=utf-8\r\n" +8 "\r\n" +9 "ÕÄ\r\n" +10 "ÖÜ", // \r\nÕÄÖÜ11 mail = new Buffer(encodedText, "utf-8");12 test.expect(1);13 var mailparser = new MailParser();14 for (var i = 0, len = mail.length; i < len; i++) {15 mailparser.write(new Buffer([mail[i]]));16 }17 mailparser.end();18 mailparser.on("end", function(mail) {19 test.equal(mail.text, "ÕÄ\nÖÜ");20 test.done();21 });22 },23 "Many chunks - split line endings": function(test) {24 var chunks = [25 "Content-Type: text/plain; charset=utf-8\r",26 "\nSubject: Hi Mom\r\n\r\n",27 "hello"28 ];29 test.expect(1);30 var mailparser = new MailParser();31 var writeNextChunk = function() {32 var chunk = chunks.shift();33 if (chunk !== undefined) {34 mailparser.write(chunk, 'utf8');35 if (typeof setImmediate == "function") {36 setImmediate(writeNextChunk);37 } else {38 process.nextTick(writeNextChunk);39 }40 } else {41 mailparser.end();42 }43 };44 mailparser.on("end", function(mail) {45 test.equal(mail.text, "hello");46 test.done();47 });48 if (typeof setImmediate == "function") {49 setImmediate(writeNextChunk);50 } else {51 process.nextTick(writeNextChunk);52 }53 },54 "Headers only": function(test) {55 var encodedText = "Content-type: text/plain; charset=utf-8\r\n" +56 "Subject: ÕÄÖÜ",57 mail = new Buffer(encodedText, "utf-8");58 test.expect(1);59 var mailparser = new MailParser();60 mailparser.end(mail);61 mailparser.on("end", function(mail) {62 test.equal(mail.subject, "ÕÄÖÜ");63 test.done();64 });65 },66 "Body only": function(test) {67 var encodedText = "\r\n" +68 "===",69 mail = new Buffer(encodedText, "utf-8");70 test.expect(1);71 var mailparser = new MailParser();72 mailparser.end(mail);73 mailparser.on("end", function(mail) {74 test.equal(mail.text, "===");75 test.done();76 });77 },78 "Different line endings": function(test) {79 var encodedText = "Content-type: text/plain; charset=utf-8\r" +80 "Subject: ÕÄÖÜ\n" +81 "\r" +82 "1234\r\n" +83 "ÕÄÖÜ\r\n" +84 "ÜÖÄÕ\n" +85 "1234",86 mail = new Buffer(encodedText, "utf-8");87 test.expect(2);88 var mailparser = new MailParser();89 mailparser.end(mail);90 mailparser.on("end", function(mail) {91 test.equal(mail.subject, "ÕÄÖÜ");92 test.equal(mail.text, "1234\nÕÄÖÜ\nÜÖÄÕ\n1234");93 test.done();94 });95 },96 "Headers event": function(test) {97 var encodedText = "Content-type: multipart/mixed; boundary=ABC\r\n" +98 "X-Test: =?UTF-8?Q?=C3=95=C3=84?= =?UTF-8?Q?=C3=96=C3=9C?=\r\n" +99 "Subject: ABCDEF\r\n" +100 "\r\n" +101 "--ABC\r\n" +102 "Content-Type: application/octet-stream\r\n" +103 "Content-Transfer-Encoding: base64\r\n" +104 "Content-Disposition: attachment; filename=\"test.pdf\"\r\n" +105 "\r\n" +106 "AAECAwQFBg==\r\n" +107 "--ABC--",108 mail = new Buffer(encodedText, "utf-8");109 test.expect(3);110 var mailparser = new MailParser();111 mailparser.on("headers", function(headers) {112 test.equal(headers.subject, "ABCDEF");113 test.equal(headers['x-test'], "ÕÄÖÜ");114 });115 mailparser.end(mail);116 mailparser.on("end", function() {117 test.ok(1, "Parsing ended");118 test.done();119 });120 },121 "No priority": function(test) {122 var encodedText = "Content-type: text/plain; charset=utf-8\r" +123 "Subject: ÕÄÖÜ\n" +124 "\r" +125 "1234",126 mail = new Buffer(encodedText, "utf-8");127 test.expect(1);128 var mailparser = new MailParser();129 mailparser.end(mail);130 mailparser.on("end", function(mail) {131 test.equal(mail.priority, "normal");132 test.done();133 });134 },135 "MS Style priority": function(test) {136 var encodedText = "Content-type: text/plain; charset=utf-8\r" +137 "Subject: ÕÄÖÜ\n" +138 "X-Priority: 1 (Highest)\n" +139 "\r" +140 "1234",141 mail = new Buffer(encodedText, "utf-8");142 test.expect(1);143 var mailparser = new MailParser();144 mailparser.end(mail);145 mailparser.on("end", function(mail) {146 test.equal(mail.priority, "high");147 test.done();148 });149 },150 "Single reference": function(test) {151 var encodedText = "Content-type: text/plain\r" +152 "References: <mail1>\n" +153 "\r" +154 "1234",155 mail = new Buffer(encodedText, "utf-8");156 test.expect(1);157 var mailparser = new MailParser();158 mailparser.end(mail);159 mailparser.on("end", function(mail) {160 test.deepEqual(mail.references, ["mail1"]);161 test.done();162 });163 },164 "Multiple reference values": function(test) {165 var encodedText = "Content-type: text/plain\r" +166 "References: <mail1>\n" +167 " <mail2> <mail3>\n" +168 "\r" +169 "1234",170 mail = new Buffer(encodedText, "utf-8");171 test.expect(1);172 var mailparser = new MailParser();173 mailparser.end(mail);174 mailparser.on("end", function(mail) {175 test.deepEqual(mail.references, ["mail1", "mail2", "mail3"]);176 test.done();177 });178 },179 "Multiple reference fields": function(test) {180 var encodedText = "Content-type: text/plain\r" +181 "References: <mail1>\n" +182 "References: <mail3>\n" +183 "\r" +184 "1234",185 mail = new Buffer(encodedText, "utf-8");186 test.expect(1);187 var mailparser = new MailParser();188 mailparser.end(mail);189 mailparser.on("end", function(mail) {190 test.deepEqual(mail.references, ["mail1", "mail3"]);191 test.done();192 });193 },194 "Single in-reply-to": function(test) {195 var encodedText = "Content-type: text/plain\r" +196 "in-reply-to: <mail1>\n" +197 "\r" +198 "1234",199 mail = new Buffer(encodedText, "utf-8");200 test.expect(1);201 var mailparser = new MailParser();202 mailparser.end(mail);203 mailparser.on("end", function(mail) {204 test.deepEqual(mail.inReplyTo, ["mail1"]);205 test.done();206 });207 },208 "Multiple in-reply-to values": function(test) {209 var encodedText = "Content-type: text/plain\r" +210 "in-reply-to: <mail1>\n" +211 " <mail2> <mail3>\n" +212 "\r" +213 "1234",214 mail = new Buffer(encodedText, "utf-8");215 test.expect(1);216 var mailparser = new MailParser();217 mailparser.end(mail);218 mailparser.on("end", function(mail) {219 test.deepEqual(mail.inReplyTo, ["mail1", "mail2", "mail3"]);220 test.done();221 });222 },223 "Multiple in-reply-to fields": function(test) {224 var encodedText = "Content-type: text/plain\r" +225 "in-reply-to: <mail1>\n" +226 "in-reply-to: <mail3>\n" +227 "\r" +228 "1234",229 mail = new Buffer(encodedText, "utf-8");230 test.expect(1);231 var mailparser = new MailParser();232 mailparser.end(mail);233 mailparser.on("end", function(mail) {234 test.deepEqual(mail.inReplyTo, ["mail1", "mail3"]);235 test.done();236 });237 },238 "Reply To address": function(test) {239 var encodedText = "Reply-TO: andris <andris@disposebox.com>\r" +240 "Subject: ÕÄÖÜ\n" +241 "\r" +242 "1234",243 mail = new Buffer(encodedText, "utf-8");244 test.expect(1);245 var mailparser = new MailParser();246 mailparser.end(mail);247 mailparser.on("end", function(mail) {248 test.deepEqual(mail.replyTo, [{249 name: "andris",250 address: "andris@disposebox.com"251 }]);252 test.done();253 });254 }255};256exports["Text encodings"] = {257 "Plaintext encoding: Default": function(test) {258 var encodedText = [13, 10, 213, 196, 214, 220], // \r\nÕÄÖÜ259 mail = new Buffer(encodedText);260 test.expect(1);261 var mailparser = new MailParser();262 mailparser.end(mail);263 mailparser.on("end", function(mail) {264 test.equal(mail.text, "ÕÄÖÜ");265 test.done();266 });267 },268 "Plaintext encoding: Header defined": function(test) {269 var encodedText = "Content-Type: TEXT/PLAIN; CHARSET=UTF-8\r\n" +270 "\r\n" +271 "ÕÄÖÜ",272 mail = new Buffer(encodedText, "utf-8");273 test.expect(1);274 var mailparser = new MailParser();275 mailparser.end(mail);276 mailparser.on("end", function(mail) {277 test.equal(mail.text, "ÕÄÖÜ");278 test.done();279 });280 },281 "HTML encoding: From <meta>": function(test) {282 var encodedText = "Content-Type: text/html\r\n" +283 "\r\n" +284 "<html><head><meta charset=\"utf-8\"/></head><body>ÕÄÖÜ",285 mail = new Buffer(encodedText, "utf-8");286 test.expect(1);287 var mailparser = new MailParser();288 mailparser.end(mail);289 mailparser.on("end", function(mail) {290 test.equal((mail.html || "").substr(-4), "ÕÄÖÜ");291 test.done();292 });293 },294 "HTML encoding: Conflicting headers": function(test) {295 var encodedText = "Content-Type: text/html; charset=iso-8859-1\r\n" +296 "\r\n" +297 "<html><head><meta charset=\"utf-8\"/></head><body>ÕÄÖÜ",298 mail = new Buffer(encodedText, "utf-8");299 test.expect(1);300 var mailparser = new MailParser();301 mailparser.end(mail);302 mailparser.on("end", function(mail) {303 test.equal((mail.html || "").substr(-4), "ÕÄÖÜ");304 test.done();305 });306 },307 "HTML encoding: Header defined": function(test) {308 var encodedText = "Content-Type: text/html; charset=iso-UTF-8\r\n" +309 "\r\n" +310 "ÕÄÖÜ",311 mail = new Buffer(encodedText, "utf-8");312 test.expect(1);313 var mailparser = new MailParser();314 mailparser.end(mail);315 mailparser.on("end", function(mail) {316 test.equal(mail.html, "ÕÄÖÜ");317 test.done();318 });319 },320 "Mime Words": function(test) {321 var encodedText = "Content-type: text/plain; charset=utf-8\r\n" +322 "From: =?utf-8?q??= <sender@email.com>\r\n" +323 "To: =?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= <to@email.com>\r\n" +324 "Subject: =?iso-8859-1?Q?Avaldu?= =?iso-8859-1?Q?s_lepingu_?=\r\n =?iso-8859-1?Q?l=F5petamise?= =?iso-8859-1?Q?ks?=\r\n",325 mail = new Buffer(encodedText, "utf-8");326 var mailparser = new MailParser();327 mailparser.end(mail);328 mailparser.on("end", function(mail) {329 test.equal(mail.subject, "Avaldus lepingu lõpetamiseks");330 test.equal(mail.from[0].name, "");331 test.equal(mail.to[0].name, "Keld Jørn Simonsen");332 test.done();333 });334 }335};336exports["Binary attachment encodings"] = {337 "Quoted-Printable": function(test) {338 var encodedText = "Content-Type: application/octet-stream\r\n" +339 "Content-Transfer-Encoding: QUOTED-PRINTABLE\r\n" +340 "\r\n" +341 "=00=01=02=03=FD=FE=FF",342 mail = new Buffer(encodedText, "utf-8");343 var mailparser = new MailParser();344 mailparser.end(mail);345 mailparser.on("end", function(mail) {346 test.equal(Array.prototype.slice.apply(mail.attachments && mail.attachments[0] && mail.attachments[0].content && mail.attachments[0].content || []).join(","), "0,1,2,3,253,254,255");347 test.done();348 });349 },350 "Base64": function(test) {351 var encodedText = "Content-Type: application/octet-stream\r\n" +352 "Content-Transfer-Encoding: base64\r\n" +353 "\r\n" +354 "AAECA/3+/w==",355 mail = new Buffer(encodedText, "utf-8");356 var mailparser = new MailParser();357 mailparser.end(mail);358 mailparser.on("end", function(mail) {359 test.equal(Array.prototype.slice.apply(mail.attachments && mail.attachments[0] && mail.attachments[0].content && mail.attachments[0].content || []).join(","), "0,1,2,3,253,254,255");360 test.done();361 });362 },363 "8bit": function(test) {364 var encodedText = "Content-Type: application/octet-stream\r\n" +365 "\r\n" +366 "ÕÄÖÜ",367 mail = new Buffer(encodedText, "utf-8");368 var mailparser = new MailParser();369 mailparser.end(mail);370 mailparser.on("end", function(mail) {371 test.equal(Array.prototype.slice.apply(mail.attachments && mail.attachments[0] && mail.attachments[0].content && mail.attachments[0].content || []).join(","), "195,149,195,132,195,150,195,156");372 test.done();373 });374 },375 "UUENCODE": function(test) {376 var encodedText = "Content-Type: application/octet-stream\r\n" +377 "Content-Transfer-Encoding: uuencode\r\n" +378 "\r\n" +379 "begin 644 buffer.bin\r\n" +380 "#0V%T\r\n" +381 "`\r\n" +382 "end",383 mail = new Buffer(encodedText, "utf-8");384 var mailparser = new MailParser();385 mailparser.end(mail);386 mailparser.on("end", function(mail) {387 test.equal(mail.attachments[0].content.toString(), "Cat");388 test.done();389 });390 }391};392exports["Attachment Content-Id"] = {393 "Default": function(test) {394 var encodedText = "Content-Type: application/octet-stream\r\n" +395 "Content-Transfer-Encoding: QUOTED-PRINTABLE\r\n" +396 "Content-Disposition: attachment; filename=\"=?UTF-8?Q?=C3=95=C3=84=C3=96=C3=9C?=\"\r\n" +397 "\r\n" +398 "=00=01=02=03=FD=FE=FF",399 mail = new Buffer(encodedText, "utf-8");400 var mailparser = new MailParser();401 mailparser.end(mail);402 mailparser.on("end", function(mail) {403 test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].contentId, "ef694232fea1c01c16fb8a03a0ca710c@mailparser");404 test.done();405 });406 },407 "Defined": function(test) {408 var encodedText = "Content-Type: application/octet-stream\r\n" +409 "Content-Transfer-Encoding: QUOTED-PRINTABLE\r\n" +410 "Content-Disposition: attachment; filename=\"=?UTF-8?Q?=C3=95=C3=84=C3=96=C3=9C?=\"\r\n" +411 "Content-Id: test@localhost\r\n" +412 "\r\n" +413 "=00=01=02=03=FD=FE=FF",414 mail = new Buffer(encodedText, "utf-8");415 var mailparser = new MailParser();416 mailparser.end(mail);417 mailparser.on("end", function(mail) {418 test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].contentId, "test@localhost");419 test.done();420 });421 }422};423exports["Attachment filename"] = {424 "Content-Disposition filename": function(test) {425 var encodedText = "Content-Type: application/octet-stream\r\n" +426 "Content-Transfer-Encoding: QUOTED-PRINTABLE\r\n" +427 "Content-Disposition: attachment; filename=\"=?UTF-8?Q?=C3=95=C3=84=C3=96=C3=9C?=\"\r\n" +428 "\r\n" +429 "=00=01=02=03=FD=FE=FF",430 mail = new Buffer(encodedText, "utf-8");431 var mailparser = new MailParser();432 mailparser.end(mail);433 mailparser.on("end", function(mail) {434 test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].content && mail.attachments[0].fileName, "ÕÄÖÜ");435 test.done();436 });437 },438 "Content-Disposition filename*": function(test) {439 var encodedText = "Content-Type: application/octet-stream\r\n" +440 "Content-Transfer-Encoding: QUOTED-PRINTABLE\r\n" +441 "Content-Disposition: attachment; filename*=\"UTF-8''%C3%95%C3%84%C3%96%C3%9C\"\r\n" +442 "\r\n" +443 "=00=01=02=03=FD=FE=FF",444 mail = new Buffer(encodedText, "utf-8");445 var mailparser = new MailParser();446 mailparser.end(mail);447 mailparser.on("end", function(mail) {448 test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].content && mail.attachments[0].fileName, "ÕÄÖÜ");449 test.done();450 });451 },452 "Content-Disposition filename*X*": function(test) {453 var encodedText = "Content-Type: application/octet-stream\r\n" +454 "Content-Transfer-Encoding: QUOTED-PRINTABLE\r\n" +455 "Content-Disposition: attachment;\r\n" +456 " filename*0*=UTF-8''%C3%95%C3%84;\r\n" +457 " filename*1*=%C3%96%C3%9C\r\n" +458 "\r\n" +459 "=00=01=02=03=FD=FE=FF",460 mail = new Buffer(encodedText, "utf-8");461 var mailparser = new MailParser();462 mailparser.end(mail);463 mailparser.on("end", function(mail) {464 test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].content && mail.attachments[0].fileName, "ÕÄÖÜ");465 test.done();466 });467 },468 "Content-Type name": function(test) {469 var encodedText = "Content-Type: application/octet-stream; name=\"=?UTF-8?Q?=C3=95=C3=84=C3=96=C3=9C?=\"\r\n" +470 "Content-Transfer-Encoding: QUOTED-PRINTABLE\r\n" +471 "\r\n" +472 "=00=01=02=03=FD=FE=FF",473 mail = new Buffer(encodedText, "utf-8");474 var mailparser = new MailParser();475 mailparser.end(mail);476 mailparser.on("end", function(mail) {477 test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].content && mail.attachments[0].fileName, "ÕÄÖÜ");478 test.done();479 });480 },481 "Content-Type name*": function(test) {482 var encodedText = "Content-Type: application/octet-stream;\r\n" +483 " name*=UTF-8''%C3%95%C3%84%C3%96%C3%9C\r\n" +484 "Content-Transfer-Encoding: QUOTED-PRINTABLE\r\n" +485 "\r\n" +486 "=00=01=02=03=FD=FE=FF",487 mail = new Buffer(encodedText, "utf-8");488 var mailparser = new MailParser();489 mailparser.end(mail);490 mailparser.on("end", function(mail) {491 test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].content && mail.attachments[0].fileName, "ÕÄÖÜ");492 test.done();493 });494 },495 "Content-Type name*X*": function(test) {496 var encodedText = "Content-Type: application/octet-stream;\r\n" +497 " name*0*=UTF-8''%C3%95%C3%84;\r\n" +498 " name*1*=%C3%96%C3%9C\r\n" +499 "Content-Transfer-Encoding: QUOTED-PRINTABLE\r\n" +500 "\r\n" +501 "=00=01=02=03=FD=FE=FF",502 mail = new Buffer(encodedText, "utf-8");503 var mailparser = new MailParser();504 mailparser.end(mail);505 mailparser.on("end", function(mail) {506 test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].content && mail.attachments[0].fileName, "ÕÄÖÜ");507 test.done();508 });509 },510 "Default name from Content-type": function(test) {511 var encodedText = "Content-Type: application/pdf\r\n" +512 "Content-Transfer-Encoding: QUOTED-PRINTABLE\r\n" +513 "\r\n" +514 "=00=01=02=03=FD=FE=FF",515 mail = new Buffer(encodedText, "utf-8");516 var mailparser = new MailParser();517 mailparser.end(mail);518 mailparser.on("end", function(mail) {519 test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].content && mail.attachments[0].generatedFileName, "attachment.pdf");520 test.done();521 });522 },523 "Default name": function(test) {524 var encodedText = "Content-Type: application/octet-stream\r\n" +525 "Content-Transfer-Encoding: QUOTED-PRINTABLE\r\n" +526 "\r\n" +527 "=00=01=02=03=FD=FE=FF",528 mail = new Buffer(encodedText, "utf-8");529 var mailparser = new MailParser();530 mailparser.end(mail);531 mailparser.on("end", function(mail) {532 test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].content && mail.attachments[0].generatedFileName, "attachment.bin");533 test.done();534 });535 },536 "Multiple filenames - Same": function(test) {537 var encodedText = "Content-Type: multipart/mixed; boundary=ABC\r\n" +538 "\r\n" +539 "--ABC\r\n" +540 "Content-Type: application/octet-stream; name=\"test.txt\"\r\n" +541 "\r\n" +542 "=00=01=02=03=FD=FE=FF\r\n" +543 "--ABC\r\n" +544 "Content-Type: application/octet-stream; name=\"test.txt\"\r\n" +545 "\r\n" +546 "=00=01=02=03=FD=FE=FF\r\n" +547 "--ABC--",548 mail = new Buffer(encodedText, "utf-8");549 var mailparser = new MailParser();550 mailparser.end(mail);551 mailparser.on("end", function(mail) {552 test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].content && mail.attachments[0].generatedFileName, "test.txt");553 test.equal(mail.attachments && mail.attachments[1] && mail.attachments[1].content && mail.attachments[1].generatedFileName, "test-1.txt");554 test.done();555 });556 },557 "Multiple filenames - Different": function(test) {558 var encodedText = "Content-Type: multipart/mixed; boundary=ABC\r\n" +559 "\r\n" +560 "--ABC\r\n" +561 "Content-Type: application/octet-stream\r\n" +562 "\r\n" +563 "=00=01=02=03=FD=FE=FF\r\n" +564 "--ABC\r\n" +565 "Content-Type: application/octet-stream; name=\"test.txt\"\r\n" +566 "\r\n" +567 "=00=01=02=03=FD=FE=FF\r\n" +568 "--ABC--",569 mail = new Buffer(encodedText, "utf-8");570 var mailparser = new MailParser();571 mailparser.end(mail);572 mailparser.on("end", function(mail) {573 test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].content && mail.attachments[0].generatedFileName, "attachment.bin");574 test.equal(mail.attachments && mail.attachments[1] && mail.attachments[1].content && mail.attachments[1].generatedFileName, "test.txt");575 test.done();576 });577 },578 "Multiple filenames - with number": function(test) {579 var encodedText = "Content-Type: multipart/mixed; boundary=ABC\r\n" +580 "\r\n" +581 "--ABC\r\n" +582 "Content-Type: application/octet-stream; name=\"somename.txt\"\r\n" +583 "\r\n" +584 "=00=01=02=03=FD=FE=FF\r\n" +585 "--ABC\r\n" +586 "Content-Type: application/octet-stream; name=\"somename-1.txt\"\r\n" +587 "\r\n" +588 "=00=01=02=03=FD=FE=FF\r\n" +589 "--ABC\r\n" +590 "Content-Type: application/octet-stream; name=\"somename.txt\"\r\n" +591 "\r\n" +592 "=00=01=02=03=FD=FE=FF\r\n" +593 "--ABC\r\n" +594 "Content-Type: application/octet-stream; name=\"somename-1-1.txt\"\r\n" +595 "\r\n" +596 "=00=01=02=03=FD=FE=FF\r\n" +597 "--ABC--",598 mail = new Buffer(encodedText, "utf-8");599 var mailparser = new MailParser();600 mailparser.end(mail);601 mailparser.on("end", function(mail) {602 test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].content && mail.attachments[0].generatedFileName, "somename.txt");603 test.equal(mail.attachments && mail.attachments[1] && mail.attachments[1].content && mail.attachments[1].generatedFileName, "somename-1-1.txt");604 test.equal(mail.attachments && mail.attachments[2] && mail.attachments[2].content && mail.attachments[2].generatedFileName, "somename-2.txt");605 test.equal(mail.attachments && mail.attachments[3] && mail.attachments[3].content && mail.attachments[3].generatedFileName, "somename-1-1-3.txt");606 test.done();607 });608 },609 "Generate filename from Content-Type": function(test) {610 var encodedText = "Content-Type: multipart/mixed; boundary=ABC\r\n" +611 "\r\n" +612 "--ABC\r\n" +613 "Content-Type: application/pdf\r\n" +614 "\r\n" +615 "=00=01=02=03=FD=FE=FF\r\n" +616 "--ABC--",617 mail = new Buffer(encodedText, "utf-8");618 var mailparser = new MailParser();619 mailparser.end(mail);620 mailparser.on("end", function(mail) {621 test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].content && mail.attachments[0].generatedFileName, "attachment.pdf");622 test.done();623 });624 },625 "Filename with semicolon": function(test) {626 var encodedText = "Content-Type: multipart/mixed; boundary=ABC\r\n" +627 "\r\n" +628 "--ABC\r\n" +629 "Content-Disposition: attachment; filename=\"hello;world;test.txt\"\r\n" +630 "\r\n" +631 "=00=01=02=03=FD=FE=FF\r\n" +632 "--ABC--",633 mail = new Buffer(encodedText, "utf-8");634 var mailparser = new MailParser();635 mailparser.end(mail);636 mailparser.on("end", function(mail) {637 test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].content && mail.attachments[0].generatedFileName, "hello;world;test.txt");638 test.done();639 });640 }641};642exports["Plaintext format"] = {643 "Default": function(test) {644 var encodedText = "Content-Type: text/plain;\r\n\r\nFirst line \r\ncontinued",645 mail = new Buffer(encodedText, "utf-8");646 var mailparser = new MailParser();647 mailparser.end(mail);648 mailparser.on("end", function(mail) {649 test.equal(mail.text, "First line \ncontinued");650 test.done();651 });652 },653 "Flowed": function(test) {654 var encodedText = "Content-Type: text/plain; format=flowed\r\n\r\nFirst line \r\ncontinued \r\nand so on",655 mail = new Buffer(encodedText, "utf-8");656 var mailparser = new MailParser();657 mailparser.end(mail);658 mailparser.on("end", function(mail) {659 test.equal(mail.text, "First line continued and so on");660 test.done();661 });662 },663 "Flowed Signature": function(test) {664 var encodedText = "Content-Type: text/plain; format=flowed\r\n\r\nHow are you today?\r\n" +665 "-- \r\n" +666 "Signature\r\n",667 mail = new Buffer(encodedText, "utf-8");668 var mailparser = new MailParser();669 mailparser.end(mail);670 mailparser.on("end", function(mail) {671 test.equal(mail.text, "How are you today?\n-- \nSignature\n");672 test.done();673 });674 },675 "Fixed": function(test) {676 var encodedText = "Content-Type: text/plain; format=fixed\r\n\r\nFirst line \r\ncontinued \r\nand so on",677 mail = new Buffer(encodedText, "utf-8");678 var mailparser = new MailParser();679 mailparser.end(mail);680 mailparser.on("end", function(mail) {681 test.equal(mail.text, "First line \ncontinued \nand so on");682 test.done();683 });684 },685 "DelSp": function(test) {686 var encodedText = "Content-Type: text/plain; format=flowed; delsp=yes\r\n\r\nFirst line \r\ncontinued \r\nand so on",687 mail = new Buffer(encodedText, "utf-8");688 var mailparser = new MailParser();689 mailparser.end(mail);690 mailparser.on("end", function(mail) {691 test.equal(mail.text, "First linecontinuedand so on");692 test.done();693 });694 },695 "Quoted printable, Flowed": function(test) {696 var encodedText = "Content-Type: text/plain; format=flowed\r\nContent-Transfer-Encoding: QUOTED-PRINTABLE\r\n\r\nFoo =\n\nBar =\n\nBaz",697 mail = new Buffer(encodedText, "utf-8");698 var mailparser = new MailParser();699 mailparser.end(mail);700 mailparser.on("end", function(mail) {701 test.equal(mail.text, "Foo Bar Baz");702 test.done();703 });704 },705 "Quoted printable, Flowed Signature": function(test) {706 var encodedText = "Content-Type: text/plain; format=flowed\r\nContent-Transfer-Encoding: QUOTED-PRINTABLE\r\n\r\nHow are you today?\r\n" +707 "-- \r\n" +708 "Signature\r\n",709 mail = new Buffer(encodedText, "utf-8");710 var mailparser = new MailParser();711 mailparser.end(mail);712 mailparser.on("end", function(mail) {713 test.equal(mail.text, "How are you today?\n-- \nSignature\n");714 test.done();715 });716 },717 "Quoted printable, DelSp": function(test) {718 var encodedText = "Content-Type: text/plain; format=flowed; delsp=yes\r\nContent-Transfer-Encoding: QUOTED-PRINTABLE\r\n\r\nFoo =\n\nBar =\n\nBaz",719 mail = new Buffer(encodedText, "utf-8");720 var mailparser = new MailParser();721 mailparser.end(mail);722 mailparser.on("end", function(mail) {723 test.equal(mail.text, "FooBarBaz");724 test.done();725 });726 }727};728exports["Transfer encoding"] = {729 "Quoted-Printable Default charset": function(test) {730 var encodedText = "Content-type: text/plain\r\nContent-Transfer-Encoding: quoted-printable\r\n\r\n=D5=C4=D6=DC",731 mail = new Buffer(encodedText, "utf-8");732 var mailparser = new MailParser();733 mailparser.end(mail);734 mailparser.on("end", function(mail) {735 test.equal(mail.text, "ÕÄÖÜ");736 test.done();737 });738 },739 "Quoted-Printable UTF-8": function(test) {740 var encodedText = "Content-type: text/plain; charset=utf-8\r\nContent-Transfer-Encoding: QUOTED-PRINTABLE\r\n\r\n=C3=95=C3=84=C3=96=C3=9C",741 mail = new Buffer(encodedText, "utf-8");742 var mailparser = new MailParser();743 mailparser.end(mail);744 mailparser.on("end", function(mail) {745 test.equal(mail.text, "ÕÄÖÜ");746 test.done();747 });748 },749 "Base64 Default charset": function(test) {750 var encodedText = "Content-type: text/plain\r\nContent-Transfer-Encoding: bAse64\r\n\r\n1cTW3A==",751 mail = new Buffer(encodedText, "utf-8");752 var mailparser = new MailParser();753 mailparser.end(mail);754 mailparser.on("end", function(mail) {755 test.equal(mail.text, "ÕÄÖÜ");756 test.done();757 });758 },759 "Base64 UTF-8": function(test) {760 var encodedText = "Content-type: text/plain; charset=utf-8\r\nContent-Transfer-Encoding: bAse64\r\n\r\nw5XDhMOWw5w=",761 mail = new Buffer(encodedText, "utf-8");762 var mailparser = new MailParser();763 mailparser.end(mail);764 mailparser.on("end", function(mail) {765 test.equal(mail.text, "ÕÄÖÜ");766 test.done();767 });768 },769 "Mime Words": function(test) {770 var encodedText = "Content-type: text/plain; charset=utf-8\r\nSubject: =?iso-8859-1?Q?Avaldu?= =?iso-8859-1?Q?s_lepingu_?=\r\n =?iso-8859-1?Q?l=F5petamise?= =?iso-8859-1?Q?ks?=\r\n",771 mail = new Buffer(encodedText, "utf-8");772 var mailparser = new MailParser();773 mailparser.end(mail);774 mailparser.on("end", function(mail) {775 test.equal(mail.subject, "Avaldus lepingu lõpetamiseks");776 test.done();777 });778 },779 "Mime Words with invalid linebreaks (Sparrow)": function(test) {780 var encodedText = "Content-type: text/plain; charset=utf-8\r\n" +781 "Subject: abc=?utf-8?Q?=C3=B6=C\r\n" +782 " 3=B5=C3=BC?=",783 mail = new Buffer(encodedText, "utf-8");784 var mailparser = new MailParser();785 mailparser.end(mail);786 mailparser.on("end", function(mail) {787 test.equal(mail.subject, "abcöõü");788 test.done();789 });790 },791 "8bit Default charset": function(test) {792 var encodedText = "Content-type: text/plain\r\nContent-Transfer-Encoding: 8bit\r\n\r\nÕÄÖÜ",793 textmap = encodedText.split('').map(function(chr) {794 return chr.charCodeAt(0);795 }),796 mail = new Buffer(textmap);797 var mailparser = new MailParser();798 mailparser.end(mail);799 mailparser.on("end", function(mail) {800 test.equal(mail.text, "ÕÄÖÜ");801 test.done();802 });803 },804 "8bit UTF-8": function(test) {805 var encodedText = "Content-type: text/plain; charset=utf-8\r\nContent-Transfer-Encoding: 8bit\r\n\r\nÕÄÖÜ",806 mail = new Buffer(encodedText, "utf-8");807 var mailparser = new MailParser();808 mailparser.end(mail);809 mailparser.on("end", function(mail) {810 test.equal(mail.text, "ÕÄÖÜ");811 test.done();812 });813 },814 "Invalid Quoted-Printable": function(test) {815 var encodedText = "Content-type: text/plain; charset=utf-8\r\nContent-Transfer-Encoding: QUOTED-PRINTABLE\r\n\r\n==C3==95=C3=84=C3=96=C3=9C=",816 mail = new Buffer(encodedText, "utf-8");817 var mailparser = new MailParser();818 mailparser.end(mail);819 mailparser.on("end", function(mail) {820 test.equal(mail.text, "=�=�ÄÖÜ");821 test.done();822 });823 },824 "Invalid BASE64": function(test) {825 var encodedText = "Content-type: text/plain; charset=utf-8\r\nContent-Transfer-Encoding: base64\r\n\r\nw5XDhMOWw5",826 mail = new Buffer(encodedText, "utf-8");827 var mailparser = new MailParser();828 mailparser.end(mail);829 mailparser.on("end", function(mail) {830 test.equal(Array.prototype.map.call(mail.text, function(chr) {831 return chr.charCodeAt(0);832 }).join(","), "213,196,214,65533");833 test.done();834 });835 },836 "gb2312 mime words": function(test) {837 var encodedText = "From: =?gb2312?B?086yyZjl?= user@ldkf.com.tw\r\n\r\nBody",838 mail = new Buffer(encodedText, "utf-8");839 var mailparser = new MailParser();840 mailparser.end(mail);841 mailparser.on("end", function(mail) {842 test.deepEqual(mail.from, [{843 address: 'user@ldkf.com.tw',844 name: '游采樺'845 }]);846 test.done();847 });848 },849 "Valid Date header": function(test) {850 var encodedText = "Date: Wed, 08 Jan 2014 09:52:26 -0800\r\n\r\n1cTW3A==",851 mail = new Buffer(encodedText, "utf-8");852 var mailparser = new MailParser();853 mailparser.end(mail);854 mailparser.on("end", function(mail) {855 test.equal(mail.date.toISOString(), "2014-01-08T17:52:26.000Z");856 test.equal(mail.headers.date, "Wed, 08 Jan 2014 09:52:26 -0800");857 test.done();858 });859 },860 "Invalid Date header": function(test) {861 var encodedText = "Date: zzzzz\r\n\r\n1cTW3A==",862 mail = new Buffer(encodedText, "utf-8");863 var mailparser = new MailParser();864 mailparser.end(mail);865 mailparser.on("end", function(mail) {866 test.ok(!mail.date);867 test.equal(mail.headers.date, "zzzzz");868 test.done();869 });870 },871 "Missing Date header": function(test) {872 var encodedText = "Subject: test\r\n\r\n1cTW3A==",873 mail = new Buffer(encodedText, "utf-8");874 var mailparser = new MailParser();875 mailparser.end(mail);876 mailparser.on("end", function(mail) {877 test.ok(!mail.date);878 test.equal(mail.headers.date, undefined);879 test.done();880 });881 },882 "Received Headers": function(test) {883 var encodedTest = "Received: by 10.25.25.72 with SMTP id 69csp2404548lfz;\r\n" +884 " Fri, 6 Feb 2015 20:15:32 -0800 (PST)\r\n" +885 "X-Received: by 10.194.200.68 with SMTP id jq4mr7518476wjc.128.1423264531879;\r\n" +886 " Fri, 06 Feb 2015 15:15:31 -0800 (PST)\r\n" +887 "Received: from mail.formilux.org (flx02.formilux.org. [195.154.117.161])\r\n" +888 " by mx.google.com with ESMTP id wn4si6920692wjc.106.2015.02.06.15.15.31\r\n" +889 " for <test@example.com>;\r\n" +890 " Fri, 06 Feb 2015 15:15:31 -0800 (PST)\r\n" +891 "Received: from flx02.formilux.org (flx02.formilux.org [127.0.0.1])\r\n" +892 " by mail.formilux.org (Postfix) with SMTP id 9D262450C77\r\n" +893 " for <test@example.com>; Sat, 7 Feb 2015 00:15:31 +0100 (CET)\r\n" +894 "Date: Fri, 6 Feb 2015 16:13:51 -0700 (MST)\r\n" +895 "\r\n" +896 "1cTW3A==",897 mail = new Buffer(encodedTest, "utf-8");898 var mailparser = new MailParser();899 mailparser.end(mail);900 mailparser.on("end", function(mail) {901 test.ok(mail.date);902 test.ok(mail.receivedDate);903 test.equal(mail.date.toISOString(), "2015-02-06T23:13:51.000Z");904 test.equal(mail.receivedDate.toISOString(), "2015-02-07T04:15:32.000Z");905 test.done();906 });907 }908};909exports["Multipart content"] = {910 "Simple": function(test) {911 var encodedText = "Content-type: multipart/mixed; boundary=ABC\r\n\r\n--ABC\r\nContent-type: text/plain; charset=utf-8\r\n\r\nÕÄÖÜ\r\n--ABC--",912 mail = new Buffer(encodedText, "utf-8");913 var mailparser = new MailParser();914 mailparser.end(mail);915 mailparser.on("end", function(mail) {916 test.equal(mail.text, "ÕÄÖÜ");917 test.done();918 });919 },920 "Nested": function(test) {921 var encodedText = "Content-type: multipart/mixed; boundary=ABC\r\n" +922 "\r\n" +923 "--ABC\r\n" +924 "Content-type: multipart/related; boundary=DEF\r\n" +925 "\r\n" +926 "--DEF\r\n" +927 "Content-type: text/plain; charset=utf-8\r\n" +928 "\r\n" +929 "ÕÄÖÜ\r\n" +930 "--DEF--\r\n" +931 "--ABC--",932 mail = new Buffer(encodedText, "utf-8");933 var mailparser = new MailParser();934 mailparser.end(mail);935 mailparser.on("end", function(mail) {936 test.equal(mail.text, "ÕÄÖÜ");937 test.done();938 });939 },940 "Inline text (Sparrow)": function(test) {941 var encodedText = "Content-type: multipart/mixed; boundary=ABC\r\n" +942 "\r\n" +943 "--ABC\r\n" +944 "Content-Type: text/plain; charset=\"utf-8\"\r\n" +945 "Content-Transfer-Encoding: 8bit\r\n" +946 "Content-Disposition: inline\r\n" +947 "\r\n" +948 "ÕÄÖÜ\r\n" +949 "--ABC--",950 mail = new Buffer(encodedText, "utf-8");951 var mailparser = new MailParser();952 mailparser.end(mail);953 mailparser.on("end", function(mail) {954 test.equal(mail.text, "ÕÄÖÜ");955 test.done();956 });957 },958 "Different Levels": function(test) {959 var encodedText = "Content-type: multipart/mixed; boundary=ABC\r\n" +960 "\r\n" +961 "--ABC\r\n" +962 "Content-type: text/html; charset=utf-8\r\n" +963 "\r\n" +964 "ÕÄÖÜ2\r\n" +965 "--ABC\r\n" +966 "Content-type: multipart/related; boundary=DEF\r\n" +967 "\r\n" +968 "--DEF\r\n" +969 "Content-type: text/plain; charset=utf-8\r\n" +970 "\r\n" +971 "ÕÄÖÜ1\r\n" +972 "--DEF--\r\n" +973 "--ABC--",974 mail = new Buffer(encodedText, "utf-8");975 var mailparser = new MailParser();976 mailparser.end(mail);977 mailparser.on("end", function(mail) {978 test.equal(mail.text, "ÕÄÖÜ1");979 test.equal(mail.html, "ÕÄÖÜ2");980 test.done();981 });982 }983};984exports["Attachment info"] = {985 "Included integrity": function(test) {986 var encodedText = "Content-type: multipart/mixed; boundary=ABC\r\n" +987 "\r\n" +988 "--ABC\r\n" +989 "Content-Type: application/octet-stream\r\n" +990 "Content-Transfer-Encoding: quoted-printable\r\n" +991 "Content-Disposition: attachment\r\n" +992 "\r\n" +993 "=00=01=02=03=04=05=06\r\n" +994 "--ABC--",995 expectedHash = "9aa461e1eca4086f9230aa49c90b0c61",996 mail = new Buffer(encodedText, "utf-8");997 var mailparser = new MailParser();998 for (var i = 0, len = mail.length; i < len; i++) {999 mailparser.write(new Buffer([mail[i]]));1000 }1001 mailparser.end();1002 mailparser.on("end", function(mail) {1003 test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].checksum, expectedHash);1004 test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].length, 7);1005 test.done();1006 });1007 },1008 "Stream integrity base64": function(test) {1009 var encodedText = "Content-type: multipart/mixed; boundary=ABC\r\n" +1010 "\r\n" +1011 "--ABC\r\n" +1012 "Content-Type: application/octet-stream\r\n" +1013 "Content-Transfer-Encoding: base64\r\n" +1014 "Content-Disposition: attachment\r\n" +1015 "\r\n" +1016 "AAECAwQFBg==\r\n" +1017 "--ABC--",1018 expectedHash = "9aa461e1eca4086f9230aa49c90b0c61",1019 mail = new Buffer(encodedText, "utf-8");1020 var mailparser = new MailParser({1021 streamAttachments: true1022 });1023 for (var i = 0, len = mail.length; i < len; i++) {1024 mailparser.write(new Buffer([mail[i]]));1025 }1026 test.expect(3);1027 mailparser.on("attachment", function(attachment) {1028 test.ok(attachment.stream, "Stream detected");1029 });1030 mailparser.end();1031 mailparser.on("end", function(mail) {1032 test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].checksum, expectedHash);1033 test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].length, 7);1034 test.done();1035 });1036 },1037 "Stream integrity - 8bit": function(test) {1038 var encodedText = "Content-type: multipart/mixed; boundary=ABC\r\n" +1039 "\r\n" +1040 "--ABC\r\n" +1041 "Content-Type: application/octet-stream\r\n" +1042 "Content-Transfer-Encoding: 8bit\r\n" +1043 "Content-Disposition: attachment\r\n" +1044 "\r\n" +1045 "ÕÄ\r\n" +1046 "ÖÜ\r\n" +1047 "--ABC--",1048 expectedHash = "cad0f72629a7245dd3d2cbf41473e3ca",1049 mail = new Buffer(encodedText, "utf-8");1050 var mailparser = new MailParser({1051 streamAttachments: true1052 });1053 for (var i = 0, len = mail.length; i < len; i++) {1054 mailparser.write(new Buffer([mail[i]]));1055 }1056 test.expect(4);1057 mailparser.on("attachment", function(attachment, node) {1058 test.ok(attachment.stream, "Stream detected");1059 test.ok(node);1060 });1061 mailparser.end();1062 mailparser.on("end", function(mail) {1063 test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].checksum, expectedHash);1064 test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].length, 10);1065 test.done();1066 });1067 },1068 "Stream integrity - binary, non utf-8": function(test) {1069 var encodedText = "Content-type: multipart/mixed; boundary=ABC\r\n" +1070 "\r\n" +1071 "--ABC\r\n" +1072 "Content-Type: application/octet-stream\r\n" +1073 "Content-Transfer-Encoding: 8bit\r\n" +1074 "Content-Disposition: attachment\r\n" +1075 "\r\n" +1076 "ÕÄ\r\n" +1077 "ÖÜ\r\n" +1078 "ŽŠ\r\n" +1079 "--ABC--",1080 expectedHash = "34bca86f8cc340bbd11446ee16ee3cae",1081 mail = encodinglib.convert(encodedText, "latin-13");1082 var mailparser = new MailParser({1083 streamAttachments: true1084 });1085 for (var i = 0, len = mail.length; i < len; i++) {1086 mailparser.write(new Buffer([mail[i]]));1087 }1088 test.expect(3);1089 mailparser.on("attachment", function(attachment) {1090 test.ok(attachment.stream, "Stream detected");1091 });1092 mailparser.end();1093 mailparser.on("end", function(mail) {1094 test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].checksum, expectedHash);1095 test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].length, 10);1096 test.done();1097 });1098 },1099 "Stream integrity - qp, non utf-8": function(test) {1100 var encodedText = "Content-type: multipart/mixed; boundary=ABC\r\n" +1101 "\r\n" +1102 "--ABC\r\n" +1103 "Content-Type: application/octet-stream; charset=iso-8859-13\r\n" +1104 "Content-Transfer-Encoding: quoted-printable\r\n" +1105 "Content-Disposition: attachment\r\n" +1106 "\r\n" +1107 "=d5=c4\r\n" +1108 "=d6=dc\r\n" +1109 "=de=d0\r\n" +1110 "--ABC--",1111 expectedHash = "34bca86f8cc340bbd11446ee16ee3cae",1112 mail = new Buffer(encodedText, "utf-8");1113 var mailparser = new MailParser({1114 streamAttachments: true1115 });1116 for (var i = 0, len = mail.length; i < len; i++) {1117 mailparser.write(new Buffer([mail[i]]));1118 }1119 test.expect(3);1120 mailparser.on("attachment", function(attachment) {1121 test.ok(attachment.stream, "Stream detected");1122 });1123 mailparser.end();1124 mailparser.on("end", function(mail) {1125 test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].checksum, expectedHash);1126 test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].length, 10);1127 test.done();1128 });1129 },1130 "Stream integrity - uuencode": function(test) {1131 var encodedText = "Content-type: multipart/mixed; boundary=ABC\r\n" +1132 "\r\n" +1133 "--ABC\r\n" +1134 "Content-Type: application/octet-stream\r\n" +1135 "Content-Transfer-Encoding: uuencode\r\n" +1136 "\r\n" +1137 "begin 644 buffer.bin\r\n" +1138 "#0V%T\r\n" +1139 "`\r\n" +1140 "end\r\n" +1141 "--ABC--",1142 expectedHash = "fa3ebd6742c360b2d9652b7f78d9bd7d",1143 mail = new Buffer(encodedText, "utf-8");1144 var mailparser = new MailParser({1145 streamAttachments: true1146 });1147 for (var i = 0, len = mail.length; i < len; i++) {1148 mailparser.write(new Buffer([mail[i]]));1149 }1150 test.expect(3);1151 mailparser.on("attachment", function(attachment) {1152 test.ok(attachment.stream, "Stream detected");1153 });1154 mailparser.end();1155 mailparser.on("end", function(mail) {1156 test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].checksum, expectedHash);1157 test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].length, 3);1158 test.done();1159 });1160 },1161 "Attachment in root node": function(test) {1162 var encodedText = "Content-Type: application/octet-stream\r\n" +1163 "Content-Transfer-Encoding: 8bit\r\n" +1164 "Content-Disposition: attachment\r\n" +1165 "\r\n" +1166 "ÕÄ\r\n" +1167 "ÖÜ",1168 expectedHash = "cad0f72629a7245dd3d2cbf41473e3ca",1169 mail = new Buffer(encodedText, "utf-8");1170 var mailparser = new MailParser({1171 streamAttachments: true1172 });1173 for (var i = 0, len = mail.length; i < len; i++) {1174 mailparser.write(new Buffer([mail[i]]));1175 }1176 test.expect(4);1177 mailparser.on("attachment", function(attachment, node) {1178 test.ok(attachment.stream, "Stream detected");1179 test.ok(node);1180 });1181 mailparser.end();1182 mailparser.on("end", function(mail) {1183 test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].checksum, expectedHash);1184 test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].length, 10);1185 test.done();1186 });1187 },1188 "Stream multiple attachments": function(test) {1189 var encodedText = "Content-type: multipart/mixed; boundary=ABC\r\n" +1190 "\r\n" +1191 "--ABC\r\n" +1192 "Content-Type: application/octet-stream\r\n" +1193 "Content-Transfer-Encoding: base64\r\n" +1194 "Content-Disposition: attachment\r\n" +1195 "\r\n" +1196 "AAECAwQFBg==\r\n" +1197 "--ABC\r\n" +1198 "Content-Type: application/octet-stream\r\n" +1199 "Content-Transfer-Encoding: base64\r\n" +1200 "Content-Disposition: attachment\r\n" +1201 "\r\n" +1202 "AAECAwQFBg==\r\n" +1203 "--ABC\r\n" +1204 "Content-Type: application/octet-stream\r\n" +1205 "Content-Transfer-Encoding: base64\r\n" +1206 "Content-Disposition: attachment; filename=\"test.txt\"\r\n" +1207 "\r\n" +1208 "AAECAwQFBg==\r\n" +1209 "--ABC--",1210 mail = new Buffer(encodedText, "utf-8");1211 var mailparser = new MailParser({1212 streamAttachments: true1213 });1214 test.expect(3); // should be 3 attachments1215 mailparser.on("attachment", function(attachment) {1216 test.ok(attachment.stream, "Stream detected");1217 });1218 mailparser.end(mail);1219 mailparser.on("end", function() {1220 test.done();1221 });1222 },1223 "Pass mail node to headers event": function(test) {1224 var encodedText = "Content-type: multipart/mixed; boundary=ABC\r\n" +1225 "Subject: ABCDEF\r\n" +1226 "\r\n" +1227 "--ABC\r\n" +1228 "Content-Type: application/octet-stream\r\n" +1229 "Content-Transfer-Encoding: base64\r\n" +1230 "Content-Disposition: attachment\r\n" +1231 "\r\n" +1232 "AAECAwQFBg==\r\n" +1233 "--ABC--",1234 mail = new Buffer(encodedText, "utf-8");1235 var mailparser = new MailParser({1236 streamAttachments: true1237 });1238 for (var i = 0, len = mail.length; i < len; i++) {1239 mailparser.write(new Buffer([mail[i]]));1240 }1241 test.expect(2);1242 mailparser.on("attachment", function(attachment, email) {1243 test.equal(email.subject, "ABCDEF");1244 });1245 mailparser.end();1246 mailparser.on("end", function() {1247 test.ok(1, "Done");1248 test.done();1249 });1250 },1251 "Detect Content-Type by filename": function(test) {1252 var encodedText = "Content-type: multipart/mixed; boundary=ABC\r\n" +1253 "\r\n" +1254 "--ABC\r\n" +1255 "Content-Type: application/octet-stream\r\n" +1256 "Content-Transfer-Encoding: base64\r\n" +1257 "Content-Disposition: attachment; filename=\"test.pdf\"\r\n" +1258 "\r\n" +1259 "AAECAwQFBg==\r\n" +1260 "--ABC--",1261 mail = new Buffer(encodedText, "utf-8");1262 var mailparser = new MailParser();1263 mailparser.write(mail);1264 mailparser.end();1265 mailparser.on("end", function(mail) {1266 test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].contentType, "application/pdf");1267 test.done();1268 });1269 },1270 "Inline attachments": function(test) {1271 var encodedText = "Content-type: multipart/mixed; boundary=ABC\r\n" +1272 "X-Test: =?UTF-8?Q?=C3=95=C3=84?= =?UTF-8?Q?=C3=96=C3=9C?=\r\n" +1273 "Subject: ABCDEF\r\n" +1274 "\r\n" +1275 "--ABC\r\n" +1276 "Content-Type: text/html\r\n" +1277 "\r\n" +1278 "<p>test 1</p>\r\n" +1279 "--ABC\r\n" +1280 "Content-Type: application/octet-stream\r\n" +1281 "Content-Transfer-Encoding: base64\r\n" +1282 "Content-Disposition: attachment; filename=\"test.pdf\"\r\n" +1283 "\r\n" +1284 "AAECAwQFBg==\r\n" +1285 "--ABC\r\n" +1286 "Content-Type: text/html\r\n" +1287 "\r\n" +1288 "<p>test 2</p>\r\n" +1289 "--ABC--",1290 mail = new Buffer(encodedText, "utf-8");1291 test.expect(1);1292 var mailparser = new MailParser({1293 showAttachmentLinks: true1294 });1295 mailparser.end(mail);1296 mailparser.on("end", function(mail) {1297 test.equal(mail.html, '<p>test 1</p><br/>\n\n<div class="mailparser-attachment"><a href="cid:754dc77d28e62763c4916970d595a10f@mailparser">&lt;test.pdf&gt;</a></div><br/>\n<p>test 2</p>');1298 test.done();1299 });1300 }1301};1302exports["Advanced nested HTML"] = function(test) {1303 var mail = fs.readFileSync(__dirname + "/nested.eml");1304 test.expect(2);1305 var mailparser = new MailParser();1306 for (var i = 0, len = mail.length; i < len; i++) {1307 mailparser.write(new Buffer([mail[i]]));1308 }1309 mailparser.end();1310 mailparser.on("end", function(mail) {1311 test.equal(mail.text, "\nDear Sir,\n\nGood evening.\n\n\n \n\n\n\nThe footer\n");1312 test.equal(mail.html, "<p>Dear Sir</p>\n<p>Good evening.</p>\n<p></p><p>The footer</p>\n");1313 test.done();1314 });1315};1316exports["MBOX format"] = {1317 "Not a mbox": function(test) {1318 var encodedText = "Content-Type: text/plain; charset=utf-8\r\n" +1319 "\r\n" +1320 "ÕÄ\r\n" +1321 "ÖÜ", // \r\nÕÄÖÜ1322 mail = new Buffer(encodedText, "utf-8");1323 test.expect(1);1324 var mailparser = new MailParser();1325 for (var i = 0, len = mail.length; i < len; i++) {1326 mailparser.write(new Buffer([mail[i]]));1327 }1328 mailparser.end();1329 mailparser.on("end", function() {1330 test.equal(mailparser._isMbox, false);1331 test.done();1332 });1333 },1334 "Is a mbox": function(test) {1335 var encodedText = "From MAILER-DAEMON Fri Jul 8 12:08:34 2011\r\n" +1336 "Content-Type: text/plain; charset=utf-8\r\n" +1337 "\r\n" +1338 "ÕÄ\r\n" +1339 "ÖÜ", // \r\nÕÄÖÜ1340 mail = new Buffer(encodedText, "utf-8");1341 test.expect(1);1342 var mailparser = new MailParser();1343 for (var i = 0, len = mail.length; i < len; i++) {1344 mailparser.write(new Buffer([mail[i]]));1345 }1346 mailparser.end();1347 mailparser.on("end", function() {1348 test.equal(mailparser._isMbox, true);1349 test.done();1350 });1351 },1352 "Don't unescape '>From '": function(test) {1353 var encodedText = "Content-Type: text/plain; charset=utf-8\r\n" +1354 "\r\n" +1355 ">From test\r\n" +1356 ">>From pest", // \r\nÕÄÖÜ1357 mail = new Buffer(encodedText, "utf-8");1358 test.expect(1);1359 var mailparser = new MailParser();1360 for (var i = 0, len = mail.length; i < len; i++) {1361 mailparser.write(new Buffer([mail[i]]));1362 }1363 mailparser.end();1364 mailparser.on("end", function(mail) {1365 test.equal(mail.text, ">From test\n>>From pest");1366 test.done();1367 });1368 },1369 "Unescape '>From '": function(test) {1370 var encodedText = "From MAILER-DAEMON Fri Jul 8 12:08:34 2011\r\n" +1371 "Content-Type: text/plain; charset=utf-8\r\n" +1372 "\r\n" +1373 ">From test\r\n" +1374 ">>From pest", // \r\nÕÄÖÜ1375 mail = new Buffer(encodedText, "utf-8");1376 test.expect(1);1377 var mailparser = new MailParser();1378 for (var i = 0, len = mail.length; i < len; i++) {1379 mailparser.write(new Buffer([mail[i]]));1380 }1381 mailparser.end();1382 mailparser.on("end", function(mail) {1383 test.equal(mail.text, "From test\n>From pest");1384 test.done();1385 });1386 }...

Full Screen

Full Screen

mailparser-tests.ts

Source:mailparser-tests.ts Github

copy

Full Screen

1// tslint:disable2import mailparser_mod = require('mailparser');3import MailParser = mailparser_mod.MailParser;4import simpleParser = mailparser_mod.simpleParser;5var mailparser = new MailParser();6mailparser.on('headers', function(headers){7 console.log('Subject:', headers.get('subject'));8});9// Attachments10mailparser.on('data', data => {11 if (data.type === 'attachment'){12 console.log(data.filename);13 data.content.pipe(process.stdout);14 data.content.on('end', () => data.release());15 }16});17mailparser.on('data', data => {18 if (data.type === 'text'){19 console.log(data.html);20 }21});22// Pipe file to MailParser23// This example pipes a readableStream file to MailParser24import fs = require('fs');25fs.createReadStream('email.eml').pipe(mailparser);26// check different sources and promise/callback api for simpleParser27var sourceString = '';28var sourceBuffer = new Buffer('');29var sourceStream = fs.createReadStream('foo.eml');30simpleParser(sourceString, (err, mail) => err ? err : mail.html);31simpleParser(sourceBuffer, (err, mail) => err ? err : mail.html);32simpleParser(sourceStream, (err, mail) => err ? err : mail.html);33simpleParser(sourceString).then(mail => mail.html).catch(err => err);34simpleParser(sourceBuffer).then(mail => mail.html).catch(err => err);35simpleParser(sourceStream).then(mail => mail.html).catch(err => err);36simpleParser(sourceString, (err, mail) => {37 console.log(mail.headers.get('subject'));38 console.log(mail.subject);39 // Attachments40 mail.attachments.forEach(attachment => console.log(attachment.filename));41 // Text42 console.log(mail.text);43 console.log(mail.html);44 console.log(mail.textAsHtml);...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var http = require('http');2var fs = require('fs');3var options = {4 headers: {5 }6};7var req = http.request(options, function(res) {8 console.log('STATUS: ' + res.statusCode);9 console.log('HEADERS: ' + JSON.stringify(res.headers));10 res.setEncoding('utf8');11 res.on('data', function(chunk) {12 console.log('BODY: ' + chunk);13 });14});15req.on('error', function(e) {16 console.log('problem with request: ' + e.message);17});18var data = JSON.stringify({19 "stubs": [{20 "responses": [{21 "is": {22 }23 }]24 }],25 "_links": {26 "self": {27 }28 }29});30req.write(data);31req.end();32var http = require('http');33var fs = require('fs');34var options = {35 headers: {36 }37};38var req = http.request(options, function(res) {39 console.log('STATUS: ' + res.statusCode);40 console.log('HEADERS: ' + JSON.stringify(res.headers));41 res.setEncoding('utf8');42 res.on('data', function(chunk) {43 console.log('BODY: ' + chunk);44 });45});46req.on('error', function(e) {47 console.log('problem with request: ' + e.message);48});49var data = JSON.stringify({50 "predicates": [{51 "equals": {52 }53 }],54 "responses": [{55 "is": {56 }57 }]58});59req.write(data);60req.end();

Full Screen

Using AI Code Generation

copy

Full Screen

1var mailParser = require('mailparser').simpleParser;2var mb = require('mountebank');3mb.create({4}, function (error) {5 if (error) {6 console.error('Failed to start server', error);7 } else {8 console.log('Server is now running');9 mb.post('/imposters', {10 {11 {12 is: {13 }14 }15 }16 }, function (error, response) {17 if (error) {18 console.error('Failed to create imposter', error);19 } else {20 console.log('Imposter created', response.body);21 }22 });23 }24});25var nodemailer = require('nodemailer');26var transporter = nodemailer.createTransport({27});28var mailOptions = {

Full Screen

Using AI Code Generation

copy

Full Screen

1const mailParser = require('mailparser').simpleParser;2const mbHelper = require('mountebank-helper');3const fs = require('fs');4const path = require('path');5mb.createImposter({6}).then(() => {7 return mb.createStub({8 {9 "equals": {10 }11 }12 {13 "is": {14 "headers": {15 },16 "body": JSON.stringify({17 })18 }19 }20 });21}).then(() => {22 fs.readFile(path.join(__dirname, 'email.html'), 'utf8', (err, data) => {23 if (err) {24 return console.log(err);25 }26 mailParser(data).then(mail => {27 console.log(mail.text);28 });29 });30});

Full Screen

Using AI Code Generation

copy

Full Screen

1var mailParser = require('mailparser').simpleParser;2var mail = require('mailparser').mail;3var mailParser = require('mailparser').simpleParser;4var mail = require('mailparser').mail;5var mailParser = require('mailparser').simpleParser;6var mail = require('mailparser').mail;7var mailParser = require('mailparser').simpleParser;8var mail = require('mailparser').mail;9var mailParser = require('mailparser').simpleParser;10var mail = require('mailparser').mail;11var mailParser = require('mailparser').simpleParser;12var mail = require('mailparser').mail;13var mailParser = require('mailparser').simpleParser;14var mail = require('mailparser').mail;15var mailParser = require('mailparser').simpleParser;16var mail = require('mailparser').mail;17var mailParser = require('mailparser').simpleParser;18var mail = require('mailparser').mail;19var mailParser = require('mailparser').simpleParser;20var mail = require('mailparser').mail;21var mailParser = require('mailparser').simpleParser;22var mail = require('mailparser').mail;23var mailParser = require('mailparser').simpleParser;24var mail = require('mailparser').mail;25var mailParser = require('mailparser').simpleParser;26var mail = require('mailparser').mail;27var mailParser = require('mailparser').simpleParser;28var mail = require('mailparser').mail;29var mailParser = require('mailparser').simpleParser;30var mail = require('mailparser').mail;

Full Screen

Using AI Code Generation

copy

Full Screen

1var mailParser = require('mailparser').simpleParser;2var mb = require('mountebank');3var port = 2525;4var mbPort = 2525;5var mbHost = 'localhost';6mb.create({7}, function (error, imposter) {8 if (error) {9 console.log('error', error);10 } else {11 console.log('imposter', imposter);12 imposter.addStub({13 responses: [{14 is: {15 headers: {16 },17 body: JSON.stringify({hello: 'world'})18 }19 }]20 });21 imposter.addStub({22 predicates: [{23 equals: {24 }25 }],26 responses: [{27 is: {28 headers: {29 },30 body: JSON.stringify({mail: 'received'})31 }32 }]33 });34 imposter.addStub({35 predicates: [{36 equals: {37 }38 }],39 responses: [{40 is: {41 headers: {42 },43 body: JSON.stringify({mail: 'received'})44 }45 }]46 });47 imposter.addStub({48 predicates: [{49 equals: {50 }51 }],52 responses: [{53 is: {54 headers: {55 },56 body: JSON.stringify({mail: 'received'})57 }58 }]59 });60 imposter.addStub({61 predicates: [{62 equals: {63 }64 }],65 responses: [{66 is: {67 headers: {68 },69 body: JSON.stringify({mail: 'received'})70 }71 }]72 });73 imposter.addStub({74 predicates: [{75 equals: {

Full Screen

Using AI Code Generation

copy

Full Screen

1var mailparse = require('mailparser').simpleParser;2mailparse(body).then(mail => {3 console.log(mail.subject);4 console.log(mail.from.text);5 console.log(mail.to.text);6 console.log(mail.text);7 console.log(mail.html);8 console.log(mail.textAsHtml);9 console.log(mail.attachments);10});

Full Screen

Using AI Code Generation

copy

Full Screen

1var mailParser = require('mailparser').simpleParser;2var fs = require('fs');3var email = fs.readFileSync('email.txt', 'utf8');4mailParser(email).then(function(mail) {5 console.log(mail);6});7{ 8 from: {

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run mountebank automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful