Lets the CI preview server stand in for the contact API

With contactForm.enabled=true the e2e and Lighthouse runs asked a token route
that CI does not have (404, console error, run 666). The preview server gets an
opt-in --contact-stub (token 200 without captcha, submit 202); the e2e server
helper and the Lighthouse start command switch it on. Tests that need another
answer still replace it per page with page.route.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-11 19:39:03 +01:00
co-authored by Claude Sonnet 5.5
parent 7e708cf3c7
commit 7737104a77
5 changed files with 87 additions and 4 deletions
+2
View File
@@ -113,6 +113,8 @@ describe('Lighthouse-configuratie', () => {
it('start de preview met de auditroute en meet een keer', () => {
expect(startCommand).toContain('--audit-route');
// CI heeft geen CMS-host: de preview bootst de contact-API na
expect(startCommand).toContain('--contact-stub');
expect(collect.numberOfRuns).toBe(1);
});
+23 -1
View File
@@ -570,7 +570,7 @@ function siteDir(): string {
async function serve(
clientDir: string,
options?: { auditRoute?: boolean },
options?: { auditRoute?: boolean; contactStub?: boolean },
): Promise<number> {
const server = await startPreviewServer(clientDir, 0, options);
servers.push(server);
@@ -652,6 +652,28 @@ describe('startPreviewServer', () => {
expect((await ask(port, '/andere')).status).toBe(404);
});
it('bootst de contact-API alleen na met de optie (--contact-stub)', async () => {
const without = await serve(siteDir());
expect(
(await ask(without, '/api/v1/contact-messages/token')).status,
).toBe(404);
const port = await serve(siteDir(), { contactStub: true });
const token = await ask(port, '/api/v1/contact-messages/token');
expect(token.status).toBe(200);
expect(token.headers['content-type']).toBe('application/json');
expect(JSON.parse(token.body)).toMatchObject({
captcha: null,
minSeconds: expect.any(Number),
});
expect(
(await ask(port, '/api/v1/contact-messages', 'POST')).status,
).toBe(202);
// Al het andere blijft zoals het was
expect((await ask(port, '/api/v1/andere')).status).toBe(404);
expect((await ask(port, '/api/v1/contact-messages')).status).toBe(404);
});
it('stuurt een 301 met een relatieve Location, de query en de securityheaders, zonder inhoud', async () => {
const port = await serve(siteDir());
const expected = securityHeaders('production');
+55 -1
View File
@@ -190,6 +190,37 @@ const searchOf = (target: string) => {
export interface PreviewOptions {
auditRoute?: boolean;
// Opt-in stand-in voor de contact-API (--contact-stub): CI heeft geen
// CMS-host, dus met contactForm.enabled=true kreeg de site een 404 op
// de tokenroute (console-fout, run 666). Met deze optie antwoordt de
// server zoals een open formulier zonder captcha: token 200, versturen
// 202. Productie gebruikt dit nooit; tests kunnen het per pagina met
// page.route vervangen (bijvoorbeeld 404 of een captcha).
contactStub?: boolean;
}
const CONTACT_API = '/api/v1/contact-messages';
// Antwoord van de stub, of undefined als het verzoek er niet voor is
export function contactStubResponse(
method: string | undefined,
path: string,
): { status: number; body: string } | undefined {
const json = (status: number, value: unknown) => ({
status,
body: JSON.stringify(value),
});
if (method === 'GET' && path === `${CONTACT_API}/token`) {
return json(200, {
token: 'preview-token',
minSeconds: 1,
captcha: null,
});
}
if (method === 'POST' && path === CONTACT_API) {
return json(202, { status: 'received' });
}
return undefined;
}
export function startPreviewServer(
@@ -207,6 +238,25 @@ export function startPreviewServer(
};
const handle = (request: IncomingMessage, response: ServerResponse) => {
if (options.contactStub) {
const stub = contactStubResponse(
request.method,
pathOf(request.url ?? '/'),
);
if (stub) {
send(
response,
stub.status,
{
'Content-Type': 'application/json',
'Cache-Control': 'no-store',
},
stub.body,
);
request.resume();
return;
}
}
if (request.method !== 'GET' && request.method !== 'HEAD') {
send(
response,
@@ -284,6 +334,7 @@ function parseArguments(args: string[]) {
strictPort: false,
dir: 'dist/client',
auditRoute: false,
contactStub: false,
};
for (let i = 0; i < args.length; i++) {
const [name, inline] = args[i].split('=', 2);
@@ -292,6 +343,7 @@ function parseArguments(args: string[]) {
else if (name === '--dir') options.dir = value();
else if (name === '--strictPort') options.strictPort = true;
else if (name === '--audit-route') options.auditRoute = true;
else if (name === '--contact-stub') options.contactStub = true;
}
return options;
}
@@ -299,7 +351,8 @@ function parseArguments(args: string[]) {
const MAX_PORT_TRIES = 10;
async function main(args: string[]) {
const { port, strictPort, dir, auditRoute } = parseArguments(args);
const { port, strictPort, dir, auditRoute, contactStub } =
parseArguments(args);
if (!Number.isInteger(port) || port < 0 || port > 65535) {
console.error(`Ongeldige poort: ${port}`);
process.exitCode = 1;
@@ -312,6 +365,7 @@ async function main(args: string[]) {
try {
const server = await startPreviewServer(dir, port + attempt, {
auditRoute,
contactStub,
});
const address = server.address();
const actual =
+6 -1
View File
@@ -10,8 +10,13 @@ export const clientDir = resolve('dist/client');
// Dezelfde server als `pnpm preview`, met de productie-CSP en echte 404's,
// op een poort die het systeem kiest zodat tests elkaar niet in de weg zitten.
// auditRoute zet de opt-in route /__audit/404 aan, zoals Lighthouse hem start.
// De contact-API wordt standaard nagebootst (CI heeft geen CMS-host); een
// test die iets anders nodig heeft vervangt het met page.route.
export async function startServer(options: { auditRoute?: boolean } = {}) {
const server = await startPreviewServer(clientDir, 0, options);
const server = await startPreviewServer(clientDir, 0, {
contactStub: true,
...options,
});
const { port } = server.address() as AddressInfo;
return {
origin: `http://127.0.0.1:${port}`,
+1 -1
View File
@@ -140,7 +140,7 @@ module.exports = {
ci: {
collect: {
startServerCommand:
'pnpm preview --port 4173 --strictPort --audit-route',
'pnpm preview --port 4173 --strictPort --audit-route --contact-stub',
startServerReadyPattern: 'Local',
url: [...indexedPaths, ...noindexPaths, notFoundPath].map(
(path) => origin + path,