parse.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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('parses jquery-param strings', function (st) {
  206. // readable = 'filter[0][]=int1&filter[0][]==&filter[0][]=77&filter[]=and&filter[2][]=int2&filter[2][]==&filter[2][]=8'
  207. var encoded = 'filter%5B0%5D%5B%5D=int1&filter%5B0%5D%5B%5D=%3D&filter%5B0%5D%5B%5D=77&filter%5B%5D=and&filter%5B2%5D%5B%5D=int2&filter%5B2%5D%5B%5D=%3D&filter%5B2%5D%5B%5D=8';
  208. var expected = { filter: [['int1', '=', '77'], 'and', ['int2', '=', '8']] };
  209. st.deepEqual(qs.parse(encoded), expected);
  210. st.end();
  211. });
  212. t.test('continues parsing when no parent is found', function (st) {
  213. st.deepEqual(qs.parse('[]=&a=b'), { 0: '', a: 'b' });
  214. st.deepEqual(qs.parse('[]&a=b', { strictNullHandling: true }), { 0: null, a: 'b' });
  215. st.deepEqual(qs.parse('[foo]=bar'), { foo: 'bar' });
  216. st.end();
  217. });
  218. t.test('does not error when parsing a very long array', function (st) {
  219. var str = 'a[]=a';
  220. while (Buffer.byteLength(str) < 128 * 1024) {
  221. str = str + '&' + str;
  222. }
  223. st.doesNotThrow(function () {
  224. qs.parse(str);
  225. });
  226. st.end();
  227. });
  228. t.test('should not throw when a native prototype has an enumerable property', function (st) {
  229. Object.prototype.crash = '';
  230. Array.prototype.crash = '';
  231. st.doesNotThrow(qs.parse.bind(null, 'a=b'));
  232. st.deepEqual(qs.parse('a=b'), { a: 'b' });
  233. st.doesNotThrow(qs.parse.bind(null, 'a[][b]=c'));
  234. st.deepEqual(qs.parse('a[][b]=c'), { a: [{ b: 'c' }] });
  235. delete Object.prototype.crash;
  236. delete Array.prototype.crash;
  237. st.end();
  238. });
  239. t.test('parses a string with an alternative string delimiter', function (st) {
  240. st.deepEqual(qs.parse('a=b;c=d', { delimiter: ';' }), { a: 'b', c: 'd' });
  241. st.end();
  242. });
  243. t.test('parses a string with an alternative RegExp delimiter', function (st) {
  244. st.deepEqual(qs.parse('a=b; c=d', { delimiter: /[;,] */ }), { a: 'b', c: 'd' });
  245. st.end();
  246. });
  247. t.test('does not use non-splittable objects as delimiters', function (st) {
  248. st.deepEqual(qs.parse('a=b&c=d', { delimiter: true }), { a: 'b', c: 'd' });
  249. st.end();
  250. });
  251. t.test('allows overriding parameter limit', function (st) {
  252. st.deepEqual(qs.parse('a=b&c=d', { parameterLimit: 1 }), { a: 'b' });
  253. st.end();
  254. });
  255. t.test('allows setting the parameter limit to Infinity', function (st) {
  256. st.deepEqual(qs.parse('a=b&c=d', { parameterLimit: Infinity }), { a: 'b', c: 'd' });
  257. st.end();
  258. });
  259. t.test('allows overriding array limit', function (st) {
  260. st.deepEqual(qs.parse('a[0]=b', { arrayLimit: -1 }), { a: { 0: 'b' } });
  261. st.deepEqual(qs.parse('a[-1]=b', { arrayLimit: -1 }), { a: { '-1': 'b' } });
  262. st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayLimit: 0 }), { a: { 0: 'b', 1: 'c' } });
  263. st.end();
  264. });
  265. t.test('allows disabling array parsing', function (st) {
  266. var indices = qs.parse('a[0]=b&a[1]=c', { parseArrays: false });
  267. st.deepEqual(indices, { a: { 0: 'b', 1: 'c' } });
  268. st.equal(Array.isArray(indices.a), false, 'parseArrays:false, indices case is not an array');
  269. var emptyBrackets = qs.parse('a[]=b', { parseArrays: false });
  270. st.deepEqual(emptyBrackets, { a: { 0: 'b' } });
  271. st.equal(Array.isArray(emptyBrackets.a), false, 'parseArrays:false, empty brackets case is not an array');
  272. st.end();
  273. });
  274. t.test('allows for query string prefix', function (st) {
  275. st.deepEqual(qs.parse('?foo=bar', { ignoreQueryPrefix: true }), { foo: 'bar' });
  276. st.deepEqual(qs.parse('foo=bar', { ignoreQueryPrefix: true }), { foo: 'bar' });
  277. st.deepEqual(qs.parse('?foo=bar', { ignoreQueryPrefix: false }), { '?foo': 'bar' });
  278. st.end();
  279. });
  280. t.test('parses an object', function (st) {
  281. var input = {
  282. 'user[name]': { 'pop[bob]': 3 },
  283. 'user[email]': null
  284. };
  285. var expected = {
  286. user: {
  287. name: { 'pop[bob]': 3 },
  288. email: null
  289. }
  290. };
  291. var result = qs.parse(input);
  292. st.deepEqual(result, expected);
  293. st.end();
  294. });
  295. t.test('parses an object in dot notation', function (st) {
  296. var input = {
  297. 'user.name': { 'pop[bob]': 3 },
  298. 'user.email.': null
  299. };
  300. var expected = {
  301. user: {
  302. name: { 'pop[bob]': 3 },
  303. email: null
  304. }
  305. };
  306. var result = qs.parse(input, { allowDots: true });
  307. st.deepEqual(result, expected);
  308. st.end();
  309. });
  310. t.test('parses an object and not child values', function (st) {
  311. var input = {
  312. 'user[name]': { 'pop[bob]': { test: 3 } },
  313. 'user[email]': null
  314. };
  315. var expected = {
  316. user: {
  317. name: { 'pop[bob]': { test: 3 } },
  318. email: null
  319. }
  320. };
  321. var result = qs.parse(input);
  322. st.deepEqual(result, expected);
  323. st.end();
  324. });
  325. t.test('does not blow up when Buffer global is missing', function (st) {
  326. var tempBuffer = global.Buffer;
  327. delete global.Buffer;
  328. var result = qs.parse('a=b&c=d');
  329. global.Buffer = tempBuffer;
  330. st.deepEqual(result, { a: 'b', c: 'd' });
  331. st.end();
  332. });
  333. t.test('does not crash when parsing circular references', function (st) {
  334. var a = {};
  335. a.b = a;
  336. var parsed;
  337. st.doesNotThrow(function () {
  338. parsed = qs.parse({ 'foo[bar]': 'baz', 'foo[baz]': a });
  339. });
  340. st.equal('foo' in parsed, true, 'parsed has "foo" property');
  341. st.equal('bar' in parsed.foo, true);
  342. st.equal('baz' in parsed.foo, true);
  343. st.equal(parsed.foo.bar, 'baz');
  344. st.deepEqual(parsed.foo.baz, a);
  345. st.end();
  346. });
  347. t.test('does not crash when parsing deep objects', function (st) {
  348. var parsed;
  349. var str = 'foo';
  350. for (var i = 0; i < 5000; i++) {
  351. str += '[p]';
  352. }
  353. str += '=bar';
  354. st.doesNotThrow(function () {
  355. parsed = qs.parse(str, { depth: 5000 });
  356. });
  357. st.equal('foo' in parsed, true, 'parsed has "foo" property');
  358. var depth = 0;
  359. var ref = parsed.foo;
  360. while ((ref = ref.p)) {
  361. depth += 1;
  362. }
  363. st.equal(depth, 5000, 'parsed is 5000 properties deep');
  364. st.end();
  365. });
  366. t.test('parses null objects correctly', { skip: !Object.create }, function (st) {
  367. var a = Object.create(null);
  368. a.b = 'c';
  369. st.deepEqual(qs.parse(a), { b: 'c' });
  370. var result = qs.parse({ a: a });
  371. st.equal('a' in result, true, 'result has "a" property');
  372. st.deepEqual(result.a, a);
  373. st.end();
  374. });
  375. t.test('parses dates correctly', function (st) {
  376. var now = new Date();
  377. st.deepEqual(qs.parse({ a: now }), { a: now });
  378. st.end();
  379. });
  380. t.test('parses regular expressions correctly', function (st) {
  381. var re = /^test$/;
  382. st.deepEqual(qs.parse({ a: re }), { a: re });
  383. st.end();
  384. });
  385. t.test('does not allow overwriting prototype properties', function (st) {
  386. st.deepEqual(qs.parse('a[hasOwnProperty]=b', { allowPrototypes: false }), {});
  387. st.deepEqual(qs.parse('hasOwnProperty=b', { allowPrototypes: false }), {});
  388. st.deepEqual(
  389. qs.parse('toString', { allowPrototypes: false }),
  390. {},
  391. 'bare "toString" results in {}'
  392. );
  393. st.end();
  394. });
  395. t.test('can allow overwriting prototype properties', function (st) {
  396. st.deepEqual(qs.parse('a[hasOwnProperty]=b', { allowPrototypes: true }), { a: { hasOwnProperty: 'b' } });
  397. st.deepEqual(qs.parse('hasOwnProperty=b', { allowPrototypes: true }), { hasOwnProperty: 'b' });
  398. st.deepEqual(
  399. qs.parse('toString', { allowPrototypes: true }),
  400. { toString: '' },
  401. 'bare "toString" results in { toString: "" }'
  402. );
  403. st.end();
  404. });
  405. t.test('params starting with a closing bracket', function (st) {
  406. st.deepEqual(qs.parse(']=toString'), { ']': 'toString' });
  407. st.deepEqual(qs.parse(']]=toString'), { ']]': 'toString' });
  408. st.deepEqual(qs.parse(']hello]=toString'), { ']hello]': 'toString' });
  409. st.end();
  410. });
  411. t.test('params starting with a starting bracket', function (st) {
  412. st.deepEqual(qs.parse('[=toString'), { '[': 'toString' });
  413. st.deepEqual(qs.parse('[[=toString'), { '[[': 'toString' });
  414. st.deepEqual(qs.parse('[hello[=toString'), { '[hello[': 'toString' });
  415. st.end();
  416. });
  417. t.test('add keys to objects', function (st) {
  418. st.deepEqual(
  419. qs.parse('a[b]=c&a=d'),
  420. { a: { b: 'c', d: true } },
  421. 'can add keys to objects'
  422. );
  423. st.deepEqual(
  424. qs.parse('a[b]=c&a=toString'),
  425. { a: { b: 'c' } },
  426. 'can not overwrite prototype'
  427. );
  428. st.deepEqual(
  429. qs.parse('a[b]=c&a=toString', { allowPrototypes: true }),
  430. { a: { b: 'c', toString: true } },
  431. 'can overwrite prototype with allowPrototypes true'
  432. );
  433. st.deepEqual(
  434. qs.parse('a[b]=c&a=toString', { plainObjects: true }),
  435. { __proto__: null, a: { __proto__: null, b: 'c', toString: true } },
  436. 'can overwrite prototype with plainObjects true'
  437. );
  438. st.end();
  439. });
  440. t.test('dunder proto is ignored', function (st) {
  441. var payload = 'categories[__proto__]=login&categories[__proto__]&categories[length]=42';
  442. var result = qs.parse(payload, { allowPrototypes: true });
  443. st.deepEqual(
  444. result,
  445. {
  446. categories: {
  447. length: '42'
  448. }
  449. },
  450. 'silent [[Prototype]] payload'
  451. );
  452. var plainResult = qs.parse(payload, { allowPrototypes: true, plainObjects: true });
  453. st.deepEqual(
  454. plainResult,
  455. {
  456. __proto__: null,
  457. categories: {
  458. __proto__: null,
  459. length: '42'
  460. }
  461. },
  462. 'silent [[Prototype]] payload: plain objects'
  463. );
  464. var query = qs.parse('categories[__proto__]=cats&categories[__proto__]=dogs&categories[some][json]=toInject', { allowPrototypes: true });
  465. st.notOk(Array.isArray(query.categories), 'is not an array');
  466. st.notOk(query.categories instanceof Array, 'is not instanceof an array');
  467. st.deepEqual(query.categories, { some: { json: 'toInject' } });
  468. st.equal(JSON.stringify(query.categories), '{"some":{"json":"toInject"}}', 'stringifies as a non-array');
  469. st.deepEqual(
  470. qs.parse('foo[__proto__][hidden]=value&foo[bar]=stuffs', { allowPrototypes: true }),
  471. {
  472. foo: {
  473. bar: 'stuffs'
  474. }
  475. },
  476. 'hidden values'
  477. );
  478. st.deepEqual(
  479. qs.parse('foo[__proto__][hidden]=value&foo[bar]=stuffs', { allowPrototypes: true, plainObjects: true }),
  480. {
  481. __proto__: null,
  482. foo: {
  483. __proto__: null,
  484. bar: 'stuffs'
  485. }
  486. },
  487. 'hidden values: plain objects'
  488. );
  489. st.end();
  490. });
  491. t.test('can return null objects', { skip: !Object.create }, function (st) {
  492. var expected = Object.create(null);
  493. expected.a = Object.create(null);
  494. expected.a.b = 'c';
  495. expected.a.hasOwnProperty = 'd';
  496. st.deepEqual(qs.parse('a[b]=c&a[hasOwnProperty]=d', { plainObjects: true }), expected);
  497. st.deepEqual(qs.parse(null, { plainObjects: true }), Object.create(null));
  498. var expectedArray = Object.create(null);
  499. expectedArray.a = Object.create(null);
  500. expectedArray.a[0] = 'b';
  501. expectedArray.a.c = 'd';
  502. st.deepEqual(qs.parse('a[]=b&a[c]=d', { plainObjects: true }), expectedArray);
  503. st.end();
  504. });
  505. t.test('can parse with custom encoding', function (st) {
  506. st.deepEqual(qs.parse('%8c%a7=%91%e5%8d%e3%95%7b', {
  507. decoder: function (str) {
  508. var reg = /%([0-9A-F]{2})/ig;
  509. var result = [];
  510. var parts = reg.exec(str);
  511. while (parts) {
  512. result.push(parseInt(parts[1], 16));
  513. parts = reg.exec(str);
  514. }
  515. return String(iconv.decode(SaferBuffer.from(result), 'shift_jis'));
  516. }
  517. }), { 県: '大阪府' });
  518. st.end();
  519. });
  520. t.test('receives the default decoder as a second argument', function (st) {
  521. st.plan(1);
  522. qs.parse('a', {
  523. decoder: function (str, defaultDecoder) {
  524. st.equal(defaultDecoder, utils.decode);
  525. }
  526. });
  527. st.end();
  528. });
  529. t.test('throws error with wrong decoder', function (st) {
  530. st['throws'](function () {
  531. qs.parse({}, { decoder: 'string' });
  532. }, new TypeError('Decoder has to be a function.'));
  533. st.end();
  534. });
  535. t.test('does not mutate the options argument', function (st) {
  536. var options = {};
  537. qs.parse('a[b]=true', options);
  538. st.deepEqual(options, {});
  539. st.end();
  540. });
  541. t.end();
  542. });