forked from unshiftio/querystringify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
140 lines (105 loc) · 3.71 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
describe('querystringify', function () {
'use strict';
var assume = require('assume')
, qs = require('./');
describe('#stringify', function () {
var obj = {
foo: 'bar',
bar: 'foo'
};
it('is exposed as method', function () {
assume(qs.stringify).is.a('function');
});
it('transforms an object', function () {
assume(qs.stringify(obj)).equals('foo=bar&bar=foo');
});
it('can optionally prefix', function () {
assume(qs.stringify(obj, true)).equals('?foo=bar&bar=foo');
});
it('can prefix with custom things', function () {
assume(qs.stringify(obj, '&')).equals('&foo=bar&bar=foo');
});
it('doesnt prefix empty query strings', function () {
assume(qs.stringify({}, true)).equals('');
assume(qs.stringify({})).equals('');
});
it('works with object keys with empty string values', function () {
assume(qs.stringify({ foo: '' })).equals('foo=');
});
it('works with nulled objects', function () {
var obj = Object.create(null);
obj.foo='bar';
assume(qs.stringify(obj)).equals('foo=bar');
});
it('transforms `undefined` into nothing', function () {
assume(qs.stringify({ foo: undefined })).equals('foo=');
});
it('transforms `NaN` into nothing', function () {
assume(qs.stringify({ foo: NaN })).equals('foo=');
});
it('transforms `null` into nothing', function () {
assume(qs.stringify({ foo: null })).equals('foo=');
});
if (typeof Symbol !== 'undefined') {
it('does not throw on invalid input', function () {
assume(qs.stringify({ foo: Symbol('foo') })).equals('');
});
}
});
describe('#parse', function () {
it('is exposed as method', function () {
assume(qs.parse).is.a('function');
});
it('will parse a querystring to an object', function () {
var obj = qs.parse('foo=bar');
assume(obj).is.a('object');
assume(obj.foo).equals('bar');
});
it('will also work if querystring is prefixed with ?', function () {
var obj = qs.parse('?foo=bar&shizzle=mynizzle');
assume(obj).is.a('object');
assume(obj.foo).equals('bar');
assume(obj.shizzle).equals('mynizzle');
});
it('will also work if querystring is prefixed with #', function () {
var obj = qs.parse('#foo=bar&shizzle=mynizzle');
assume(obj).is.a('object');
assume(obj.foo).equals('bar');
assume(obj.shizzle).equals('mynizzle');
});
it('does not overide prototypes', function () {
var obj = qs.parse('?toString&__proto__=lol');
assume(obj).is.a('object');
assume(obj.toString).is.a('function');
assume(obj.__proto__).does.not.equals('lol');
});
it('works with querystring parameters without values', function () {
var obj = qs.parse('?foo&bar=&shizzle=mynizzle');
assume(obj).is.a('object');
assume(obj.foo).equals('');
assume(obj.bar).equals('');
assume(obj.shizzle).equals('mynizzle');
});
it('first value wins', function () {
var obj = qs.parse('foo=1&foo=2');
assume(obj.foo).equals('1');
});
it('decodes plus signs', function () {
var obj = qs.parse('foo+bar=baz+qux');
assume(obj).is.a('object');
assume(obj['foo bar']).equals('baz qux');
obj = qs.parse('foo+bar=baz%2Bqux');
assume(obj).is.a('object');
assume(obj['foo bar']).equals('baz+qux');
});
it('does not throw on invalid input', function () {
var obj = qs.parse('?%&');
assume(obj).is.a('object');
});
it('does not include invalid output', function () {
var obj = qs.parse('?%&');
assume(obj).is.a('object');
assume(obj).is.length(0);
});
});
});