Pages now map to their html file, other forms get one redirect, unknown URLs get a real 404 and responses are compressed. A browser-free test suite runs the rules against a real nginx, in CI only so far. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
89 lines
3.0 KiB
TypeScript
89 lines
3.0 KiB
TypeScript
import { randomBytes } from 'node:crypto';
|
|
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
|
|
import { tmpdir } from 'node:os';
|
|
import { dirname, join } from 'node:path';
|
|
import { pagePaths } from '../../config/canonicalUrls.ts';
|
|
import { NOT_FOUND_FILE, pageFile } from '../../config/pageFiles.ts';
|
|
|
|
// Een kleine site in de tijdelijke map met dezelfde vorm als dist/client, voor
|
|
// de serverregeltests: elk paginabestand, de map soorten-fotoshoots/ naast
|
|
// soorten-fotoshoots.html (BR-HST-07) en assets met hoofdletters in de naam.
|
|
// Zo hangen die tests niet af van een build.
|
|
|
|
export interface FixtureSite {
|
|
dir: string;
|
|
// Bestand (relatief aan dir) naar de tekst die erin staat, zodat een test
|
|
// aan het antwoord kan zien welk bestand geserveerd is
|
|
markers: ReadonlyMap<string, string>;
|
|
remove(): Promise<void>;
|
|
}
|
|
|
|
// Alles boven de 256 bytes van gzip_min_length, anders blijft het onbewerkt
|
|
const padding = ' vulling'.repeat(80);
|
|
|
|
export const markerOf = (file: string) => `fixture:${file}`;
|
|
|
|
export async function createFixtureSite(): Promise<FixtureSite> {
|
|
const dir = await mkdtemp(join(tmpdir(), 'andreaslop-fixture-'));
|
|
const markers = new Map<string, string>();
|
|
|
|
const write = async (file: string, content: string | Buffer) => {
|
|
const target = join(dir, file);
|
|
await mkdir(dirname(target), { recursive: true });
|
|
await writeFile(target, content);
|
|
};
|
|
const writeMarked = async (
|
|
file: string,
|
|
wrap: (text: string) => string,
|
|
) => {
|
|
markers.set(file, markerOf(file));
|
|
await write(file, wrap(markerOf(file)));
|
|
};
|
|
|
|
for (const path of [...pagePaths, null]) {
|
|
const file = path === null ? NOT_FOUND_FILE : pageFile(path);
|
|
await writeMarked(
|
|
file,
|
|
(marker) =>
|
|
`<!doctype html><html><body><p>${marker}</p><!--${padding}--></body></html>`,
|
|
);
|
|
}
|
|
await writeMarked(
|
|
'robots.txt',
|
|
(marker) => `${marker}\nUser-agent: *\nAllow: /\n`,
|
|
);
|
|
await writeMarked(
|
|
'sitemap.xml',
|
|
(marker) =>
|
|
`<?xml version="1.0"?><urlset><!--${marker}${padding}--></urlset>`,
|
|
);
|
|
await write('favicon.png', randomBytes(64));
|
|
await writeMarked(
|
|
'assets/index-AbC123xYz.js',
|
|
(marker) => `// ${marker}\n${'console.log("regel");\n'.repeat(200)}`,
|
|
);
|
|
await writeMarked(
|
|
'assets/style-Qw9.css',
|
|
(marker) => `/* ${marker} */\n${'.a{color:red}\n'.repeat(200)}`,
|
|
);
|
|
await writeMarked(
|
|
'assets/icon-Zz1.svg',
|
|
(marker) =>
|
|
`<svg xmlns="http://www.w3.org/2000/svg"><!--${marker}${padding}--></svg>`,
|
|
);
|
|
await writeMarked('assets/data.json', (marker) =>
|
|
JSON.stringify({ marker, vulling: padding }),
|
|
);
|
|
await writeMarked(
|
|
'assets/readme.txt',
|
|
(marker) => `${marker}\n${'tekst\n'.repeat(200)}`,
|
|
);
|
|
await write('assets/font-Ab.woff2', randomBytes(2048));
|
|
|
|
return {
|
|
dir,
|
|
markers,
|
|
remove: () => rm(dir, { recursive: true, force: true }),
|
|
};
|
|
}
|