61 lines
3.6 KiB
JavaScript
61 lines
3.6 KiB
JavaScript
#!/usr/bin/env node
|
|
"use strict";
|
|
// Small dependency-free smoke test for the cache-rule editor's VCL output.
|
|
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 source = fs.readFileSync(path.join(root, "src/admin/web/cache-rules.js"), "utf8");
|
|
const sandbox = { window: {} };
|
|
vm.runInNewContext(source, sandbox, { filename: "cache-rules.js" });
|
|
const editor = sandbox.window.rustProxyCacheRules;
|
|
assert.equal(typeof editor?.exportVcl, "function");
|
|
const vcl = editor.exportVcl([
|
|
{ id: "r1", stage: "request", conditions: [], actions: [
|
|
{ type: "remove_request_header", name: "X-Tracking" },
|
|
{ type: "set_request_header", name: "X-Device", value: "mobile" },
|
|
{ type: "lookup" },
|
|
], stop: true },
|
|
{ id: "r2", stage: "request", hash_stage: true, conditions: [], actions: [
|
|
{ type: "hash_header", name: "X-Device" },
|
|
] },
|
|
{ id: "r3", stage: "response", conditions: [], actions: [
|
|
{ type: "no_store" }, { type: "allow_store" },
|
|
{ type: "set_backend_header", name: "Vary", value: "Accept-Language" },
|
|
] },
|
|
{ id: "r4", stage: "deliver", conditions: [], actions: [], stop: true },
|
|
]);
|
|
for (const statement of [
|
|
"sub vcl_recv {", "unset req.http.X-Tracking;",
|
|
'set req.http.X-Device = "mobile";', "return (hash);",
|
|
"sub vcl_hash {", "hash_data(req.http.X-Device);",
|
|
"sub vcl_backend_response {", "set beresp.uncacheable = true;",
|
|
"set beresp.uncacheable = false;", 'set beresp.http.Vary = "Accept-Language";',
|
|
"sub vcl_deliver {", "return (deliver);",
|
|
]) assert.ok(vcl.includes(statement), `missing in VCL export: ${statement}`);
|
|
assert.ok(!vcl.includes("return (lookup);"), "should not generate lookup in recv");
|
|
assert.equal((vcl.match(/return \(deliver\);/g) || []).length, 1, "single terminal deliver");
|
|
// Cookie/session guards: templates emit cookie_pass, explicit guards keep pass.
|
|
sandbox.t = key => key;
|
|
const actionsOf = key => JSON.parse(JSON.stringify(editor.TEMPLATES[key]().map(rule => rule.actions.map(a => a.type).join(","))));
|
|
for (const key of ["cookiepass", "sessioncookies", "django", "laravel", "authjs", "aspnet", "drupal"])
|
|
assert.deepEqual(actionsOf(key), ["cookie_pass"], key);
|
|
assert.deepEqual(actionsOf("wordpress"), ["pass", "cookie_pass"]);
|
|
assert.deepEqual(actionsOf("woocommerce"), ["pass", "cookie_pass"]);
|
|
assert.deepEqual(actionsOf("cdn"), ["pass", "pass", "pass", "strip_cookies", "cookie_pass"]);
|
|
for (const key of ["safemethods", "authbypass", "checkout", "preview", "webhooks", "graphql", "signedurls"])
|
|
assert.deepEqual(actionsOf(key), ["pass"], key);
|
|
for (const key of ["privateroutes", "privateapi"])
|
|
assert.equal(actionsOf(key)[0], "pass", key);
|
|
// Every cookie_pass template rule has a non-negated Cookie condition (server validation).
|
|
for (const key of Object.keys(editor.TEMPLATES)) for (const rule of editor.TEMPLATES[key]())
|
|
if (rule.actions.some(a => a.type === "cookie_pass"))
|
|
assert.ok(rule.conditions.some(c => !c.negate && (c.field === "cookie" || (c.field === "header" && String(c.name).toLowerCase() === "cookie"))), key);
|
|
const guardVcl = editor.exportVcl([{ id: "g", stage: "request", match: "all", conditions: [{ field: "header", name: "Cookie", operator: "exists", values: [] }], actions: [{ type: "cookie_pass" }], stop: true }]);
|
|
assert.ok(guardVcl.includes("return (cookie_pass);"), guardVcl);
|
|
assert.ok(guardVcl.includes("RustProxy extension (Cookie guard)"), guardVcl);
|
|
console.log("PASS: cache rules JS export, request headers, store override, vcl_hash and terminal deliver");
|
|
console.log("PASS: cookie_pass templates and VCL export");
|