qs 0.4.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/test/stringify.js CHANGED
@@ -1,95 +1,73 @@
1
1
 
2
- /**
3
- * Module dependencies.
4
- */
5
-
6
- var qs = require('../')
7
- , should = require('should')
8
- , query_string_identities = {
9
- 'basics': [
10
- {query_string: 'foo=bar', parsed: {'foo' : 'bar'}},
11
- {query_string: 'foo=%22bar%22', parsed: {'foo' : '\"bar\"'}},
12
- {query_string: 'foo=', parsed: {'foo': ''}},
13
- {query_string: 'foo=1&bar=2', parsed: {'foo' : '1', 'bar' : '2'}},
14
- {query_string: 'my%20weird%20field=q1!2%22\'w%245%267%2Fz8)%3F', parsed: {'my weird field': "q1!2\"'w$5&7/z8)?"}},
15
- {query_string: 'foo%3Dbaz=bar', parsed: {'foo=baz': 'bar'}},
16
- {query_string: 'foo=bar&bar=baz', parsed: {foo: 'bar', bar: 'baz'}}
17
- ],
18
- 'escaping': [
19
- {query_string: 'foo=foo%20bar', parsed: {foo: 'foo bar'}},
20
- {query_string: 'cht=p3&chd=t%3A60%2C40&chs=250x100&chl=Hello%7CWorld', parsed: {
21
- cht: 'p3'
22
- , chd: 't:60,40'
23
- , chs: '250x100'
24
- , chl: 'Hello|World'
25
- }}
26
- ],
27
- 'nested': [
28
- {query_string: 'foo[]=bar&foo[]=quux', parsed: {'foo' : ['bar', 'quux']}},
29
- {query_string: 'foo[]=bar', parsed: {foo: ['bar']}},
30
- {query_string: 'foo[]=1&foo[]=2', parsed: {'foo' : ['1', '2']}},
31
- {query_string: 'foo=bar&baz[]=1&baz[]=2&baz[]=3', parsed: {'foo' : 'bar', 'baz' : ['1', '2', '3']}},
32
- {query_string: 'foo[]=bar&baz[]=1&baz[]=2&baz[]=3', parsed: {'foo' : ['bar'], 'baz' : ['1', '2', '3']}},
33
- {query_string: 'x[y][z]=1', parsed: {'x' : {'y' : {'z' : '1'}}}},
34
- {query_string: 'x[y][z][]=1', parsed: {'x' : {'y' : {'z' : ['1']}}}},
35
- {query_string: 'x[y][z]=2', parsed: {'x' : {'y' : {'z' : '2'}}}},
36
- {query_string: 'x[y][z][]=1&x[y][z][]=2', parsed: {'x' : {'y' : {'z' : ['1', '2']}}}},
37
- {query_string: 'x[y][][z]=1', parsed: {'x' : {'y' : [{'z' : '1'}]}}},
38
- {query_string: 'x[y][][z][]=1', parsed: {'x' : {'y' : [{'z' : ['1']}]}}},
39
- {query_string: 'x[y][][z]=1&x[y][][w]=2', parsed: {'x' : {'y' : [{'z' : '1', 'w' : '2'}]}}},
40
- {query_string: 'x[y][][v][w]=1', parsed: {'x' : {'y' : [{'v' : {'w' : '1'}}]}}},
41
- {query_string: 'x[y][][z]=1&x[y][][v][w]=2', parsed: {'x' : {'y' : [{'z' : '1', 'v' : {'w' : '2'}}]}}},
42
- {query_string: 'x[y][][z]=1&x[y][][z]=2', parsed: {'x' : {'y' : [{'z' : '1'}, {'z' : '2'}]}}},
43
- {query_string: 'x[y][][z]=1&x[y][][w]=a&x[y][][z]=2&x[y][][w]=3', parsed: {'x' : {'y' : [{'z' : '1', 'w' : 'a'}, {'z' : '2', 'w' : '3'}]}}},
44
- {query_string: 'user[name][first]=tj&user[name][last]=holowaychuk', parsed: { user: { name: { first: 'tj', last: 'holowaychuk' }}}}
45
- ],
46
- 'errors': [
47
- { parsed: 'foo=bar', message: 'stringify expects an object' },
48
- { parsed: ['foo', 'bar'], message: 'stringify expects an object' }
49
- ]
50
- };
51
-
52
-
53
- // Assert error
54
- function err(fn, msg){
55
- var err;
56
- try {
57
- fn();
58
- } catch (e) {
59
- should.equal(e.message, msg);
60
- return;
61
- }
62
- throw new Error('no exception thrown, expected "' + msg + '"');
2
+ if (require.register) {
3
+ var qs = require('querystring');
4
+ } else {
5
+ var qs = require('../')
6
+ , expect = require('expect.js');
63
7
  }
64
8
 
9
+ var str_identities = {
10
+ 'basics': [
11
+ { str: 'foo=bar', obj: {'foo' : 'bar'}},
12
+ { str: 'foo=%22bar%22', obj: {'foo' : '\"bar\"'}},
13
+ { str: 'foo=', obj: {'foo': ''}},
14
+ { str: 'foo=1&bar=2', obj: {'foo' : '1', 'bar' : '2'}},
15
+ { str: 'my%20weird%20field=q1!2%22\'w%245%267%2Fz8)%3F', obj: {'my weird field': "q1!2\"'w$5&7/z8)?"}},
16
+ { str: 'foo%3Dbaz=bar', obj: {'foo=baz': 'bar'}},
17
+ { str: 'foo=bar&bar=baz', obj: {foo: 'bar', bar: 'baz'}}
18
+ ],
19
+ 'escaping': [
20
+ { str: 'foo=foo%20bar', obj: {foo: 'foo bar'}},
21
+ { str: 'cht=p3&chd=t%3A60%2C40&chs=250x100&chl=Hello%7CWorld', obj: {
22
+ cht: 'p3'
23
+ , chd: 't:60,40'
24
+ , chs: '250x100'
25
+ , chl: 'Hello|World'
26
+ }}
27
+ ],
28
+ 'nested': [
29
+ { str: 'foo[0]=bar&foo[1]=quux', obj: {'foo' : ['bar', 'quux']}},
30
+ { str: 'foo[0]=bar', obj: {foo: ['bar']}},
31
+ { str: 'foo[0]=1&foo[1]=2', obj: {'foo' : ['1', '2']}},
32
+ { str: 'foo=bar&baz[0]=1&baz[1]=2&baz[2]=3', obj: {'foo' : 'bar', 'baz' : ['1', '2', '3']}},
33
+ { str: 'foo[0]=bar&baz[0]=1&baz[1]=2&baz[2]=3', obj: {'foo' : ['bar'], 'baz' : ['1', '2', '3']}},
34
+ { str: 'x[y][z]=1', obj: {'x' : {'y' : {'z' : '1'}}}},
35
+ { str: 'x[y][z][0]=1', obj: {'x' : {'y' : {'z' : ['1']}}}},
36
+ { str: 'x[y][z]=2', obj: {'x' : {'y' : {'z' : '2'}}}},
37
+ { str: 'x[y][z][0]=1&x[y][z][1]=2', obj: {'x' : {'y' : {'z' : ['1', '2']}}}},
38
+ { str: 'x[y][0][z]=1', obj: {'x' : {'y' : [{'z' : '1'}]}}},
39
+ { str: 'x[y][0][z][0]=1', obj: {'x' : {'y' : [{'z' : ['1']}]}}},
40
+ { str: 'x[y][0][z]=1&x[y][0][w]=2', obj: {'x' : {'y' : [{'z' : '1', 'w' : '2'}]}}},
41
+ { str: 'x[y][0][v][w]=1', obj: {'x' : {'y' : [{'v' : {'w' : '1'}}]}}},
42
+ { str: 'x[y][0][z]=1&x[y][0][v][w]=2', obj: {'x' : {'y' : [{'z' : '1', 'v' : {'w' : '2'}}]}}},
43
+ { str: 'x[y][0][z]=1&x[y][1][z]=2', obj: {'x' : {'y' : [{'z' : '1'}, {'z' : '2'}]}}},
44
+ { str: 'x[y][0][z]=1&x[y][0][w]=a&x[y][1][z]=2&x[y][1][w]=3', obj: {'x' : {'y' : [{'z' : '1', 'w' : 'a'}, {'z' : '2', 'w' : '3'}]}}},
45
+ { str: 'user[name][first]=tj&user[name][last]=holowaychuk', obj: { user: { name: { first: 'tj', last: 'holowaychuk' }}}}
46
+ ],
47
+ 'errors': [
48
+ { obj: 'foo=bar', message: 'stringify expects an object' },
49
+ { obj: ['foo', 'bar'], message: 'stringify expects an object' }
50
+ ],
51
+ 'numbers': [
52
+ { str: 'limit[0]=1&limit[1]=2&limit[2]=3', obj: { limit: [1, 2, '3'] }},
53
+ { str: 'limit=1', obj: { limit: 1 }}
54
+ ]
55
+ };
56
+
65
57
  function test(type) {
66
- var str, obj;
67
- for (var i = 0; i < query_string_identities[type].length; i++) {
68
- str = query_string_identities[type][i].query_string;
69
- obj = query_string_identities[type][i].parsed;
70
- qs.stringify(obj).should.eql(str);
58
+ return function(){
59
+ var str, obj;
60
+ for (var i = 0; i < str_identities[type].length; i++) {
61
+ str = str_identities[type][i].str;
62
+ obj = str_identities[type][i].obj;
63
+ expect(qs.stringify(obj)).to.eql(str);
64
+ }
71
65
  }
72
66
  }
73
67
 
74
- module.exports = {
75
- 'test basics': function() {
76
- test('basics');
77
- },
78
-
79
- 'test escaping': function() {
80
- test('escaping');
81
- },
82
-
83
- 'test nested': function() {
84
- test('nested');
85
- },
86
-
87
- 'test errors': function() {
88
- var parsed, message;
89
- for (var i = 0; i < query_string_identities['errors'].length; i++) {
90
- message = query_string_identities['errors'][i].message;
91
- parsed = query_string_identities['errors'][i].parsed;
92
- err(function(){ qs.stringify(parsed) }, message);
93
- }
94
- }
95
- };
68
+ describe('qs.stringify()', function(){
69
+ it('should support the basics', test('basics'))
70
+ it('should support escapes', test('escaping'))
71
+ it('should support nesting', test('nested'))
72
+ it('should support numbers', test('numbers'))
73
+ })
package/test/mocha.opts DELETED
@@ -1,2 +0,0 @@
1
- --require should
2
- --ui exports