36 lines
1.7 KiB
JavaScript
36 lines
1.7 KiB
JavaScript
#!/usr/bin/env node
|
|
"use strict";
|
|
// Checks that the panel routing tester (src/admin/web/vhosts/routing.js) selects the
|
|
// same location as the proxy for tests/fixtures/routing_conditions_cases.json, which
|
|
// the Rust test `fixture_cases_select_the_expected_location` also uses.
|
|
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
const vm = require("node:vm");
|
|
const assert = require("node:assert/strict");
|
|
|
|
const root = path.resolve(__dirname, "../..");
|
|
const sandbox = { window: {}, BigInt, RegExp, Math, Number, String, Array, Boolean };
|
|
vm.runInNewContext(fs.readFileSync(path.join(root, "src/admin/web/vhosts/routing.js"), "utf8"), sandbox, { filename: "routing.js" });
|
|
const core = sandbox.window.rustProxyRoutingCore;
|
|
assert.equal(typeof core?.evaluate, "function");
|
|
|
|
const fixture = JSON.parse(fs.readFileSync(path.join(root, "tests/fixtures/routing_conditions_cases.json"), "utf8"));
|
|
for (const item of fixture.cases) {
|
|
const request = {
|
|
method: item.method || "GET", path: item.path, query: item.query ?? null, host: item.host || "site.test",
|
|
client_ip: item.client_ip || "203.0.113.5", country: item.country || null,
|
|
headers: Object.entries(item.headers || {}),
|
|
};
|
|
const { winner } = core.evaluate(fixture.vhost.routes, request);
|
|
assert.equal(winner ? winner.route.id : "main", item.expect, item.name);
|
|
}
|
|
|
|
const cidr = core.cidrContains;
|
|
assert.ok(cidr("10.0.0.0/8", "10.200.1.1"));
|
|
assert.ok(!cidr("10.0.0.0/8", "11.0.0.1"));
|
|
assert.ok(cidr("2001:db8::/32", "2001:db8:1::5"));
|
|
assert.ok(!cidr("2001:db8::/32", "2001:db9::1"));
|
|
assert.ok(cidr("192.0.2.7", "::ffff:192.0.2.7"), "IPv4-mapped addresses are compared as IPv4");
|
|
assert.ok(cidr("::1", "::1"));
|
|
console.log(`routing tester: ok (${fixture.cases.length} cases)`);
|