项目原始demo,不改动
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
Questo repository è archiviato. Puoi vedere i file e clonarli, ma non puoi effettuare richieste di pushj o aprire problemi/richieste di pull.
 
 
 
 

660 righe
26 KiB

  1. 'use strict';
  2. var test = require('tape');
  3. var qs = require('../');
  4. var utils = require('../lib/utils');
  5. var iconv = require('iconv-lite');
  6. var SaferBuffer = require('safer-buffer').Buffer;
  7. test('parse()', function (t) {
  8. t.test('parses a simple string', function (st) {
  9. st.deepEqual(qs.parse('0=foo'), { 0: 'foo' });
  10. st.deepEqual(qs.parse('foo=c++'), { foo: 'c ' });
  11. st.deepEqual(qs.parse('a[>=]=23'), { a: { '>=': '23' } });
  12. st.deepEqual(qs.parse('a[<=>]==23'), { a: { '<=>': '=23' } });
  13. st.deepEqual(qs.parse('a[==]=23'), { a: { '==': '23' } });
  14. st.deepEqual(qs.parse('foo', { strictNullHandling: true }), { foo: null });
  15. st.deepEqual(qs.parse('foo'), { foo: '' });
  16. st.deepEqual(qs.parse('foo='), { foo: '' });
  17. st.deepEqual(qs.parse('foo=bar'), { foo: 'bar' });
  18. st.deepEqual(qs.parse(' foo = bar = baz '), { ' foo ': ' bar = baz ' });
  19. st.deepEqual(qs.parse('foo=bar=baz'), { foo: 'bar=baz' });
  20. st.deepEqual(qs.parse('foo=bar&bar=baz'), { foo: 'bar', bar: 'baz' });
  21. st.deepEqual(qs.parse('foo2=bar2&baz2='), { foo2: 'bar2', baz2: '' });
  22. st.deepEqual(qs.parse('foo=bar&baz', { strictNullHandling: true }), { foo: 'bar', baz: null });
  23. st.deepEqual(qs.parse('foo=bar&baz'), { foo: 'bar', baz: '' });
  24. st.deepEqual(qs.parse('cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World'), {
  25. cht: 'p3',
  26. chd: 't:60,40',
  27. chs: '250x100',
  28. chl: 'Hello|World'
  29. });
  30. st.end();
  31. });
  32. t.test('allows enabling dot notation', function (st) {
  33. st.deepEqual(qs.parse('a.b=c'), { 'a.b': 'c' });
  34. st.deepEqual(qs.parse('a.b=c', { allowDots: true }), { a: { b: 'c' } });
  35. st.end();
  36. });
  37. t.deepEqual(qs.parse('a[b]=c'), { a: { b: 'c' } }, 'parses a single nested string');
  38. t.deepEqual(qs.parse('a[b][c]=d'), { a: { b: { c: 'd' } } }, 'parses a double nested string');
  39. t.deepEqual(
  40. qs.parse('a[b][c][d][e][f][g][h]=i'),
  41. { a: { b: { c: { d: { e: { f: { '[g][h]': 'i' } } } } } } },
  42. 'defaults to a depth of 5'
  43. );
  44. t.test('only parses one level when depth = 1', function (st) {
  45. st.deepEqual(qs.parse('a[b][c]=d', { depth: 1 }), { a: { b: { '[c]': 'd' } } });
  46. st.deepEqual(qs.parse('a[b][c][d]=e', { depth: 1 }), { a: { b: { '[c][d]': 'e' } } });
  47. st.end();
  48. });
  49. t.deepEqual(qs.parse('a=b&a=c'), { a: ['b', 'c'] }, 'parses a simple array');
  50. t.test('parses an explicit array', function (st) {
  51. st.deepEqual(qs.parse('a[]=b'), { a: ['b'] });
  52. st.deepEqual(qs.parse('a[]=b&a[]=c'), { a: ['b', 'c'] });
  53. st.deepEqual(qs.parse('a[]=b&a[]=c&a[]=d'), { a: ['b', 'c', 'd'] });
  54. st.end();
  55. });
  56. t.test('parses a mix of simple and explicit arrays', function (st) {
  57. st.deepEqual(qs.parse('a=b&a[]=c'), { a: ['b', 'c'] });
  58. st.deepEqual(qs.parse('a[]=b&a=c'), { a: ['b', 'c'] });
  59. st.deepEqual(qs.parse('a[0]=b&a=c'), { a: ['b', 'c'] });
  60. st.deepEqual(qs.parse('a=b&a[0]=c'), { a: ['b', 'c'] });
  61. st.deepEqual(qs.parse('a[1]=b&a=c', { arrayLimit: 20 }), { a: ['b', 'c'] });
  62. st.deepEqual(qs.parse('a[]=b&a=c', { arrayLimit: 0 }), { a: ['b', 'c'] });
  63. st.deepEqual(qs.parse('a[]=b&a=c'), { a: ['b', 'c'] });
  64. st.deepEqual(qs.parse('a=b&a[1]=c', { arrayLimit: 20 }), { a: ['b', 'c'] });
  65. st.deepEqual(qs.parse('a=b&a[]=c', { arrayLimit: 0 }), { a: ['b', 'c'] });
  66. st.deepEqual(qs.parse('a=b&a[]=c'), { a: ['b', 'c'] });
  67. st.end();
  68. });
  69. t.test('parses a nested array', function (st) {
  70. st.deepEqual(qs.parse('a[b][]=c&a[b][]=d'), { a: { b: ['c', 'd'] } });
  71. st.deepEqual(qs.parse('a[>=]=25'), { a: { '>=': '25' } });
  72. st.end();
  73. });
  74. t.test('allows to specify array indices', function (st) {
  75. st.deepEqual(qs.parse('a[1]=c&a[0]=b&a[2]=d'), { a: ['b', 'c', 'd'] });
  76. st.deepEqual(qs.parse('a[1]=c&a[0]=b'), { a: ['b', 'c'] });
  77. st.deepEqual(qs.parse('a[1]=c', { arrayLimit: 20 }), { a: ['c'] });
  78. st.deepEqual(qs.parse('a[1]=c', { arrayLimit: 0 }), { a: { 1: 'c' } });
  79. st.deepEqual(qs.parse('a[1]=c'), { a: ['c'] });
  80. st.end();
  81. });
  82. t.test('limits specific array indices to arrayLimit', function (st) {
  83. st.deepEqual(qs.parse('a[20]=a', { arrayLimit: 20 }), { a: ['a'] });
  84. st.deepEqual(qs.parse('a[21]=a', { arrayLimit: 20 }), { a: { 21: 'a' } });
  85. st.end();
  86. });
  87. t.deepEqual(qs.parse('a[12b]=c'), { a: { '12b': 'c' } }, 'supports keys that begin with a number');
  88. t.test('supports encoded = signs', function (st) {
  89. st.deepEqual(qs.parse('he%3Dllo=th%3Dere'), { 'he=llo': 'th=ere' });
  90. st.end();
  91. });
  92. t.test('is ok with url encoded strings', function (st) {
  93. st.deepEqual(qs.parse('a[b%20c]=d'), { a: { 'b c': 'd' } });
  94. st.deepEqual(qs.parse('a[b]=c%20d'), { a: { b: 'c d' } });
  95. st.end();
  96. });
  97. t.test('allows brackets in the value', function (st) {
  98. st.deepEqual(qs.parse('pets=["tobi"]'), { pets: '["tobi"]' });
  99. st.deepEqual(qs.parse('operators=[">=", "<="]'), { operators: '[">=", "<="]' });
  100. st.end();
  101. });
  102. t.test('allows empty values', function (st) {
  103. st.deepEqual(qs.parse(''), {});
  104. st.deepEqual(qs.parse(null), {});
  105. st.deepEqual(qs.parse(undefined), {});
  106. st.end();
  107. });
  108. t.test('transforms arrays to objects', function (st) {
  109. st.deepEqual(qs.parse('foo[0]=bar&foo[bad]=baz'), { foo: { 0: 'bar', bad: 'baz' } });
  110. st.deepEqual(qs.parse('foo[bad]=baz&foo[0]=bar'), { foo: { bad: 'baz', 0: 'bar' } });
  111. st.deepEqual(qs.parse('foo[bad]=baz&foo[]=bar'), { foo: { bad: 'baz', 0: 'bar' } });
  112. st.deepEqual(qs.parse('foo[]=bar&foo[bad]=baz'), { foo: { 0: 'bar', bad: 'baz' } });
  113. st.deepEqual(qs.parse('foo[bad]=baz&foo[]=bar&foo[]=foo'), { foo: { bad: 'baz', 0: 'bar', 1: 'foo' } });
  114. st.deepEqual(qs.parse('foo[0][a]=a&foo[0][b]=b&foo[1][a]=aa&foo[1][b]=bb'), { foo: [{ a: 'a', b: 'b' }, { a: 'aa', b: 'bb' }] });
  115. st.deepEqual(qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c', { allowPrototypes: false }), { a: { 0: 'b', t: 'u' } });
  116. st.deepEqual(qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c', { allowPrototypes: true }), { a: { 0: 'b', t: 'u', hasOwnProperty: 'c' } });
  117. st.deepEqual(qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y', { allowPrototypes: false }), { a: { 0: 'b', x: 'y' } });
  118. st.deepEqual(qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y', { allowPrototypes: true }), { a: { 0: 'b', hasOwnProperty: 'c', x: 'y' } });
  119. st.end();
  120. });
  121. t.test('transforms arrays to objects (dot notation)', function (st) {
  122. st.deepEqual(qs.parse('foo[0].baz=bar&fool.bad=baz', { allowDots: true }), { foo: [{ baz: 'bar' }], fool: { bad: 'baz' } });
  123. st.deepEqual(qs.parse('foo[0].baz=bar&fool.bad.boo=baz', { allowDots: true }), { foo: [{ baz: 'bar' }], fool: { bad: { boo: 'baz' } } });
  124. st.deepEqual(qs.parse('foo[0][0].baz=bar&fool.bad=baz', { allowDots: true }), { foo: [[{ baz: 'bar' }]], fool: { bad: 'baz' } });
  125. st.deepEqual(qs.parse('foo[0].baz[0]=15&foo[0].bar=2', { allowDots: true }), { foo: [{ baz: ['15'], bar: '2' }] });
  126. st.deepEqual(qs.parse('foo[0].baz[0]=15&foo[0].baz[1]=16&foo[0].bar=2', { allowDots: true }), { foo: [{ baz: ['15', '16'], bar: '2' }] });
  127. st.deepEqual(qs.parse('foo.bad=baz&foo[0]=bar', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar' } });
  128. st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar' } });
  129. st.deepEqual(qs.parse('foo[]=bar&foo.bad=baz', { allowDots: true }), { foo: { 0: 'bar', bad: 'baz' } });
  130. st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar&foo[]=foo', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar', 1: 'foo' } });
  131. st.deepEqual(qs.parse('foo[0].a=a&foo[0].b=b&foo[1].a=aa&foo[1].b=bb', { allowDots: true }), { foo: [{ a: 'a', b: 'b' }, { a: 'aa', b: 'bb' }] });
  132. st.end();
  133. });
  134. t.test('correctly prunes undefined values when converting an array to an object', function (st) {
  135. st.deepEqual(qs.parse('a[2]=b&a[99999999]=c'), { a: { 2: 'b', 99999999: 'c' } });
  136. st.end();
  137. });
  138. t.test('supports malformed uri characters', function (st) {
  139. st.deepEqual(qs.parse('{%:%}', { strictNullHandling: true }), { '{%:%}': null });
  140. st.deepEqual(qs.parse('{%:%}='), { '{%:%}': '' });
  141. st.deepEqual(qs.parse('foo=%:%}'), { foo: '%:%}' });
  142. st.end();
  143. });
  144. t.test('doesn\'t produce empty keys', function (st) {
  145. st.deepEqual(qs.parse('_r=1&'), { _r: '1' });
  146. st.end();
  147. });
  148. t.test('cannot access Object prototype', function (st) {
  149. qs.parse('constructor[prototype][bad]=bad');
  150. qs.parse('bad[constructor][prototype][bad]=bad');
  151. st.equal(typeof Object.prototype.bad, 'undefined');
  152. st.end();
  153. });
  154. t.test('parses arrays of objects', function (st) {
  155. st.deepEqual(qs.parse('a[][b]=c'), { a: [{ b: 'c' }] });
  156. st.deepEqual(qs.parse('a[0][b]=c'), { a: [{ b: 'c' }] });
  157. st.end();
  158. });
  159. t.test('allows for empty strings in arrays', function (st) {
  160. st.deepEqual(qs.parse('a[]=b&a[]=&a[]=c'), { a: ['b', '', 'c'] });
  161. st.deepEqual(
  162. qs.parse('a[0]=b&a[1]&a[2]=c&a[19]=', { strictNullHandling: true, arrayLimit: 20 }),
  163. { a: ['b', null, 'c', ''] },
  164. 'with arrayLimit 20 + array indices: null then empty string works'
  165. );
  166. st.deepEqual(
  167. qs.parse('a[]=b&a[]&a[]=c&a[]=', { strictNullHandling: true, arrayLimit: 0 }),
  168. { a: ['b', null, 'c', ''] },
  169. 'with arrayLimit 0 + array brackets: null then empty string works'
  170. );
  171. st.deepEqual(
  172. qs.parse('a[0]=b&a[1]=&a[2]=c&a[19]', { strictNullHandling: true, arrayLimit: 20 }),
  173. { a: ['b', '', 'c', null] },
  174. 'with arrayLimit 20 + array indices: empty string then null works'
  175. );
  176. st.deepEqual(
  177. qs.parse('a[]=b&a[]=&a[]=c&a[]', { strictNullHandling: true, arrayLimit: 0 }),
  178. { a: ['b', '', 'c', null] },
  179. 'with arrayLimit 0 + array brackets: empty string then null works'
  180. );
  181. st.deepEqual(
  182. qs.parse('a[]=&a[]=b&a[]=c'),
  183. { a: ['', 'b', 'c'] },
  184. 'array brackets: empty strings work'
  185. );
  186. st.end();
  187. });
  188. t.test('compacts sparse arrays', function (st) {
  189. st.deepEqual(qs.parse('a[10]=1&a[2]=2', { arrayLimit: 20 }), { a: ['2', '1'] });
  190. st.deepEqual(qs.parse('a[1][b][2][c]=1', { arrayLimit: 20 }), { a: [{ b: [{ c: '1' }] }] });
  191. st.deepEqual(qs.parse('a[1][2][3][c]=1', { arrayLimit: 20 }), { a: [[[{ c: '1' }]]] });
  192. st.deepEqual(qs.parse('a[1][2][3][c][1]=1', { arrayLimit: 20 }), { a: [[[{ c: ['1'] }]]] });
  193. st.end();
  194. });
  195. t.test('parses semi-parsed strings', function (st) {
  196. st.deepEqual(qs.parse({ 'a[b]': 'c' }), { a: { b: 'c' } });
  197. st.deepEqual(qs.parse({ 'a[b]': 'c', 'a[d]': 'e' }), { a: { b: 'c', d: 'e' } });
  198. st.end();
  199. });
  200. t.test('parses buffers correctly', function (st) {
  201. var b = SaferBuffer.from('test');
  202. st.deepEqual(qs.parse({ a: b }), { a: b });
  203. st.end();
  204. });
  205. t.test('continues parsing when no parent is found', function (st) {
  206. st.deepEqual(qs.parse('[]=&a=b'), { 0: '', a: 'b' });
  207. st.deepEqual(qs.parse('[]&a=b', { strictNullHandling: true }), { 0: null, a: 'b' });
  208. st.deepEqual(qs.parse('[foo]=bar'), { foo: 'bar' });
  209. st.end();
  210. });
  211. t.test('does not error when parsing a very long array', function (st) {
  212. var str = 'a[]=a';
  213. while (Buffer.byteLength(str) < 128 * 1024) {
  214. str = str + '&' + str;
  215. }
  216. st.doesNotThrow(function () {
  217. qs.parse(str);
  218. });
  219. st.end();
  220. });
  221. t.test('should not throw when a native prototype has an enumerable property', function (st) {
  222. Object.prototype.crash = '';
  223. Array.prototype.crash = '';
  224. st.doesNotThrow(qs.parse.bind(null, 'a=b'));
  225. st.deepEqual(qs.parse('a=b'), { a: 'b' });
  226. st.doesNotThrow(qs.parse.bind(null, 'a[][b]=c'));
  227. st.deepEqual(qs.parse('a[][b]=c'), { a: [{ b: 'c' }] });
  228. delete Object.prototype.crash;
  229. delete Array.prototype.crash;
  230. st.end();
  231. });
  232. t.test('parses a string with an alternative string delimiter', function (st) {
  233. st.deepEqual(qs.parse('a=b;c=d', { delimiter: ';' }), { a: 'b', c: 'd' });
  234. st.end();
  235. });
  236. t.test('parses a string with an alternative RegExp delimiter', function (st) {
  237. st.deepEqual(qs.parse('a=b; c=d', { delimiter: /[;,] */ }), { a: 'b', c: 'd' });
  238. st.end();
  239. });
  240. t.test('does not use non-splittable objects as delimiters', function (st) {
  241. st.deepEqual(qs.parse('a=b&c=d', { delimiter: true }), { a: 'b', c: 'd' });
  242. st.end();
  243. });
  244. t.test('allows overriding parameter limit', function (st) {
  245. st.deepEqual(qs.parse('a=b&c=d', { parameterLimit: 1 }), { a: 'b' });
  246. st.end();
  247. });
  248. t.test('allows setting the parameter limit to Infinity', function (st) {
  249. st.deepEqual(qs.parse('a=b&c=d', { parameterLimit: Infinity }), { a: 'b', c: 'd' });
  250. st.end();
  251. });
  252. t.test('allows overriding array limit', function (st) {
  253. st.deepEqual(qs.parse('a[0]=b', { arrayLimit: -1 }), { a: { 0: 'b' } });
  254. st.deepEqual(qs.parse('a[-1]=b', { arrayLimit: -1 }), { a: { '-1': 'b' } });
  255. st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayLimit: 0 }), { a: { 0: 'b', 1: 'c' } });
  256. st.end();
  257. });
  258. t.test('allows disabling array parsing', function (st) {
  259. var indices = qs.parse('a[0]=b&a[1]=c', { parseArrays: false });
  260. st.deepEqual(indices, { a: { 0: 'b', 1: 'c' } });
  261. st.equal(Array.isArray(indices.a), false, 'parseArrays:false, indices case is not an array');
  262. var emptyBrackets = qs.parse('a[]=b', { parseArrays: false });
  263. st.deepEqual(emptyBrackets, { a: { 0: 'b' } });
  264. st.equal(Array.isArray(emptyBrackets.a), false, 'parseArrays:false, empty brackets case is not an array');
  265. st.end();
  266. });
  267. t.test('allows for query string prefix', function (st) {
  268. st.deepEqual(qs.parse('?foo=bar', { ignoreQueryPrefix: true }), { foo: 'bar' });
  269. st.deepEqual(qs.parse('foo=bar', { ignoreQueryPrefix: true }), { foo: 'bar' });
  270. st.deepEqual(qs.parse('?foo=bar', { ignoreQueryPrefix: false }), { '?foo': 'bar' });
  271. st.end();
  272. });
  273. t.test('parses an object', function (st) {
  274. var input = {
  275. 'user[name]': { 'pop[bob]': 3 },
  276. 'user[email]': null
  277. };
  278. var expected = {
  279. user: {
  280. name: { 'pop[bob]': 3 },
  281. email: null
  282. }
  283. };
  284. var result = qs.parse(input);
  285. st.deepEqual(result, expected);
  286. st.end();
  287. });
  288. t.test('parses an object in dot notation', function (st) {
  289. var input = {
  290. 'user.name': { 'pop[bob]': 3 },
  291. 'user.email.': null
  292. };
  293. var expected = {
  294. user: {
  295. name: { 'pop[bob]': 3 },
  296. email: null
  297. }
  298. };
  299. var result = qs.parse(input, { allowDots: true });
  300. st.deepEqual(result, expected);
  301. st.end();
  302. });
  303. t.test('parses an object and not child values', function (st) {
  304. var input = {
  305. 'user[name]': { 'pop[bob]': { test: 3 } },
  306. 'user[email]': null
  307. };
  308. var expected = {
  309. user: {
  310. name: { 'pop[bob]': { test: 3 } },
  311. email: null
  312. }
  313. };
  314. var result = qs.parse(input);
  315. st.deepEqual(result, expected);
  316. st.end();
  317. });
  318. t.test('does not blow up when Buffer global is missing', function (st) {
  319. var tempBuffer = global.Buffer;
  320. delete global.Buffer;
  321. var result = qs.parse('a=b&c=d');
  322. global.Buffer = tempBuffer;
  323. st.deepEqual(result, { a: 'b', c: 'd' });
  324. st.end();
  325. });
  326. t.test('does not crash when parsing circular references', function (st) {
  327. var a = {};
  328. a.b = a;
  329. var parsed;
  330. st.doesNotThrow(function () {
  331. parsed = qs.parse({ 'foo[bar]': 'baz', 'foo[baz]': a });
  332. });
  333. st.equal('foo' in parsed, true, 'parsed has "foo" property');
  334. st.equal('bar' in parsed.foo, true);
  335. st.equal('baz' in parsed.foo, true);
  336. st.equal(parsed.foo.bar, 'baz');
  337. st.deepEqual(parsed.foo.baz, a);
  338. st.end();
  339. });
  340. t.test('does not crash when parsing deep objects', function (st) {
  341. var parsed;
  342. var str = 'foo';
  343. for (var i = 0; i < 5000; i++) {
  344. str += '[p]';
  345. }
  346. str += '=bar';
  347. st.doesNotThrow(function () {
  348. parsed = qs.parse(str, { depth: 5000 });
  349. });
  350. st.equal('foo' in parsed, true, 'parsed has "foo" property');
  351. var depth = 0;
  352. var ref = parsed.foo;
  353. while ((ref = ref.p)) {
  354. depth += 1;
  355. }
  356. st.equal(depth, 5000, 'parsed is 5000 properties deep');
  357. st.end();
  358. });
  359. t.test('parses null objects correctly', { skip: !Object.create }, function (st) {
  360. var a = Object.create(null);
  361. a.b = 'c';
  362. st.deepEqual(qs.parse(a), { b: 'c' });
  363. var result = qs.parse({ a: a });
  364. st.equal('a' in result, true, 'result has "a" property');
  365. st.deepEqual(result.a, a);
  366. st.end();
  367. });
  368. t.test('parses dates correctly', function (st) {
  369. var now = new Date();
  370. st.deepEqual(qs.parse({ a: now }), { a: now });
  371. st.end();
  372. });
  373. t.test('parses regular expressions correctly', function (st) {
  374. var re = /^test$/;
  375. st.deepEqual(qs.parse({ a: re }), { a: re });
  376. st.end();
  377. });
  378. t.test('does not allow overwriting prototype properties', function (st) {
  379. st.deepEqual(qs.parse('a[hasOwnProperty]=b', { allowPrototypes: false }), {});
  380. st.deepEqual(qs.parse('hasOwnProperty=b', { allowPrototypes: false }), {});
  381. st.deepEqual(
  382. qs.parse('toString', { allowPrototypes: false }),
  383. {},
  384. 'bare "toString" results in {}'
  385. );
  386. st.end();
  387. });
  388. t.test('can allow overwriting prototype properties', function (st) {
  389. st.deepEqual(qs.parse('a[hasOwnProperty]=b', { allowPrototypes: true }), { a: { hasOwnProperty: 'b' } });
  390. st.deepEqual(qs.parse('hasOwnProperty=b', { allowPrototypes: true }), { hasOwnProperty: 'b' });
  391. st.deepEqual(
  392. qs.parse('toString', { allowPrototypes: true }),
  393. { toString: '' },
  394. 'bare "toString" results in { toString: "" }'
  395. );
  396. st.end();
  397. });
  398. t.test('params starting with a closing bracket', function (st) {
  399. st.deepEqual(qs.parse(']=toString'), { ']': 'toString' });
  400. st.deepEqual(qs.parse(']]=toString'), { ']]': 'toString' });
  401. st.deepEqual(qs.parse(']hello]=toString'), { ']hello]': 'toString' });
  402. st.end();
  403. });
  404. t.test('params starting with a starting bracket', function (st) {
  405. st.deepEqual(qs.parse('[=toString'), { '[': 'toString' });
  406. st.deepEqual(qs.parse('[[=toString'), { '[[': 'toString' });
  407. st.deepEqual(qs.parse('[hello[=toString'), { '[hello[': 'toString' });
  408. st.end();
  409. });
  410. t.test('add keys to objects', function (st) {
  411. st.deepEqual(
  412. qs.parse('a[b]=c&a=d'),
  413. { a: { b: 'c', d: true } },
  414. 'can add keys to objects'
  415. );
  416. st.deepEqual(
  417. qs.parse('a[b]=c&a=toString'),
  418. { a: { b: 'c' } },
  419. 'can not overwrite prototype'
  420. );
  421. st.deepEqual(
  422. qs.parse('a[b]=c&a=toString', { allowPrototypes: true }),
  423. { a: { b: 'c', toString: true } },
  424. 'can overwrite prototype with allowPrototypes true'
  425. );
  426. st.deepEqual(
  427. qs.parse('a[b]=c&a=toString', { plainObjects: true }),
  428. { a: { b: 'c', toString: true } },
  429. 'can overwrite prototype with plainObjects true'
  430. );
  431. st.end();
  432. });
  433. t.test('can return null objects', { skip: !Object.create }, function (st) {
  434. var expected = Object.create(null);
  435. expected.a = Object.create(null);
  436. expected.a.b = 'c';
  437. expected.a.hasOwnProperty = 'd';
  438. st.deepEqual(qs.parse('a[b]=c&a[hasOwnProperty]=d', { plainObjects: true }), expected);
  439. st.deepEqual(qs.parse(null, { plainObjects: true }), Object.create(null));
  440. var expectedArray = Object.create(null);
  441. expectedArray.a = Object.create(null);
  442. expectedArray.a[0] = 'b';
  443. expectedArray.a.c = 'd';
  444. st.deepEqual(qs.parse('a[]=b&a[c]=d', { plainObjects: true }), expectedArray);
  445. st.end();
  446. });
  447. t.test('can parse with custom encoding', function (st) {
  448. st.deepEqual(qs.parse('%8c%a7=%91%e5%8d%e3%95%7b', {
  449. decoder: function (str) {
  450. var reg = /%([0-9A-F]{2})/ig;
  451. var result = [];
  452. var parts = reg.exec(str);
  453. while (parts) {
  454. result.push(parseInt(parts[1], 16));
  455. parts = reg.exec(str);
  456. }
  457. return iconv.decode(SaferBuffer.from(result), 'shift_jis').toString();
  458. }
  459. }), { 県: '大阪府' });
  460. st.end();
  461. });
  462. t.test('receives the default decoder as a second argument', function (st) {
  463. st.plan(1);
  464. qs.parse('a', {
  465. decoder: function (str, defaultDecoder) {
  466. st.equal(defaultDecoder, utils.decode);
  467. }
  468. });
  469. st.end();
  470. });
  471. t.test('throws error with wrong decoder', function (st) {
  472. st['throws'](function () {
  473. qs.parse({}, { decoder: 'string' });
  474. }, new TypeError('Decoder has to be a function.'));
  475. st.end();
  476. });
  477. t.test('does not mutate the options argument', function (st) {
  478. var options = {};
  479. qs.parse('a[b]=true', options);
  480. st.deepEqual(options, {});
  481. st.end();
  482. });
  483. t.test('throws if an invalid charset is specified', function (st) {
  484. st['throws'](function () {
  485. qs.parse('a=b', { charset: 'foobar' });
  486. }, new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined'));
  487. st.end();
  488. });
  489. t.test('parses an iso-8859-1 string if asked to', function (st) {
  490. st.deepEqual(qs.parse('%A2=%BD', { charset: 'iso-8859-1' }), { '¢': '½' });
  491. st.end();
  492. });
  493. var urlEncodedCheckmarkInUtf8 = '%E2%9C%93';
  494. var urlEncodedOSlashInUtf8 = '%C3%B8';
  495. var urlEncodedNumCheckmark = '%26%2310003%3B';
  496. var urlEncodedNumSmiley = '%26%239786%3B';
  497. t.test('prefers an utf-8 charset specified by the utf8 sentinel to a default charset of iso-8859-1', function (st) {
  498. st.deepEqual(qs.parse('utf8=' + urlEncodedCheckmarkInUtf8 + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'iso-8859-1' }), { ø: 'ø' });
  499. st.end();
  500. });
  501. t.test('prefers an iso-8859-1 charset specified by the utf8 sentinel to a default charset of utf-8', function (st) {
  502. st.deepEqual(qs.parse('utf8=' + urlEncodedNumCheckmark + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'utf-8' }), { 'ø': 'ø' });
  503. st.end();
  504. });
  505. t.test('does not require the utf8 sentinel to be defined before the parameters whose decoding it affects', function (st) {
  506. st.deepEqual(qs.parse('a=' + urlEncodedOSlashInUtf8 + '&utf8=' + urlEncodedNumCheckmark, { charsetSentinel: true, charset: 'utf-8' }), { a: 'ø' });
  507. st.end();
  508. });
  509. t.test('should ignore an utf8 sentinel with an unknown value', function (st) {
  510. st.deepEqual(qs.parse('utf8=foo&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'utf-8' }), { ø: 'ø' });
  511. st.end();
  512. });
  513. t.test('uses the utf8 sentinel to switch to utf-8 when no default charset is given', function (st) {
  514. st.deepEqual(qs.parse('utf8=' + urlEncodedCheckmarkInUtf8 + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true }), { ø: 'ø' });
  515. st.end();
  516. });
  517. t.test('uses the utf8 sentinel to switch to iso-8859-1 when no default charset is given', function (st) {
  518. st.deepEqual(qs.parse('utf8=' + urlEncodedNumCheckmark + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true }), { 'ø': 'ø' });
  519. st.end();
  520. });
  521. t.test('interprets numeric entities in iso-8859-1 when `interpretNumericEntities`', function (st) {
  522. st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'iso-8859-1', interpretNumericEntities: true }), { foo: '☺' });
  523. st.end();
  524. });
  525. t.test('handles a custom decoder returning `null`, in the `iso-8859-1` charset, when `interpretNumericEntities`', function (st) {
  526. st.deepEqual(qs.parse('foo=&bar=' + urlEncodedNumSmiley, {
  527. charset: 'iso-8859-1',
  528. decoder: function (str, defaultDecoder, charset) {
  529. return str ? defaultDecoder(str, defaultDecoder, charset) : null;
  530. },
  531. interpretNumericEntities: true
  532. }), { foo: null, bar: '☺' });
  533. st.end();
  534. });
  535. t.test('does not interpret numeric entities in iso-8859-1 when `interpretNumericEntities` is absent', function (st) {
  536. st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'iso-8859-1' }), { foo: '&#9786;' });
  537. st.end();
  538. });
  539. t.test('does not interpret numeric entities when the charset is utf-8, even when `interpretNumericEntities`', function (st) {
  540. st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'utf-8', interpretNumericEntities: true }), { foo: '&#9786;' });
  541. st.end();
  542. });
  543. t.test('does not interpret %uXXXX syntax in iso-8859-1 mode', function (st) {
  544. st.deepEqual(qs.parse('%u263A=%u263A', { charset: 'iso-8859-1' }), { '%u263A': '%u263A' });
  545. st.end();
  546. });
  547. t.end();
  548. });