Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"math-helpers": "~0.1.0",
"nodemon": "^3.1.3",
"prettier": "2.2.1",
"tap": "^21.1.3",
"ws": "^8.17.0"
},
"scripts": {
Expand All @@ -39,7 +38,7 @@
"pre-commit": "npm run autofix && npm test",
"lint": "eslint .",
"autofix": "eslint . --fix",
"test-spec": "tap --allow-incomplete-coverage test/*spec.js",
"test-spec": "node --test test/*spec.js",
"test-perf": "node test/performance.js"
},
"license": "AGPL-3.0",
Expand Down
129 changes: 52 additions & 77 deletions test/charset_spec.js
Original file line number Diff line number Diff line change
@@ -1,101 +1,76 @@
"use strict";

var test = require("tap").test;
var fs = require("fs");
var crypto = require("crypto");
var http = require("http");
var concat = require("concat-stream");
var getServers = require("./test_utils.js").getServers;
const assert = require("node:assert/strict");
const fs = require("node:fs");
const crypto = require("node:crypto");
const { test } = require("node:test");
const { getServersAsync, closeServers, readUrl } = require("./test_utils.js");
const Unblocker = require("../lib/unblocker.js");

// source is http://qa-dev.w3.org/wmvs/HEAD/dev/tests/xhtml-windows-1250.xhtml which is linked to from http://validator.w3.org/dev/tests/#encoding
var sourceContent = fs.readFileSync(
const sourceContent = fs.readFileSync(
__dirname + "/source/xhtml-windows-1250.xhtml"
);
var expected = fs.readFileSync(
const expected = fs.readFileSync(
__dirname + "/expected/xhtml-windows-1250-converted-to-utf-8.xhtml"
);

// first validate that the IDE or whatever didn't change the file encoding
var SOURCE_HASH = "11f694099b205b26a19648ab22602b39c6deb125";
var EXPECTED_HASH = "4a04a0aa660da6f0eec9534c0e25212a7045ea7c";
test("source and expected xhtml-windows-1250.xhtml files should not have changed", function (t) {
t.equal(
const SOURCE_HASH = "11f694099b205b26a19648ab22602b39c6deb125";
const EXPECTED_HASH = "4a04a0aa660da6f0eec9534c0e25212a7045ea7c";

test("source and expected xhtml-windows-1250.xhtml files should not have changed", () => {
assert.strictEqual(
crypto.createHash("sha1").update(sourceContent).digest("hex"),
SOURCE_HASH
);
t.equal(
assert.strictEqual(
crypto.createHash("sha1").update(expected).digest("hex"),
EXPECTED_HASH
);
t.end();
});

test("should properly decode and update non-native charsets when charset is in header", function (t) {
t.plan(1);
getServers(
{
unblocker: new Unblocker({ clientScripts: false }),
sourceContent,
charset: "windows-1250",
},
function (err, servers) {
http
.get(servers.proxiedUrl, function (res) {
res.pipe(
concat(function (actual) {
servers.kill();
t.same(actual, expected);
})
);
})
.on("error", function (e) {
t.bailout(e);
});
}
);
test("should properly decode and update non-native charsets when charset is in header", async () => {
const servers = await getServersAsync({
unblocker: new Unblocker({ clientScripts: false }),
sourceContent,
charset: "windows-1250",
});

try {
const actual = await readUrl(servers.proxiedUrl);
assert.deepStrictEqual(actual, expected);
} finally {
await closeServers(servers);
}
});

test("should properly decode and update charsets when charset is in body", function (t) {
t.plan(1);
getServers(
{ unblocker: new Unblocker({ clientScripts: false }), sourceContent },
function (err, servers) {
http
.get(servers.proxiedUrl, function (res) {
res.pipe(
concat(function (actual) {
servers.kill();
t.same(actual, expected);
})
);
})
.on("error", function (e) {
t.bailout(e);
});
}
);
test("should properly decode and update charsets when charset is in body", async () => {
const servers = await getServersAsync({
unblocker: new Unblocker({ clientScripts: false }),
sourceContent,
});

try {
const actual = await readUrl(servers.proxiedUrl);
assert.deepStrictEqual(actual, expected);
} finally {
await closeServers(servers);
}
});

test("should still work when charset can be determined", function (t) {
t.plan(1);
var sourceContent = "<h1>test</h1>",
expected = "<h1>test</h1>";
getServers(
{ unblocker: new Unblocker({ clientScripts: false }), sourceContent },
function (err, servers) {
http
.get(servers.proxiedUrl, function (res) {
res.pipe(
concat(function (actual) {
servers.kill();
t.same(actual.toString(), expected);
})
);
})
.on("error", function (e) {
t.bailout(e);
});
}
);
test("should still work when charset can be determined", async () => {
const sourceContent = "<h1>test</h1>";
const expectedValue = "<h1>test</h1>";
const servers = await getServersAsync({
unblocker: new Unblocker({ clientScripts: false }),
sourceContent,
});

try {
const actual = await readUrl(servers.proxiedUrl);
assert.strictEqual(actual.toString(), expectedValue);
} finally {
await closeServers(servers);
}
});
14 changes: 7 additions & 7 deletions test/content-types_spec.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
"use strict";

var test = require("tap").test;
var contentTypes = require("../lib/content-types.js");
const assert = require("node:assert/strict");
const contentTypes = require("../lib/content-types.js");
const { test } = require("node:test");

test("should handle content types with a charset", function (t) {
var config = {
test("should handle content types with a charset", () => {
const config = {
processContentTypes: ["text/html"],
};
var data = {
const data = {
headers: {
"content-type": "text/html; charset=utf-8",
},
};
data.contentType = contentTypes.getType(data);
t.ok(contentTypes.shouldProcess(config, data));
t.end();
assert.ok(contentTypes.shouldProcess(config, data));
});
Loading
Loading