3 var test = require('tape');
4 var qs = require('../');
5 var iconv = require('iconv-lite');
7 test('stringify()', function (t) {
8 t.test('stringifies a querystring object', function (st) {
9 st.equal(qs.stringify({ a: 'b' }), 'a=b');
10 st.equal(qs.stringify({ a: 1 }), 'a=1');
11 st.equal(qs.stringify({ a: 1, b: 2 }), 'a=1&b=2');
12 st.equal(qs.stringify({ a: 'A_Z' }), 'a=A_Z');
13 st.equal(qs.stringify({ a: 'β¬' }), 'a=%E2%82%AC');
14 st.equal(qs.stringify({ a: 'ξ' }), 'a=%EE%80%80');
15 st.equal(qs.stringify({ a: 'Χ' }), 'a=%D7%90');
16 st.equal(qs.stringify({ a: 'π·' }), 'a=%F0%90%90%B7');
20 t.test('stringifies a nested object', function (st) {
21 st.equal(qs.stringify({ a: { b: 'c' } }), 'a%5Bb%5D=c');
22 st.equal(qs.stringify({ a: { b: { c: { d: 'e' } } } }), 'a%5Bb%5D%5Bc%5D%5Bd%5D=e');
26 t.test('stringifies a nested object with dots notation', function (st) {
27 st.equal(qs.stringify({ a: { b: 'c' } }, { allowDots: true }), 'a.b=c');
28 st.equal(qs.stringify({ a: { b: { c: { d: 'e' } } } }, { allowDots: true }), 'a.b.c.d=e');
32 t.test('stringifies an array value', function (st) {
34 qs.stringify({ a: ['b', 'c', 'd'] }, { arrayFormat: 'indices' }),
35 'a%5B0%5D=b&a%5B1%5D=c&a%5B2%5D=d',
39 qs.stringify({ a: ['b', 'c', 'd'] }, { arrayFormat: 'brackets' }),
40 'a%5B%5D=b&a%5B%5D=c&a%5B%5D=d',
41 'brackets => brackets'
44 qs.stringify({ a: ['b', 'c', 'd'] }),
45 'a%5B0%5D=b&a%5B1%5D=c&a%5B2%5D=d',
51 t.test('omits nulls when asked', function (st) {
52 st.equal(qs.stringify({ a: 'b', c: null }, { skipNulls: true }), 'a=b');
56 t.test('omits nested nulls when asked', function (st) {
57 st.equal(qs.stringify({ a: { b: 'c', d: null } }, { skipNulls: true }), 'a%5Bb%5D=c');
61 t.test('omits array indices when asked', function (st) {
62 st.equal(qs.stringify({ a: ['b', 'c', 'd'] }, { indices: false }), 'a=b&a=c&a=d');
66 t.test('stringifies a nested array value', function (st) {
67 st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { arrayFormat: 'indices' }), 'a%5Bb%5D%5B0%5D=c&a%5Bb%5D%5B1%5D=d');
68 st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { arrayFormat: 'brackets' }), 'a%5Bb%5D%5B%5D=c&a%5Bb%5D%5B%5D=d');
69 st.equal(qs.stringify({ a: { b: ['c', 'd'] } }), 'a%5Bb%5D%5B0%5D=c&a%5Bb%5D%5B1%5D=d');
73 t.test('stringifies a nested array value with dots notation', function (st) {
76 { a: { b: ['c', 'd'] } },
77 { allowDots: true, encode: false, arrayFormat: 'indices' }
80 'indices: stringifies with dots + indices'
84 { a: { b: ['c', 'd'] } },
85 { allowDots: true, encode: false, arrayFormat: 'brackets' }
88 'brackets: stringifies with dots + brackets'
92 { a: { b: ['c', 'd'] } },
93 { allowDots: true, encode: false }
96 'default: stringifies with dots + indices'
101 t.test('stringifies an object inside an array', function (st) {
103 qs.stringify({ a: [{ b: 'c' }] }, { arrayFormat: 'indices' }),
105 'indices => brackets'
108 qs.stringify({ a: [{ b: 'c' }] }, { arrayFormat: 'brackets' }),
110 'brackets => brackets'
113 qs.stringify({ a: [{ b: 'c' }] }),
119 qs.stringify({ a: [{ b: { c: [1] } }] }, { arrayFormat: 'indices' }),
120 'a%5B0%5D%5Bb%5D%5Bc%5D%5B0%5D=1',
125 qs.stringify({ a: [{ b: { c: [1] } }] }, { arrayFormat: 'brackets' }),
126 'a%5B%5D%5Bb%5D%5Bc%5D%5B%5D=1',
127 'brackets => brackets'
131 qs.stringify({ a: [{ b: { c: [1] } }] }),
132 'a%5B0%5D%5Bb%5D%5Bc%5D%5B0%5D=1',
139 t.test('stringifies an array with mixed objects and primitives', function (st) {
141 qs.stringify({ a: [{ b: 1 }, 2, 3] }, { encode: false, arrayFormat: 'indices' }),
142 'a[0][b]=1&a[1]=2&a[2]=3',
146 qs.stringify({ a: [{ b: 1 }, 2, 3] }, { encode: false, arrayFormat: 'brackets' }),
147 'a[][b]=1&a[]=2&a[]=3',
148 'brackets => brackets'
151 qs.stringify({ a: [{ b: 1 }, 2, 3] }, { encode: false }),
152 'a[0][b]=1&a[1]=2&a[2]=3',
159 t.test('stringifies an object inside an array with dots notation', function (st) {
163 { allowDots: true, encode: false, arrayFormat: 'indices' }
171 { allowDots: true, encode: false, arrayFormat: 'brackets' }
174 'brackets => brackets'
179 { allowDots: true, encode: false }
187 { a: [{ b: { c: [1] } }] },
188 { allowDots: true, encode: false, arrayFormat: 'indices' }
195 { a: [{ b: { c: [1] } }] },
196 { allowDots: true, encode: false, arrayFormat: 'brackets' }
199 'brackets => brackets'
203 { a: [{ b: { c: [1] } }] },
204 { allowDots: true, encode: false }
213 t.test('does not omit object keys when indices = false', function (st) {
214 st.equal(qs.stringify({ a: [{ b: 'c' }] }, { indices: false }), 'a%5Bb%5D=c');
218 t.test('uses indices notation for arrays when indices=true', function (st) {
219 st.equal(qs.stringify({ a: ['b', 'c'] }, { indices: true }), 'a%5B0%5D=b&a%5B1%5D=c');
223 t.test('uses indices notation for arrays when no arrayFormat is specified', function (st) {
224 st.equal(qs.stringify({ a: ['b', 'c'] }), 'a%5B0%5D=b&a%5B1%5D=c');
228 t.test('uses indices notation for arrays when no arrayFormat=indices', function (st) {
229 st.equal(qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'indices' }), 'a%5B0%5D=b&a%5B1%5D=c');
233 t.test('uses repeat notation for arrays when no arrayFormat=repeat', function (st) {
234 st.equal(qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'repeat' }), 'a=b&a=c');
238 t.test('uses brackets notation for arrays when no arrayFormat=brackets', function (st) {
239 st.equal(qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'brackets' }), 'a%5B%5D=b&a%5B%5D=c');
243 t.test('stringifies a complicated object', function (st) {
244 st.equal(qs.stringify({ a: { b: 'c', d: 'e' } }), 'a%5Bb%5D=c&a%5Bd%5D=e');
248 t.test('stringifies an empty value', function (st) {
249 st.equal(qs.stringify({ a: '' }), 'a=');
250 st.equal(qs.stringify({ a: null }, { strictNullHandling: true }), 'a');
252 st.equal(qs.stringify({ a: '', b: '' }), 'a=&b=');
253 st.equal(qs.stringify({ a: null, b: '' }, { strictNullHandling: true }), 'a&b=');
255 st.equal(qs.stringify({ a: { b: '' } }), 'a%5Bb%5D=');
256 st.equal(qs.stringify({ a: { b: null } }, { strictNullHandling: true }), 'a%5Bb%5D');
257 st.equal(qs.stringify({ a: { b: null } }, { strictNullHandling: false }), 'a%5Bb%5D=');
262 t.test('stringifies a null object', { skip: !Object.create }, function (st) {
263 var obj = Object.create(null);
265 st.equal(qs.stringify(obj), 'a=b');
269 t.test('returns an empty string for invalid input', function (st) {
270 st.equal(qs.stringify(undefined), '');
271 st.equal(qs.stringify(false), '');
272 st.equal(qs.stringify(null), '');
273 st.equal(qs.stringify(''), '');
277 t.test('stringifies an object with a null object as a child', { skip: !Object.create }, function (st) {
278 var obj = { a: Object.create(null) };
281 st.equal(qs.stringify(obj), 'a%5Bb%5D=c');
285 t.test('drops keys with a value of undefined', function (st) {
286 st.equal(qs.stringify({ a: undefined }), '');
288 st.equal(qs.stringify({ a: { b: undefined, c: null } }, { strictNullHandling: true }), 'a%5Bc%5D');
289 st.equal(qs.stringify({ a: { b: undefined, c: null } }, { strictNullHandling: false }), 'a%5Bc%5D=');
290 st.equal(qs.stringify({ a: { b: undefined, c: '' } }), 'a%5Bc%5D=');
294 t.test('url encodes values', function (st) {
295 st.equal(qs.stringify({ a: 'b c' }), 'a=b%20c');
299 t.test('stringifies a date', function (st) {
300 var now = new Date();
301 var str = 'a=' + encodeURIComponent(now.toISOString());
302 st.equal(qs.stringify({ a: now }), str);
306 t.test('stringifies the weird object from qs', function (st) {
307 st.equal(qs.stringify({ 'my weird field': '~q1!2"\'w$5&7/z8)?' }), 'my%20weird%20field=~q1%212%22%27w%245%267%2Fz8%29%3F');
311 t.test('skips properties that are part of the object prototype', function (st) {
312 Object.prototype.crash = 'test';
313 st.equal(qs.stringify({ a: 'b' }), 'a=b');
314 st.equal(qs.stringify({ a: { b: 'c' } }), 'a%5Bb%5D=c');
315 delete Object.prototype.crash;
319 t.test('stringifies boolean values', function (st) {
320 st.equal(qs.stringify({ a: true }), 'a=true');
321 st.equal(qs.stringify({ a: { b: true } }), 'a%5Bb%5D=true');
322 st.equal(qs.stringify({ b: false }), 'b=false');
323 st.equal(qs.stringify({ b: { c: false } }), 'b%5Bc%5D=false');
327 t.test('stringifies buffer values', function (st) {
328 st.equal(qs.stringify({ a: new Buffer('test') }), 'a=test');
329 st.equal(qs.stringify({ a: { b: new Buffer('test') } }), 'a%5Bb%5D=test');
333 t.test('stringifies an object using an alternative delimiter', function (st) {
334 st.equal(qs.stringify({ a: 'b', c: 'd' }, { delimiter: ';' }), 'a=b;c=d');
338 t.test('doesn\'t blow up when Buffer global is missing', function (st) {
339 var tempBuffer = global.Buffer;
340 delete global.Buffer;
341 var result = qs.stringify({ a: 'b', c: 'd' });
342 global.Buffer = tempBuffer;
343 st.equal(result, 'a=b&c=d');
347 t.test('selects properties when filter=array', function (st) {
348 st.equal(qs.stringify({ a: 'b' }, { filter: ['a'] }), 'a=b');
349 st.equal(qs.stringify({ a: 1 }, { filter: [] }), '');
353 { a: { b: [1, 2, 3, 4], c: 'd' }, c: 'f' },
354 { filter: ['a', 'b', 0, 2], arrayFormat: 'indices' }
356 'a%5Bb%5D%5B0%5D=1&a%5Bb%5D%5B2%5D=3',
361 { a: { b: [1, 2, 3, 4], c: 'd' }, c: 'f' },
362 { filter: ['a', 'b', 0, 2], arrayFormat: 'brackets' }
364 'a%5Bb%5D%5B%5D=1&a%5Bb%5D%5B%5D=3',
365 'brackets => brackets'
369 { a: { b: [1, 2, 3, 4], c: 'd' }, c: 'f' },
370 { filter: ['a', 'b', 0, 2] }
372 'a%5Bb%5D%5B0%5D=1&a%5Bb%5D%5B2%5D=3',
379 t.test('supports custom representations when filter=function', function (st) {
381 var obj = { a: 'b', c: 'd', e: { f: new Date(1257894000000) } };
382 var filterFunc = function (prefix, value) {
385 st.equal(prefix, '', 'prefix is empty');
386 st.equal(value, obj);
387 } else if (prefix === 'c') {
389 } else if (value instanceof Date) {
390 st.equal(prefix, 'e[f]');
391 return value.getTime();
396 st.equal(qs.stringify(obj, { filter: filterFunc }), 'a=b&e%5Bf%5D=1257894000000');
401 t.test('can disable uri encoding', function (st) {
402 st.equal(qs.stringify({ a: 'b' }, { encode: false }), 'a=b');
403 st.equal(qs.stringify({ a: { b: 'c' } }, { encode: false }), 'a[b]=c');
404 st.equal(qs.stringify({ a: 'b', c: null }, { strictNullHandling: true, encode: false }), 'a=b&c');
408 t.test('can sort the keys', function (st) {
409 var sort = function (a, b) {
410 return a.localeCompare(b);
412 st.equal(qs.stringify({ a: 'c', z: 'y', b: 'f' }, { sort: sort }), 'a=c&b=f&z=y');
413 st.equal(qs.stringify({ a: 'c', z: { j: 'a', i: 'b' }, b: 'f' }, { sort: sort }), 'a=c&b=f&z%5Bi%5D=b&z%5Bj%5D=a');
417 t.test('can sort the keys at depth 3 or more too', function (st) {
418 var sort = function (a, b) {
419 return a.localeCompare(b);
423 { a: 'a', z: { zj: { zjb: 'zjb', zja: 'zja' }, zi: { zib: 'zib', zia: 'zia' } }, b: 'b' },
424 { sort: sort, encode: false }
426 'a=a&b=b&z[zi][zia]=zia&z[zi][zib]=zib&z[zj][zja]=zja&z[zj][zjb]=zjb'
430 { a: 'a', z: { zj: { zjb: 'zjb', zja: 'zja' }, zi: { zib: 'zib', zia: 'zia' } }, b: 'b' },
431 { sort: null, encode: false }
433 'a=a&z[zj][zjb]=zjb&z[zj][zja]=zja&z[zi][zib]=zib&z[zi][zia]=zia&b=b'
438 t.test('can stringify with custom encoding', function (st) {
439 st.equal(qs.stringify({ η: '倧ιͺεΊ', '': '' }, {
440 encoder: function (str) {
441 if (str.length === 0) {
444 var buf = iconv.encode(str, 'shiftjis');
446 for (var i = 0; i < buf.length; ++i) {
447 result.push(buf.readUInt8(i).toString(16));
449 return '%' + result.join('%');
451 }), '%8c%a7=%91%e5%8d%e3%95%7b&=');
455 t.test('throws error with wrong encoder', function (st) {
456 st.throws(function () {
457 qs.stringify({}, { encoder: 'string' });
458 }, new TypeError('Encoder has to be a function.'));
462 t.test('can use custom encoder for a buffer object', { skip: typeof Buffer === 'undefined' }, function (st) {
463 st.equal(qs.stringify({ a: new Buffer([1]) }, {
464 encoder: function (buffer) {
465 if (typeof buffer === 'string') {
468 return String.fromCharCode(buffer.readUInt8(0) + 97);
474 t.test('serializeDate option', function (st) {
475 var date = new Date();
477 qs.stringify({ a: date }),
478 'a=' + date.toISOString().replace(/:/g, '%3A'),
479 'default is toISOString'
482 var mutatedDate = new Date();
483 mutatedDate.toISOString = function () {
484 throw new SyntaxError();
486 st.throws(function () {
487 mutatedDate.toISOString();
490 qs.stringify({ a: mutatedDate }),
491 'a=' + Date.prototype.toISOString.call(mutatedDate).replace(/:/g, '%3A'),
492 'toISOString works even when method is not locally present'
495 var specificDate = new Date(6);
499 { serializeDate: function (d) { return d.getTime() * 7; } }
502 'custom serializeDate function called'
508 t.test('RFC 1738 spaces serialization', function (st) {
509 st.equal(qs.stringify({ a: 'b c' }, { format: qs.formats.RFC1738 }), 'a=b+c');
510 st.equal(qs.stringify({ 'a b': 'c d' }, { format: qs.formats.RFC1738 }), 'a+b=c+d');
514 t.test('RFC 3986 spaces serialization', function (st) {
515 st.equal(qs.stringify({ a: 'b c' }, { format: qs.formats.RFC3986 }), 'a=b%20c');
516 st.equal(qs.stringify({ 'a b': 'c d' }, { format: qs.formats.RFC3986 }), 'a%20b=c%20d');
520 t.test('Backward compatibility to RFC 3986', function (st) {
521 st.equal(qs.stringify({ a: 'b c' }), 'a=b%20c');
525 t.test('Edge cases and unknown formats', function (st) {
526 ['UFO1234', false, 1234, null, {}, []].forEach(
530 qs.stringify({ a: 'b c' }, { format: format });
532 new TypeError('Unknown format option provided.')