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>
176 lines
6.0 KiB
TypeScript
176 lines
6.0 KiB
TypeScript
import fc from 'fast-check';
|
|
import { describe, expect, it } from 'vitest';
|
|
import {
|
|
hostilePath,
|
|
pageVariant,
|
|
unknownPath,
|
|
} from '../src/test/urlArbitraries';
|
|
import {
|
|
canonicalRedirect,
|
|
escapeRegex,
|
|
pagePaths,
|
|
redirectNames,
|
|
stripTrailingSlashes,
|
|
variantPattern,
|
|
} from './canonicalUrls';
|
|
|
|
// Wat de resolver per stap doet: eerst de paginavariant, anders de slashes
|
|
const hop = (path: string) =>
|
|
canonicalRedirect(path) ?? stripTrailingSlashes(path);
|
|
|
|
describe('pagePaths', () => {
|
|
it('begint met de beginpagina en bevat elke andere pagina eenmaal', () => {
|
|
expect(pagePaths[0]).toBe('/');
|
|
expect(new Set(pagePaths).size).toBe(pagePaths.length);
|
|
expect(redirectNames).toEqual(pagePaths.slice(1));
|
|
});
|
|
});
|
|
|
|
describe('canonicalRedirect', () => {
|
|
it.each([
|
|
['/over-mij/', '/over-mij'],
|
|
['/over-mij///', '/over-mij'],
|
|
['/over-mij.html', '/over-mij'],
|
|
['/soorten-fotoshoots/', '/soorten-fotoshoots'],
|
|
['/soorten-fotoshoots/newborn.html', '/soorten-fotoshoots/newborn'],
|
|
['/index', '/'],
|
|
['/index.html', '/'],
|
|
['/index/', '/'],
|
|
['/Over-Mij', '/over-mij'],
|
|
['/OVER-MIJ.HTML', '/over-mij'],
|
|
['/Over-Mij/', '/over-mij'],
|
|
['/Index.HTML//', '/'],
|
|
])('%s gaat naar %s', (path, target) => {
|
|
expect(canonicalRedirect(path)).toBe(target);
|
|
});
|
|
|
|
it.each([
|
|
'/',
|
|
'/over-mij',
|
|
'/soorten-fotoshoots/newborn',
|
|
'/onbekend',
|
|
'/Onbekend',
|
|
'/onbekend/',
|
|
'/x.html',
|
|
'/404',
|
|
'/404.html',
|
|
'/assets',
|
|
'/assets/',
|
|
'/over-mij/extra',
|
|
'',
|
|
])('laat %s met rust', (path) => {
|
|
expect(canonicalRedirect(path)).toBeUndefined();
|
|
});
|
|
|
|
it('P-HST-02: elke variant van een bekende pagina gaat naar het canonieke pad (invariant)', () => {
|
|
fc.assert(
|
|
fc.property(pageVariant, (variant) => {
|
|
const name = variant
|
|
.toLowerCase()
|
|
.replace(/\/+$/, '')
|
|
.replace(/\.html$/, '');
|
|
const canonical = name === '/index' ? '/' : name;
|
|
const target = canonicalRedirect(variant);
|
|
// De canonieke vorm zelf blijft staan
|
|
expect(target).toBe(
|
|
variant === canonical ? undefined : canonical,
|
|
);
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('P-HST-03: het doel is een dekpunt en een variant heeft hooguit twee stappen nodig (idempotent)', () => {
|
|
fc.assert(
|
|
fc.property(pageVariant, (variant) => {
|
|
const target = canonicalRedirect(variant) ?? variant;
|
|
expect(canonicalRedirect(target)).toBeUndefined();
|
|
expect(stripTrailingSlashes(target)).toBeUndefined();
|
|
}),
|
|
);
|
|
fc.assert(
|
|
fc.property(
|
|
unknownPath,
|
|
fc.integer({ min: 0, max: 3 }),
|
|
(path, slashes) => {
|
|
const first = hop(path + '/'.repeat(slashes));
|
|
// Na de eerste stap is er niets meer te doen
|
|
expect(first === undefined ? undefined : hop(first)).toBe(
|
|
undefined,
|
|
);
|
|
},
|
|
),
|
|
);
|
|
});
|
|
|
|
it('P-HST-04: een doel begint met een slash, nooit met // of /\\, en is een bekende pagina (invariant)', () => {
|
|
const check = (path: string) => {
|
|
const target = canonicalRedirect(path);
|
|
if (target === undefined) return;
|
|
expect(pagePaths).toContain(target);
|
|
expect(target.startsWith('/')).toBe(true);
|
|
expect(target.startsWith('//')).toBe(false);
|
|
expect(target.startsWith('/\\')).toBe(false);
|
|
// eslint-disable-next-line no-control-regex
|
|
expect(target).not.toMatch(/[\u0000-\u001f\u007f]/);
|
|
};
|
|
fc.assert(fc.property(pageVariant, check));
|
|
fc.assert(fc.property(hostilePath, check));
|
|
});
|
|
});
|
|
|
|
describe('stripTrailingSlashes', () => {
|
|
it.each([
|
|
['/onbekend/', '/onbekend'],
|
|
['/onbekend///', '/onbekend'],
|
|
['/assets/', '/assets'],
|
|
['/a/b/', '/a/b'],
|
|
['//', '/'],
|
|
])('%s wordt %s', (path, stripped) => {
|
|
expect(stripTrailingSlashes(path)).toBe(stripped);
|
|
});
|
|
|
|
it.each(['/', '', '/onbekend', '/a/b'])('laat %s met rust', (path) => {
|
|
expect(stripTrailingSlashes(path)).toBeUndefined();
|
|
});
|
|
|
|
it.each([
|
|
'//evil.example/',
|
|
'/\\evil.example/',
|
|
'/over-mij\r\nSet-Cookie: x=1/',
|
|
'/over-mij\0/',
|
|
])('geeft geen doel voor het gevaarlijke pad %j (BR-HST-06)', (path) => {
|
|
expect(stripTrailingSlashes(path)).toBeUndefined();
|
|
});
|
|
|
|
it('P-HST-04: een doel is veilig, ook voor vijandige paden (invariant)', () => {
|
|
fc.assert(
|
|
fc.property(hostilePath, (path) => {
|
|
const target = stripTrailingSlashes(path);
|
|
if (target === undefined) return;
|
|
expect(target.startsWith('/')).toBe(true);
|
|
expect(target.startsWith('//')).toBe(false);
|
|
expect(target.startsWith('/\\')).toBe(false);
|
|
expect(target.endsWith('/') && target !== '/').toBe(false);
|
|
// eslint-disable-next-line no-control-regex
|
|
expect(target).not.toMatch(/[\u0000-\u001f\u007f]/);
|
|
}),
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('variantPattern en escapeRegex', () => {
|
|
it('zet de naam, een optionele .html en slashes in het patroon', () => {
|
|
expect(variantPattern('/over-mij')).toBe('over-mij(?:\\.html)?/*');
|
|
expect(variantPattern('/')).toBe('index(?:\\.html)?/*');
|
|
expect(variantPattern('/soorten-fotoshoots/newborn')).toBe(
|
|
'soorten-fotoshoots/newborn(?:\\.html)?/*',
|
|
);
|
|
});
|
|
|
|
it('escapet tekens met een betekenis in een reguliere expressie', () => {
|
|
expect(escapeRegex('a.b*c(d)|e[f]\\')).toBe(
|
|
'a\\.b\\*c\\(d\\)\\|e\\[f\\]\\\\',
|
|
);
|
|
});
|
|
});
|