initial commit
[squeep-querystring] / test / index.js
1 /* eslint-disable capitalized-comments */
2 /* eslint-env mocha */
3 'use strict';
4
5
6 const assert = require('assert');
7 const qs = require('../index.js');
8
9 describe('querystring', function () {
10 describe('parse', function () {
11
12 it('should return empty object from no input', function () {
13 assert.deepStrictEqual(
14 qs.parse(undefined),
15 {},
16 );
17 });
18
19 it('should return empty object from empty string', function () {
20 assert.deepStrictEqual(
21 qs.parse(''),
22 {},
23 );
24 });
25
26 it('should return simple mapping', function () {
27 assert.deepStrictEqual(
28 qs.parse('foo=1&bar=2&baz=quux'),
29 {
30 foo: '1',
31 bar: '2',
32 baz: 'quux',
33 },
34 );
35 });
36
37 it('should return array for repeated keys', function () {
38 assert.deepStrictEqual(
39 qs.parse('foo=1&foo=2&foo=quux'),
40 {
41 foo: [ '1', '2', 'quux' ],
42 },
43 );
44 });
45
46 it('should return null values for bare keys', function () {
47 assert.deepStrictEqual(
48 qs.parse('foo&foo&bar&quux'),
49 {
50 foo: [ null, null ],
51 bar: null,
52 quux: null,
53 },
54 );
55 });
56
57 it('should handle space encodings and equals in values', function () {
58 assert.deepStrictEqual(
59 qs.parse('this+key=+spacey+string+;&foo=equally=string&'),
60 {
61 '': [ null, null ],
62 'this key': ' spacey string ',
63 foo: 'equally=string',
64 },
65 );
66 });
67
68 it('should handle raw spaces and encoded spaces', function () {
69 assert.deepStrictEqual(
70 qs.parse('this+has actual+spaces=but okay%20whatever'),
71 {
72 'this has actual spaces': 'but okay whatever',
73 },
74 );
75 });
76
77 it('should handle empty values', function () {
78 assert.deepStrictEqual(
79 qs.parse(';;;;;;;+;;;;'),
80 {
81 '': [ null, null, null, null, null, null, null, null, null, null, null ],
82 ' ': null,
83 },
84 );
85 });
86
87 it('should handle PHP style array declarations', function () {
88 assert.deepStrictEqual(
89 qs.parse('foo%5B%5D&bar[]=1&bar[]=2&baz', true),
90 {
91 'foo': [null],
92 bar: ['1', '2'],
93 baz: null,
94 },
95 )
96 });
97
98 }); // parse
99 });
100