Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81146 views
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 array indices when asked', function (done) {
51
52
expect(Qs.stringify({ a: ['b', 'c', 'd'] }, { indices: false })).to.equal('a=b&a=c&a=d');
53
done();
54
});
55
56
it('stringifies a nested array value', function (done) {
57
58
expect(Qs.stringify({ a: { b: ['c', 'd'] } })).to.equal('a%5Bb%5D%5B0%5D=c&a%5Bb%5D%5B1%5D=d');
59
done();
60
});
61
62
it('stringifies an object inside an array', function (done) {
63
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');
66
done();
67
});
68
69
it('does not omit object keys when indices = false', function (done) {
70
71
expect(Qs.stringify({ a: [{ b: 'c' }] }, { indices: false })).to.equal('a%5Bb%5D=c');
72
done();
73
});
74
75
it('uses indices notation for arrays when indices=true', function (done) {
76
77
expect(Qs.stringify({ a: ['b', 'c'] }, { indices: true })).to.equal('a%5B0%5D=b&a%5B1%5D=c');
78
done();
79
});
80
81
it('uses indices notation for arrays when no arrayFormat is specified', function (done) {
82
83
expect(Qs.stringify({ a: ['b', 'c'] })).to.equal('a%5B0%5D=b&a%5B1%5D=c');
84
done();
85
});
86
87
it('uses indices notation for arrays when no arrayFormat=indices', function (done) {
88
89
expect(Qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'indices' })).to.equal('a%5B0%5D=b&a%5B1%5D=c');
90
done();
91
});
92
93
it('uses repeat notation for arrays when no arrayFormat=repeat', function (done) {
94
95
expect(Qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'repeat' })).to.equal('a=b&a=c');
96
done();
97
});
98
99
it('uses brackets notation for arrays when no arrayFormat=brackets', function (done) {
100
101
expect(Qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'brackets' })).to.equal('a%5B%5D=b&a%5B%5D=c');
102
done();
103
});
104
105
it('stringifies a complicated object', function (done) {
106
107
expect(Qs.stringify({ a: { b: 'c', d: 'e' } })).to.equal('a%5Bb%5D=c&a%5Bd%5D=e');
108
done();
109
});
110
111
it('stringifies an empty value', function (done) {
112
113
expect(Qs.stringify({ a: '' })).to.equal('a=');
114
expect(Qs.stringify({ a: null }, {strictNullHandling: true})).to.equal('a');
115
116
expect(Qs.stringify({ a: '', b: '' })).to.equal('a=&b=');
117
expect(Qs.stringify({ a: null, b: '' }, {strictNullHandling: true})).to.equal('a&b=');
118
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=');
122
123
done();
124
});
125
126
it('stringifies an empty object', function (done) {
127
128
var obj = Object.create(null);
129
obj.a = 'b';
130
expect(Qs.stringify(obj)).to.equal('a=b');
131
done();
132
});
133
134
it('returns an empty string for invalid input', function (done) {
135
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('');
140
done();
141
});
142
143
it('stringifies an object with an empty object as a child', function (done) {
144
145
var obj = {
146
a: Object.create(null)
147
};
148
149
obj.a.b = 'c';
150
expect(Qs.stringify(obj)).to.equal('a%5Bb%5D=c');
151
done();
152
});
153
154
it('drops keys with a value of undefined', function (done) {
155
156
expect(Qs.stringify({ a: undefined })).to.equal('');
157
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=');
161
done();
162
});
163
164
it('url encodes values', function (done) {
165
166
expect(Qs.stringify({ a: 'b c' })).to.equal('a=b%20c');
167
done();
168
});
169
170
it('stringifies a date', function (done) {
171
172
var now = new Date();
173
var str = 'a=' + encodeURIComponent(now.toISOString());
174
expect(Qs.stringify({ a: now })).to.equal(str);
175
done();
176
});
177
178
it('stringifies the weird object from qs', function (done) {
179
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');
181
done();
182
});
183
184
it('skips properties that are part of the object prototype', function (done) {
185
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;
190
done();
191
});
192
193
it('stringifies boolean values', function (done) {
194
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');
199
done();
200
});
201
202
it('stringifies buffer values', function (done) {
203
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');
206
done();
207
});
208
209
it('stringifies an object using an alternative delimiter', function (done) {
210
211
expect(Qs.stringify({ a: 'b', c: 'd' }, { delimiter: ';' })).to.equal('a=b;c=d');
212
done();
213
});
214
215
it('doesn\'t blow up when Buffer global is missing', function (done) {
216
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;
221
done();
222
});
223
224
it('selects properties when filter=array', function (done) {
225
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');
229
done();
230
231
});
232
233
it('supports custom representations when filter=function', function (done) {
234
235
var calls = 0;
236
var obj = { a: 'b', c: 'd', e: { f: new Date(1257894000000) } };
237
var filterFunc = function (prefix, value) {
238
239
calls++;
240
if (calls === 1) {
241
expect(prefix).to.be.empty();
242
expect(value).to.equal(obj);
243
}
244
else if (prefix === 'c') {
245
return;
246
}
247
else if (value instanceof Date) {
248
expect(prefix).to.equal('e[f]');
249
return value.getTime();
250
}
251
return value;
252
};
253
254
expect(Qs.stringify(obj, { filter: filterFunc })).to.equal('a=b&e%5Bf%5D=1257894000000');
255
expect(calls).to.equal(5);
256
done();
257
258
});
259
});
260
261