-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
parseQuery.spec.ts
99 lines (83 loc) 路 2.15 KB
/
parseQuery.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
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
import { parseQuery } from '../src/query'
import { mockWarn } from 'jest-mock-warn'
describe('parseQuery', () => {
mockWarn()
it('works with leading ?', () => {
expect(parseQuery('?foo=a')).toEqual({
foo: 'a',
})
})
it('works without leading ?', () => {
expect(parseQuery('foo=a')).toEqual({
foo: 'a',
})
})
it('works with an empty string', () => {
const emptyQuery = parseQuery('')
expect(Object.keys(emptyQuery)).toHaveLength(0)
expect(emptyQuery).toEqual({})
expect(parseQuery('?')).toEqual({})
})
it('decodes values in query', () => {
expect(parseQuery('e=%25')).toEqual({
e: '%',
})
})
it('parses empty string values', () => {
expect(parseQuery('e=&c=a')).toEqual({
e: '',
c: 'a',
})
})
it('allows = inside values', () => {
expect(parseQuery('e=c=a')).toEqual({
e: 'c=a',
})
})
it('parses empty values as null', () => {
expect(parseQuery('e&b&c=a')).toEqual({
e: null,
b: null,
c: 'a',
})
})
it('parses empty values as null in arrays', () => {
expect(parseQuery('e&e&e=a')).toEqual({
e: [null, null, 'a'],
})
})
it('decodes array values in query', () => {
expect(parseQuery('e=%25&e=%22')).toEqual({
e: ['%', '"'],
})
expect(parseQuery('e=%25&e=a')).toEqual({
e: ['%', 'a'],
})
})
it('decodes the + as space', () => {
expect(parseQuery('a+b=c+d')).toEqual({
'a b': 'c d',
})
})
it('decodes the encoded + as +', () => {
expect(parseQuery('a%2Bb=c%2Bd')).toEqual({
'a+b': 'c+d',
})
})
// this is for browsers like IE that allow invalid characters
it('keep invalid values as is', () => {
expect(parseQuery('e=%&e=%25')).toEqual({
e: ['%', '%'],
})
expect('decoding "%"').toHaveBeenWarnedTimes(1)
})
it('keep prototype', () => {
const query = parseQuery('__proto__=1')
expect(query.__proto__).toEqual(Object.prototype)
expect(query.constructor).toEqual(Object)
})
it('keep build-in methods', () => {
const query = parseQuery('toString=1')
expect(query.toString).toEqual(Object.prototype.toString)
})
})