-
-
Notifications
You must be signed in to change notification settings - Fork 175
/
HostArbitrary.spec.ts
30 lines (26 loc) · 1.2 KB
/
HostArbitrary.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
import { domain } from '../../../../src/check/arbitrary/HostArbitrary';
import * as genericHelper from './generic/GenericArbitraryHelper';
const isValidDomain = (t: string) => {
// According to https://www.ietf.org/rfc/rfc1034.txt
// <domain> ::= <subdomain> | " "
// <subdomain> ::= <label> | <subdomain> "." <label>
// <label> ::= <letter> [ [ <ldh-str> ] <let-dig> ]
// <ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str>
// <let-dig-hyp> ::= <let-dig> | "-"
// <let-dig> ::= <letter> | <digit>
// Relaxed by https://www.ietf.org/rfc/rfc1123.txt
// allowing first character of subdomain to be a digit
const rfc1123SubDomain = /^[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?$/;
return t.split('.').every(sd => rfc1123SubDomain.test(sd) && sd.length <= 63) && t.length <= 255;
};
const isValidDomainWithExtension = (t: string) => {
const subdomains = t.split('.');
return isValidDomain(t) && subdomains.length >= 2 && /^[a-z]{2,}$/.test(subdomains[subdomains.length - 1]);
};
describe('DomainArbitrary', () => {
describe('domain', () => {
genericHelper.isValidArbitrary(() => domain(), {
isValidValue: (g: string) => isValidDomainWithExtension(g)
});
});
});