From d9ab2a0e983e1e613a08ee36052319e40c39fd73 Mon Sep 17 00:00:00 2001 From: Camillo Bruni Date: Tue, 21 Jul 2026 16:18:41 +0200 Subject: [PATCH 1/2] add-cached --- tests/helper.mjs | 4 +++- tests/server.mjs | 30 ++++++++++++++++++++++++++---- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/tests/helper.mjs b/tests/helper.mjs index fb25cbc3a..9f63abec1 100644 --- a/tests/helper.mjs +++ b/tests/helper.mjs @@ -73,7 +73,9 @@ export default async function testSetup(helpText) { } } const PORT = options.port; - const server = await serve(PORT); + const CACHE_DURATION = 3600; + + const server = await serve(PORT, CACHE_DURATION); let driver, logInspector; process.on("unhandledRejection", (err) => { diff --git a/tests/server.mjs b/tests/server.mjs index a05cf3af1..e9d170e51 100644 --- a/tests/server.mjs +++ b/tests/server.mjs @@ -11,9 +11,23 @@ import "lws-static"; const ROOT_DIR = path.join(process.cwd(), "./"); -export default async function serve(port) { +class CacheControlPlugin { + middleware(config) { + return async (ctx, next) => { + ctx.set("Cache-Control", `max-age=${config.cache}`); + await next(); + }; + } +} + +export default async function serve(port, cacheDuration) { if (!port) throw new Error("Port is required"); + + const stack = ["lws-log", "lws-cors", "lws-static", "lws-index"]; + if (cacheDuration !== undefined) + stack.unshift(CacheControlPlugin); + const ws = await LocalWebServer.create({ port: port, hostname: "127.0.0.1", @@ -21,7 +35,8 @@ export default async function serve(port) { corsOpenerPolicy: "same-origin", corsEmbedderPolicy: "require-corp", logFormat: "dev", - stack: ["lws-log", "lws-cors", "lws-static", "lws-index"], + stack, + cache: cacheDuration, }); await verifyStartup(ws, port); @@ -51,9 +66,16 @@ async function verifyStartup(ws, port) { } function main() { - const optionDefinitions = [{ name: "port", type: Number, defaultValue: 8080, description: "Set the test-server port, The default value is 8080." }]; + const optionDefinitions = [ + { name: "port", type: Number, defaultValue: 8080, description: "Set the test-server port, The default value is 8080." }, + { name: "cache", type: Number, description: "Set the cache duration in seconds. If flag is present without a value, defaults to 3600." }, + ]; const options = commandLineArgs(optionDefinitions); - serve(options.port); + let cacheDuration = undefined; + if ("cache" in options) + cacheDuration = options.cache ?? 3600; + + serve(options.port, cacheDuration); } if (esMain(import.meta)) From 91219d7d20fed977758394d4737ee6adafe12334 Mon Sep 17 00:00:00 2001 From: Camillo Bruni Date: Tue, 21 Jul 2026 16:24:22 +0200 Subject: [PATCH 2/2] cleanup --- tests/helper.mjs | 5 ++--- tests/server.mjs | 5 +++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/helper.mjs b/tests/helper.mjs index 9f63abec1..dfcd9c3ce 100644 --- a/tests/helper.mjs +++ b/tests/helper.mjs @@ -1,6 +1,6 @@ import commandLineUsage from "command-line-usage"; import commandLineArgs from "command-line-args"; -import serve from "./server.mjs"; +import serve, { DEFAULT_CACHE_DURATION } from "./server.mjs"; import firefox from "selenium-webdriver/firefox.js"; import chrome from "selenium-webdriver/chrome.js"; @@ -73,9 +73,8 @@ export default async function testSetup(helpText) { } } const PORT = options.port; - const CACHE_DURATION = 3600; - const server = await serve(PORT, CACHE_DURATION); + const server = await serve(PORT, DEFAULT_CACHE_DURATION); let driver, logInspector; process.on("unhandledRejection", (err) => { diff --git a/tests/server.mjs b/tests/server.mjs index e9d170e51..b40c6d76c 100644 --- a/tests/server.mjs +++ b/tests/server.mjs @@ -10,6 +10,7 @@ import "lws-log"; import "lws-static"; const ROOT_DIR = path.join(process.cwd(), "./"); +export const DEFAULT_CACHE_DURATION = 3600; class CacheControlPlugin { middleware(config) { @@ -68,12 +69,12 @@ async function verifyStartup(ws, port) { function main() { const optionDefinitions = [ { name: "port", type: Number, defaultValue: 8080, description: "Set the test-server port, The default value is 8080." }, - { name: "cache", type: Number, description: "Set the cache duration in seconds. If flag is present without a value, defaults to 3600." }, + { name: "cache", type: Number, description: `Set the cache duration in seconds. If flag is present without a value, defaults to ${DEFAULT_CACHE_DURATION}.` }, ]; const options = commandLineArgs(optionDefinitions); let cacheDuration = undefined; if ("cache" in options) - cacheDuration = options.cache ?? 3600; + cacheDuration = options.cache ?? DEFAULT_CACHE_DURATION; serve(options.port, cacheDuration); }