-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilters.test.js
121 lines (97 loc) · 5.37 KB
/
filters.test.js
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const { test } = require('@jest/globals');
const nunjucks = require('nunjucks');
const filters = require('./filters');
describe('validateDnsSubdomain', () => {
test('returns true for valid DNS Subdomain Names', () => {
expect(validateDnsSubdomain('simple')).toBe(true);
});
test('throws when name length is zero', () => {
expect(() => validateDnsSubdomain('')).toThrow('DNS Subdomain Names cannot be zero characters in length');
});
test('throws when name is longer than 253 characters', () => {
let name = '0'.repeat(255);
expect(() => validateDnsSubdomain(name)).toThrow('DNS Subdomain Names cannot be more than 253 characters in length');
});
test('throws when name contains uppercase characters', () => {
expect(() => validateDnsSubdomain('INVALID')).toThrow("DNS Subdomain Names must contain only lowercase alphanumeric characters, '-' or '.'");
});
test('throws when name contains special characters other than hyphens and dots', () => {
expect(() => validateDnsSubdomain('invalid!')).toThrow("DNS Subdomain Names must contain only lowercase alphanumeric characters, '-' or '.'");
});
test('throws when name starts with a hyphen or dot', () => {
expect(() => validateDnsSubdomain('-invalid')).toThrow('DNS Subdomain Names must start with an alphanumeric character');
expect(() => validateDnsSubdomain('.invalid')).toThrow('DNS Subdomain Names must start with an alphanumeric character');
});
test('throws when name ends with a hyphen or dot', () => {
expect(() => validateDnsSubdomain('invalid-')).toThrow('DNS Subdomain Names must end with an alphanumeric character');
expect(() => validateDnsSubdomain('invalid.')).toThrow('DNS Subdomain Names must end with an alphanumeric character');
});
});
describe('dnsSubdomainify', () => {
test('truncates to 253 characters', () => {
let name = '0'.repeat(300);
expect(dnsSubdomainify(name).length).toBe(253);
});
test('lowercases alphabetic characters', () => {
expect(dnsSubdomainify('ABCD')).toBe('abcd');
});
test('replaces slashes with dots', () => {
expect(dnsSubdomainify('test.opsani.com/my-great-app')).toBe('test.opsani.com.my-great-app');
});
test('replaces whitespace with hyphens', () => {
expect(dnsSubdomainify('test.opsani.com/My Amazing App')).toBe('test.opsani.com.my-amazing-app');
});
test('strips non-alphanumerics', () => {
expect(dnsSubdomainify("test.opsani.com/Blake's Super Awesome App!")).toBe('test.opsani.com.blakes-super-awesome-app');
});
test('ensure starts with an alphanumeric', () => {
expect(dnsSubdomainify("..test.opsani.com/Blake's Super Awesome App!")).toBe('0-..test.opsani.com.blakes-super-awesome-app');
});
test('ensure ends with an alphanumeric', () => {
expect(dnsSubdomainify("test.opsani.com/Blake's Super Awesome App!-")).toBe('test.opsani.com.blakes-super-awesome-app--1');
});
test('pathological inputs are handled', () => {
let name = '.'.repeat(253);
let expected = '0-.........................................................................................................................................................................................................................................................-1';
expect(dnsSubdomainify(name)).toBe(expected);
});
});
describe('dnsLabelize', () => {
test('truncates to 63 characters', () => {
let name = '0'.repeat(100);
expect(dnsLabelize(name).length).toBe(63);
});
test('replaces slashes with underscores', () => {
expect(dnsLabelize('test.opsani.com/my-great-app')).toBe('test.opsani.com_my-great-app');
});
test('replaces whitespace with hyphens', () => {
expect(dnsLabelize('test.opsani.com/My Amazing App')).toBe('test.opsani.com_My-Amazing-App');
});
test('strips non-alphanumerics', () => {
expect(dnsLabelize("test.opsani.com/Blake's Super Awesome App!")).toBe('test.opsani.com_Blakes-Super-Awesome-App');
});
test('ensure starts with an alphanumeric', () => {
expect(dnsLabelize("..test.opsani.com/Blake's Super Awesome App!")).toBe('0-..test.opsani.com_Blakes-Super-Awesome-App');
});
test('ensure ends with an alphanumeric', () => {
expect(dnsLabelize("test.opsani.com/Blake's Super Awesome App!-")).toBe('test.opsani.com_Blakes-Super-Awesome-App--1');
});
test('pathological inputs are handled', () => {
let name = '.'.repeat(100);
let expected = '0-...........................................................-1';
expect(dnsLabelize(name)).toBe(expected);
});
});
describe('nunjucks rendering', () => {
test('renderString', () => {
let nunjucksEnv = new nunjucks.Environment();
nunjucksEnv.addFilter('dnsSubdomainify', dnsSubdomainify);
nunjucksEnv.addFilter('escapeRegExp', escapeRegExp);
nunjucksEnv.addFilter('dnsLabelize', dnsLabelize);
const templateContext = {
optimizer: 'testing.opsani.com/My Super Amazing App Extravaganza!',
}
output = nunjucksEnv.renderString("{{ optimizer | dnsSubdomainify }}\n{{ optimizer | dnsLabelize }}", templateContext);
expect(output).toBe('testing.opsani.com.my-super-amazing-app-extravaganza\ntesting.opsani.com_My-Super-Amazing-App-Extravaganza');
});
});