/
parse.test.js
161 lines (122 loc) · 4.56 KB
/
parse.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* Module dependencies.
*/
var qs = require('../')
, should = require('should');
module.exports = {
'test basics': function(){
qs.parse('0=foo').should.eql({ '0': 'foo' });
qs.parse('foo=c++')
.should.eql({ foo: 'c ' });
qs.parse('a[>=]=23')
.should.eql({ a: { '>=': '23' }});
qs.parse('a[<=>]==23')
.should.eql({ a: { '<=>': '=23' }});
qs.parse('a[==]=23')
.should.eql({ a: { '==': '23' }});
qs.parse('foo')
.should.eql({ foo: '' });
qs.parse('foo=bar')
.should.eql({ foo: 'bar' });
qs.parse('foo%3Dbar=baz')
.should.eql({ foo: 'bar=baz' });
qs.parse(' foo = bar = baz ')
.should.eql({ ' foo ': ' bar = baz ' });
qs.parse('foo=bar=baz')
.should.eql({ foo: 'bar=baz' });
qs.parse('foo=bar&bar=baz')
.should.eql({ foo: 'bar', bar: 'baz' });
qs.parse('foo=bar&baz')
.should.eql({ foo: 'bar', baz: '' });
qs.parse('cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World')
.should.eql({
cht: 'p3'
, chd: 't:60,40'
, chs: '250x100'
, chl: 'Hello|World'
});
},
'test nesting': function(){
qs.parse('ops[>=]=25')
.should.eql({ ops: { '>=': '25' }});
qs.parse('user[name]=tj')
.should.eql({ user: { name: 'tj' }});
qs.parse('user[name][first]=tj&user[name][last]=holowaychuk')
.should.eql({ user: { name: { first: 'tj', last: 'holowaychuk' }}});
},
'test escaping': function(){
qs.parse('foo=foo%20bar')
.should.eql({ foo: 'foo bar' });
},
'test arrays': function(){
qs.parse('images[]')
.should.eql({ images: [] });
qs.parse('user[]=tj')
.should.eql({ user: ['tj'] });
qs.parse('user[]=tj&user[]=tobi&user[]=jane')
.should.eql({ user: ['tj', 'tobi', 'jane'] });
qs.parse('user[names][]=tj&user[names][]=tyler')
.should.eql({ user: { names: ['tj', 'tyler'] }});
qs.parse('user[names][]=tj&user[names][]=tyler&user[email]=tj@vision-media.ca')
.should.eql({ user: { names: ['tj', 'tyler'], email: 'tj@vision-media.ca' }});
qs.parse('items=a&items=b')
.should.eql({ items: ['a', 'b'] });
qs.parse('user[names]=tj&user[names]=holowaychuk&user[names]=TJ')
.should.eql({ user: { names: ['tj', 'holowaychuk', 'TJ'] }});
qs.parse('user[name][first]=tj&user[name][first]=TJ')
.should.eql({ user: { name: { first: ['tj', 'TJ'] }}});
},
'test right-hand brackets': function(){
qs.parse('pets=["tobi"]')
.should.eql({ pets: '["tobi"]' });
qs.parse('operators=[">=", "<="]')
.should.eql({ operators: '[">=", "<="]' });
qs.parse('op[>=]=[1,2,3]')
.should.eql({ op: { '>=': '[1,2,3]' }});
qs.parse('op[>=]=[1,2,3]&op[=]=[[[[1]]]]')
.should.eql({ op: { '>=': '[1,2,3]', '=': '[[[[1]]]]' }});
},
'test duplicates': function(){
qs.parse('items=bar&items=baz&items=raz')
.should.eql({ items: ['bar', 'baz', 'raz'] });
},
'test empty': function(){
qs.parse('').should.eql({});
qs.parse(undefined).should.eql({});
qs.parse(null).should.eql({});
},
'test arrays with indexes': function(){
qs.parse('foo[0]=bar&foo[1]=baz').should.eql({ foo: ['bar', 'baz'] });
qs.parse('foo[1]=bar&foo[0]=baz').should.eql({ foo: ['baz', 'bar'] });
qs.parse('foo[base64]=RAWR').should.eql({ foo: { base64: 'RAWR' }});
qs.parse('foo[64base]=RAWR').should.eql({ foo: { '64base': 'RAWR' }});
},
'test arrays becoming objects': function(){
qs.parse('foo[0]=bar&foo[bad]=baz').should.eql({ foo: { 0: "bar", bad: "baz" }});
qs.parse('foo[bad]=baz&foo[0]=bar').should.eql({ foo: { 0: "bar", bad: "baz" }});
},
'test bleed-through of Array native properties/methods': function(){
Array.prototype.protoProperty = true;
Array.prototype.protoFunction = function () {};
qs.parse('foo=bar').should.eql({ foo: 'bar' });
},
'test malformed uri': function(){
qs.parse('{%:%}').should.eql({ '{%:%}': '' });
qs.parse('foo=%:%}').should.eql({ 'foo': '%:%}' });
qs.parse('foo=%:%}%20').should.eql({ 'foo': '%:%} ' });
},
'test unescaping of non-utf8 encoded data': function(){
qs.parse('foo=%E4%20bar').should.eql({ 'foo': String.fromCharCode('228') + ' bar' });
}
// 'test complex': function(){
// qs.parse('users[][name][first]=tj&users[foo]=bar')
// .should.eql({
// users: [ { name: 'tj' }, { name: 'tobi' }, { foo: 'bar' }]
// });
//
// qs.parse('users[][name][first]=tj&users[][name][first]=tobi')
// .should.eql({
// users: [ { name: 'tj' }, { name: 'tobi' }]
// });
// }
};