Projekt

Obecné

Profil

Stáhnout (7.33 KB) Statistiky
| Větev: | Revize:
1
// Copyright Joyent, Inc. and other Node contributors.
2
//
3
// Permission is hereby granted, free of charge, to any person obtaining a
4
// copy of this software and associated documentation files (the
5
// "Software"), to deal in the Software without restriction, including
6
// without limitation the rights to use, copy, modify, merge, publish,
7
// distribute, sublicense, and/or sell copies of the Software, and to permit
8
// persons to whom the Software is furnished to do so, subject to the
9
// following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21

    
22
"use strict";
23

    
24
// test using assert
25
var qs = require('../');
26

    
27
// folding block, commented to pass gjslint
28
// {{{
29
// [ wonkyQS, canonicalQS, obj ]
30
var qsTestCases = [
31
  ['foo=918854443121279438895193',
32
   'foo=918854443121279438895193',
33
   {'foo': '918854443121279438895193'}],
34
  ['foo=bar', 'foo=bar', {'foo': 'bar'}],
35
  ['foo=bar&foo=quux', 'foo=bar&foo=quux', {'foo': ['bar', 'quux']}],
36
  ['foo=1&bar=2', 'foo=1&bar=2', {'foo': '1', 'bar': '2'}],
37
  ['my+weird+field=q1%212%22%27w%245%267%2Fz8%29%3F',
38
  'my%20weird%20field=q1!2%22\'w%245%267%2Fz8)%3F',
39
  {'my weird field': 'q1!2"\'w$5&7/z8)?' }],
40
  ['foo%3Dbaz=bar', 'foo%3Dbaz=bar', {'foo=baz': 'bar'}],
41
  ['foo=baz=bar', 'foo=baz%3Dbar', {'foo': 'baz=bar'}],
42
  ['str=foo&arr=1&arr=2&arr=3&somenull=&undef=',
43
   'str=foo&arr=1&arr=2&arr=3&somenull=&undef=',
44
   { 'str': 'foo',
45
     'arr': ['1', '2', '3'],
46
     'somenull': '',
47
     'undef': ''}],
48
  [' foo = bar ', '%20foo%20=%20bar%20', {' foo ': ' bar '}],
49
  // disable test that fails ['foo=%zx', 'foo=%25zx', {'foo': '%zx'}],
50
  ['foo=%EF%BF%BD', 'foo=%EF%BF%BD', {'foo': '\ufffd' }],
51
  // See: https://github.com/joyent/node/issues/1707
52
  ['hasOwnProperty=x&toString=foo&valueOf=bar&__defineGetter__=baz',
53
    'hasOwnProperty=x&toString=foo&valueOf=bar&__defineGetter__=baz',
54
    { hasOwnProperty: 'x',
55
      toString: 'foo',
56
      valueOf: 'bar',
57
      __defineGetter__: 'baz' }],
58
  // See: https://github.com/joyent/node/issues/3058
59
  ['foo&bar=baz', 'foo=&bar=baz', { foo: '', bar: 'baz' }]
60
];
61

    
62
// [ wonkyQS, canonicalQS, obj ]
63
var qsColonTestCases = [
64
  ['foo:bar', 'foo:bar', {'foo': 'bar'}],
65
  ['foo:bar;foo:quux', 'foo:bar;foo:quux', {'foo': ['bar', 'quux']}],
66
  ['foo:1&bar:2;baz:quux',
67
   'foo:1%26bar%3A2;baz:quux',
68
   {'foo': '1&bar:2', 'baz': 'quux'}],
69
  ['foo%3Abaz:bar', 'foo%3Abaz:bar', {'foo:baz': 'bar'}],
70
  ['foo:baz:bar', 'foo:baz%3Abar', {'foo': 'baz:bar'}]
71
];
72

    
73
// [wonkyObj, qs, canonicalObj]
74
var extendedFunction = function() {};
75
extendedFunction.prototype = {a: 'b'};
76
var qsWeirdObjects = [
77
  [{regexp: /./g}, 'regexp=', {'regexp': ''}],
78
  [{regexp: new RegExp('.', 'g')}, 'regexp=', {'regexp': ''}],
79
  [{fn: function() {}}, 'fn=', {'fn': ''}],
80
  [{fn: new Function('')}, 'fn=', {'fn': ''}],
81
  [{math: Math}, 'math=', {'math': ''}],
82
  [{e: extendedFunction}, 'e=', {'e': ''}],
83
  [{d: new Date()}, 'd=', {'d': ''}],
84
  [{d: Date}, 'd=', {'d': ''}],
85
  [{f: new Boolean(false), t: new Boolean(true)}, 'f=&t=', {'f': '', 't': ''}],
86
  [{f: false, t: true}, 'f=false&t=true', {'f': 'false', 't': 'true'}],
87
  [{n: null}, 'n=', {'n': ''}],
88
  [{nan: NaN}, 'nan=', {'nan': ''}],
89
  [{inf: Infinity}, 'inf=', {'inf': ''}]
90
];
91
// }}}
92

    
93
var qsNoMungeTestCases = [
94
  ['', {}],
95
  ['foo=bar&foo=baz', {'foo': ['bar', 'baz']}],
96
  ['blah=burp', {'blah': 'burp'}],
97
  ['gragh=1&gragh=3&goo=2', {'gragh': ['1', '3'], 'goo': '2'}],
98
  ['frappucino=muffin&goat%5B%5D=scone&pond=moose',
99
   {'frappucino': 'muffin', 'goat[]': 'scone', 'pond': 'moose'}],
100
  ['trololol=yes&lololo=no', {'trololol': 'yes', 'lololo': 'no'}]
101
];
102

    
103
exports['test basic'] = function(assert) {
104
  assert.strictEqual('918854443121279438895193',
105
                   qs.parse('id=918854443121279438895193').id,
106
                   'prase id=918854443121279438895193');
107
};
108

    
109
exports['test that the canonical qs is parsed properly'] = function(assert) {
110
  qsTestCases.forEach(function(testCase) {
111
    assert.deepEqual(testCase[2], qs.parse(testCase[0]),
112
                     'parse ' + testCase[0]);
113
  });
114
};
115

    
116

    
117
exports['test that the colon test cases can do the same'] = function(assert) {
118
  qsColonTestCases.forEach(function(testCase) {
119
    assert.deepEqual(testCase[2], qs.parse(testCase[0], ';', ':'),
120
                     'parse ' + testCase[0] + ' -> ; :');
121
  });
122
};
123

    
124
exports['test the weird objects, that they get parsed properly'] = function(assert) {
125
  qsWeirdObjects.forEach(function(testCase) {
126
    assert.deepEqual(testCase[2], qs.parse(testCase[1]),
127
                     'parse ' + testCase[1]);
128
  });
129
};
130

    
131
exports['test non munge test cases'] = function(assert) {
132
  qsNoMungeTestCases.forEach(function(testCase) {
133
    assert.deepEqual(testCase[0], qs.stringify(testCase[1], '&', '=', false),
134
                     'stringify ' + JSON.stringify(testCase[1]) + ' -> & =');
135
  });
136
};
137

    
138
exports['test the nested qs-in-qs case'] = function(assert) {
139
  var f = qs.parse('a=b&q=x%3Dy%26y%3Dz');
140
  f.q = qs.parse(f.q);
141
  assert.deepEqual(f, { a: 'b', q: { x: 'y', y: 'z' } },
142
                   'parse a=b&q=x%3Dy%26y%3Dz');
143
};
144

    
145
exports['test nested in colon'] = function(assert) {
146
  var f = qs.parse('a:b;q:x%3Ay%3By%3Az', ';', ':');
147
  f.q = qs.parse(f.q, ';', ':');
148
  assert.deepEqual(f, { a: 'b', q: { x: 'y', y: 'z' } },
149
                   'parse a:b;q:x%3Ay%3By%3Az -> ; :');
150
};
151

    
152
exports['test stringifying'] = function(assert) {
153
  qsTestCases.forEach(function(testCase) {
154
    assert.equal(testCase[1], qs.stringify(testCase[2]),
155
                 'stringify ' + JSON.stringify(testCase[2]));
156
  });
157

    
158
  qsColonTestCases.forEach(function(testCase) {
159
    assert.equal(testCase[1], qs.stringify(testCase[2], ';', ':'),
160
                 'stringify ' + JSON.stringify(testCase[2]) + ' -> ; :');
161
  });
162

    
163
  qsWeirdObjects.forEach(function(testCase) {
164
    assert.equal(testCase[1], qs.stringify(testCase[0]),
165
                 'stringify ' + JSON.stringify(testCase[0]));
166
  });
167
};
168

    
169
exports['test stringifying nested'] = function(assert) {
170
  var f = qs.stringify({
171
    a: 'b',
172
    q: qs.stringify({
173
      x: 'y',
174
      y: 'z'
175
    })
176
  });
177
  assert.equal(f, 'a=b&q=x%3Dy%26y%3Dz',
178
               JSON.stringify({
179
                  a: 'b',
180
                  'qs.stringify -> q': {
181
                    x: 'y',
182
                    y: 'z'
183
                  }
184
                }));
185

    
186
  var threw = false;
187
  try { qs.parse(undefined); } catch(error) { threw = true; }
188
  assert.ok(!threw, "does not throws on undefined");
189
};
190

    
191
exports['test nested in colon'] = function(assert) {
192
  var f = qs.stringify({
193
    a: 'b',
194
    q: qs.stringify({
195
      x: 'y',
196
      y: 'z'
197
    }, ';', ':')
198
  }, ';', ':');
199
  assert.equal(f, 'a:b;q:x%3Ay%3By%3Az',
200
               'stringify ' + JSON.stringify({
201
                  a: 'b',
202
                  'qs.stringify -> q': {
203
                    x: 'y',
204
                    y: 'z'
205
                  }
206
                }) + ' -> ; : ');
207

    
208

    
209
  assert.deepEqual({}, qs.parse(), 'parse undefined');
210
};
(2-2/3)