3 var test = require('tape');
4 var qs = require('../');
5 var utils = require('../lib/utils');
6 var iconv = require('iconv-lite');
7 var SaferBuffer = require('safer-buffer').Buffer;
9 test('stringify()', function (t) {
10 t.test('stringifies a querystring object', function (st) {
11 st.equal(qs.stringify({ a: 'b' }), 'a=b');
12 st.equal(qs.stringify({ a: 1 }), 'a=1');
13 st.equal(qs.stringify({ a: 1, b: 2 }), 'a=1&b=2');
14 st.equal(qs.stringify({ a: 'A_Z' }), 'a=A_Z');
15 st.equal(qs.stringify({ a: '€' }), 'a=%E2%82%AC');
16 st.equal(qs.stringify({ a: '' }), 'a=%EE%80%80');
17 st.equal(qs.stringify({ a: 'א' }), 'a=%D7%90');
18 st.equal(qs.stringify({ a: '𐐷' }), 'a=%F0%90%90%B7');
22 t.test('stringifies falsy values', function (st) {
23 st.equal(qs.stringify(undefined), '');
24 st.equal(qs.stringify(null), '');
25 st.equal(qs.stringify(null, { strictNullHandling: true }), '');
26 st.equal(qs.stringify(false), '');
27 st.equal(qs.stringify(0), '');
31 t.test('adds query prefix', function (st) {
32 st.equal(qs.stringify({ a: 'b' }, { addQueryPrefix: true }), '?a=b');
36 t.test('with query prefix, outputs blank string given an empty object', function (st) {
37 st.equal(qs.stringify({}, { addQueryPrefix: true }), '');
41 t.test('stringifies nested falsy values', function (st) {
42 st.equal(qs.stringify({ a: { b: { c: null } } }), 'a%5Bb%5D%5Bc%5D=');
43 st.equal(qs.stringify({ a: { b: { c: null } } }, { strictNullHandling: true }), 'a%5Bb%5D%5Bc%5D');
44 st.equal(qs.stringify({ a: { b: { c: false } } }), 'a%5Bb%5D%5Bc%5D=false');
48 t.test('stringifies a nested object', function (st) {
49 st.equal(qs.stringify({ a: { b: 'c' } }), 'a%5Bb%5D=c');
50 st.equal(qs.stringify({ a: { b: { c: { d: 'e' } } } }), 'a%5Bb%5D%5Bc%5D%5Bd%5D=e');
54 t.test('stringifies a nested object with dots notation', function (st) {
55 st.equal(qs.stringify({ a: { b: 'c' } }, { allowDots: true }), 'a.b=c');
56 st.equal(qs.stringify({ a: { b: { c: { d: 'e' } } } }, { allowDots: true }), 'a.b.c.d=e');
60 t.test('stringifies an array value', function (st) {
62 qs.stringify({ a: ['b', 'c', 'd'] }, { arrayFormat: 'indices' }),
63 'a%5B0%5D=b&a%5B1%5D=c&a%5B2%5D=d',
67 qs.stringify({ a: ['b', 'c', 'd'] }, { arrayFormat: 'brackets' }),
68 'a%5B%5D=b&a%5B%5D=c&a%5B%5D=d',
69 'brackets => brackets'
72 qs.stringify({ a: ['b', 'c', 'd'] }, { arrayFormat: 'comma' }),
77 qs.stringify({ a: ['b', 'c', 'd'] }),
78 'a%5B0%5D=b&a%5B1%5D=c&a%5B2%5D=d',
84 t.test('omits nulls when asked', function (st) {
85 st.equal(qs.stringify({ a: 'b', c: null }, { skipNulls: true }), 'a=b');
89 t.test('omits nested nulls when asked', function (st) {
90 st.equal(qs.stringify({ a: { b: 'c', d: null } }, { skipNulls: true }), 'a%5Bb%5D=c');
94 t.test('omits array indices when asked', function (st) {
95 st.equal(qs.stringify({ a: ['b', 'c', 'd'] }, { indices: false }), 'a=b&a=c&a=d');
99 t.test('stringifies a nested array value', function (st) {
100 st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { arrayFormat: 'indices' }), 'a%5Bb%5D%5B0%5D=c&a%5Bb%5D%5B1%5D=d');
101 st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { arrayFormat: 'brackets' }), 'a%5Bb%5D%5B%5D=c&a%5Bb%5D%5B%5D=d');
102 st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { arrayFormat: 'comma' }), 'a%5Bb%5D=c%2Cd'); // a[b]=c,d
103 st.equal(qs.stringify({ a: { b: ['c', 'd'] } }), 'a%5Bb%5D%5B0%5D=c&a%5Bb%5D%5B1%5D=d');
107 t.test('stringifies a nested array value with dots notation', function (st) {
110 { a: { b: ['c', 'd'] } },
111 { allowDots: true, encode: false, arrayFormat: 'indices' }
114 'indices: stringifies with dots + indices'
118 { a: { b: ['c', 'd'] } },
119 { allowDots: true, encode: false, arrayFormat: 'brackets' }
122 'brackets: stringifies with dots + brackets'
126 { a: { b: ['c', 'd'] } },
127 { allowDots: true, encode: false, arrayFormat: 'comma' }
130 'comma: stringifies with dots + comma'
134 { a: { b: ['c', 'd'] } },
135 { allowDots: true, encode: false }
138 'default: stringifies with dots + indices'
143 t.test('stringifies an object inside an array', function (st) {
145 qs.stringify({ a: [{ b: 'c' }] }, { arrayFormat: 'indices' }),
146 'a%5B0%5D%5Bb%5D=c', // a[0][b]=c
147 'indices => brackets'
150 qs.stringify({ a: [{ b: 'c' }] }, { arrayFormat: 'brackets' }),
151 'a%5B%5D%5Bb%5D=c', // a[][b]=c
152 'brackets => brackets'
155 qs.stringify({ a: [{ b: 'c' }] }),
161 qs.stringify({ a: [{ b: { c: [1] } }] }, { arrayFormat: 'indices' }),
162 'a%5B0%5D%5Bb%5D%5Bc%5D%5B0%5D=1',
167 qs.stringify({ a: [{ b: { c: [1] } }] }, { arrayFormat: 'brackets' }),
168 'a%5B%5D%5Bb%5D%5Bc%5D%5B%5D=1',
169 'brackets => brackets'
173 qs.stringify({ a: [{ b: { c: [1] } }] }),
174 'a%5B0%5D%5Bb%5D%5Bc%5D%5B0%5D=1',
181 t.test('stringifies an array with mixed objects and primitives', function (st) {
183 qs.stringify({ a: [{ b: 1 }, 2, 3] }, { encode: false, arrayFormat: 'indices' }),
184 'a[0][b]=1&a[1]=2&a[2]=3',
188 qs.stringify({ a: [{ b: 1 }, 2, 3] }, { encode: false, arrayFormat: 'brackets' }),
189 'a[][b]=1&a[]=2&a[]=3',
190 'brackets => brackets'
193 qs.stringify({ a: [{ b: 1 }, 2, 3] }, { encode: false }),
194 'a[0][b]=1&a[1]=2&a[2]=3',
201 t.test('stringifies an object inside an array with dots notation', function (st) {
205 { allowDots: true, encode: false, arrayFormat: 'indices' }
213 { allowDots: true, encode: false, arrayFormat: 'brackets' }
216 'brackets => brackets'
221 { allowDots: true, encode: false }
229 { a: [{ b: { c: [1] } }] },
230 { allowDots: true, encode: false, arrayFormat: 'indices' }
237 { a: [{ b: { c: [1] } }] },
238 { allowDots: true, encode: false, arrayFormat: 'brackets' }
241 'brackets => brackets'
245 { a: [{ b: { c: [1] } }] },
246 { allowDots: true, encode: false }
255 t.test('does not omit object keys when indices = false', function (st) {
256 st.equal(qs.stringify({ a: [{ b: 'c' }] }, { indices: false }), 'a%5Bb%5D=c');
260 t.test('uses indices notation for arrays when indices=true', function (st) {
261 st.equal(qs.stringify({ a: ['b', 'c'] }, { indices: true }), 'a%5B0%5D=b&a%5B1%5D=c');
265 t.test('uses indices notation for arrays when no arrayFormat is specified', function (st) {
266 st.equal(qs.stringify({ a: ['b', 'c'] }), 'a%5B0%5D=b&a%5B1%5D=c');
270 t.test('uses indices notation for arrays when no arrayFormat=indices', function (st) {
271 st.equal(qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'indices' }), 'a%5B0%5D=b&a%5B1%5D=c');
275 t.test('uses repeat notation for arrays when no arrayFormat=repeat', function (st) {
276 st.equal(qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'repeat' }), 'a=b&a=c');
280 t.test('uses brackets notation for arrays when no arrayFormat=brackets', function (st) {
281 st.equal(qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'brackets' }), 'a%5B%5D=b&a%5B%5D=c');
285 t.test('stringifies a complicated object', function (st) {
286 st.equal(qs.stringify({ a: { b: 'c', d: 'e' } }), 'a%5Bb%5D=c&a%5Bd%5D=e');
290 t.test('stringifies an empty value', function (st) {
291 st.equal(qs.stringify({ a: '' }), 'a=');
292 st.equal(qs.stringify({ a: null }, { strictNullHandling: true }), 'a');
294 st.equal(qs.stringify({ a: '', b: '' }), 'a=&b=');
295 st.equal(qs.stringify({ a: null, b: '' }, { strictNullHandling: true }), 'a&b=');
297 st.equal(qs.stringify({ a: { b: '' } }), 'a%5Bb%5D=');
298 st.equal(qs.stringify({ a: { b: null } }, { strictNullHandling: true }), 'a%5Bb%5D');
299 st.equal(qs.stringify({ a: { b: null } }, { strictNullHandling: false }), 'a%5Bb%5D=');
304 t.test('stringifies a null object', { skip: !Object.create }, function (st) {
305 var obj = Object.create(null);
307 st.equal(qs.stringify(obj), 'a=b');
311 t.test('returns an empty string for invalid input', function (st) {
312 st.equal(qs.stringify(undefined), '');
313 st.equal(qs.stringify(false), '');
314 st.equal(qs.stringify(null), '');
315 st.equal(qs.stringify(''), '');
319 t.test('stringifies an object with a null object as a child', { skip: !Object.create }, function (st) {
320 var obj = { a: Object.create(null) };
323 st.equal(qs.stringify(obj), 'a%5Bb%5D=c');
327 t.test('drops keys with a value of undefined', function (st) {
328 st.equal(qs.stringify({ a: undefined }), '');
330 st.equal(qs.stringify({ a: { b: undefined, c: null } }, { strictNullHandling: true }), 'a%5Bc%5D');
331 st.equal(qs.stringify({ a: { b: undefined, c: null } }, { strictNullHandling: false }), 'a%5Bc%5D=');
332 st.equal(qs.stringify({ a: { b: undefined, c: '' } }), 'a%5Bc%5D=');
336 t.test('url encodes values', function (st) {
337 st.equal(qs.stringify({ a: 'b c' }), 'a=b%20c');
341 t.test('stringifies a date', function (st) {
342 var now = new Date();
343 var str = 'a=' + encodeURIComponent(now.toISOString());
344 st.equal(qs.stringify({ a: now }), str);
348 t.test('stringifies the weird object from qs', function (st) {
349 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');
353 t.test('skips properties that are part of the object prototype', function (st) {
354 Object.prototype.crash = 'test';
355 st.equal(qs.stringify({ a: 'b' }), 'a=b');
356 st.equal(qs.stringify({ a: { b: 'c' } }), 'a%5Bb%5D=c');
357 delete Object.prototype.crash;
361 t.test('stringifies boolean values', function (st) {
362 st.equal(qs.stringify({ a: true }), 'a=true');
363 st.equal(qs.stringify({ a: { b: true } }), 'a%5Bb%5D=true');
364 st.equal(qs.stringify({ b: false }), 'b=false');
365 st.equal(qs.stringify({ b: { c: false } }), 'b%5Bc%5D=false');
369 t.test('stringifies buffer values', function (st) {
370 st.equal(qs.stringify({ a: SaferBuffer.from('test') }), 'a=test');
371 st.equal(qs.stringify({ a: { b: SaferBuffer.from('test') } }), 'a%5Bb%5D=test');
375 t.test('stringifies an object using an alternative delimiter', function (st) {
376 st.equal(qs.stringify({ a: 'b', c: 'd' }, { delimiter: ';' }), 'a=b;c=d');
380 t.test('doesn\'t blow up when Buffer global is missing', function (st) {
381 var tempBuffer = global.Buffer;
382 delete global.Buffer;
383 var result = qs.stringify({ a: 'b', c: 'd' });
384 global.Buffer = tempBuffer;
385 st.equal(result, 'a=b&c=d');
389 t.test('selects properties when filter=array', function (st) {
390 st.equal(qs.stringify({ a: 'b' }, { filter: ['a'] }), 'a=b');
391 st.equal(qs.stringify({ a: 1 }, { filter: [] }), '');
395 { a: { b: [1, 2, 3, 4], c: 'd' }, c: 'f' },
396 { filter: ['a', 'b', 0, 2], arrayFormat: 'indices' }
398 'a%5Bb%5D%5B0%5D=1&a%5Bb%5D%5B2%5D=3',
403 { a: { b: [1, 2, 3, 4], c: 'd' }, c: 'f' },
404 { filter: ['a', 'b', 0, 2], arrayFormat: 'brackets' }
406 'a%5Bb%5D%5B%5D=1&a%5Bb%5D%5B%5D=3',
407 'brackets => brackets'
411 { a: { b: [1, 2, 3, 4], c: 'd' }, c: 'f' },
412 { filter: ['a', 'b', 0, 2] }
414 'a%5Bb%5D%5B0%5D=1&a%5Bb%5D%5B2%5D=3',
421 t.test('supports custom representations when filter=function', function (st) {
423 var obj = { a: 'b', c: 'd', e: { f: new Date(1257894000000) } };
424 var filterFunc = function (prefix, value) {
427 st.equal(prefix, '', 'prefix is empty');
428 st.equal(value, obj);
429 } else if (prefix === 'c') {
431 } else if (value instanceof Date) {
432 st.equal(prefix, 'e[f]');
433 return value.getTime();
438 st.equal(qs.stringify(obj, { filter: filterFunc }), 'a=b&e%5Bf%5D=1257894000000');
443 t.test('can disable uri encoding', function (st) {
444 st.equal(qs.stringify({ a: 'b' }, { encode: false }), 'a=b');
445 st.equal(qs.stringify({ a: { b: 'c' } }, { encode: false }), 'a[b]=c');
446 st.equal(qs.stringify({ a: 'b', c: null }, { strictNullHandling: true, encode: false }), 'a=b&c');
450 t.test('can sort the keys', function (st) {
451 var sort = function (a, b) {
452 return a.localeCompare(b);
454 st.equal(qs.stringify({ a: 'c', z: 'y', b: 'f' }, { sort: sort }), 'a=c&b=f&z=y');
455 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');
459 t.test('can sort the keys at depth 3 or more too', function (st) {
460 var sort = function (a, b) {
461 return a.localeCompare(b);
465 { a: 'a', z: { zj: { zjb: 'zjb', zja: 'zja' }, zi: { zib: 'zib', zia: 'zia' } }, b: 'b' },
466 { sort: sort, encode: false }
468 'a=a&b=b&z[zi][zia]=zia&z[zi][zib]=zib&z[zj][zja]=zja&z[zj][zjb]=zjb'
472 { a: 'a', z: { zj: { zjb: 'zjb', zja: 'zja' }, zi: { zib: 'zib', zia: 'zia' } }, b: 'b' },
473 { sort: null, encode: false }
475 'a=a&z[zj][zjb]=zjb&z[zj][zja]=zja&z[zi][zib]=zib&z[zi][zia]=zia&b=b'
480 t.test('can stringify with custom encoding', function (st) {
481 st.equal(qs.stringify({ 県: '大阪府', '': '' }, {
482 encoder: function (str) {
483 if (str.length === 0) {
486 var buf = iconv.encode(str, 'shiftjis');
488 for (var i = 0; i < buf.length; ++i) {
489 result.push(buf.readUInt8(i).toString(16));
491 return '%' + result.join('%');
493 }), '%8c%a7=%91%e5%8d%e3%95%7b&=');
497 t.test('receives the default encoder as a second argument', function (st) {
499 qs.stringify({ a: 1 }, {
500 encoder: function (str, defaultEncoder) {
501 st.equal(defaultEncoder, utils.encode);
507 t.test('throws error with wrong encoder', function (st) {
508 st['throws'](function () {
509 qs.stringify({}, { encoder: 'string' });
510 }, new TypeError('Encoder has to be a function.'));
514 t.test('can use custom encoder for a buffer object', { skip: typeof Buffer === 'undefined' }, function (st) {
515 st.equal(qs.stringify({ a: SaferBuffer.from([1]) }, {
516 encoder: function (buffer) {
517 if (typeof buffer === 'string') {
520 return String.fromCharCode(buffer.readUInt8(0) + 97);
526 t.test('serializeDate option', function (st) {
527 var date = new Date();
529 qs.stringify({ a: date }),
530 'a=' + date.toISOString().replace(/:/g, '%3A'),
531 'default is toISOString'
534 var mutatedDate = new Date();
535 mutatedDate.toISOString = function () {
536 throw new SyntaxError();
538 st['throws'](function () {
539 mutatedDate.toISOString();
542 qs.stringify({ a: mutatedDate }),
543 'a=' + Date.prototype.toISOString.call(mutatedDate).replace(/:/g, '%3A'),
544 'toISOString works even when method is not locally present'
547 var specificDate = new Date(6);
551 { serializeDate: function (d) { return d.getTime() * 7; } }
554 'custom serializeDate function called'
560 t.test('RFC 1738 spaces serialization', function (st) {
561 st.equal(qs.stringify({ a: 'b c' }, { format: qs.formats.RFC1738 }), 'a=b+c');
562 st.equal(qs.stringify({ 'a b': 'c d' }, { format: qs.formats.RFC1738 }), 'a+b=c+d');
566 t.test('RFC 3986 spaces serialization', function (st) {
567 st.equal(qs.stringify({ a: 'b c' }, { format: qs.formats.RFC3986 }), 'a=b%20c');
568 st.equal(qs.stringify({ 'a b': 'c d' }, { format: qs.formats.RFC3986 }), 'a%20b=c%20d');
572 t.test('Backward compatibility to RFC 3986', function (st) {
573 st.equal(qs.stringify({ a: 'b c' }), 'a=b%20c');
577 t.test('Edge cases and unknown formats', function (st) {
578 ['UFO1234', false, 1234, null, {}, []].forEach(
582 qs.stringify({ a: 'b c' }, { format: format });
584 new TypeError('Unknown format option provided.')
591 t.test('encodeValuesOnly', function (st) {
594 { a: 'b', c: ['d', 'e=f'], f: [['g'], ['h']] },
595 { encodeValuesOnly: true }
597 'a=b&c[0]=d&c[1]=e%3Df&f[0][0]=g&f[1][0]=h'
601 { a: 'b', c: ['d', 'e'], f: [['g'], ['h']] }
603 'a=b&c%5B0%5D=d&c%5B1%5D=e&f%5B0%5D%5B0%5D=g&f%5B1%5D%5B0%5D=h'
608 t.test('encodeValuesOnly - strictNullHandling', function (st) {
612 { encodeValuesOnly: true, strictNullHandling: true }
619 t.test('throws if an invalid charset is specified', function (st) {
620 st['throws'](function () {
621 qs.stringify({ a: 'b' }, { charset: 'foobar' });
622 }, new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined'));
626 t.test('respects a charset of iso-8859-1', function (st) {
627 st.equal(qs.stringify({ æ: 'æ' }, { charset: 'iso-8859-1' }), '%E6=%E6');
631 t.test('encodes unrepresentable chars as numeric entities in iso-8859-1 mode', function (st) {
632 st.equal(qs.stringify({ a: '☺' }, { charset: 'iso-8859-1' }), 'a=%26%239786%3B');
636 t.test('respects an explicit charset of utf-8 (the default)', function (st) {
637 st.equal(qs.stringify({ a: 'æ' }, { charset: 'utf-8' }), 'a=%C3%A6');
641 t.test('adds the right sentinel when instructed to and the charset is utf-8', function (st) {
642 st.equal(qs.stringify({ a: 'æ' }, { charsetSentinel: true, charset: 'utf-8' }), 'utf8=%E2%9C%93&a=%C3%A6');
646 t.test('adds the right sentinel when instructed to and the charset is iso-8859-1', function (st) {
647 st.equal(qs.stringify({ a: 'æ' }, { charsetSentinel: true, charset: 'iso-8859-1' }), 'utf8=%26%2310003%3B&a=%E6');
651 t.test('does not mutate the options argument', function (st) {
653 qs.stringify({}, options);
654 st.deepEqual(options, {});
658 t.test('strictNullHandling works with custom filter', function (st) {
659 var filter = function (prefix, value) {
663 var options = { strictNullHandling: true, filter: filter };
664 st.equal(qs.stringify({ key: null }, options), 'key');
668 t.test('strictNullHandling works with null serializeDate', function (st) {
669 var serializeDate = function () {
672 var options = { strictNullHandling: true, serializeDate: serializeDate };
673 var date = new Date();
674 st.equal(qs.stringify({ key: date }, options), 'key');