-
Notifications
You must be signed in to change notification settings - Fork 1
/
host.test.ts
84 lines (73 loc) · 2.58 KB
/
host.test.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import * as fc from 'fast-check';
import * as E from 'fp-ts/Either';
import { DecodeFailed } from '..';
import { Variable } from '../Variable';
import { host } from './host';
describe(host, () => {
it('accepts an arbitrary host string', () => {
const decoder = host();
fc.assert(
fc.property(
fc
.tuple(
fc.webUrl(),
fc.nat().filter((n) => n < 65536),
)
.map(([url, port]) => `${new URL(url).host}:${port}`),
(str) => {
const variable = new Variable('KEY', str);
expect(decoder(variable)).toStrictEqual(E.right(str));
},
),
);
});
it('requires a port number', () => {
const decoder = host();
fc.assert(
fc.property(
fc.webUrl().map((url) => new URL(url).hostname),
(str) => {
const variable = new Variable('KEY', str);
expect(decoder(variable)).toStrictEqual(E.left(new DecodeFailed(variable, 'must be a valid host')));
},
),
);
});
it('rejects an arbitrary URL string', () => {
const decoder = host();
fc.assert(
fc.property(
fc.webUrl().map((str) => {
const url = new URL(str);
url.port = '80';
return url.href;
}),
(str) => {
const variable = new Variable('KEY', str);
expect(decoder(variable)).toStrictEqual(E.left(new DecodeFailed(variable, 'must be a valid host')));
},
),
);
});
it('rejects the trailing pathname', () => {
const decoder = host();
fc.assert(
fc.property(
fc
.tuple(
fc.webUrl().filter((url) => new URL(url).pathname !== '/'),
fc.nat().filter((n) => n < 65536),
)
.map(([orig, port]) => {
const url = new URL(orig);
url.port = String(port);
return url.host + url.pathname;
}),
(str) => {
const variable = new Variable('KEY', str);
expect(decoder(variable)).toStrictEqual(E.left(new DecodeFailed(variable, 'must be a valid host')));
},
),
);
});
});