You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
7.3 KiB

4 years ago
  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. "use strict";
  22. // test using assert
  23. var qs = require('../');
  24. // folding block, commented to pass gjslint
  25. // {{{
  26. // [ wonkyQS, canonicalQS, obj ]
  27. var qsTestCases = [
  28. ['foo=918854443121279438895193',
  29. 'foo=918854443121279438895193',
  30. {'foo': '918854443121279438895193'}],
  31. ['foo=bar', 'foo=bar', {'foo': 'bar'}],
  32. ['foo=bar&foo=quux', 'foo=bar&foo=quux', {'foo': ['bar', 'quux']}],
  33. ['foo=1&bar=2', 'foo=1&bar=2', {'foo': '1', 'bar': '2'}],
  34. ['my+weird+field=q1%212%22%27w%245%267%2Fz8%29%3F',
  35. 'my%20weird%20field=q1!2%22\'w%245%267%2Fz8)%3F',
  36. {'my weird field': 'q1!2"\'w$5&7/z8)?' }],
  37. ['foo%3Dbaz=bar', 'foo%3Dbaz=bar', {'foo=baz': 'bar'}],
  38. ['foo=baz=bar', 'foo=baz%3Dbar', {'foo': 'baz=bar'}],
  39. ['str=foo&arr=1&arr=2&arr=3&somenull=&undef=',
  40. 'str=foo&arr=1&arr=2&arr=3&somenull=&undef=',
  41. { 'str': 'foo',
  42. 'arr': ['1', '2', '3'],
  43. 'somenull': '',
  44. 'undef': ''}],
  45. [' foo = bar ', '%20foo%20=%20bar%20', {' foo ': ' bar '}],
  46. // disable test that fails ['foo=%zx', 'foo=%25zx', {'foo': '%zx'}],
  47. ['foo=%EF%BF%BD', 'foo=%EF%BF%BD', {'foo': '\ufffd' }],
  48. // See: https://github.com/joyent/node/issues/1707
  49. ['hasOwnProperty=x&toString=foo&valueOf=bar&__defineGetter__=baz',
  50. 'hasOwnProperty=x&toString=foo&valueOf=bar&__defineGetter__=baz',
  51. { hasOwnProperty: 'x',
  52. toString: 'foo',
  53. valueOf: 'bar',
  54. __defineGetter__: 'baz' }],
  55. // See: https://github.com/joyent/node/issues/3058
  56. ['foo&bar=baz', 'foo=&bar=baz', { foo: '', bar: 'baz' }]
  57. ];
  58. // [ wonkyQS, canonicalQS, obj ]
  59. var qsColonTestCases = [
  60. ['foo:bar', 'foo:bar', {'foo': 'bar'}],
  61. ['foo:bar;foo:quux', 'foo:bar;foo:quux', {'foo': ['bar', 'quux']}],
  62. ['foo:1&bar:2;baz:quux',
  63. 'foo:1%26bar%3A2;baz:quux',
  64. {'foo': '1&bar:2', 'baz': 'quux'}],
  65. ['foo%3Abaz:bar', 'foo%3Abaz:bar', {'foo:baz': 'bar'}],
  66. ['foo:baz:bar', 'foo:baz%3Abar', {'foo': 'baz:bar'}]
  67. ];
  68. // [wonkyObj, qs, canonicalObj]
  69. var extendedFunction = function() {};
  70. extendedFunction.prototype = {a: 'b'};
  71. var qsWeirdObjects = [
  72. [{regexp: /./g}, 'regexp=', {'regexp': ''}],
  73. [{regexp: new RegExp('.', 'g')}, 'regexp=', {'regexp': ''}],
  74. [{fn: function() {}}, 'fn=', {'fn': ''}],
  75. [{fn: new Function('')}, 'fn=', {'fn': ''}],
  76. [{math: Math}, 'math=', {'math': ''}],
  77. [{e: extendedFunction}, 'e=', {'e': ''}],
  78. [{d: new Date()}, 'd=', {'d': ''}],
  79. [{d: Date}, 'd=', {'d': ''}],
  80. [{f: new Boolean(false), t: new Boolean(true)}, 'f=&t=', {'f': '', 't': ''}],
  81. [{f: false, t: true}, 'f=false&t=true', {'f': 'false', 't': 'true'}],
  82. [{n: null}, 'n=', {'n': ''}],
  83. [{nan: NaN}, 'nan=', {'nan': ''}],
  84. [{inf: Infinity}, 'inf=', {'inf': ''}]
  85. ];
  86. // }}}
  87. var qsNoMungeTestCases = [
  88. ['', {}],
  89. ['foo=bar&foo=baz', {'foo': ['bar', 'baz']}],
  90. ['blah=burp', {'blah': 'burp'}],
  91. ['gragh=1&gragh=3&goo=2', {'gragh': ['1', '3'], 'goo': '2'}],
  92. ['frappucino=muffin&goat%5B%5D=scone&pond=moose',
  93. {'frappucino': 'muffin', 'goat[]': 'scone', 'pond': 'moose'}],
  94. ['trololol=yes&lololo=no', {'trololol': 'yes', 'lololo': 'no'}]
  95. ];
  96. exports['test basic'] = function(assert) {
  97. assert.strictEqual('918854443121279438895193',
  98. qs.parse('id=918854443121279438895193').id,
  99. 'prase id=918854443121279438895193');
  100. };
  101. exports['test that the canonical qs is parsed properly'] = function(assert) {
  102. qsTestCases.forEach(function(testCase) {
  103. assert.deepEqual(testCase[2], qs.parse(testCase[0]),
  104. 'parse ' + testCase[0]);
  105. });
  106. };
  107. exports['test that the colon test cases can do the same'] = function(assert) {
  108. qsColonTestCases.forEach(function(testCase) {
  109. assert.deepEqual(testCase[2], qs.parse(testCase[0], ';', ':'),
  110. 'parse ' + testCase[0] + ' -> ; :');
  111. });
  112. };
  113. exports['test the weird objects, that they get parsed properly'] = function(assert) {
  114. qsWeirdObjects.forEach(function(testCase) {
  115. assert.deepEqual(testCase[2], qs.parse(testCase[1]),
  116. 'parse ' + testCase[1]);
  117. });
  118. };
  119. exports['test non munge test cases'] = function(assert) {
  120. qsNoMungeTestCases.forEach(function(testCase) {
  121. assert.deepEqual(testCase[0], qs.stringify(testCase[1], '&', '=', false),
  122. 'stringify ' + JSON.stringify(testCase[1]) + ' -> & =');
  123. });
  124. };
  125. exports['test the nested qs-in-qs case'] = function(assert) {
  126. var f = qs.parse('a=b&q=x%3Dy%26y%3Dz');
  127. f.q = qs.parse(f.q);
  128. assert.deepEqual(f, { a: 'b', q: { x: 'y', y: 'z' } },
  129. 'parse a=b&q=x%3Dy%26y%3Dz');
  130. };
  131. exports['test nested in colon'] = function(assert) {
  132. var f = qs.parse('a:b;q:x%3Ay%3By%3Az', ';', ':');
  133. f.q = qs.parse(f.q, ';', ':');
  134. assert.deepEqual(f, { a: 'b', q: { x: 'y', y: 'z' } },
  135. 'parse a:b;q:x%3Ay%3By%3Az -> ; :');
  136. };
  137. exports['test stringifying'] = function(assert) {
  138. qsTestCases.forEach(function(testCase) {
  139. assert.equal(testCase[1], qs.stringify(testCase[2]),
  140. 'stringify ' + JSON.stringify(testCase[2]));
  141. });
  142. qsColonTestCases.forEach(function(testCase) {
  143. assert.equal(testCase[1], qs.stringify(testCase[2], ';', ':'),
  144. 'stringify ' + JSON.stringify(testCase[2]) + ' -> ; :');
  145. });
  146. qsWeirdObjects.forEach(function(testCase) {
  147. assert.equal(testCase[1], qs.stringify(testCase[0]),
  148. 'stringify ' + JSON.stringify(testCase[0]));
  149. });
  150. };
  151. exports['test stringifying nested'] = function(assert) {
  152. var f = qs.stringify({
  153. a: 'b',
  154. q: qs.stringify({
  155. x: 'y',
  156. y: 'z'
  157. })
  158. });
  159. assert.equal(f, 'a=b&q=x%3Dy%26y%3Dz',
  160. JSON.stringify({
  161. a: 'b',
  162. 'qs.stringify -> q': {
  163. x: 'y',
  164. y: 'z'
  165. }
  166. }));
  167. var threw = false;
  168. try { qs.parse(undefined); } catch(error) { threw = true; }
  169. assert.ok(!threw, "does not throws on undefined");
  170. };
  171. exports['test nested in colon'] = function(assert) {
  172. var f = qs.stringify({
  173. a: 'b',
  174. q: qs.stringify({
  175. x: 'y',
  176. y: 'z'
  177. }, ';', ':')
  178. }, ';', ':');
  179. assert.equal(f, 'a:b;q:x%3Ay%3By%3Az',
  180. 'stringify ' + JSON.stringify({
  181. a: 'b',
  182. 'qs.stringify -> q': {
  183. x: 'y',
  184. y: 'z'
  185. }
  186. }) + ' -> ; : ');
  187. assert.deepEqual({}, qs.parse(), 'parse undefined');
  188. };