The preview server must behave like the real server, or Lighthouse and the browser tests prove the wrong thing. The tests also cover the new URLs in a real browser: one redirect, one title, a clean hydration. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
703 lines
27 KiB
TypeScript
703 lines
27 KiB
TypeScript
import fc from 'fast-check';
|
|
import { existsSync, readFileSync, statSync } from 'node:fs';
|
|
import { request, type IncomingMessage, type Server } from 'node:http';
|
|
import { connect, type AddressInfo } from 'node:net';
|
|
import { join } from 'node:path';
|
|
import { gunzipSync } from 'node:zlib';
|
|
import { afterAll, beforeAll, describe, expect, inject, it } from 'vitest';
|
|
import { pagePaths } from '../config/canonicalUrls.ts';
|
|
import { NOT_FOUND_FILE, pageFile } from '../config/pageFiles.ts';
|
|
import { startPreviewServer } from '../config/previewServer.ts';
|
|
import { securityHeaders } from '../config/securityHeaders.ts';
|
|
import {
|
|
pageVariant,
|
|
queryString,
|
|
unknownPath,
|
|
} from '../src/test/urlArbitraries.ts';
|
|
import { createFixtureSite, type FixtureSite } from './support/fixtureSite.ts';
|
|
import {
|
|
findNginx,
|
|
startNginx,
|
|
type MimeVariant,
|
|
type RunningNginx,
|
|
} from './support/nginx.ts';
|
|
import { clientDir } from './support/server.ts';
|
|
|
|
// BR-HST-24: de nginx-regels worden bewezen door een echte nginx, niet door de
|
|
// tekst te lezen. De site is een kleine fixture (geen build nodig); dezelfde
|
|
// regels voor de previewserver staan in previewServer.test.ts.
|
|
|
|
// Seed van deze run, gekozen en gelogd in config/testSeed.ts (PBT-08)
|
|
fc.configureGlobal({ seed: inject('fcSeed') });
|
|
|
|
const nginxBin = findNginx();
|
|
const inCi = Boolean(process.env.CI);
|
|
|
|
// Zonder nginx: in CI een duidelijke fout, lokaal een zichtbare melding
|
|
if (!nginxBin) {
|
|
if (inCi) {
|
|
describe('serverregels (nginx)', () => {
|
|
it('vindt nginx', () => {
|
|
throw new Error('nginx ontbreekt in CI (BR-HST-24)');
|
|
});
|
|
});
|
|
} else {
|
|
describe('serverregels (nginx)', () => {
|
|
it('nginx niet gevonden: serverregeltests overgeslagen (zie BR-HST-24)', (context) => {
|
|
console.log(context.task.name);
|
|
context.skip();
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
interface Reply {
|
|
status: number;
|
|
headers: Record<string, string>;
|
|
rawHeaders: string[];
|
|
body: Buffer;
|
|
}
|
|
|
|
// node:http en niet fetch: fetch voegt dubbele headers samen en pakt gzip uit.
|
|
// Met raw gaat het pad ongewijzigd over de lijn; anders maakt de URL-parser er
|
|
// zijn eigen vorm van (een backslash wordt een slash).
|
|
function get(
|
|
origin: string,
|
|
path: string,
|
|
options: {
|
|
method?: string;
|
|
headers?: Record<string, string>;
|
|
raw?: boolean;
|
|
} = {},
|
|
): Promise<Reply> {
|
|
return new Promise((resolve, reject) => {
|
|
const init = {
|
|
method: options.method ?? 'GET',
|
|
headers: options.headers,
|
|
};
|
|
const onReply = (res: IncomingMessage) => {
|
|
const chunks: Buffer[] = [];
|
|
res.on('data', (chunk: Buffer) => chunks.push(chunk));
|
|
res.on('end', () =>
|
|
resolve({
|
|
status: res.statusCode ?? 0,
|
|
headers: Object.fromEntries(
|
|
Object.entries(res.headers).map(([k, v]) => [
|
|
k,
|
|
String(v),
|
|
]),
|
|
),
|
|
rawHeaders: res.rawHeaders,
|
|
body: Buffer.concat(chunks),
|
|
}),
|
|
);
|
|
};
|
|
const { hostname, port } = new URL(origin);
|
|
const req = options.raw
|
|
? request({ hostname, port, path, ...init }, onReply)
|
|
: request(origin + path, init, onReply);
|
|
req.on('error', reject);
|
|
req.end();
|
|
});
|
|
}
|
|
|
|
const countHeader = (reply: Reply, name: string) =>
|
|
reply.rawHeaders.filter(
|
|
(value, i) => i % 2 === 0 && value.toLowerCase() === name.toLowerCase(),
|
|
).length;
|
|
|
|
// Alle productieheaders, X-Robots-Tag en precies één Cache-Control
|
|
function expectFullHeaders(reply: Reply, cacheControl = 'no-cache') {
|
|
for (const [name, value] of Object.entries(securityHeaders('production'))) {
|
|
expect(countHeader(reply, name), name).toBe(1);
|
|
expect(reply.headers[name.toLowerCase()], name).toBe(value);
|
|
}
|
|
expect(countHeader(reply, 'X-Robots-Tag')).toBe(1);
|
|
expect(reply.headers['x-robots-tag']).toBe('noindex, nofollow');
|
|
expect(countHeader(reply, 'Cache-Control')).toBe(1);
|
|
expect(reply.headers['cache-control']).toBe(cacheControl);
|
|
}
|
|
|
|
// Een ruw HTTP/1.0-verzoek, zoals de proxy-Pi dat naar de hosting-Pi stuurt
|
|
function getHttp10(origin: string, path: string, headers: string[]) {
|
|
const { port } = new URL(origin);
|
|
return new Promise<{ head: string; body: Buffer }>((resolve, reject) => {
|
|
const socket = connect(Number(port), '127.0.0.1');
|
|
const chunks: Buffer[] = [];
|
|
socket.on('connect', () =>
|
|
socket.write(
|
|
[
|
|
`GET ${path} HTTP/1.0`,
|
|
'Host: test.andreaslop.nl',
|
|
...headers,
|
|
'',
|
|
'',
|
|
].join('\r\n'),
|
|
),
|
|
);
|
|
socket.on('data', (chunk: Buffer) => chunks.push(chunk));
|
|
socket.on('error', reject);
|
|
socket.on('close', () => {
|
|
const all = Buffer.concat(chunks);
|
|
const split = all.indexOf('\r\n\r\n');
|
|
resolve({
|
|
head: all.subarray(0, split).toString('latin1'),
|
|
body: all.subarray(split + 4),
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
// De versie van nginx één keer in de uitvoer, hoeveel instanties er ook starten
|
|
let versionAnnounced = false;
|
|
function announce(nginx: RunningNginx) {
|
|
if (versionAnnounced) return;
|
|
versionAnnounced = true;
|
|
console.log(`[serverRules] nginx -v: ${nginx.version}`);
|
|
}
|
|
|
|
const mimeVariants: MimeVariant[] = ['new', 'old'];
|
|
|
|
const rulesSuite = nginxBin ? describe : describe.skip;
|
|
rulesSuite.each(mimeVariants)(
|
|
'serverregels op een echte nginx (.js als %s mime-type)',
|
|
(variant) => {
|
|
let site: FixtureSite;
|
|
let nginx: RunningNginx;
|
|
const markerOf = (file: string) => site.markers.get(file) ?? file;
|
|
|
|
beforeAll(async () => {
|
|
site = await createFixtureSite();
|
|
nginx = await startNginx({
|
|
siteDir: site.dir,
|
|
mimeVariant: variant,
|
|
});
|
|
announce(nginx);
|
|
});
|
|
afterAll(async () => {
|
|
await nginx?.stop();
|
|
await site?.remove();
|
|
});
|
|
|
|
const page = (path: string, init?: Parameters<typeof get>[2]) =>
|
|
get(nginx.origin, path, init);
|
|
|
|
describe("pagina's en bestanden (BR-HST-01, V3)", () => {
|
|
it.each([
|
|
['/', 'index.html'],
|
|
['/over-mij', 'over-mij.html'],
|
|
['/soorten-fotoshoots', 'soorten-fotoshoots.html'],
|
|
[
|
|
'/soorten-fotoshoots/newborn',
|
|
'soorten-fotoshoots/newborn.html',
|
|
],
|
|
['/over-mij?x=1', 'over-mij.html'],
|
|
['/?x=1', 'index.html'],
|
|
])('%s geeft 200 met %s', async (path, file) => {
|
|
const reply = await page(path);
|
|
expect(reply.status).toBe(200);
|
|
expect(reply.body.toString()).toContain(markerOf(file));
|
|
expectFullHeaders(reply);
|
|
});
|
|
|
|
it.each([
|
|
['/assets/index-AbC123xYz.js', 'assets/index-AbC123xYz.js'],
|
|
['/sitemap.xml', 'sitemap.xml'],
|
|
])('%s geeft 200 met het bestand', async (path, file) => {
|
|
const reply = await page(path);
|
|
expect(reply.status).toBe(200);
|
|
expect(reply.body.toString()).toContain(markerOf(file));
|
|
});
|
|
|
|
it('de map soorten-fotoshoots/ naast het paginabestand stuurt niet door en toont geen lijst (V3)', async () => {
|
|
const file = await page('/soorten-fotoshoots');
|
|
expect(file.status).toBe(200);
|
|
expect(file.body.toString()).toContain(
|
|
markerOf('soorten-fotoshoots.html'),
|
|
);
|
|
const dir = await page('/soorten-fotoshoots/');
|
|
expect(dir.status).toBe(301);
|
|
expect(dir.headers.location).toBe('/soorten-fotoshoots');
|
|
});
|
|
|
|
it('/ werkt zonder index-directive (V2)', async () => {
|
|
const reply = await page('/');
|
|
expect(reply.status).toBe(200);
|
|
expect(reply.body.toString()).toContain(markerOf('index.html'));
|
|
});
|
|
|
|
it('/robots.txt verbiedt alles op de testsite', async () => {
|
|
const reply = await page('/robots.txt');
|
|
expect(reply.status).toBe(200);
|
|
expect(reply.body.toString()).toContain('Disallow: /');
|
|
expect(reply.body.toString()).not.toContain('fixture:');
|
|
expectFullHeaders(reply);
|
|
});
|
|
});
|
|
|
|
describe('doorverwijzingen (BR-HST-03, 04, 06, V2)', () => {
|
|
it.each([
|
|
['/over-mij/', '/over-mij'],
|
|
['/over-mij///', '/over-mij'],
|
|
['/soorten-fotoshoots/', '/soorten-fotoshoots'],
|
|
['/over-mij/?x=1', '/over-mij?x=1'],
|
|
['/soorten-fotoshoots/?x=1', '/soorten-fotoshoots?x=1'],
|
|
['/over-mij.html', '/over-mij'],
|
|
[
|
|
'/soorten-fotoshoots/newborn.html',
|
|
'/soorten-fotoshoots/newborn',
|
|
],
|
|
['/index', '/'],
|
|
['/index.html', '/'],
|
|
['/index/', '/'],
|
|
['/Over-Mij', '/over-mij'],
|
|
['/OVER-MIJ.HTML', '/over-mij'],
|
|
['/Over-Mij/', '/over-mij'],
|
|
['/Over-Mij?x=1&y=%20z', '/over-mij?x=1&y=%20z'],
|
|
['/onbekend/', '/onbekend'],
|
|
['/assets/', '/assets'],
|
|
])('%s gaat met één 301 naar %s', async (path, target) => {
|
|
const reply = await page(path);
|
|
expect(reply.status).toBe(301);
|
|
// Relatief: absolute_redirect staat uit
|
|
expect(reply.headers.location).toBe(target);
|
|
expectFullHeaders(reply);
|
|
});
|
|
});
|
|
|
|
describe('404 (BR-HST-02, 05, V4)', () => {
|
|
it.each([
|
|
'/404',
|
|
'/404.html',
|
|
'/onbekend',
|
|
'/Onbekend',
|
|
'/x.html',
|
|
'/assets/missing.js',
|
|
'/assets',
|
|
])('%s geeft 404 met de 404-pagina', async (path) => {
|
|
const reply = await page(path);
|
|
expect(reply.status).toBe(404);
|
|
expect(reply.body.toString()).toContain(markerOf('404.html'));
|
|
// Ook de 404 van een ontbrekend asset is niet immutable
|
|
expectFullHeaders(reply);
|
|
});
|
|
|
|
it('HEAD op een pagina en op een onbekende URL', async () => {
|
|
const ok = await page('/over-mij', { method: 'HEAD' });
|
|
expect(ok.status).toBe(200);
|
|
expect(ok.body).toHaveLength(0);
|
|
expectFullHeaders(ok);
|
|
const missing = await page('/onbekend', { method: 'HEAD' });
|
|
expect(missing.status).toBe(404);
|
|
expect(missing.body).toHaveLength(0);
|
|
expectFullHeaders(missing);
|
|
});
|
|
});
|
|
|
|
describe('caching (BR-HST-12)', () => {
|
|
it('/assets/* heeft precies één Cache-Control: immutable', async () => {
|
|
for (const path of [
|
|
'/assets/index-AbC123xYz.js',
|
|
'/assets/style-Qw9.css',
|
|
'/assets/font-Ab.woff2',
|
|
]) {
|
|
const reply = await page(path);
|
|
expect(reply.status, path).toBe(200);
|
|
expectFullHeaders(
|
|
reply,
|
|
'public, max-age=31536000, immutable',
|
|
);
|
|
}
|
|
});
|
|
|
|
it('al het andere revalideert', async () => {
|
|
for (const path of ['/', '/sitemap.xml', '/favicon.png']) {
|
|
expectFullHeaders(await page(path));
|
|
}
|
|
});
|
|
});
|
|
|
|
describe('compressie (BR-HST-09, V5)', () => {
|
|
const compressed = [
|
|
'/',
|
|
'/over-mij',
|
|
'/assets/index-AbC123xYz.js',
|
|
'/assets/style-Qw9.css',
|
|
'/assets/icon-Zz1.svg',
|
|
'/assets/data.json',
|
|
'/sitemap.xml',
|
|
'/assets/readme.txt',
|
|
];
|
|
const plain = ['/assets/font-Ab.woff2', '/favicon.png'];
|
|
|
|
it.each(compressed)('%s krijgt gzip', async (path) => {
|
|
const reply = await page(path, {
|
|
headers: { 'Accept-Encoding': 'gzip' },
|
|
});
|
|
expect(reply.status).toBe(200);
|
|
expect(reply.headers['content-encoding']).toBe('gzip');
|
|
expect(reply.headers.vary).toContain('Accept-Encoding');
|
|
expect(gunzipSync(reply.body).length).toBeGreaterThan(256);
|
|
});
|
|
|
|
it.each(plain)('%s krijgt geen gzip', async (path) => {
|
|
const reply = await page(path, {
|
|
headers: { 'Accept-Encoding': 'gzip' },
|
|
});
|
|
expect(reply.status).toBe(200);
|
|
expect(reply.headers['content-encoding']).toBeUndefined();
|
|
});
|
|
|
|
it.each(compressed)(
|
|
'%s krijgt gzip over HTTP/1.0 (zoals de proxy het vraagt)',
|
|
async (path) => {
|
|
const reply = await getHttp10(nginx.origin, path, [
|
|
'Accept-Encoding: gzip',
|
|
]);
|
|
expect(reply.head).toMatch(/^HTTP\/1\.\d 200/);
|
|
expect(reply.head).toMatch(/content-encoding: gzip/i);
|
|
expect(gunzipSync(reply.body).length).toBeGreaterThan(256);
|
|
},
|
|
);
|
|
|
|
it.each(plain)(
|
|
'%s krijgt over HTTP/1.0 geen gzip',
|
|
async (path) => {
|
|
const reply = await getHttp10(nginx.origin, path, [
|
|
'Accept-Encoding: gzip',
|
|
]);
|
|
expect(reply.head).toMatch(/^HTTP\/1\.\d 200/);
|
|
expect(reply.head).not.toMatch(/content-encoding/i);
|
|
},
|
|
);
|
|
|
|
it('zonder Accept-Encoding blijft het antwoord onbewerkt', async () => {
|
|
const reply = await page('/assets/index-AbC123xYz.js');
|
|
expect(reply.headers['content-encoding']).toBeUndefined();
|
|
expect(reply.body.toString()).toContain(
|
|
markerOf('assets/index-AbC123xYz.js'),
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('vijandige paden (BR-HST-06)', () => {
|
|
it.each([
|
|
'//evil.example/',
|
|
'//evil.example',
|
|
'/\\evil.example/',
|
|
'/\\evil.example',
|
|
'/%5Cevil.example/',
|
|
'/%2F%2Fevil.example/',
|
|
'/%0d%0aSet-Cookie:%20x=1/',
|
|
'/over-mij%0d%0aSet-Cookie:%20x=1/',
|
|
'/%2e%2e/',
|
|
])('%s leidt niet naar een ander domein', async (path) => {
|
|
const reply = await page(path);
|
|
const location = reply.headers.location;
|
|
if (reply.status >= 300 && reply.status < 400) {
|
|
expect(location).toBeDefined();
|
|
expect(location?.startsWith('//')).toBe(false);
|
|
expect(location?.startsWith('/\\')).toBe(false);
|
|
expect(location).not.toMatch(/[\r\n]/);
|
|
}
|
|
expect(reply.headers['set-cookie']).toBeUndefined();
|
|
});
|
|
});
|
|
},
|
|
);
|
|
|
|
// P-HST-09 (oracle): dezelfde fixturesite achter de echte nginx en achter de
|
|
// previewserver. Voor elk pad moeten ze gelijk antwoorden op status, Location
|
|
// (zonder schema en host), het bestand dat geserveerd is en gzip. Bewust
|
|
// anders, en dus niet vergeleken:
|
|
// - 405-tekst en de Server-header: nginx heeft eigen teksten;
|
|
// - het MIME-type van .js: hangt van de nginx-versie af (beide tabellen staan
|
|
// hierboven in de suite per mime-variant);
|
|
// - /robots.txt: nginx vervangt hem door Disallow, de previewserver serveert
|
|
// het bestand (de fixture heeft er een);
|
|
// - X-Robots-Tag: alleen nginx (testsite), de previewserver stuurt hem nooit;
|
|
// - Cache-Control op een 301: nginx zet no-cache via add_header op
|
|
// server-niveau, de previewserver stuurt er geen (BR-HST-12); op bestanden
|
|
// buiten HTML zet de previewserver er geen;
|
|
// - paden met een leeg segment (//), zie het blok onderaan de suite;
|
|
// - /%2e%2e/ en een NUL in het pad: nginx weigert die met 400, de
|
|
// previewserver geeft 404.
|
|
const parityRules = nginxBin ? describe : describe.skip;
|
|
parityRules('pariteit: previewserver tegen echte nginx (P-HST-09)', () => {
|
|
let site: FixtureSite;
|
|
let nginx: RunningNginx;
|
|
let preview: Server;
|
|
let previewOrigin: string;
|
|
|
|
beforeAll(async () => {
|
|
site = await createFixtureSite();
|
|
nginx = await startNginx({ siteDir: site.dir });
|
|
announce(nginx);
|
|
preview = await startPreviewServer(site.dir, 0);
|
|
previewOrigin = `http://127.0.0.1:${(preview.address() as AddressInfo).port}`;
|
|
});
|
|
afterAll(async () => {
|
|
preview?.closeAllConnections();
|
|
await new Promise<void>((resolve) =>
|
|
preview ? preview.close(() => resolve()) : resolve(),
|
|
);
|
|
await nginx?.stop();
|
|
await site?.remove();
|
|
});
|
|
|
|
const acceptGzip = { 'Accept-Encoding': 'gzip' };
|
|
const search = (query: string) => (query ? `?${query}` : '');
|
|
const decoded = (reply: Reply) =>
|
|
reply.headers['content-encoding'] === 'gzip'
|
|
? gunzipSync(reply.body)
|
|
: reply.body;
|
|
const servedFile = (reply: Reply) =>
|
|
/fixture:[\w./-]+/.exec(decoded(reply).toString('latin1'))?.[0];
|
|
const locationOf = (reply: Reply) =>
|
|
reply.headers.location?.replace(/^https?:\/\/[^/]+/, '');
|
|
|
|
// Wat aan de headers van één antwoord niet klopt
|
|
function headerFindings(
|
|
server: 'nginx' | 'preview',
|
|
reply: Reply,
|
|
checkCache: boolean,
|
|
) {
|
|
const found: string[] = [];
|
|
for (const [name, value] of Object.entries(
|
|
securityHeaders('production'),
|
|
)) {
|
|
const count = countHeader(reply, name);
|
|
const actual = reply.headers[name.toLowerCase()];
|
|
if (count !== 1 || actual !== value) {
|
|
found.push(`${server}: ${name} ${count}x, waarde ${actual}`);
|
|
}
|
|
}
|
|
const robots = countHeader(reply, 'X-Robots-Tag');
|
|
if (robots !== (server === 'nginx' ? 1 : 0)) {
|
|
found.push(`${server}: X-Robots-Tag ${robots}x`);
|
|
}
|
|
if (checkCache) {
|
|
const count = countHeader(reply, 'Cache-Control');
|
|
const expected =
|
|
server === 'preview' && reply.status === 301 ? 0 : 1;
|
|
if (count !== expected) {
|
|
found.push(
|
|
`${server}: Cache-Control ${count}x, verwacht ${expected}x`,
|
|
);
|
|
}
|
|
}
|
|
return found;
|
|
}
|
|
|
|
// Alle verschillen voor één pad, leesbaar met het pad erbij
|
|
async function differences(path: string, checkCache = true) {
|
|
const [fromNginx, fromPreview] = await Promise.all([
|
|
get(nginx.origin, path, { headers: acceptGzip, raw: true }),
|
|
get(previewOrigin, path, { headers: acceptGzip, raw: true }),
|
|
]);
|
|
const found: string[] = [];
|
|
const same = (what: string, a: unknown, b: unknown) => {
|
|
if (a !== b) found.push(`${what}: nginx ${a}, preview ${b}`);
|
|
};
|
|
same('status', fromNginx.status, fromPreview.status);
|
|
same('Location', locationOf(fromNginx), locationOf(fromPreview));
|
|
same('bestand', servedFile(fromNginx), servedFile(fromPreview));
|
|
same(
|
|
'Content-Encoding',
|
|
fromNginx.headers['content-encoding'],
|
|
fromPreview.headers['content-encoding'],
|
|
);
|
|
if (
|
|
fromNginx.status !== 301 &&
|
|
!decoded(fromNginx).equals(decoded(fromPreview))
|
|
) {
|
|
found.push('inhoud: de bytes verschillen');
|
|
}
|
|
if (checkCache && fromNginx.status !== 301) {
|
|
same(
|
|
'Cache-Control',
|
|
fromNginx.headers['cache-control'],
|
|
fromPreview.headers['cache-control'],
|
|
);
|
|
}
|
|
found.push(
|
|
...headerFindings('nginx', fromNginx, checkCache),
|
|
...headerFindings('preview', fromPreview, checkCache),
|
|
);
|
|
return found.map((finding) => `${path} ${finding}`);
|
|
}
|
|
|
|
// De tabel van het URL-contract, rij voor rij
|
|
it.each([
|
|
'/',
|
|
'/over-mij',
|
|
'/soorten-fotoshoots',
|
|
'/soorten-fotoshoots/newborn',
|
|
'/over-mij?x=1',
|
|
'/?x=1',
|
|
'/over-mij/',
|
|
'/over-mij///',
|
|
'/soorten-fotoshoots/',
|
|
'/soorten-fotoshoots/?x=1',
|
|
'/over-mij.html',
|
|
'/soorten-fotoshoots/newborn.html',
|
|
'/index',
|
|
'/index.html',
|
|
'/index/',
|
|
'/Over-Mij',
|
|
'/OVER-MIJ.HTML',
|
|
'/Over-Mij/',
|
|
'/Over-Mij?x=1&y=%20z',
|
|
'/404',
|
|
'/404.html',
|
|
'/onbekend',
|
|
'/Onbekend',
|
|
'/x.html',
|
|
'/onbekend/',
|
|
'/onbekend/?a=b',
|
|
'/assets',
|
|
'/assets/',
|
|
'/assets/missing.js',
|
|
'/%5Cevil.example',
|
|
'/%5Cevil.example/',
|
|
])('%s: nginx en previewserver antwoorden gelijk', async (path) => {
|
|
expect(await differences(path)).toEqual([]);
|
|
});
|
|
|
|
// Bestanden: zelfde status, bytes en gzip-keuze (Cache-Control: zie boven)
|
|
it.each([
|
|
'/sitemap.xml',
|
|
'/favicon.png',
|
|
'/assets/index-AbC123xYz.js',
|
|
'/assets/style-Qw9.css',
|
|
'/assets/icon-Zz1.svg',
|
|
'/assets/data.json',
|
|
'/assets/readme.txt',
|
|
'/assets/font-Ab.woff2',
|
|
])('%s: gelijke status, bytes en gzip', async (path) => {
|
|
expect(await differences(path, false)).toEqual([]);
|
|
});
|
|
|
|
// Bekende pagina's in elke vorm en onbekende paden met en zonder slash,
|
|
// met en zonder query
|
|
const parityPath = fc.oneof(
|
|
fc
|
|
.tuple(pageVariant, queryString)
|
|
.map(([path, query]) => path + search(query)),
|
|
fc
|
|
.tuple(unknownPath, fc.boolean(), queryString)
|
|
.map(
|
|
([path, slash, query]) =>
|
|
path + (slash ? '/' : '') + search(query),
|
|
),
|
|
);
|
|
it('nginx en previewserver antwoorden gelijk op gegenereerde paden (oracle)', async () => {
|
|
await fc.assert(
|
|
fc.asyncProperty(parityPath, async (path) => {
|
|
expect(await differences(path)).toEqual([]);
|
|
}),
|
|
{ numRuns: 100 },
|
|
);
|
|
});
|
|
|
|
// Buiten de pariteit: een leeg segment. nginx voegt de slashes samen
|
|
// (merge_slashes) en bedient of verwijst het samengevoegde pad; de
|
|
// previewserver kent een leeg segment niet (BR-SRV-07) en geeft 404, op
|
|
// slashes alleen na (// en /// gaan naar /). Een verschil in uitkomst kan
|
|
// geen verkeerde eerste render geven, want browser en router maken zulke
|
|
// paden niet, en geen doorverwijzing begint met // (BR-HST-06).
|
|
describe('lege segmenten, buiten de pariteit', () => {
|
|
it.each([
|
|
['//', 'index.html'],
|
|
['///', 'index.html'],
|
|
['//over-mij', 'over-mij.html'],
|
|
])(
|
|
'nginx voegt de slashes van %s samen en bedient %s',
|
|
async (path, file) => {
|
|
const reply = await get(nginx.origin, path, { raw: true });
|
|
expect(reply.status).toBe(200);
|
|
expect(reply.body.toString()).toContain(
|
|
site.markers.get(file) ?? file,
|
|
);
|
|
},
|
|
);
|
|
|
|
it.each([
|
|
['/a//b/', '/a/b'],
|
|
['//evil.example/', '/evil.example'],
|
|
])('nginx verwijst %s naar %s', async (path, target) => {
|
|
const reply = await get(nginx.origin, path, { raw: true });
|
|
expect(reply.status).toBe(301);
|
|
expect(reply.headers.location).toBe(target);
|
|
});
|
|
|
|
it.each(['//', '///', '//over-mij', '/a//b/', '//evil.example/'])(
|
|
'de previewserver geeft voor %s 404 of een doorverwijzing naar /',
|
|
async (path) => {
|
|
const reply = await get(previewOrigin, path, { raw: true });
|
|
if (reply.status === 301) {
|
|
expect(reply.headers.location).toBe('/');
|
|
} else {
|
|
expect(reply.status).toBe(404);
|
|
}
|
|
},
|
|
);
|
|
});
|
|
});
|
|
|
|
// De echte build achter de echte nginx: elke pagina, de 404 en de map naast
|
|
// het paginabestand. Heeft een actuele dist nodig (pnpm build).
|
|
const buildSuite = nginxBin ? describe : describe.skip;
|
|
buildSuite('nginx op de echte build (dist/client)', () => {
|
|
let nginx: RunningNginx;
|
|
const bytesOf = (file: string) => readFileSync(join(clientDir, file));
|
|
|
|
beforeAll(async () => {
|
|
nginx = await startNginx({ siteDir: clientDir });
|
|
announce(nginx);
|
|
});
|
|
afterAll(async () => {
|
|
await nginx?.stop();
|
|
});
|
|
|
|
it.each([...pagePaths])(
|
|
'%s geeft 200 met de bytes van het paginabestand',
|
|
async (path) => {
|
|
const reply = await get(nginx.origin, path, { raw: true });
|
|
expect(reply.status).toBe(200);
|
|
expect(reply.body.equals(bytesOf(pageFile(path)))).toBe(true);
|
|
},
|
|
);
|
|
|
|
it('een onbekende URL geeft 404 met de bytes van 404.html', async () => {
|
|
const reply = await get(nginx.origin, '/bestaat-niet', { raw: true });
|
|
expect(reply.status).toBe(404);
|
|
expect(reply.body.equals(bytesOf(NOT_FOUND_FILE))).toBe(true);
|
|
});
|
|
|
|
it('de map soorten-fotoshoots/ staat naast soorten-fotoshoots.html zonder dat /soorten-fotoshoots omleidt', async () => {
|
|
expect(
|
|
statSync(join(clientDir, 'soorten-fotoshoots')).isDirectory(),
|
|
).toBe(true);
|
|
expect(existsSync(join(clientDir, 'soorten-fotoshoots.html'))).toBe(
|
|
true,
|
|
);
|
|
const page = await get(nginx.origin, '/soorten-fotoshoots', {
|
|
raw: true,
|
|
});
|
|
expect(page.status).toBe(200);
|
|
expect(page.body.equals(bytesOf('soorten-fotoshoots.html'))).toBe(true);
|
|
const dir = await get(nginx.origin, '/soorten-fotoshoots/', {
|
|
raw: true,
|
|
});
|
|
expect(dir.status).toBe(301);
|
|
expect(dir.headers.location).toBe('/soorten-fotoshoots');
|
|
});
|
|
|
|
it('toont geen maplijst voor /assets', async () => {
|
|
const reply = await get(nginx.origin, '/assets', { raw: true });
|
|
expect(reply.status).toBe(404);
|
|
expect(reply.body.equals(bytesOf(NOT_FOUND_FILE))).toBe(true);
|
|
});
|
|
});
|