Forked from harshallmstudio/document-streamer
Forked from harshallmstudio/document-streamer
src / tests / mhtmlParser.test.ts
import assert from "node:assert/strict";
import test from "node:test";
import { extractMhtmlHtml } from "../mhtmlParser";
test("extracts a quoted-printable HTML document from MHTML", () => {
const archive = [
"From: <Saved by Test>",
"MIME-Version: 1.0",
'Content-Type: multipart/related; boundary="test-boundary"',
"",
"--test-boundary",
"Content-Type: text/html; charset=UTF-8",
"Content-Transfer-Encoding: quoted-printable",
"",
"<html><body><h1>Saved page</h1><p>Price: =E2=82=AC10</p></body></html>",
"--test-boundary--",
"",
].join("\r\n");
const html = extractMhtmlHtml(Buffer.from(archive, "utf8"));
assert.match(html, /<h1>Saved page<\/h1>/);
assert.match(html, /Price: €10/);
});
test("extracts a Base64 HTML document from MHTML", () => {
const html = "<html><body><h1>Saved report</h1></body></html>";
const encoded = Buffer.from(html, "utf8").toString("base64");
const archive = [
"MIME-Version: 1.0",
'Content-Type: multipart/related; boundary="boundary42"',
"",
"--boundary42",
"Content-Type: text/html; charset=UTF-8",
"Content-Transfer-Encoding: base64",
"",
encoded,
"--boundary42--",
"",
].join("\r\n");
assert.equal(extractMhtmlHtml(Buffer.from(archive)), html);
});
test("rejects an archive without an HTML part", () => {
const archive = [
"Content-Type: multipart/related; boundary=sample",
"",
"--sample",
"Content-Type: image/png",
"Content-Transfer-Encoding: base64",
"",
"aGVsbG8=",
"--sample--",
"",
].join("\r\n");
assert.throws(
() => extractMhtmlHtml(Buffer.from(archive, "utf8")),
/No HTML document part/,
);
});
src / tests / mhtmlParser.test.ts
import assert from "node:assert/strict";
import test from "node:test";
import { extractMhtmlHtml } from "../mhtmlParser";
test("extracts a quoted-printable HTML document from MHTML", () => {
const archive = [
"From: <Saved by Test>",
"MIME-Version: 1.0",
'Content-Type: multipart/related; boundary="test-boundary"',
"",
"--test-boundary",
"Content-Type: text/html; charset=UTF-8",
"Content-Transfer-Encoding: quoted-printable",
"",
"<html><body><h1>Saved page</h1><p>Price: =E2=82=AC10</p></body></html>",
"--test-boundary--",
"",
].join("\r\n");
const html = extractMhtmlHtml(Buffer.from(archive, "utf8"));
assert.match(html, /<h1>Saved page<\/h1>/);
assert.match(html, /Price: €10/);
});
test("extracts a Base64 HTML document from MHTML", () => {
const html = "<html><body><h1>Saved report</h1></body></html>";
const encoded = Buffer.from(html, "utf8").toString("base64");
const archive = [
"MIME-Version: 1.0",
'Content-Type: multipart/related; boundary="boundary42"',
"",
"--boundary42",
"Content-Type: text/html; charset=UTF-8",
"Content-Transfer-Encoding: base64",
"",
encoded,
"--boundary42--",
"",
].join("\r\n");
assert.equal(extractMhtmlHtml(Buffer.from(archive)), html);
});
test("rejects an archive without an HTML part", () => {
const archive = [
"Content-Type: multipart/related; boundary=sample",
"",
"--sample",
"Content-Type: image/png",
"Content-Transfer-Encoding: base64",
"",
"aGVsbG8=",
"--sample--",
"",
].join("\r\n");
assert.throws(
() => extractMhtmlHtml(Buffer.from(archive, "utf8")),
/No HTML document part/,
);
});