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
5 changes: 3 additions & 2 deletions tests/helper.mjs
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -73,7 +73,8 @@ export default async function testSetup(helpText) {
}
}
const PORT = options.port;
const server = await serve(PORT);

const server = await serve(PORT, DEFAULT_CACHE_DURATION);
let driver, logInspector;

process.on("unhandledRejection", (err) => {
Expand Down
31 changes: 27 additions & 4 deletions tests/server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,34 @@ import "lws-log";
import "lws-static";

const ROOT_DIR = path.join(process.cwd(), "./");
export const DEFAULT_CACHE_DURATION = 3600;

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",
directory: ROOT_DIR,
corsOpenerPolicy: "same-origin",
corsEmbedderPolicy: "require-corp",
logFormat: "dev",
stack: ["lws-log", "lws-cors", "lws-static", "lws-index"],
stack,
cache: cacheDuration,
});
await verifyStartup(ws, port);

Expand Down Expand Up @@ -51,9 +67,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 ${DEFAULT_CACHE_DURATION}.` },
];
const options = commandLineArgs(optionDefinitions);
serve(options.port);
let cacheDuration = undefined;
if ("cache" in options)
cacheDuration = options.cache ?? DEFAULT_CACHE_DURATION;

serve(options.port, cacheDuration);
}

if (esMain(import.meta))
Expand Down
Loading