-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
108 lines (84 loc) · 2.35 KB
/
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
'use strict'
const test = require('mvt')
const tp = require('./index.js')
const q = [
'ary=a',
'ary[]=b',
'boolval=false',
'first=andrew',
'zip=37615',
'last=carpenter',
'zip=37601&q=%3F',
'number=22.55',
'nil=undefined',
'flag',
'amp=%26',
'eq=%3D',
'ary=c',
'encodedAry%5B%5D=1',
'#we=are&totally=done'
].join('&')
const p = tp(`http://localhost:80/base/path/resource?${q}`)
test('First name matches', (assert) => {
assert.is(p.first, 'andrew')
})
test('Last name matches', (assert) => {
assert.is(p.last, 'carpenter')
})
test('Encoded question mark is correctly parsed', (assert) => {
assert.is(p.q, '?')
})
test('Encoded ampersand is correctly parsed', (assert) => {
assert.is(p.amp, '&')
})
test('Encoded equal sign is correctly parsed', (assert) => {
assert.is(p.eq, '=')
})
test('Numeric value parses as such', (assert) => {
assert.is(p.number, 22.55)
})
test('Null and undefined stings parse as null', (assert) => {
assert.is(p.nil, null)
})
test('Boolean value parses as such', (assert) => {
assert.is(p.boolval, false)
})
test('Flag (key with no value) parses to true', (assert) => {
assert.is(p.flag, true)
})
test('Zip code array is correctly parsed', (assert) => {
assert.is(JSON.stringify(p.zip), '[37615,37601]')
})
test('Array values are correctly parsed', (assert) => {
assert.is(JSON.stringify(p.ary), '["a","b","c"]')
})
test('Encoded array values are correctly parsed', (assert) => {
assert.is(JSON.stringify(p.encodedAry), '[1]')
})
test('It ignores things after #', (assert) => {
assert.falsy(p.we || p.totally)
})
test('Parse undefined', (assert) => {
assert.notThrows(() => tp())
})
test('Parse empty string', (assert) => {
assert.notThrows(() => tp(''))
})
test('Parse only host', (assert) => {
assert.notThrows(() => tp('http://localhost:80'))
})
test('Parse trailing slash', (assert) => {
assert.notThrows(() => tp('http://localhost:80/'))
})
test('Parse trailing question mark', (assert) => {
assert.notThrows(() => tp('http://localhost:80?'))
})
test('Parse trailing ampersand', (assert) => {
assert.notThrows(() => tp('http://localhost:80?key=value&'))
})
test('Parse trailing ampersand again', (assert) => {
assert.notThrows(() => tp('http://localhost:80&'))
})
test('Parse trailing question mark and ampersand', (assert) => {
assert.notThrows(() => tp('http://localhost:80?&'))
})