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 array indices when asked', function (done) {
52 expect(Qs.stringify({ a: ['b', 'c', 'd'] }, { indices: false })).to.equal('a=b&a=c&a=d');
56 it('stringifies a nested array value', function (done) {
58 expect(Qs.stringify({ a: { b: ['c', 'd'] } })).to.equal('a%5Bb%5D%5B0%5D=c&a%5Bb%5D%5B1%5D=d');
62 it('stringifies an object inside an array', function (done) {
64 expect(Qs.stringify({ a: [{ b: 'c' }] })).to.equal('a%5B0%5D%5Bb%5D=c');
65 expect(Qs.stringify({ a: [{ b: { c: [1] } }] })).to.equal('a%5B0%5D%5Bb%5D%5Bc%5D%5B0%5D=1');
69 it('does not omit object keys when indices = false', function (done) {
71 expect(Qs.stringify({ a: [{ b: 'c' }] }, { indices: false })).to.equal('a%5Bb%5D=c');
75 it('uses indices notation for arrays when indices=true', function (done) {
77 expect(Qs.stringify({ a: ['b', 'c'] }, { indices: true })).to.equal('a%5B0%5D=b&a%5B1%5D=c');
81 it('uses indices notation for arrays when no arrayFormat is specified', function (done) {
83 expect(Qs.stringify({ a: ['b', 'c'] })).to.equal('a%5B0%5D=b&a%5B1%5D=c');
87 it('uses indices notation for arrays when no arrayFormat=indices', function (done) {
89 expect(Qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'indices' })).to.equal('a%5B0%5D=b&a%5B1%5D=c');
93 it('uses repeat notation for arrays when no arrayFormat=repeat', function (done) {
95 expect(Qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'repeat' })).to.equal('a=b&a=c');
99 it('uses brackets notation for arrays when no arrayFormat=brackets', function (done) {
101 expect(Qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'brackets' })).to.equal('a%5B%5D=b&a%5B%5D=c');
105 it('stringifies a complicated object', function (done) {
107 expect(Qs.stringify({ a: { b: 'c', d: 'e' } })).to.equal('a%5Bb%5D=c&a%5Bd%5D=e');
111 it('stringifies an empty value', function (done) {
113 expect(Qs.stringify({ a: '' })).to.equal('a=');
114 expect(Qs.stringify({ a: null }, { strictNullHandling: true })).to.equal('a');
116 expect(Qs.stringify({ a: '', b: '' })).to.equal('a=&b=');
117 expect(Qs.stringify({ a: null, b: '' }, { strictNullHandling: true })).to.equal('a&b=');
119 expect(Qs.stringify({ a: { b: '' } })).to.equal('a%5Bb%5D=');
120 expect(Qs.stringify({ a: { b: null } }, { strictNullHandling: true })).to.equal('a%5Bb%5D');
121 expect(Qs.stringify({ a: { b: null } }, { strictNullHandling: false })).to.equal('a%5Bb%5D=');
126 it('stringifies an empty object', function (done) {
128 var obj = Object.create(null);
130 expect(Qs.stringify(obj)).to.equal('a=b');
134 it('returns an empty string for invalid input', function (done) {
136 expect(Qs.stringify(undefined)).to.equal('');
137 expect(Qs.stringify(false)).to.equal('');
138 expect(Qs.stringify(null)).to.equal('');
139 expect(Qs.stringify('')).to.equal('');
143 it('stringifies an object with an empty object as a child', function (done) {
146 a: Object.create(null)
150 expect(Qs.stringify(obj)).to.equal('a%5Bb%5D=c');
154 it('drops keys with a value of undefined', function (done) {
156 expect(Qs.stringify({ a: undefined })).to.equal('');
158 expect(Qs.stringify({ a: { b: undefined, c: null } }, { strictNullHandling: true })).to.equal('a%5Bc%5D');
159 expect(Qs.stringify({ a: { b: undefined, c: null } }, { strictNullHandling: false })).to.equal('a%5Bc%5D=');
160 expect(Qs.stringify({ a: { b: undefined, c: '' } })).to.equal('a%5Bc%5D=');
164 it('url encodes values', function (done) {
166 expect(Qs.stringify({ a: 'b c' })).to.equal('a=b%20c');
170 it('stringifies a date', function (done) {
172 var now = new Date();
173 var str = 'a=' + encodeURIComponent(now.toISOString());
174 expect(Qs.stringify({ a: now })).to.equal(str);
178 it('stringifies the weird object from qs', function (done) {
180 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');
184 it('skips properties that are part of the object prototype', function (done) {
186 Object.prototype.crash = 'test';
187 expect(Qs.stringify({ a: 'b' })).to.equal('a=b');
188 expect(Qs.stringify({ a: { b: 'c' } })).to.equal('a%5Bb%5D=c');
189 delete Object.prototype.crash;
193 it('stringifies boolean values', function (done) {
195 expect(Qs.stringify({ a: true })).to.equal('a=true');
196 expect(Qs.stringify({ a: { b: true } })).to.equal('a%5Bb%5D=true');
197 expect(Qs.stringify({ b: false })).to.equal('b=false');
198 expect(Qs.stringify({ b: { c: false } })).to.equal('b%5Bc%5D=false');
202 it('stringifies buffer values', function (done) {
204 expect(Qs.stringify({ a: new Buffer('test') })).to.equal('a=test');
205 expect(Qs.stringify({ a: { b: new Buffer('test') } })).to.equal('a%5Bb%5D=test');
209 it('stringifies an object using an alternative delimiter', function (done) {
211 expect(Qs.stringify({ a: 'b', c: 'd' }, { delimiter: ';' })).to.equal('a=b;c=d');
215 it('doesn\'t blow up when Buffer global is missing', function (done) {
217 var tempBuffer = global.Buffer;
218 delete global.Buffer;
219 expect(Qs.stringify({ a: 'b', c: 'd' })).to.equal('a=b&c=d');
220 global.Buffer = tempBuffer;
224 it('selects properties when filter=array', function (done) {
226 expect(Qs.stringify({ a: 'b' }, { filter: ['a'] })).to.equal('a=b');
227 expect(Qs.stringify({ a: 1 }, { filter: [] })).to.equal('');
228 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');
233 it('supports custom representations when filter=function', function (done) {
236 var obj = { a: 'b', c: 'd', e: { f: new Date(1257894000000) } };
237 var filterFunc = function (prefix, value) {
241 expect(prefix).to.be.empty();
242 expect(value).to.equal(obj);
244 else if (prefix === 'c') {
247 else if (value instanceof Date) {
248 expect(prefix).to.equal('e[f]');
249 return value.getTime();
254 expect(Qs.stringify(obj, { filter: filterFunc })).to.equal('a=b&e%5Bf%5D=1257894000000');
255 expect(calls).to.equal(5);