Node-Red configuration
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

parse.js 35KB

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