/
WebArbitrary.spec.ts
64 lines (62 loc) · 2.21 KB
/
WebArbitrary.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import * as fc from '../../../src/fast-check';
import { URL } from 'url';
const seed = Date.now();
describe(`WebArbitrary (seed: ${seed})`, () => {
it('Should produce valid domains', () => {
fc.assert(
fc.property(fc.domain(), domain => {
const p = `http://user:pass@${domain}/path/?query#fragment`;
const u = new URL(p);
expect(u.hostname).toEqual(domain);
}),
{ seed: seed }
);
});
it('Should produce valid authorities', () => {
fc.assert(
fc.property(
fc.webAuthority({
withIPv4: false,
withIPv6: false,
withUserInfo: true,
withPort: true
}),
authority => {
const domain = /(^|@)([-a-z0-9\.]+)(:\d+$|$)/.exec(authority)![2];
const p = `http://${authority}`;
const u = new URL(p);
expect(u.hostname).toEqual(domain);
}
),
{ seed: seed }
);
});
it('Should produce valid URL parts', () => {
fc.assert(
fc.property(
fc.webAuthority({ withIPv4: true, withIPv6: true, withUserInfo: true, withPort: true }),
fc.array(fc.webSegment()).map(p => p.map(v => `/${v}`).join('')),
fc.webQueryParameters(),
fc.webFragments(),
(authority, path, query, fragment) => {
const p = `http://${authority}${path}?${query}#${fragment}`;
const u = new URL(p);
expect({ search: decodeURIComponent(u.search), hash: u.hash }).toEqual({
search: query === '' ? '' : decodeURIComponent(`?${query}`),
hash: fragment === '' ? '' : `#${fragment}`
});
const dotSanitizedPath = path
.replace(/\/(%2e|%2E)($|\/)/g, '/.$2')
.replace(/\/(%2e|%2E)(%2e|%2E)($|\/)/g, '/..$3');
if (!dotSanitizedPath.includes('/..')) {
const sanitizedPath = dotSanitizedPath
.replace(/\/\.\/(\.\/)*/g, '/') // replace /./, /././, etc.. by /
.replace(/\/\.$/, '/'); // replace trailing /. by / if any
expect(u.pathname).toEqual(sanitizedPath === '' ? '/' : sanitizedPath);
}
}
),
{ seed: seed }
);
});
});