parse.js 37 KB

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