parse.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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('arrayFormat: brackets allows only explicit arrays', function (st) {
  33. st.deepEqual(qs.parse('a[]=b&a[]=c', { arrayFormat: 'brackets' }), { a: ['b', 'c'] });
  34. st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayFormat: 'brackets' }), { a: ['b', 'c'] });
  35. st.deepEqual(qs.parse('a=b,c', { arrayFormat: 'brackets' }), { a: 'b,c' });
  36. st.deepEqual(qs.parse('a=b&a=c', { arrayFormat: 'brackets' }), { a: ['b', 'c'] });
  37. st.end();
  38. });
  39. t.test('arrayFormat: indices allows only indexed arrays', function (st) {
  40. st.deepEqual(qs.parse('a[]=b&a[]=c', { arrayFormat: 'indices' }), { a: ['b', 'c'] });
  41. st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayFormat: 'indices' }), { a: ['b', 'c'] });
  42. st.deepEqual(qs.parse('a=b,c', { arrayFormat: 'indices' }), { a: 'b,c' });
  43. st.deepEqual(qs.parse('a=b&a=c', { arrayFormat: 'indices' }), { a: ['b', 'c'] });
  44. st.end();
  45. });
  46. t.test('arrayFormat: comma allows only comma-separated arrays', function (st) {
  47. st.deepEqual(qs.parse('a[]=b&a[]=c', { arrayFormat: 'comma' }), { a: ['b', 'c'] });
  48. st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayFormat: 'comma' }), { a: ['b', 'c'] });
  49. st.deepEqual(qs.parse('a=b,c', { arrayFormat: 'comma' }), { a: 'b,c' });
  50. st.deepEqual(qs.parse('a=b&a=c', { arrayFormat: 'comma' }), { a: ['b', 'c'] });
  51. st.end();
  52. });
  53. t.test('arrayFormat: repeat allows only repeated values', function (st) {
  54. st.deepEqual(qs.parse('a[]=b&a[]=c', { arrayFormat: 'repeat' }), { a: ['b', 'c'] });
  55. st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayFormat: 'repeat' }), { a: ['b', 'c'] });
  56. st.deepEqual(qs.parse('a=b,c', { arrayFormat: 'repeat' }), { a: 'b,c' });
  57. st.deepEqual(qs.parse('a=b&a=c', { arrayFormat: 'repeat' }), { a: ['b', 'c'] });
  58. st.end();
  59. });
  60. t.test('allows enabling dot notation', function (st) {
  61. st.deepEqual(qs.parse('a.b=c'), { 'a.b': 'c' });
  62. st.deepEqual(qs.parse('a.b=c', { allowDots: true }), { a: { b: 'c' } });
  63. st.end();
  64. });
  65. t.deepEqual(qs.parse('a[b]=c'), { a: { b: 'c' } }, 'parses a single nested string');
  66. t.deepEqual(qs.parse('a[b][c]=d'), { a: { b: { c: 'd' } } }, 'parses a double nested string');
  67. t.deepEqual(
  68. qs.parse('a[b][c][d][e][f][g][h]=i'),
  69. { a: { b: { c: { d: { e: { f: { '[g][h]': 'i' } } } } } } },
  70. 'defaults to a depth of 5'
  71. );
  72. t.test('only parses one level when depth = 1', function (st) {
  73. st.deepEqual(qs.parse('a[b][c]=d', { depth: 1 }), { a: { b: { '[c]': 'd' } } });
  74. st.deepEqual(qs.parse('a[b][c][d]=e', { depth: 1 }), { a: { b: { '[c][d]': 'e' } } });
  75. st.end();
  76. });
  77. t.test('uses original key when depth = 0', function (st) {
  78. st.deepEqual(qs.parse('a[0]=b&a[1]=c', { depth: 0 }), { 'a[0]': 'b', 'a[1]': 'c' });
  79. st.deepEqual(qs.parse('a[0][0]=b&a[0][1]=c&a[1]=d&e=2', { depth: 0 }), { 'a[0][0]': 'b', 'a[0][1]': 'c', 'a[1]': 'd', e: '2' });
  80. st.end();
  81. });
  82. t.test('uses original key when depth = false', function (st) {
  83. st.deepEqual(qs.parse('a[0]=b&a[1]=c', { depth: false }), { 'a[0]': 'b', 'a[1]': 'c' });
  84. st.deepEqual(qs.parse('a[0][0]=b&a[0][1]=c&a[1]=d&e=2', { depth: false }), { 'a[0][0]': 'b', 'a[0][1]': 'c', 'a[1]': 'd', e: '2' });
  85. st.end();
  86. });
  87. t.deepEqual(qs.parse('a=b&a=c'), { a: ['b', 'c'] }, 'parses a simple array');
  88. t.test('parses an explicit array', function (st) {
  89. st.deepEqual(qs.parse('a[]=b'), { a: ['b'] });
  90. st.deepEqual(qs.parse('a[]=b&a[]=c'), { a: ['b', 'c'] });
  91. st.deepEqual(qs.parse('a[]=b&a[]=c&a[]=d'), { a: ['b', 'c', 'd'] });
  92. st.end();
  93. });
  94. t.test('parses a mix of simple and explicit arrays', function (st) {
  95. st.deepEqual(qs.parse('a=b&a[]=c'), { a: ['b', 'c'] });
  96. st.deepEqual(qs.parse('a[]=b&a=c'), { a: ['b', 'c'] });
  97. st.deepEqual(qs.parse('a[0]=b&a=c'), { a: ['b', 'c'] });
  98. st.deepEqual(qs.parse('a=b&a[0]=c'), { a: ['b', 'c'] });
  99. st.deepEqual(qs.parse('a[1]=b&a=c', { arrayLimit: 20 }), { a: ['b', 'c'] });
  100. st.deepEqual(qs.parse('a[]=b&a=c', { arrayLimit: 0 }), { a: ['b', 'c'] });
  101. st.deepEqual(qs.parse('a[]=b&a=c'), { a: ['b', 'c'] });
  102. st.deepEqual(qs.parse('a=b&a[1]=c', { arrayLimit: 20 }), { a: ['b', 'c'] });
  103. st.deepEqual(qs.parse('a=b&a[]=c', { arrayLimit: 0 }), { a: ['b', 'c'] });
  104. st.deepEqual(qs.parse('a=b&a[]=c'), { a: ['b', 'c'] });
  105. st.end();
  106. });
  107. t.test('parses a nested array', function (st) {
  108. st.deepEqual(qs.parse('a[b][]=c&a[b][]=d'), { a: { b: ['c', 'd'] } });
  109. st.deepEqual(qs.parse('a[>=]=25'), { a: { '>=': '25' } });
  110. st.end();
  111. });
  112. t.test('allows to specify array indices', function (st) {
  113. st.deepEqual(qs.parse('a[1]=c&a[0]=b&a[2]=d'), { a: ['b', 'c', 'd'] });
  114. st.deepEqual(qs.parse('a[1]=c&a[0]=b'), { a: ['b', 'c'] });
  115. st.deepEqual(qs.parse('a[1]=c', { arrayLimit: 20 }), { a: ['c'] });
  116. st.deepEqual(qs.parse('a[1]=c', { arrayLimit: 0 }), { a: { 1: 'c' } });
  117. st.deepEqual(qs.parse('a[1]=c'), { a: ['c'] });
  118. st.end();
  119. });
  120. t.test('limits specific array indices to arrayLimit', function (st) {
  121. st.deepEqual(qs.parse('a[20]=a', { arrayLimit: 20 }), { a: ['a'] });
  122. st.deepEqual(qs.parse('a[21]=a', { arrayLimit: 20 }), { a: { 21: 'a' } });
  123. st.end();
  124. });
  125. t.deepEqual(qs.parse('a[12b]=c'), { a: { '12b': 'c' } }, 'supports keys that begin with a number');
  126. t.test('supports encoded = signs', function (st) {
  127. st.deepEqual(qs.parse('he%3Dllo=th%3Dere'), { 'he=llo': 'th=ere' });
  128. st.end();
  129. });
  130. t.test('is ok with url encoded strings', function (st) {
  131. st.deepEqual(qs.parse('a[b%20c]=d'), { a: { 'b c': 'd' } });
  132. st.deepEqual(qs.parse('a[b]=c%20d'), { a: { b: 'c d' } });
  133. st.end();
  134. });
  135. t.test('allows brackets in the value', function (st) {
  136. st.deepEqual(qs.parse('pets=["tobi"]'), { pets: '["tobi"]' });
  137. st.deepEqual(qs.parse('operators=[">=", "<="]'), { operators: '[">=", "<="]' });
  138. st.end();
  139. });
  140. t.test('allows empty values', function (st) {
  141. st.deepEqual(qs.parse(''), {});
  142. st.deepEqual(qs.parse(null), {});
  143. st.deepEqual(qs.parse(undefined), {});
  144. st.end();
  145. });
  146. t.test('transforms arrays to objects', function (st) {
  147. st.deepEqual(qs.parse('foo[0]=bar&foo[bad]=baz'), { foo: { 0: 'bar', bad: 'baz' } });
  148. st.deepEqual(qs.parse('foo[bad]=baz&foo[0]=bar'), { foo: { bad: 'baz', 0: 'bar' } });
  149. st.deepEqual(qs.parse('foo[bad]=baz&foo[]=bar'), { foo: { bad: 'baz', 0: 'bar' } });
  150. st.deepEqual(qs.parse('foo[]=bar&foo[bad]=baz'), { foo: { 0: 'bar', bad: 'baz' } });
  151. st.deepEqual(qs.parse('foo[bad]=baz&foo[]=bar&foo[]=foo'), { foo: { bad: 'baz', 0: 'bar', 1: 'foo' } });
  152. 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' }] });
  153. st.deepEqual(qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c', { allowPrototypes: false }), { a: { 0: 'b', t: 'u' } });
  154. st.deepEqual(qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c', { allowPrototypes: true }), { a: { 0: 'b', t: 'u', hasOwnProperty: 'c' } });
  155. st.deepEqual(qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y', { allowPrototypes: false }), { a: { 0: 'b', x: 'y' } });
  156. st.deepEqual(qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y', { allowPrototypes: true }), { a: { 0: 'b', hasOwnProperty: 'c', x: 'y' } });
  157. st.end();
  158. });
  159. t.test('transforms arrays to objects (dot notation)', function (st) {
  160. st.deepEqual(qs.parse('foo[0].baz=bar&fool.bad=baz', { allowDots: true }), { foo: [{ baz: 'bar' }], fool: { bad: 'baz' } });
  161. st.deepEqual(qs.parse('foo[0].baz=bar&fool.bad.boo=baz', { allowDots: true }), { foo: [{ baz: 'bar' }], fool: { bad: { boo: 'baz' } } });
  162. st.deepEqual(qs.parse('foo[0][0].baz=bar&fool.bad=baz', { allowDots: true }), { foo: [[{ baz: 'bar' }]], fool: { bad: 'baz' } });
  163. st.deepEqual(qs.parse('foo[0].baz[0]=15&foo[0].bar=2', { allowDots: true }), { foo: [{ baz: ['15'], bar: '2' }] });
  164. 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' }] });
  165. st.deepEqual(qs.parse('foo.bad=baz&foo[0]=bar', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar' } });
  166. st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar' } });
  167. st.deepEqual(qs.parse('foo[]=bar&foo.bad=baz', { allowDots: true }), { foo: { 0: 'bar', bad: 'baz' } });
  168. st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar&foo[]=foo', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar', 1: 'foo' } });
  169. 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' }] });
  170. st.end();
  171. });
  172. t.test('correctly prunes undefined values when converting an array to an object', function (st) {
  173. st.deepEqual(qs.parse('a[2]=b&a[99999999]=c'), { a: { 2: 'b', 99999999: 'c' } });
  174. st.end();
  175. });
  176. t.test('supports malformed uri characters', function (st) {
  177. st.deepEqual(qs.parse('{%:%}', { strictNullHandling: true }), { '{%:%}': null });
  178. st.deepEqual(qs.parse('{%:%}='), { '{%:%}': '' });
  179. st.deepEqual(qs.parse('foo=%:%}'), { foo: '%:%}' });
  180. st.end();
  181. });
  182. t.test('doesn\'t produce empty keys', function (st) {
  183. st.deepEqual(qs.parse('_r=1&'), { _r: '1' });
  184. st.end();
  185. });
  186. t.test('cannot access Object prototype', function (st) {
  187. qs.parse('constructor[prototype][bad]=bad');
  188. qs.parse('bad[constructor][prototype][bad]=bad');
  189. st.equal(typeof Object.prototype.bad, 'undefined');
  190. st.end();
  191. });
  192. t.test('parses arrays of objects', function (st) {
  193. st.deepEqual(qs.parse('a[][b]=c'), { a: [{ b: 'c' }] });
  194. st.deepEqual(qs.parse('a[0][b]=c'), { a: [{ b: 'c' }] });
  195. st.end();
  196. });
  197. t.test('allows for empty strings in arrays', function (st) {
  198. st.deepEqual(qs.parse('a[]=b&a[]=&a[]=c'), { a: ['b', '', 'c'] });
  199. st.deepEqual(
  200. qs.parse('a[0]=b&a[1]&a[2]=c&a[19]=', { strictNullHandling: true, arrayLimit: 20 }),
  201. { a: ['b', null, 'c', ''] },
  202. 'with arrayLimit 20 + array indices: null then empty string works'
  203. );
  204. st.deepEqual(
  205. qs.parse('a[]=b&a[]&a[]=c&a[]=', { strictNullHandling: true, arrayLimit: 0 }),
  206. { a: ['b', null, 'c', ''] },
  207. 'with arrayLimit 0 + array brackets: null then empty string works'
  208. );
  209. st.deepEqual(
  210. qs.parse('a[0]=b&a[1]=&a[2]=c&a[19]', { strictNullHandling: true, arrayLimit: 20 }),
  211. { a: ['b', '', 'c', null] },
  212. 'with arrayLimit 20 + array indices: empty string then null works'
  213. );
  214. st.deepEqual(
  215. qs.parse('a[]=b&a[]=&a[]=c&a[]', { strictNullHandling: true, arrayLimit: 0 }),
  216. { a: ['b', '', 'c', null] },
  217. 'with arrayLimit 0 + array brackets: empty string then null works'
  218. );
  219. st.deepEqual(
  220. qs.parse('a[]=&a[]=b&a[]=c'),
  221. { a: ['', 'b', 'c'] },
  222. 'array brackets: empty strings work'
  223. );
  224. st.end();
  225. });
  226. t.test('compacts sparse arrays', function (st) {
  227. st.deepEqual(qs.parse('a[10]=1&a[2]=2', { arrayLimit: 20 }), { a: ['2', '1'] });
  228. st.deepEqual(qs.parse('a[1][b][2][c]=1', { arrayLimit: 20 }), { a: [{ b: [{ c: '1' }] }] });
  229. st.deepEqual(qs.parse('a[1][2][3][c]=1', { arrayLimit: 20 }), { a: [[[{ c: '1' }]]] });
  230. st.deepEqual(qs.parse('a[1][2][3][c][1]=1', { arrayLimit: 20 }), { a: [[[{ c: ['1'] }]]] });
  231. st.end();
  232. });
  233. t.test('parses sparse arrays', function (st) {
  234. /* eslint no-sparse-arrays: 0 */
  235. st.deepEqual(qs.parse('a[4]=1&a[1]=2', { allowSparse: true }), { a: [, '2', , , '1'] });
  236. st.deepEqual(qs.parse('a[1][b][2][c]=1', { allowSparse: true }), { a: [, { b: [, , { c: '1' }] }] });
  237. st.deepEqual(qs.parse('a[1][2][3][c]=1', { allowSparse: true }), { a: [, [, , [, , , { c: '1' }]]] });
  238. st.deepEqual(qs.parse('a[1][2][3][c][1]=1', { allowSparse: true }), { a: [, [, , [, , , { c: [, '1'] }]]] });
  239. st.end();
  240. });
  241. t.test('parses semi-parsed strings', function (st) {
  242. st.deepEqual(qs.parse({ 'a[b]': 'c' }), { a: { b: 'c' } });
  243. st.deepEqual(qs.parse({ 'a[b]': 'c', 'a[d]': 'e' }), { a: { b: 'c', d: 'e' } });
  244. st.end();
  245. });
  246. t.test('parses buffers correctly', function (st) {
  247. var b = SaferBuffer.from('test');
  248. st.deepEqual(qs.parse({ a: b }), { a: b });
  249. st.end();
  250. });
  251. t.test('parses jquery-param strings', function (st) {
  252. // readable = 'filter[0][]=int1&filter[0][]==&filter[0][]=77&filter[]=and&filter[2][]=int2&filter[2][]==&filter[2][]=8'
  253. 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';
  254. var expected = { filter: [['int1', '=', '77'], 'and', ['int2', '=', '8']] };
  255. st.deepEqual(qs.parse(encoded), expected);
  256. st.end();
  257. });
  258. t.test('continues parsing when no parent is found', function (st) {
  259. st.deepEqual(qs.parse('[]=&a=b'), { 0: '', a: 'b' });
  260. st.deepEqual(qs.parse('[]&a=b', { strictNullHandling: true }), { 0: null, a: 'b' });
  261. st.deepEqual(qs.parse('[foo]=bar'), { foo: 'bar' });
  262. st.end();
  263. });
  264. t.test('does not error when parsing a very long array', function (st) {
  265. var str = 'a[]=a';
  266. while (Buffer.byteLength(str) < 128 * 1024) {
  267. str = str + '&' + str;
  268. }
  269. st.doesNotThrow(function () {
  270. qs.parse(str);
  271. });
  272. st.end();
  273. });
  274. t.test('should not throw when a native prototype has an enumerable property', function (st) {
  275. Object.prototype.crash = '';
  276. Array.prototype.crash = '';
  277. st.doesNotThrow(qs.parse.bind(null, 'a=b'));
  278. st.deepEqual(qs.parse('a=b'), { a: 'b' });
  279. st.doesNotThrow(qs.parse.bind(null, 'a[][b]=c'));
  280. st.deepEqual(qs.parse('a[][b]=c'), { a: [{ b: 'c' }] });
  281. delete Object.prototype.crash;
  282. delete Array.prototype.crash;
  283. st.end();
  284. });
  285. t.test('parses a string with an alternative string delimiter', function (st) {
  286. st.deepEqual(qs.parse('a=b;c=d', { delimiter: ';' }), { a: 'b', c: 'd' });
  287. st.end();
  288. });
  289. t.test('parses a string with an alternative RegExp delimiter', function (st) {
  290. st.deepEqual(qs.parse('a=b; c=d', { delimiter: /[;,] */ }), { a: 'b', c: 'd' });
  291. st.end();
  292. });
  293. t.test('does not use non-splittable objects as delimiters', function (st) {
  294. st.deepEqual(qs.parse('a=b&c=d', { delimiter: true }), { a: 'b', c: 'd' });
  295. st.end();
  296. });
  297. t.test('allows overriding parameter limit', function (st) {
  298. st.deepEqual(qs.parse('a=b&c=d', { parameterLimit: 1 }), { a: 'b' });
  299. st.end();
  300. });
  301. t.test('allows setting the parameter limit to Infinity', function (st) {
  302. st.deepEqual(qs.parse('a=b&c=d', { parameterLimit: Infinity }), { a: 'b', c: 'd' });
  303. st.end();
  304. });
  305. t.test('allows overriding array limit', function (st) {
  306. st.deepEqual(qs.parse('a[0]=b', { arrayLimit: -1 }), { a: { 0: 'b' } });
  307. st.deepEqual(qs.parse('a[-1]=b', { arrayLimit: -1 }), { a: { '-1': 'b' } });
  308. st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayLimit: 0 }), { a: { 0: 'b', 1: 'c' } });
  309. st.end();
  310. });
  311. t.test('allows disabling array parsing', function (st) {
  312. var indices = qs.parse('a[0]=b&a[1]=c', { parseArrays: false });
  313. st.deepEqual(indices, { a: { 0: 'b', 1: 'c' } });
  314. st.equal(Array.isArray(indices.a), false, 'parseArrays:false, indices case is not an array');
  315. var emptyBrackets = qs.parse('a[]=b', { parseArrays: false });
  316. st.deepEqual(emptyBrackets, { a: { 0: 'b' } });
  317. st.equal(Array.isArray(emptyBrackets.a), false, 'parseArrays:false, empty brackets case is not an array');
  318. st.end();
  319. });
  320. t.test('allows for query string prefix', function (st) {
  321. st.deepEqual(qs.parse('?foo=bar', { ignoreQueryPrefix: true }), { foo: 'bar' });
  322. st.deepEqual(qs.parse('foo=bar', { ignoreQueryPrefix: true }), { foo: 'bar' });
  323. st.deepEqual(qs.parse('?foo=bar', { ignoreQueryPrefix: false }), { '?foo': 'bar' });
  324. st.end();
  325. });
  326. t.test('parses an object', function (st) {
  327. var input = {
  328. 'user[name]': { 'pop[bob]': 3 },
  329. 'user[email]': null
  330. };
  331. var expected = {
  332. user: {
  333. name: { 'pop[bob]': 3 },
  334. email: null
  335. }
  336. };
  337. var result = qs.parse(input);
  338. st.deepEqual(result, expected);
  339. st.end();
  340. });
  341. t.test('parses string with comma as array divider', function (st) {
  342. st.deepEqual(qs.parse('foo=bar,tee', { comma: true }), { foo: ['bar', 'tee'] });
  343. st.deepEqual(qs.parse('foo[bar]=coffee,tee', { comma: true }), { foo: { bar: ['coffee', 'tee'] } });
  344. st.deepEqual(qs.parse('foo=', { comma: true }), { foo: '' });
  345. st.deepEqual(qs.parse('foo', { comma: true }), { foo: '' });
  346. st.deepEqual(qs.parse('foo', { comma: true, strictNullHandling: true }), { foo: null });
  347. st.end();
  348. });
  349. t.test('parses values with comma as array divider', function (st) {
  350. st.deepEqual(qs.parse({ foo: 'bar,tee' }, { comma: false }), { foo: 'bar,tee' });
  351. st.deepEqual(qs.parse({ foo: 'bar,tee' }, { comma: true }), { foo: ['bar', 'tee'] });
  352. st.end();
  353. });
  354. t.test('use number decoder, parses string that has one number with comma option enabled', function (st) {
  355. var decoder = function (str, defaultDecoder, charset, type) {
  356. if (!isNaN(Number(str))) {
  357. return parseFloat(str);
  358. }
  359. return defaultDecoder(str, defaultDecoder, charset, type);
  360. };
  361. st.deepEqual(qs.parse('foo=1', { comma: true, decoder: decoder }), { foo: 1 });
  362. st.deepEqual(qs.parse('foo=0', { comma: true, decoder: decoder }), { foo: 0 });
  363. st.end();
  364. });
  365. t.test('parses brackets holds array of arrays when having two parts of strings with comma as array divider', function (st) {
  366. st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=4,5,6', { comma: true }), { foo: [['1', '2', '3'], ['4', '5', '6']] });
  367. st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=', { comma: true }), { foo: [['1', '2', '3'], ''] });
  368. st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=,', { comma: true }), { foo: [['1', '2', '3'], ['', '']] });
  369. st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=a', { comma: true }), { foo: [['1', '2', '3'], 'a'] });
  370. st.end();
  371. });
  372. t.test('parses comma delimited array while having percent-encoded comma treated as normal text', function (st) {
  373. st.deepEqual(qs.parse('foo=a%2Cb', { comma: true }), { foo: 'a,b' });
  374. st.deepEqual(qs.parse('foo=a%2C%20b,d', { comma: true }), { foo: ['a, b', 'd'] });
  375. st.deepEqual(qs.parse('foo=a%2C%20b,c%2C%20d', { comma: true }), { foo: ['a, b', 'c, d'] });
  376. st.end();
  377. });
  378. t.test('parses an object in dot notation', function (st) {
  379. var input = {
  380. 'user.name': { 'pop[bob]': 3 },
  381. 'user.email.': null
  382. };
  383. var expected = {
  384. user: {
  385. name: { 'pop[bob]': 3 },
  386. email: null
  387. }
  388. };
  389. var result = qs.parse(input, { allowDots: true });
  390. st.deepEqual(result, expected);
  391. st.end();
  392. });
  393. t.test('parses an object and not child values', function (st) {
  394. var input = {
  395. 'user[name]': { 'pop[bob]': { test: 3 } },
  396. 'user[email]': null
  397. };
  398. var expected = {
  399. user: {
  400. name: { 'pop[bob]': { test: 3 } },
  401. email: null
  402. }
  403. };
  404. var result = qs.parse(input);
  405. st.deepEqual(result, expected);
  406. st.end();
  407. });
  408. t.test('does not blow up when Buffer global is missing', function (st) {
  409. var tempBuffer = global.Buffer;
  410. delete global.Buffer;
  411. var result = qs.parse('a=b&c=d');
  412. global.Buffer = tempBuffer;
  413. st.deepEqual(result, { a: 'b', c: 'd' });
  414. st.end();
  415. });
  416. t.test('does not crash when parsing circular references', function (st) {
  417. var a = {};
  418. a.b = a;
  419. var parsed;
  420. st.doesNotThrow(function () {
  421. parsed = qs.parse({ 'foo[bar]': 'baz', 'foo[baz]': a });
  422. });
  423. st.equal('foo' in parsed, true, 'parsed has "foo" property');
  424. st.equal('bar' in parsed.foo, true);
  425. st.equal('baz' in parsed.foo, true);
  426. st.equal(parsed.foo.bar, 'baz');
  427. st.deepEqual(parsed.foo.baz, a);
  428. st.end();
  429. });
  430. t.test('does not crash when parsing deep objects', function (st) {
  431. var parsed;
  432. var str = 'foo';
  433. for (var i = 0; i < 5000; i++) {
  434. str += '[p]';
  435. }
  436. str += '=bar';
  437. st.doesNotThrow(function () {
  438. parsed = qs.parse(str, { depth: 5000 });
  439. });
  440. st.equal('foo' in parsed, true, 'parsed has "foo" property');
  441. var depth = 0;
  442. var ref = parsed.foo;
  443. while ((ref = ref.p)) {
  444. depth += 1;
  445. }
  446. st.equal(depth, 5000, 'parsed is 5000 properties deep');
  447. st.end();
  448. });
  449. t.test('parses null objects correctly', { skip: !Object.create }, function (st) {
  450. var a = Object.create(null);
  451. a.b = 'c';
  452. st.deepEqual(qs.parse(a), { b: 'c' });
  453. var result = qs.parse({ a: a });
  454. st.equal('a' in result, true, 'result has "a" property');
  455. st.deepEqual(result.a, a);
  456. st.end();
  457. });
  458. t.test('parses dates correctly', function (st) {
  459. var now = new Date();
  460. st.deepEqual(qs.parse({ a: now }), { a: now });
  461. st.end();
  462. });
  463. t.test('parses regular expressions correctly', function (st) {
  464. var re = /^test$/;
  465. st.deepEqual(qs.parse({ a: re }), { a: re });
  466. st.end();
  467. });
  468. t.test('does not allow overwriting prototype properties', function (st) {
  469. st.deepEqual(qs.parse('a[hasOwnProperty]=b', { allowPrototypes: false }), {});
  470. st.deepEqual(qs.parse('hasOwnProperty=b', { allowPrototypes: false }), {});
  471. st.deepEqual(
  472. qs.parse('toString', { allowPrototypes: false }),
  473. {},
  474. 'bare "toString" results in {}'
  475. );
  476. st.end();
  477. });
  478. t.test('can allow overwriting prototype properties', function (st) {
  479. st.deepEqual(qs.parse('a[hasOwnProperty]=b', { allowPrototypes: true }), { a: { hasOwnProperty: 'b' } });
  480. st.deepEqual(qs.parse('hasOwnProperty=b', { allowPrototypes: true }), { hasOwnProperty: 'b' });
  481. st.deepEqual(
  482. qs.parse('toString', { allowPrototypes: true }),
  483. { toString: '' },
  484. 'bare "toString" results in { toString: "" }'
  485. );
  486. st.end();
  487. });
  488. t.test('params starting with a closing bracket', function (st) {
  489. st.deepEqual(qs.parse(']=toString'), { ']': 'toString' });
  490. st.deepEqual(qs.parse(']]=toString'), { ']]': 'toString' });
  491. st.deepEqual(qs.parse(']hello]=toString'), { ']hello]': 'toString' });
  492. st.end();
  493. });
  494. t.test('params starting with a starting bracket', function (st) {
  495. st.deepEqual(qs.parse('[=toString'), { '[': 'toString' });
  496. st.deepEqual(qs.parse('[[=toString'), { '[[': 'toString' });
  497. st.deepEqual(qs.parse('[hello[=toString'), { '[hello[': 'toString' });
  498. st.end();
  499. });
  500. t.test('add keys to objects', function (st) {
  501. st.deepEqual(
  502. qs.parse('a[b]=c&a=d'),
  503. { a: { b: 'c', d: true } },
  504. 'can add keys to objects'
  505. );
  506. st.deepEqual(
  507. qs.parse('a[b]=c&a=toString'),
  508. { a: { b: 'c' } },
  509. 'can not overwrite prototype'
  510. );
  511. st.deepEqual(
  512. qs.parse('a[b]=c&a=toString', { allowPrototypes: true }),
  513. { a: { b: 'c', toString: true } },
  514. 'can overwrite prototype with allowPrototypes true'
  515. );
  516. st.deepEqual(
  517. qs.parse('a[b]=c&a=toString', { plainObjects: true }),
  518. { __proto__: null, a: { __proto__: null, b: 'c', toString: true } },
  519. 'can overwrite prototype with plainObjects true'
  520. );
  521. st.end();
  522. });
  523. t.test('dunder proto is ignored', function (st) {
  524. var payload = 'categories[__proto__]=login&categories[__proto__]&categories[length]=42';
  525. var result = qs.parse(payload, { allowPrototypes: true });
  526. st.deepEqual(
  527. result,
  528. {
  529. categories: {
  530. length: '42'
  531. }
  532. },
  533. 'silent [[Prototype]] payload'
  534. );
  535. var plainResult = qs.parse(payload, { allowPrototypes: true, plainObjects: true });
  536. st.deepEqual(
  537. plainResult,
  538. {
  539. __proto__: null,
  540. categories: {
  541. __proto__: null,
  542. length: '42'
  543. }
  544. },
  545. 'silent [[Prototype]] payload: plain objects'
  546. );
  547. var query = qs.parse('categories[__proto__]=cats&categories[__proto__]=dogs&categories[some][json]=toInject', { allowPrototypes: true });
  548. st.notOk(Array.isArray(query.categories), 'is not an array');
  549. st.notOk(query.categories instanceof Array, 'is not instanceof an array');
  550. st.deepEqual(query.categories, { some: { json: 'toInject' } });
  551. st.equal(JSON.stringify(query.categories), '{"some":{"json":"toInject"}}', 'stringifies as a non-array');
  552. st.deepEqual(
  553. qs.parse('foo[__proto__][hidden]=value&foo[bar]=stuffs', { allowPrototypes: true }),
  554. {
  555. foo: {
  556. bar: 'stuffs'
  557. }
  558. },
  559. 'hidden values'
  560. );
  561. st.deepEqual(
  562. qs.parse('foo[__proto__][hidden]=value&foo[bar]=stuffs', { allowPrototypes: true, plainObjects: true }),
  563. {
  564. __proto__: null,
  565. foo: {
  566. __proto__: null,
  567. bar: 'stuffs'
  568. }
  569. },
  570. 'hidden values: plain objects'
  571. );
  572. st.end();
  573. });
  574. t.test('can return null objects', { skip: !Object.create }, function (st) {
  575. var expected = Object.create(null);
  576. expected.a = Object.create(null);
  577. expected.a.b = 'c';
  578. expected.a.hasOwnProperty = 'd';
  579. st.deepEqual(qs.parse('a[b]=c&a[hasOwnProperty]=d', { plainObjects: true }), expected);
  580. st.deepEqual(qs.parse(null, { plainObjects: true }), Object.create(null));
  581. var expectedArray = Object.create(null);
  582. expectedArray.a = Object.create(null);
  583. expectedArray.a[0] = 'b';
  584. expectedArray.a.c = 'd';
  585. st.deepEqual(qs.parse('a[]=b&a[c]=d', { plainObjects: true }), expectedArray);
  586. st.end();
  587. });
  588. t.test('can parse with custom encoding', function (st) {
  589. st.deepEqual(qs.parse('%8c%a7=%91%e5%8d%e3%95%7b', {
  590. decoder: function (str) {
  591. var reg = /%([0-9A-F]{2})/ig;
  592. var result = [];
  593. var parts = reg.exec(str);
  594. while (parts) {
  595. result.push(parseInt(parts[1], 16));
  596. parts = reg.exec(str);
  597. }
  598. return String(iconv.decode(SaferBuffer.from(result), 'shift_jis'));
  599. }
  600. }), { 県: '大阪府' });
  601. st.end();
  602. });
  603. t.test('receives the default decoder as a second argument', function (st) {
  604. st.plan(1);
  605. qs.parse('a', {
  606. decoder: function (str, defaultDecoder) {
  607. st.equal(defaultDecoder, utils.decode);
  608. }
  609. });
  610. st.end();
  611. });
  612. t.test('throws error with wrong decoder', function (st) {
  613. st['throws'](function () {
  614. qs.parse({}, { decoder: 'string' });
  615. }, new TypeError('Decoder has to be a function.'));
  616. st.end();
  617. });
  618. t.test('does not mutate the options argument', function (st) {
  619. var options = {};
  620. qs.parse('a[b]=true', options);
  621. st.deepEqual(options, {});
  622. st.end();
  623. });
  624. t.test('throws if an invalid charset is specified', function (st) {
  625. st['throws'](function () {
  626. qs.parse('a=b', { charset: 'foobar' });
  627. }, new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined'));
  628. st.end();
  629. });
  630. t.test('parses an iso-8859-1 string if asked to', function (st) {
  631. st.deepEqual(qs.parse('%A2=%BD', { charset: 'iso-8859-1' }), { '¢': '½' });
  632. st.end();
  633. });
  634. var urlEncodedCheckmarkInUtf8 = '%E2%9C%93';
  635. var urlEncodedOSlashInUtf8 = '%C3%B8';
  636. var urlEncodedNumCheckmark = '%26%2310003%3B';
  637. var urlEncodedNumSmiley = '%26%239786%3B';
  638. t.test('prefers an utf-8 charset specified by the utf8 sentinel to a default charset of iso-8859-1', function (st) {
  639. st.deepEqual(qs.parse('utf8=' + urlEncodedCheckmarkInUtf8 + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'iso-8859-1' }), { ø: 'ø' });
  640. st.end();
  641. });
  642. t.test('prefers an iso-8859-1 charset specified by the utf8 sentinel to a default charset of utf-8', function (st) {
  643. st.deepEqual(qs.parse('utf8=' + urlEncodedNumCheckmark + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'utf-8' }), { 'ø': 'ø' });
  644. st.end();
  645. });
  646. t.test('does not require the utf8 sentinel to be defined before the parameters whose decoding it affects', function (st) {
  647. st.deepEqual(qs.parse('a=' + urlEncodedOSlashInUtf8 + '&utf8=' + urlEncodedNumCheckmark, { charsetSentinel: true, charset: 'utf-8' }), { a: 'ø' });
  648. st.end();
  649. });
  650. t.test('should ignore an utf8 sentinel with an unknown value', function (st) {
  651. st.deepEqual(qs.parse('utf8=foo&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'utf-8' }), { ø: 'ø' });
  652. st.end();
  653. });
  654. t.test('uses the utf8 sentinel to switch to utf-8 when no default charset is given', function (st) {
  655. st.deepEqual(qs.parse('utf8=' + urlEncodedCheckmarkInUtf8 + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true }), { ø: 'ø' });
  656. st.end();
  657. });
  658. t.test('uses the utf8 sentinel to switch to iso-8859-1 when no default charset is given', function (st) {
  659. st.deepEqual(qs.parse('utf8=' + urlEncodedNumCheckmark + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true }), { 'ø': 'ø' });
  660. st.end();
  661. });
  662. t.test('interprets numeric entities in iso-8859-1 when `interpretNumericEntities`', function (st) {
  663. st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'iso-8859-1', interpretNumericEntities: true }), { foo: '☺' });
  664. st.end();
  665. });
  666. t.test('handles a custom decoder returning `null`, in the `iso-8859-1` charset, when `interpretNumericEntities`', function (st) {
  667. st.deepEqual(qs.parse('foo=&bar=' + urlEncodedNumSmiley, {
  668. charset: 'iso-8859-1',
  669. decoder: function (str, defaultDecoder, charset) {
  670. return str ? defaultDecoder(str, defaultDecoder, charset) : null;
  671. },
  672. interpretNumericEntities: true
  673. }), { foo: null, bar: '☺' });
  674. st.end();
  675. });
  676. t.test('does not interpret numeric entities in iso-8859-1 when `interpretNumericEntities` is absent', function (st) {
  677. st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'iso-8859-1' }), { foo: '&#9786;' });
  678. st.end();
  679. });
  680. t.test('does not interpret numeric entities when the charset is utf-8, even when `interpretNumericEntities`', function (st) {
  681. st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'utf-8', interpretNumericEntities: true }), { foo: '&#9786;' });
  682. st.end();
  683. });
  684. t.test('does not interpret %uXXXX syntax in iso-8859-1 mode', function (st) {
  685. st.deepEqual(qs.parse('%u263A=%u263A', { charset: 'iso-8859-1' }), { '%u263A': '%u263A' });
  686. st.end();
  687. });
  688. t.test('allows for decoding keys and values differently', function (st) {
  689. var decoder = function (str, defaultDecoder, charset, type) {
  690. if (type === 'key') {
  691. return defaultDecoder(str, defaultDecoder, charset, type).toLowerCase();
  692. }
  693. if (type === 'value') {
  694. return defaultDecoder(str, defaultDecoder, charset, type).toUpperCase();
  695. }
  696. throw 'this should never happen! type: ' + type;
  697. };
  698. st.deepEqual(qs.parse('KeY=vAlUe', { decoder: decoder }), { key: 'VALUE' });
  699. st.end();
  700. });
  701. t.end();
  702. });