parse.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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', { parallel: false }, 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. st.deepEqual(qs.parse('a[0]=b&a[1]=c', { parseArrays: false }), { a: { 0: 'b', 1: 'c' } });
  260. st.end();
  261. });
  262. t.test('allows for query string prefix', function (st) {
  263. st.deepEqual(qs.parse('?foo=bar', { ignoreQueryPrefix: true }), { foo: 'bar' });
  264. st.deepEqual(qs.parse('foo=bar', { ignoreQueryPrefix: true }), { foo: 'bar' });
  265. st.deepEqual(qs.parse('?foo=bar', { ignoreQueryPrefix: false }), { '?foo': 'bar' });
  266. st.end();
  267. });
  268. t.test('parses an object', function (st) {
  269. var input = {
  270. 'user[name]': { 'pop[bob]': 3 },
  271. 'user[email]': null
  272. };
  273. var expected = {
  274. user: {
  275. name: { 'pop[bob]': 3 },
  276. email: null
  277. }
  278. };
  279. var result = qs.parse(input);
  280. st.deepEqual(result, expected);
  281. st.end();
  282. });
  283. t.test('parses an object in dot notation', function (st) {
  284. var input = {
  285. 'user.name': { 'pop[bob]': 3 },
  286. 'user.email.': null
  287. };
  288. var expected = {
  289. user: {
  290. name: { 'pop[bob]': 3 },
  291. email: null
  292. }
  293. };
  294. var result = qs.parse(input, { allowDots: true });
  295. st.deepEqual(result, expected);
  296. st.end();
  297. });
  298. t.test('parses an object and not child values', function (st) {
  299. var input = {
  300. 'user[name]': { 'pop[bob]': { test: 3 } },
  301. 'user[email]': null
  302. };
  303. var expected = {
  304. user: {
  305. name: { 'pop[bob]': { test: 3 } },
  306. email: null
  307. }
  308. };
  309. var result = qs.parse(input);
  310. st.deepEqual(result, expected);
  311. st.end();
  312. });
  313. t.test('does not blow up when Buffer global is missing', function (st) {
  314. var tempBuffer = global.Buffer;
  315. delete global.Buffer;
  316. var result = qs.parse('a=b&c=d');
  317. global.Buffer = tempBuffer;
  318. st.deepEqual(result, { a: 'b', c: 'd' });
  319. st.end();
  320. });
  321. t.test('does not crash when parsing circular references', function (st) {
  322. var a = {};
  323. a.b = a;
  324. var parsed;
  325. st.doesNotThrow(function () {
  326. parsed = qs.parse({ 'foo[bar]': 'baz', 'foo[baz]': a });
  327. });
  328. st.equal('foo' in parsed, true, 'parsed has "foo" property');
  329. st.equal('bar' in parsed.foo, true);
  330. st.equal('baz' in parsed.foo, true);
  331. st.equal(parsed.foo.bar, 'baz');
  332. st.deepEqual(parsed.foo.baz, a);
  333. st.end();
  334. });
  335. t.test('does not crash when parsing deep objects', function (st) {
  336. var parsed;
  337. var str = 'foo';
  338. for (var i = 0; i < 5000; i++) {
  339. str += '[p]';
  340. }
  341. str += '=bar';
  342. st.doesNotThrow(function () {
  343. parsed = qs.parse(str, { depth: 5000 });
  344. });
  345. st.equal('foo' in parsed, true, 'parsed has "foo" property');
  346. var depth = 0;
  347. var ref = parsed.foo;
  348. while ((ref = ref.p)) {
  349. depth += 1;
  350. }
  351. st.equal(depth, 5000, 'parsed is 5000 properties deep');
  352. st.end();
  353. });
  354. t.test('parses null objects correctly', { skip: !Object.create }, function (st) {
  355. var a = Object.create(null);
  356. a.b = 'c';
  357. st.deepEqual(qs.parse(a), { b: 'c' });
  358. var result = qs.parse({ a: a });
  359. st.equal('a' in result, true, 'result has "a" property');
  360. st.deepEqual(result.a, a);
  361. st.end();
  362. });
  363. t.test('parses dates correctly', function (st) {
  364. var now = new Date();
  365. st.deepEqual(qs.parse({ a: now }), { a: now });
  366. st.end();
  367. });
  368. t.test('parses regular expressions correctly', function (st) {
  369. var re = /^test$/;
  370. st.deepEqual(qs.parse({ a: re }), { a: re });
  371. st.end();
  372. });
  373. t.test('does not allow overwriting prototype properties', function (st) {
  374. st.deepEqual(qs.parse('a[hasOwnProperty]=b', { allowPrototypes: false }), {});
  375. st.deepEqual(qs.parse('hasOwnProperty=b', { allowPrototypes: false }), {});
  376. st.deepEqual(
  377. qs.parse('toString', { allowPrototypes: false }),
  378. {},
  379. 'bare "toString" results in {}'
  380. );
  381. st.end();
  382. });
  383. t.test('can allow overwriting prototype properties', function (st) {
  384. st.deepEqual(qs.parse('a[hasOwnProperty]=b', { allowPrototypes: true }), { a: { hasOwnProperty: 'b' } });
  385. st.deepEqual(qs.parse('hasOwnProperty=b', { allowPrototypes: true }), { hasOwnProperty: 'b' });
  386. st.deepEqual(
  387. qs.parse('toString', { allowPrototypes: true }),
  388. { toString: '' },
  389. 'bare "toString" results in { toString: "" }'
  390. );
  391. st.end();
  392. });
  393. t.test('params starting with a closing bracket', function (st) {
  394. st.deepEqual(qs.parse(']=toString'), { ']': 'toString' });
  395. st.deepEqual(qs.parse(']]=toString'), { ']]': 'toString' });
  396. st.deepEqual(qs.parse(']hello]=toString'), { ']hello]': 'toString' });
  397. st.end();
  398. });
  399. t.test('params starting with a starting bracket', function (st) {
  400. st.deepEqual(qs.parse('[=toString'), { '[': 'toString' });
  401. st.deepEqual(qs.parse('[[=toString'), { '[[': 'toString' });
  402. st.deepEqual(qs.parse('[hello[=toString'), { '[hello[': 'toString' });
  403. st.end();
  404. });
  405. t.test('add keys to objects', function (st) {
  406. st.deepEqual(
  407. qs.parse('a[b]=c&a=d'),
  408. { a: { b: 'c', d: true } },
  409. 'can add keys to objects'
  410. );
  411. st.deepEqual(
  412. qs.parse('a[b]=c&a=toString'),
  413. { a: { b: 'c' } },
  414. 'can not overwrite prototype'
  415. );
  416. st.deepEqual(
  417. qs.parse('a[b]=c&a=toString', { allowPrototypes: true }),
  418. { a: { b: 'c', toString: true } },
  419. 'can overwrite prototype with allowPrototypes true'
  420. );
  421. st.deepEqual(
  422. qs.parse('a[b]=c&a=toString', { plainObjects: true }),
  423. { a: { b: 'c', toString: true } },
  424. 'can overwrite prototype with plainObjects true'
  425. );
  426. st.end();
  427. });
  428. t.test('can return null objects', { skip: !Object.create }, function (st) {
  429. var expected = Object.create(null);
  430. expected.a = Object.create(null);
  431. expected.a.b = 'c';
  432. expected.a.hasOwnProperty = 'd';
  433. st.deepEqual(qs.parse('a[b]=c&a[hasOwnProperty]=d', { plainObjects: true }), expected);
  434. st.deepEqual(qs.parse(null, { plainObjects: true }), Object.create(null));
  435. var expectedArray = Object.create(null);
  436. expectedArray.a = Object.create(null);
  437. expectedArray.a[0] = 'b';
  438. expectedArray.a.c = 'd';
  439. st.deepEqual(qs.parse('a[]=b&a[c]=d', { plainObjects: true }), expectedArray);
  440. st.end();
  441. });
  442. t.test('can parse with custom encoding', function (st) {
  443. st.deepEqual(qs.parse('%8c%a7=%91%e5%8d%e3%95%7b', {
  444. decoder: function (str) {
  445. var reg = /%([0-9A-F]{2})/ig;
  446. var result = [];
  447. var parts = reg.exec(str);
  448. while (parts) {
  449. result.push(parseInt(parts[1], 16));
  450. parts = reg.exec(str);
  451. }
  452. return iconv.decode(SaferBuffer.from(result), 'shift_jis').toString();
  453. }
  454. }), { 県: '大阪府' });
  455. st.end();
  456. });
  457. t.test('receives the default decoder as a second argument', function (st) {
  458. st.plan(1);
  459. qs.parse('a', {
  460. decoder: function (str, defaultDecoder) {
  461. st.equal(defaultDecoder, utils.decode);
  462. }
  463. });
  464. st.end();
  465. });
  466. t.test('throws error with wrong decoder', function (st) {
  467. st['throws'](function () {
  468. qs.parse({}, { decoder: 'string' });
  469. }, new TypeError('Decoder has to be a function.'));
  470. st.end();
  471. });
  472. t.test('does not mutate the options argument', function (st) {
  473. var options = {};
  474. qs.parse('a[b]=true', options);
  475. st.deepEqual(options, {});
  476. st.end();
  477. });
  478. t.end();
  479. });