parse.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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 semi-parsed strings', function (st) {
  234. st.deepEqual(qs.parse({ 'a[b]': 'c' }), { a: { b: 'c' } });
  235. st.deepEqual(qs.parse({ 'a[b]': 'c', 'a[d]': 'e' }), { a: { b: 'c', d: 'e' } });
  236. st.end();
  237. });
  238. t.test('parses buffers correctly', function (st) {
  239. var b = SaferBuffer.from('test');
  240. st.deepEqual(qs.parse({ a: b }), { a: b });
  241. st.end();
  242. });
  243. t.test('parses jquery-param strings', function (st) {
  244. // readable = 'filter[0][]=int1&filter[0][]==&filter[0][]=77&filter[]=and&filter[2][]=int2&filter[2][]==&filter[2][]=8'
  245. 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';
  246. var expected = { filter: [['int1', '=', '77'], 'and', ['int2', '=', '8']] };
  247. st.deepEqual(qs.parse(encoded), expected);
  248. st.end();
  249. });
  250. t.test('continues parsing when no parent is found', function (st) {
  251. st.deepEqual(qs.parse('[]=&a=b'), { 0: '', a: 'b' });
  252. st.deepEqual(qs.parse('[]&a=b', { strictNullHandling: true }), { 0: null, a: 'b' });
  253. st.deepEqual(qs.parse('[foo]=bar'), { foo: 'bar' });
  254. st.end();
  255. });
  256. t.test('does not error when parsing a very long array', function (st) {
  257. var str = 'a[]=a';
  258. while (Buffer.byteLength(str) < 128 * 1024) {
  259. str = str + '&' + str;
  260. }
  261. st.doesNotThrow(function () {
  262. qs.parse(str);
  263. });
  264. st.end();
  265. });
  266. t.test('should not throw when a native prototype has an enumerable property', function (st) {
  267. Object.prototype.crash = '';
  268. Array.prototype.crash = '';
  269. st.doesNotThrow(qs.parse.bind(null, 'a=b'));
  270. st.deepEqual(qs.parse('a=b'), { a: 'b' });
  271. st.doesNotThrow(qs.parse.bind(null, 'a[][b]=c'));
  272. st.deepEqual(qs.parse('a[][b]=c'), { a: [{ b: 'c' }] });
  273. delete Object.prototype.crash;
  274. delete Array.prototype.crash;
  275. st.end();
  276. });
  277. t.test('parses a string with an alternative string delimiter', function (st) {
  278. st.deepEqual(qs.parse('a=b;c=d', { delimiter: ';' }), { a: 'b', c: 'd' });
  279. st.end();
  280. });
  281. t.test('parses a string with an alternative RegExp delimiter', function (st) {
  282. st.deepEqual(qs.parse('a=b; c=d', { delimiter: /[;,] */ }), { a: 'b', c: 'd' });
  283. st.end();
  284. });
  285. t.test('does not use non-splittable objects as delimiters', function (st) {
  286. st.deepEqual(qs.parse('a=b&c=d', { delimiter: true }), { a: 'b', c: 'd' });
  287. st.end();
  288. });
  289. t.test('allows overriding parameter limit', function (st) {
  290. st.deepEqual(qs.parse('a=b&c=d', { parameterLimit: 1 }), { a: 'b' });
  291. st.end();
  292. });
  293. t.test('allows setting the parameter limit to Infinity', function (st) {
  294. st.deepEqual(qs.parse('a=b&c=d', { parameterLimit: Infinity }), { a: 'b', c: 'd' });
  295. st.end();
  296. });
  297. t.test('allows overriding array limit', function (st) {
  298. st.deepEqual(qs.parse('a[0]=b', { arrayLimit: -1 }), { a: { 0: 'b' } });
  299. st.deepEqual(qs.parse('a[-1]=b', { arrayLimit: -1 }), { a: { '-1': 'b' } });
  300. st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayLimit: 0 }), { a: { 0: 'b', 1: 'c' } });
  301. st.end();
  302. });
  303. t.test('allows disabling array parsing', function (st) {
  304. var indices = qs.parse('a[0]=b&a[1]=c', { parseArrays: false });
  305. st.deepEqual(indices, { a: { 0: 'b', 1: 'c' } });
  306. st.equal(Array.isArray(indices.a), false, 'parseArrays:false, indices case is not an array');
  307. var emptyBrackets = qs.parse('a[]=b', { parseArrays: false });
  308. st.deepEqual(emptyBrackets, { a: { 0: 'b' } });
  309. st.equal(Array.isArray(emptyBrackets.a), false, 'parseArrays:false, empty brackets case is not an array');
  310. st.end();
  311. });
  312. t.test('allows for query string prefix', function (st) {
  313. st.deepEqual(qs.parse('?foo=bar', { ignoreQueryPrefix: true }), { foo: 'bar' });
  314. st.deepEqual(qs.parse('foo=bar', { ignoreQueryPrefix: true }), { foo: 'bar' });
  315. st.deepEqual(qs.parse('?foo=bar', { ignoreQueryPrefix: false }), { '?foo': 'bar' });
  316. st.end();
  317. });
  318. t.test('parses an object', function (st) {
  319. var input = {
  320. 'user[name]': { 'pop[bob]': 3 },
  321. 'user[email]': null
  322. };
  323. var expected = {
  324. user: {
  325. name: { 'pop[bob]': 3 },
  326. email: null
  327. }
  328. };
  329. var result = qs.parse(input);
  330. st.deepEqual(result, expected);
  331. st.end();
  332. });
  333. t.test('parses string with comma as array divider', function (st) {
  334. st.deepEqual(qs.parse('foo=bar,tee', { comma: true }), { foo: ['bar', 'tee'] });
  335. st.deepEqual(qs.parse('foo[bar]=coffee,tee', { comma: true }), { foo: { bar: ['coffee', 'tee'] } });
  336. st.deepEqual(qs.parse('foo=', { comma: true }), { foo: '' });
  337. st.deepEqual(qs.parse('foo', { comma: true }), { foo: '' });
  338. st.deepEqual(qs.parse('foo', { comma: true, strictNullHandling: true }), { foo: null });
  339. st.end();
  340. });
  341. t.test('parses values with comma as array divider', function (st) {
  342. st.deepEqual(qs.parse({ foo: 'bar,tee' }, { comma: false }), { foo: 'bar,tee' });
  343. st.deepEqual(qs.parse({ foo: 'bar,tee' }, { comma: true }), { foo: ['bar', 'tee'] });
  344. st.end();
  345. });
  346. t.test('use number decoder, parses string that has one number with comma option enabled', function (st) {
  347. var decoder = function (str, defaultDecoder, charset, type) {
  348. if (!isNaN(Number(str))) {
  349. return parseFloat(str);
  350. }
  351. return defaultDecoder(str, defaultDecoder, charset, type);
  352. };
  353. st.deepEqual(qs.parse('foo=1', { comma: true, decoder: decoder }), { foo: 1 });
  354. st.deepEqual(qs.parse('foo=0', { comma: true, decoder: decoder }), { foo: 0 });
  355. st.end();
  356. });
  357. t.test('parses brackets holds array of arrays when having two parts of strings with comma as array divider', function (st) {
  358. st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=4,5,6', { comma: true }), { foo: [['1', '2', '3'], ['4', '5', '6']] });
  359. st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=', { comma: true }), { foo: [['1', '2', '3'], ''] });
  360. st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=,', { comma: true }), { foo: [['1', '2', '3'], ['', '']] });
  361. st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=a', { comma: true }), { foo: [['1', '2', '3'], 'a'] });
  362. st.end();
  363. });
  364. t.test('parses comma delimited array while having percent-encoded comma treated as normal text', function (st) {
  365. st.deepEqual(qs.parse('foo=a%2Cb', { comma: true }), { foo: 'a,b' });
  366. st.deepEqual(qs.parse('foo=a%2C%20b,d', { comma: true }), { foo: ['a, b', 'd'] });
  367. st.deepEqual(qs.parse('foo=a%2C%20b,c%2C%20d', { comma: true }), { foo: ['a, b', 'c, d'] });
  368. st.end();
  369. });
  370. t.test('parses an object in dot notation', function (st) {
  371. var input = {
  372. 'user.name': { 'pop[bob]': 3 },
  373. 'user.email.': null
  374. };
  375. var expected = {
  376. user: {
  377. name: { 'pop[bob]': 3 },
  378. email: null
  379. }
  380. };
  381. var result = qs.parse(input, { allowDots: true });
  382. st.deepEqual(result, expected);
  383. st.end();
  384. });
  385. t.test('parses an object and not child values', function (st) {
  386. var input = {
  387. 'user[name]': { 'pop[bob]': { test: 3 } },
  388. 'user[email]': null
  389. };
  390. var expected = {
  391. user: {
  392. name: { 'pop[bob]': { test: 3 } },
  393. email: null
  394. }
  395. };
  396. var result = qs.parse(input);
  397. st.deepEqual(result, expected);
  398. st.end();
  399. });
  400. t.test('does not blow up when Buffer global is missing', function (st) {
  401. var tempBuffer = global.Buffer;
  402. delete global.Buffer;
  403. var result = qs.parse('a=b&c=d');
  404. global.Buffer = tempBuffer;
  405. st.deepEqual(result, { a: 'b', c: 'd' });
  406. st.end();
  407. });
  408. t.test('does not crash when parsing circular references', function (st) {
  409. var a = {};
  410. a.b = a;
  411. var parsed;
  412. st.doesNotThrow(function () {
  413. parsed = qs.parse({ 'foo[bar]': 'baz', 'foo[baz]': a });
  414. });
  415. st.equal('foo' in parsed, true, 'parsed has "foo" property');
  416. st.equal('bar' in parsed.foo, true);
  417. st.equal('baz' in parsed.foo, true);
  418. st.equal(parsed.foo.bar, 'baz');
  419. st.deepEqual(parsed.foo.baz, a);
  420. st.end();
  421. });
  422. t.test('does not crash when parsing deep objects', function (st) {
  423. var parsed;
  424. var str = 'foo';
  425. for (var i = 0; i < 5000; i++) {
  426. str += '[p]';
  427. }
  428. str += '=bar';
  429. st.doesNotThrow(function () {
  430. parsed = qs.parse(str, { depth: 5000 });
  431. });
  432. st.equal('foo' in parsed, true, 'parsed has "foo" property');
  433. var depth = 0;
  434. var ref = parsed.foo;
  435. while ((ref = ref.p)) {
  436. depth += 1;
  437. }
  438. st.equal(depth, 5000, 'parsed is 5000 properties deep');
  439. st.end();
  440. });
  441. t.test('parses null objects correctly', { skip: !Object.create }, function (st) {
  442. var a = Object.create(null);
  443. a.b = 'c';
  444. st.deepEqual(qs.parse(a), { b: 'c' });
  445. var result = qs.parse({ a: a });
  446. st.equal('a' in result, true, 'result has "a" property');
  447. st.deepEqual(result.a, a);
  448. st.end();
  449. });
  450. t.test('parses dates correctly', function (st) {
  451. var now = new Date();
  452. st.deepEqual(qs.parse({ a: now }), { a: now });
  453. st.end();
  454. });
  455. t.test('parses regular expressions correctly', function (st) {
  456. var re = /^test$/;
  457. st.deepEqual(qs.parse({ a: re }), { a: re });
  458. st.end();
  459. });
  460. t.test('does not allow overwriting prototype properties', function (st) {
  461. st.deepEqual(qs.parse('a[hasOwnProperty]=b', { allowPrototypes: false }), {});
  462. st.deepEqual(qs.parse('hasOwnProperty=b', { allowPrototypes: false }), {});
  463. st.deepEqual(
  464. qs.parse('toString', { allowPrototypes: false }),
  465. {},
  466. 'bare "toString" results in {}'
  467. );
  468. st.end();
  469. });
  470. t.test('can allow overwriting prototype properties', function (st) {
  471. st.deepEqual(qs.parse('a[hasOwnProperty]=b', { allowPrototypes: true }), { a: { hasOwnProperty: 'b' } });
  472. st.deepEqual(qs.parse('hasOwnProperty=b', { allowPrototypes: true }), { hasOwnProperty: 'b' });
  473. st.deepEqual(
  474. qs.parse('toString', { allowPrototypes: true }),
  475. { toString: '' },
  476. 'bare "toString" results in { toString: "" }'
  477. );
  478. st.end();
  479. });
  480. t.test('params starting with a closing bracket', function (st) {
  481. st.deepEqual(qs.parse(']=toString'), { ']': 'toString' });
  482. st.deepEqual(qs.parse(']]=toString'), { ']]': 'toString' });
  483. st.deepEqual(qs.parse(']hello]=toString'), { ']hello]': 'toString' });
  484. st.end();
  485. });
  486. t.test('params starting with a starting bracket', function (st) {
  487. st.deepEqual(qs.parse('[=toString'), { '[': 'toString' });
  488. st.deepEqual(qs.parse('[[=toString'), { '[[': 'toString' });
  489. st.deepEqual(qs.parse('[hello[=toString'), { '[hello[': 'toString' });
  490. st.end();
  491. });
  492. t.test('add keys to objects', function (st) {
  493. st.deepEqual(
  494. qs.parse('a[b]=c&a=d'),
  495. { a: { b: 'c', d: true } },
  496. 'can add keys to objects'
  497. );
  498. st.deepEqual(
  499. qs.parse('a[b]=c&a=toString'),
  500. { a: { b: 'c' } },
  501. 'can not overwrite prototype'
  502. );
  503. st.deepEqual(
  504. qs.parse('a[b]=c&a=toString', { allowPrototypes: true }),
  505. { a: { b: 'c', toString: true } },
  506. 'can overwrite prototype with allowPrototypes true'
  507. );
  508. st.deepEqual(
  509. qs.parse('a[b]=c&a=toString', { plainObjects: true }),
  510. { __proto__: null, a: { __proto__: null, b: 'c', toString: true } },
  511. 'can overwrite prototype with plainObjects true'
  512. );
  513. st.end();
  514. });
  515. t.test('can return null objects', { skip: !Object.create }, function (st) {
  516. var expected = Object.create(null);
  517. expected.a = Object.create(null);
  518. expected.a.b = 'c';
  519. expected.a.hasOwnProperty = 'd';
  520. st.deepEqual(qs.parse('a[b]=c&a[hasOwnProperty]=d', { plainObjects: true }), expected);
  521. st.deepEqual(qs.parse(null, { plainObjects: true }), Object.create(null));
  522. var expectedArray = Object.create(null);
  523. expectedArray.a = Object.create(null);
  524. expectedArray.a[0] = 'b';
  525. expectedArray.a.c = 'd';
  526. st.deepEqual(qs.parse('a[]=b&a[c]=d', { plainObjects: true }), expectedArray);
  527. st.end();
  528. });
  529. t.test('can parse with custom encoding', function (st) {
  530. st.deepEqual(qs.parse('%8c%a7=%91%e5%8d%e3%95%7b', {
  531. decoder: function (str) {
  532. var reg = /%([0-9A-F]{2})/ig;
  533. var result = [];
  534. var parts = reg.exec(str);
  535. while (parts) {
  536. result.push(parseInt(parts[1], 16));
  537. parts = reg.exec(str);
  538. }
  539. return String(iconv.decode(SaferBuffer.from(result), 'shift_jis'));
  540. }
  541. }), { 県: '大阪府' });
  542. st.end();
  543. });
  544. t.test('receives the default decoder as a second argument', function (st) {
  545. st.plan(1);
  546. qs.parse('a', {
  547. decoder: function (str, defaultDecoder) {
  548. st.equal(defaultDecoder, utils.decode);
  549. }
  550. });
  551. st.end();
  552. });
  553. t.test('throws error with wrong decoder', function (st) {
  554. st['throws'](function () {
  555. qs.parse({}, { decoder: 'string' });
  556. }, new TypeError('Decoder has to be a function.'));
  557. st.end();
  558. });
  559. t.test('does not mutate the options argument', function (st) {
  560. var options = {};
  561. qs.parse('a[b]=true', options);
  562. st.deepEqual(options, {});
  563. st.end();
  564. });
  565. t.test('throws if an invalid charset is specified', function (st) {
  566. st['throws'](function () {
  567. qs.parse('a=b', { charset: 'foobar' });
  568. }, new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined'));
  569. st.end();
  570. });
  571. t.test('parses an iso-8859-1 string if asked to', function (st) {
  572. st.deepEqual(qs.parse('%A2=%BD', { charset: 'iso-8859-1' }), { '¢': '½' });
  573. st.end();
  574. });
  575. var urlEncodedCheckmarkInUtf8 = '%E2%9C%93';
  576. var urlEncodedOSlashInUtf8 = '%C3%B8';
  577. var urlEncodedNumCheckmark = '%26%2310003%3B';
  578. var urlEncodedNumSmiley = '%26%239786%3B';
  579. t.test('prefers an utf-8 charset specified by the utf8 sentinel to a default charset of iso-8859-1', function (st) {
  580. st.deepEqual(qs.parse('utf8=' + urlEncodedCheckmarkInUtf8 + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'iso-8859-1' }), { ø: 'ø' });
  581. st.end();
  582. });
  583. t.test('prefers an iso-8859-1 charset specified by the utf8 sentinel to a default charset of utf-8', function (st) {
  584. st.deepEqual(qs.parse('utf8=' + urlEncodedNumCheckmark + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'utf-8' }), { 'ø': 'ø' });
  585. st.end();
  586. });
  587. t.test('does not require the utf8 sentinel to be defined before the parameters whose decoding it affects', function (st) {
  588. st.deepEqual(qs.parse('a=' + urlEncodedOSlashInUtf8 + '&utf8=' + urlEncodedNumCheckmark, { charsetSentinel: true, charset: 'utf-8' }), { a: 'ø' });
  589. st.end();
  590. });
  591. t.test('should ignore an utf8 sentinel with an unknown value', function (st) {
  592. st.deepEqual(qs.parse('utf8=foo&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'utf-8' }), { ø: 'ø' });
  593. st.end();
  594. });
  595. t.test('uses the utf8 sentinel to switch to utf-8 when no default charset is given', function (st) {
  596. st.deepEqual(qs.parse('utf8=' + urlEncodedCheckmarkInUtf8 + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true }), { ø: 'ø' });
  597. st.end();
  598. });
  599. t.test('uses the utf8 sentinel to switch to iso-8859-1 when no default charset is given', function (st) {
  600. st.deepEqual(qs.parse('utf8=' + urlEncodedNumCheckmark + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true }), { 'ø': 'ø' });
  601. st.end();
  602. });
  603. t.test('interprets numeric entities in iso-8859-1 when `interpretNumericEntities`', function (st) {
  604. st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'iso-8859-1', interpretNumericEntities: true }), { foo: '☺' });
  605. st.end();
  606. });
  607. t.test('handles a custom decoder returning `null`, in the `iso-8859-1` charset, when `interpretNumericEntities`', function (st) {
  608. st.deepEqual(qs.parse('foo=&bar=' + urlEncodedNumSmiley, {
  609. charset: 'iso-8859-1',
  610. decoder: function (str, defaultDecoder, charset) {
  611. return str ? defaultDecoder(str, defaultDecoder, charset) : null;
  612. },
  613. interpretNumericEntities: true
  614. }), { foo: null, bar: '☺' });
  615. st.end();
  616. });
  617. t.test('does not interpret numeric entities in iso-8859-1 when `interpretNumericEntities` is absent', function (st) {
  618. st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'iso-8859-1' }), { foo: '&#9786;' });
  619. st.end();
  620. });
  621. t.test('does not interpret numeric entities when the charset is utf-8, even when `interpretNumericEntities`', function (st) {
  622. st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'utf-8', interpretNumericEntities: true }), { foo: '&#9786;' });
  623. st.end();
  624. });
  625. t.test('does not interpret %uXXXX syntax in iso-8859-1 mode', function (st) {
  626. st.deepEqual(qs.parse('%u263A=%u263A', { charset: 'iso-8859-1' }), { '%u263A': '%u263A' });
  627. st.end();
  628. });
  629. t.test('allows for decoding keys and values differently', function (st) {
  630. var decoder = function (str, defaultDecoder, charset, type) {
  631. if (type === 'key') {
  632. return defaultDecoder(str, defaultDecoder, charset, type).toLowerCase();
  633. }
  634. if (type === 'value') {
  635. return defaultDecoder(str, defaultDecoder, charset, type).toUpperCase();
  636. }
  637. throw 'this should never happen! type: ' + type;
  638. };
  639. st.deepEqual(qs.parse('KeY=vAlUe', { decoder: decoder }), { key: 'VALUE' });
  640. st.end();
  641. });
  642. t.end();
  643. });