Initial commit
[yaffs-website] / node_modules / body-parser / node_modules / qs / test / stringify.js
1 /* eslint no-extend-native:0 */
2 // Load modules
3
4 var Code = require('code');
5 var Lab = require('lab');
6 var Qs = require('../');
7
8
9 // Declare internals
10
11 var internals = {};
12
13
14 // Test shortcuts
15
16 var lab = exports.lab = Lab.script();
17 var expect = Code.expect;
18 var describe = lab.experiment;
19 var it = lab.test;
20
21
22 describe('stringify()', function () {
23
24     it('stringifies a querystring object', function (done) {
25
26         expect(Qs.stringify({ a: 'b' })).to.equal('a=b');
27         expect(Qs.stringify({ a: 1 })).to.equal('a=1');
28         expect(Qs.stringify({ a: 1, b: 2 })).to.equal('a=1&b=2');
29         expect(Qs.stringify({ a: 'A_Z' })).to.equal('a=A_Z');
30         expect(Qs.stringify({ a: '€' })).to.equal('a=%E2%82%AC');
31         expect(Qs.stringify({ a: '' })).to.equal('a=%EE%80%80');
32         expect(Qs.stringify({ a: 'א' })).to.equal('a=%D7%90');
33         expect(Qs.stringify({ a: '𐐷' })).to.equal('a=%F0%90%90%B7');
34         done();
35     });
36
37     it('stringifies a nested object', function (done) {
38
39         expect(Qs.stringify({ a: { b: 'c' } })).to.equal('a%5Bb%5D=c');
40         expect(Qs.stringify({ a: { b: { c: { d: 'e' } } } })).to.equal('a%5Bb%5D%5Bc%5D%5Bd%5D=e');
41         done();
42     });
43
44     it('stringifies an array value', function (done) {
45
46         expect(Qs.stringify({ a: ['b', 'c', 'd'] })).to.equal('a%5B0%5D=b&a%5B1%5D=c&a%5B2%5D=d');
47         done();
48     });
49
50     it('omits nulls when asked', function (done) {
51
52         expect(Qs.stringify({ a: 'b', c: null }, { skipNulls: true })).to.equal('a=b');
53         done();
54     });
55
56
57     it('omits nested nulls when asked', function (done) {
58
59         expect(Qs.stringify({ a: { b: 'c', d: null } }, { skipNulls: true })).to.equal('a%5Bb%5D=c');
60         done();
61     });
62
63     it('omits array indices when asked', function (done) {
64
65         expect(Qs.stringify({ a: ['b', 'c', 'd'] }, { indices: false })).to.equal('a=b&a=c&a=d');
66         done();
67     });
68
69     it('stringifies a nested array value', function (done) {
70
71         expect(Qs.stringify({ a: { b: ['c', 'd'] } })).to.equal('a%5Bb%5D%5B0%5D=c&a%5Bb%5D%5B1%5D=d');
72         done();
73     });
74
75     it('stringifies an object inside an array', function (done) {
76
77         expect(Qs.stringify({ a: [{ b: 'c' }] })).to.equal('a%5B0%5D%5Bb%5D=c');
78         expect(Qs.stringify({ a: [{ b: { c: [1] } }] })).to.equal('a%5B0%5D%5Bb%5D%5Bc%5D%5B0%5D=1');
79         done();
80     });
81
82     it('does not omit object keys when indices = false', function (done) {
83
84         expect(Qs.stringify({ a: [{ b: 'c' }] }, { indices: false })).to.equal('a%5Bb%5D=c');
85         done();
86     });
87
88     it('uses indices notation for arrays when indices=true', function (done) {
89
90         expect(Qs.stringify({ a: ['b', 'c'] }, { indices: true })).to.equal('a%5B0%5D=b&a%5B1%5D=c');
91         done();
92     });
93
94     it('uses indices notation for arrays when no arrayFormat is specified', function (done) {
95
96         expect(Qs.stringify({ a: ['b', 'c'] })).to.equal('a%5B0%5D=b&a%5B1%5D=c');
97         done();
98     });
99
100     it('uses indices notation for arrays when no arrayFormat=indices', function (done) {
101
102         expect(Qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'indices' })).to.equal('a%5B0%5D=b&a%5B1%5D=c');
103         done();
104     });
105
106     it('uses repeat notation for arrays when no arrayFormat=repeat', function (done) {
107
108         expect(Qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'repeat' })).to.equal('a=b&a=c');
109         done();
110     });
111
112     it('uses brackets notation for arrays when no arrayFormat=brackets', function (done) {
113
114         expect(Qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'brackets' })).to.equal('a%5B%5D=b&a%5B%5D=c');
115         done();
116     });
117
118     it('stringifies a complicated object', function (done) {
119
120         expect(Qs.stringify({ a: { b: 'c', d: 'e' } })).to.equal('a%5Bb%5D=c&a%5Bd%5D=e');
121         done();
122     });
123
124     it('stringifies an empty value', function (done) {
125
126         expect(Qs.stringify({ a: '' })).to.equal('a=');
127         expect(Qs.stringify({ a: null }, { strictNullHandling: true })).to.equal('a');
128
129         expect(Qs.stringify({ a: '', b: '' })).to.equal('a=&b=');
130         expect(Qs.stringify({ a: null, b: '' }, { strictNullHandling: true })).to.equal('a&b=');
131
132         expect(Qs.stringify({ a: { b: '' } })).to.equal('a%5Bb%5D=');
133         expect(Qs.stringify({ a: { b: null } }, { strictNullHandling: true })).to.equal('a%5Bb%5D');
134         expect(Qs.stringify({ a: { b: null } }, { strictNullHandling: false })).to.equal('a%5Bb%5D=');
135
136         done();
137     });
138
139     it('stringifies an empty object', function (done) {
140
141         var obj = Object.create(null);
142         obj.a = 'b';
143         expect(Qs.stringify(obj)).to.equal('a=b');
144         done();
145     });
146
147     it('returns an empty string for invalid input', function (done) {
148
149         expect(Qs.stringify(undefined)).to.equal('');
150         expect(Qs.stringify(false)).to.equal('');
151         expect(Qs.stringify(null)).to.equal('');
152         expect(Qs.stringify('')).to.equal('');
153         done();
154     });
155
156     it('stringifies an object with an empty object as a child', function (done) {
157
158         var obj = {
159             a: Object.create(null)
160         };
161
162         obj.a.b = 'c';
163         expect(Qs.stringify(obj)).to.equal('a%5Bb%5D=c');
164         done();
165     });
166
167     it('drops keys with a value of undefined', function (done) {
168
169         expect(Qs.stringify({ a: undefined })).to.equal('');
170
171         expect(Qs.stringify({ a: { b: undefined, c: null } }, { strictNullHandling: true })).to.equal('a%5Bc%5D');
172         expect(Qs.stringify({ a: { b: undefined, c: null } }, { strictNullHandling: false })).to.equal('a%5Bc%5D=');
173         expect(Qs.stringify({ a: { b: undefined, c: '' } })).to.equal('a%5Bc%5D=');
174         done();
175     });
176
177     it('url encodes values', function (done) {
178
179         expect(Qs.stringify({ a: 'b c' })).to.equal('a=b%20c');
180         done();
181     });
182
183     it('stringifies a date', function (done) {
184
185         var now = new Date();
186         var str = 'a=' + encodeURIComponent(now.toISOString());
187         expect(Qs.stringify({ a: now })).to.equal(str);
188         done();
189     });
190
191     it('stringifies the weird object from qs', function (done) {
192
193         expect(Qs.stringify({ 'my weird field': '~q1!2"\'w$5&7/z8)?' })).to.equal('my%20weird%20field=~q1%212%22%27w%245%267%2Fz8%29%3F');
194         done();
195     });
196
197     it('skips properties that are part of the object prototype', function (done) {
198
199         Object.prototype.crash = 'test';
200         expect(Qs.stringify({ a: 'b' })).to.equal('a=b');
201         expect(Qs.stringify({ a: { b: 'c' } })).to.equal('a%5Bb%5D=c');
202         delete Object.prototype.crash;
203         done();
204     });
205
206     it('stringifies boolean values', function (done) {
207
208         expect(Qs.stringify({ a: true })).to.equal('a=true');
209         expect(Qs.stringify({ a: { b: true } })).to.equal('a%5Bb%5D=true');
210         expect(Qs.stringify({ b: false })).to.equal('b=false');
211         expect(Qs.stringify({ b: { c: false } })).to.equal('b%5Bc%5D=false');
212         done();
213     });
214
215     it('stringifies buffer values', function (done) {
216
217         expect(Qs.stringify({ a: new Buffer('test') })).to.equal('a=test');
218         expect(Qs.stringify({ a: { b: new Buffer('test') } })).to.equal('a%5Bb%5D=test');
219         done();
220     });
221
222     it('stringifies an object using an alternative delimiter', function (done) {
223
224         expect(Qs.stringify({ a: 'b', c: 'd' }, { delimiter: ';' })).to.equal('a=b;c=d');
225         done();
226     });
227
228     it('doesn\'t blow up when Buffer global is missing', function (done) {
229
230         var tempBuffer = global.Buffer;
231         delete global.Buffer;
232         var result = Qs.stringify({ a: 'b', c: 'd' });
233         global.Buffer = tempBuffer;
234         expect(result).to.equal('a=b&c=d');
235         done();
236     });
237
238     it('selects properties when filter=array', function (done) {
239
240         expect(Qs.stringify({ a: 'b' }, { filter: ['a'] })).to.equal('a=b');
241         expect(Qs.stringify({ a: 1 }, { filter: [] })).to.equal('');
242         expect(Qs.stringify({ a: { b: [1, 2, 3, 4], c: 'd' }, c: 'f' }, { filter: ['a', 'b', 0, 2] })).to.equal('a%5Bb%5D%5B0%5D=1&a%5Bb%5D%5B2%5D=3');
243         done();
244
245     });
246
247     it('supports custom representations when filter=function', function (done) {
248
249         var calls = 0;
250         var obj = { a: 'b', c: 'd', e: { f: new Date(1257894000000) } };
251         var filterFunc = function (prefix, value) {
252
253             calls++;
254             if (calls === 1) {
255                 expect(prefix).to.be.empty();
256                 expect(value).to.equal(obj);
257             }
258             else if (prefix === 'c') {
259                 return;
260             }
261             else if (value instanceof Date) {
262                 expect(prefix).to.equal('e[f]');
263                 return value.getTime();
264             }
265             return value;
266         };
267
268         expect(Qs.stringify(obj, { filter: filterFunc })).to.equal('a=b&e%5Bf%5D=1257894000000');
269         expect(calls).to.equal(5);
270         done();
271
272     });
273
274     it('can disable uri encoding', function (done) {
275
276         expect(Qs.stringify({ a: 'b' }, { encode: false })).to.equal('a=b');
277         expect(Qs.stringify({ a: { b: 'c' } }, { encode: false })).to.equal('a[b]=c');
278         expect(Qs.stringify({ a: 'b', c: null }, { strictNullHandling: true, encode: false })).to.equal('a=b&c');
279         done();
280     });
281
282     it('can sort the keys', function (done) {
283
284         var sort = function alphabeticalSort (a, b) {
285
286             return a.localeCompare(b);
287         };
288
289         expect(Qs.stringify({ a: 'c', z: 'y', b : 'f' }, { sort : sort })).to.equal('a=c&b=f&z=y');
290         expect(Qs.stringify({ a: 'c', z: { j: 'a', i:'b' }, b : 'f' }, { sort : sort })).to.equal('a=c&b=f&z%5Bi%5D=b&z%5Bj%5D=a');
291         done();
292     });
293 });