1 /* eslint no-extend-native:0 */
4 var Code = require('code');
5 var Lab = require('lab');
6 var Qs = require('../');
16 var lab = exports.lab = Lab.script();
17 var expect = Code.expect;
18 var describe = lab.experiment;
22 describe('stringify()', function () {
24 it('stringifies a querystring object', function (done) {
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');
37 it('stringifies a nested object', function (done) {
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');
44 it('stringifies an array value', function (done) {
46 expect(Qs.stringify({ a: ['b', 'c', 'd'] })).to.equal('a%5B0%5D=b&a%5B1%5D=c&a%5B2%5D=d');
50 it('omits nulls when asked', function (done) {
52 expect(Qs.stringify({ a: 'b', c: null }, { skipNulls: true })).to.equal('a=b');
57 it('omits nested nulls when asked', function (done) {
59 expect(Qs.stringify({ a: { b: 'c', d: null } }, { skipNulls: true })).to.equal('a%5Bb%5D=c');
63 it('omits array indices when asked', function (done) {
65 expect(Qs.stringify({ a: ['b', 'c', 'd'] }, { indices: false })).to.equal('a=b&a=c&a=d');
69 it('stringifies a nested array value', function (done) {
71 expect(Qs.stringify({ a: { b: ['c', 'd'] } })).to.equal('a%5Bb%5D%5B0%5D=c&a%5Bb%5D%5B1%5D=d');
75 it('stringifies an object inside an array', function (done) {
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');
82 it('does not omit object keys when indices = false', function (done) {
84 expect(Qs.stringify({ a: [{ b: 'c' }] }, { indices: false })).to.equal('a%5Bb%5D=c');
88 it('uses indices notation for arrays when indices=true', function (done) {
90 expect(Qs.stringify({ a: ['b', 'c'] }, { indices: true })).to.equal('a%5B0%5D=b&a%5B1%5D=c');
94 it('uses indices notation for arrays when no arrayFormat is specified', function (done) {
96 expect(Qs.stringify({ a: ['b', 'c'] })).to.equal('a%5B0%5D=b&a%5B1%5D=c');
100 it('uses indices notation for arrays when no arrayFormat=indices', function (done) {
102 expect(Qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'indices' })).to.equal('a%5B0%5D=b&a%5B1%5D=c');
106 it('uses repeat notation for arrays when no arrayFormat=repeat', function (done) {
108 expect(Qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'repeat' })).to.equal('a=b&a=c');
112 it('uses brackets notation for arrays when no arrayFormat=brackets', function (done) {
114 expect(Qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'brackets' })).to.equal('a%5B%5D=b&a%5B%5D=c');
118 it('stringifies a complicated object', function (done) {
120 expect(Qs.stringify({ a: { b: 'c', d: 'e' } })).to.equal('a%5Bb%5D=c&a%5Bd%5D=e');
124 it('stringifies an empty value', function (done) {
126 expect(Qs.stringify({ a: '' })).to.equal('a=');
127 expect(Qs.stringify({ a: null }, { strictNullHandling: true })).to.equal('a');
129 expect(Qs.stringify({ a: '', b: '' })).to.equal('a=&b=');
130 expect(Qs.stringify({ a: null, b: '' }, { strictNullHandling: true })).to.equal('a&b=');
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=');
139 it('stringifies an empty object', function (done) {
141 var obj = Object.create(null);
143 expect(Qs.stringify(obj)).to.equal('a=b');
147 it('returns an empty string for invalid input', function (done) {
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('');
156 it('stringifies an object with an empty object as a child', function (done) {
159 a: Object.create(null)
163 expect(Qs.stringify(obj)).to.equal('a%5Bb%5D=c');
167 it('drops keys with a value of undefined', function (done) {
169 expect(Qs.stringify({ a: undefined })).to.equal('');
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=');
177 it('url encodes values', function (done) {
179 expect(Qs.stringify({ a: 'b c' })).to.equal('a=b%20c');
183 it('stringifies a date', function (done) {
185 var now = new Date();
186 var str = 'a=' + encodeURIComponent(now.toISOString());
187 expect(Qs.stringify({ a: now })).to.equal(str);
191 it('stringifies the weird object from qs', function (done) {
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');
197 it('skips properties that are part of the object prototype', function (done) {
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;
206 it('stringifies boolean values', function (done) {
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');
215 it('stringifies buffer values', function (done) {
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');
222 it('stringifies an object using an alternative delimiter', function (done) {
224 expect(Qs.stringify({ a: 'b', c: 'd' }, { delimiter: ';' })).to.equal('a=b;c=d');
228 it('doesn\'t blow up when Buffer global is missing', function (done) {
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');
238 it('selects properties when filter=array', function (done) {
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');
247 it('supports custom representations when filter=function', function (done) {
250 var obj = { a: 'b', c: 'd', e: { f: new Date(1257894000000) } };
251 var filterFunc = function (prefix, value) {
255 expect(prefix).to.be.empty();
256 expect(value).to.equal(obj);
258 else if (prefix === 'c') {
261 else if (value instanceof Date) {
262 expect(prefix).to.equal('e[f]');
263 return value.getTime();
268 expect(Qs.stringify(obj, { filter: filterFunc })).to.equal('a=b&e%5Bf%5D=1257894000000');
269 expect(calls).to.equal(5);
274 it('can disable uri encoding', function (done) {
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');