generated from nginx/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathutils.test.ts
76 lines (74 loc) · 2.26 KB
/
utils.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
import assert from 'assert'
import { describe, it } from 'mocha'
import { acmeServerNames, isValidHostname } from '../src/utils'
describe('utils', () => {
describe('isValidHostname', () => {
it('returns true for valid names', () => {
assert(isValidHostname('nginx.com'))
assert(isValidHostname('5guys.nginx.com'))
assert(isValidHostname('5guys.nginx.com.'))
assert(isValidHostname('5-guys.'))
assert(isValidHostname('5'))
assert(isValidHostname('a-z.123'))
assert(isValidHostname('a.'.repeat(100)))
assert(isValidHostname('a'.repeat(61) + '.com'))
})
it('returns false for invalid names', () => {
assert(!isValidHostname('.com'))
assert(!isValidHostname('.foobarbaz'))
assert(!isValidHostname('*.nginx.com'))
assert(!isValidHostname('-5guys.'))
assert(!isValidHostname('5guys-'))
assert(!isValidHostname('domäin.'))
assert(!isValidHostname(' '))
assert(!isValidHostname(''))
assert(!isValidHostname('a'.repeat(65) + '.com'))
assert(!isValidHostname('*'))
assert(
!isValidHostname(
// too long - 256 chars
'1234567890abcdef'.repeat(16)
)
)
})
})
describe('acmeServerNames', () => {
it('returns an array given valid input', () => {
const r = {
variables: {
njs_acme_server_names: null,
},
} as unknown as NginxHTTPRequest
const testCases = {
'nginx.com': 1,
'foo.bar.baz': 1,
'foo.bar.baz foo.baz': 2,
'foo. bar. baz.': 3,
'foo.,bar.': 2,
'foo.\tbar.': 2,
'foo. bar.': 2,
}
for (const [names, expected] of Object.entries(testCases)) {
r.variables.njs_acme_server_names = names
const result = acmeServerNames(r)
assert(result.length === expected)
}
})
it('throws given invalid input', () => {
const r = {
variables: {
njs_acme_server_names: null,
},
} as unknown as NginxHTTPRequest
for (const name of [
'nginx-.com',
'*.bar.baz',
'foo.bar.baz *.baz',
'-foo. bar. baz.',
]) {
r.variables.njs_acme_server_names = name
assert.throws(() => acmeServerNames(r))
}
})
})
})