forked from sindresorhus/query-string
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
58 lines (46 loc) · 1.79 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
/*global describe, it */
'use strict';
var assert = require('assert');
var qs = require('./query-string');
describe('.parse()', function () {
it('should handle query strings starting with a `?`', function () {
assert.deepEqual(qs.parse('?foo=bar'), {foo: 'bar'});
});
it('should parse a qseter', function () {
assert.deepEqual(qs.parse('foo=bar'), {foo: 'bar'});
});
it('should parse multiple qseters', function () {
assert.deepEqual(qs.parse('foo=bar&key=val'), {foo: 'bar', key: 'val'});
});
it('should parse qseters without a value', function () {
assert.deepEqual(qs.parse('foo'), {foo: null});
assert.deepEqual(qs.parse('foo&key'), {foo: null, key: null});
assert.deepEqual(qs.parse('foo=bar&key'), {foo: 'bar', key: null});
});
it('should return empty object if no qss can be found', function () {
assert.deepEqual(qs.parse('?'), {});
assert.deepEqual(qs.parse(' '), {});
});
it('should handle `+` correctly', function () {
assert.deepEqual(qs.parse('foo+faz=bar+baz++'), {'foo faz': 'bar baz '});
});
it('should handle multiple of the same key', function () {
assert.deepEqual(qs.parse('foo=bar&foo=baz'), {foo: ['bar', 'baz']});
});
});
describe('.stringify()', function () {
it('should stringify', function () {
assert.strictEqual(qs.stringify({foo: 'bar'}), 'foo=bar');
assert.strictEqual(qs.stringify({foo: 'bar', bar: 'baz'}), 'foo=bar&bar=baz');
});
it('should handle different types', function () {
assert.strictEqual(qs.stringify(), '');
assert.strictEqual(qs.stringify(0), '');
});
it('should URI encode', function () {
assert.strictEqual(qs.stringify({'foo bar': 'baz faz'}), 'foo%20bar=baz%20faz');
});
it('should handle array value', function () {
assert.strictEqual(qs.stringify({abc: 'abc', foo: ['bar', 'baz']}), 'abc=abc&foo=bar&foo=baz');
});
});