test-querystring.js 7.1 KB

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