用工小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1276 lines
50 KiB

10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
  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 v = require('es-value-fixtures');
  9. var inspect = require('object-inspect');
  10. var emptyTestCases = require('./empty-keys-cases').emptyTestCases;
  11. var hasProto = require('has-proto')();
  12. var qs = require('../');
  13. var utils = require('../lib/utils');
  14. test('parse()', function (t) {
  15. t.test('parses a simple string', function (st) {
  16. st.deepEqual(qs.parse('0=foo'), { 0: 'foo' });
  17. st.deepEqual(qs.parse('foo=c++'), { foo: 'c ' });
  18. st.deepEqual(qs.parse('a[>=]=23'), { a: { '>=': '23' } });
  19. st.deepEqual(qs.parse('a[<=>]==23'), { a: { '<=>': '=23' } });
  20. st.deepEqual(qs.parse('a[==]=23'), { a: { '==': '23' } });
  21. st.deepEqual(qs.parse('foo', { strictNullHandling: true }), { foo: null });
  22. st.deepEqual(qs.parse('foo'), { foo: '' });
  23. st.deepEqual(qs.parse('foo='), { foo: '' });
  24. st.deepEqual(qs.parse('foo=bar'), { foo: 'bar' });
  25. st.deepEqual(qs.parse(' foo = bar = baz '), { ' foo ': ' bar = baz ' });
  26. st.deepEqual(qs.parse('foo=bar=baz'), { foo: 'bar=baz' });
  27. st.deepEqual(qs.parse('foo=bar&bar=baz'), { foo: 'bar', bar: 'baz' });
  28. st.deepEqual(qs.parse('foo2=bar2&baz2='), { foo2: 'bar2', baz2: '' });
  29. st.deepEqual(qs.parse('foo=bar&baz', { strictNullHandling: true }), { foo: 'bar', baz: null });
  30. st.deepEqual(qs.parse('foo=bar&baz'), { foo: 'bar', baz: '' });
  31. st.deepEqual(qs.parse('cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World'), {
  32. cht: 'p3',
  33. chd: 't:60,40',
  34. chs: '250x100',
  35. chl: 'Hello|World'
  36. });
  37. st.end();
  38. });
  39. t.test('comma: false', function (st) {
  40. st.deepEqual(qs.parse('a[]=b&a[]=c'), { a: ['b', 'c'] });
  41. st.deepEqual(qs.parse('a[0]=b&a[1]=c'), { a: ['b', 'c'] });
  42. st.deepEqual(qs.parse('a=b,c'), { a: 'b,c' });
  43. st.deepEqual(qs.parse('a=b&a=c'), { a: ['b', 'c'] });
  44. st.end();
  45. });
  46. t.test('comma: true', function (st) {
  47. st.deepEqual(qs.parse('a[]=b&a[]=c', { comma: true }), { a: ['b', 'c'] });
  48. st.deepEqual(qs.parse('a[0]=b&a[1]=c', { comma: true }), { a: ['b', 'c'] });
  49. st.deepEqual(qs.parse('a=b,c', { comma: true }), { a: ['b', 'c'] });
  50. st.deepEqual(qs.parse('a=b&a=c', { comma: true }), { a: ['b', 'c'] });
  51. st.end();
  52. });
  53. t.test('allows enabling dot notation', function (st) {
  54. st.deepEqual(qs.parse('a.b=c'), { 'a.b': 'c' });
  55. st.deepEqual(qs.parse('a.b=c', { allowDots: true }), { a: { b: 'c' } });
  56. st.end();
  57. });
  58. t.test('decode dot keys correctly', function (st) {
  59. st.deepEqual(
  60. qs.parse('name%252Eobj.first=John&name%252Eobj.last=Doe', { allowDots: false, decodeDotInKeys: false }),
  61. { 'name%2Eobj.first': 'John', 'name%2Eobj.last': 'Doe' },
  62. 'with allowDots false and decodeDotInKeys false'
  63. );
  64. st.deepEqual(
  65. qs.parse('name.obj.first=John&name.obj.last=Doe', { allowDots: true, decodeDotInKeys: false }),
  66. { name: { obj: { first: 'John', last: 'Doe' } } },
  67. 'with allowDots false and decodeDotInKeys false'
  68. );
  69. st.deepEqual(
  70. qs.parse('name%252Eobj.first=John&name%252Eobj.last=Doe', { allowDots: true, decodeDotInKeys: false }),
  71. { 'name%2Eobj': { first: 'John', last: 'Doe' } },
  72. 'with allowDots true and decodeDotInKeys false'
  73. );
  74. st.deepEqual(
  75. qs.parse('name%252Eobj.first=John&name%252Eobj.last=Doe', { allowDots: true, decodeDotInKeys: true }),
  76. { 'name.obj': { first: 'John', last: 'Doe' } },
  77. 'with allowDots true and decodeDotInKeys true'
  78. );
  79. st.deepEqual(
  80. qs.parse(
  81. 'name%252Eobj%252Esubobject.first%252Egodly%252Ename=John&name%252Eobj%252Esubobject.last=Doe',
  82. { allowDots: false, decodeDotInKeys: false }
  83. ),
  84. { 'name%2Eobj%2Esubobject.first%2Egodly%2Ename': 'John', 'name%2Eobj%2Esubobject.last': 'Doe' },
  85. 'with allowDots false and decodeDotInKeys false'
  86. );
  87. st.deepEqual(
  88. qs.parse(
  89. 'name.obj.subobject.first.godly.name=John&name.obj.subobject.last=Doe',
  90. { allowDots: true, decodeDotInKeys: false }
  91. ),
  92. { name: { obj: { subobject: { first: { godly: { name: 'John' } }, last: 'Doe' } } } },
  93. 'with allowDots true and decodeDotInKeys false'
  94. );
  95. st.deepEqual(
  96. qs.parse(
  97. 'name%252Eobj%252Esubobject.first%252Egodly%252Ename=John&name%252Eobj%252Esubobject.last=Doe',
  98. { allowDots: true, decodeDotInKeys: true }
  99. ),
  100. { 'name.obj.subobject': { 'first.godly.name': 'John', last: 'Doe' } },
  101. 'with allowDots true and decodeDotInKeys true'
  102. );
  103. st.deepEqual(
  104. qs.parse('name%252Eobj.first=John&name%252Eobj.last=Doe'),
  105. { 'name%2Eobj.first': 'John', 'name%2Eobj.last': 'Doe' },
  106. 'with allowDots and decodeDotInKeys undefined'
  107. );
  108. st.end();
  109. });
  110. t.test('decodes dot in key of object, and allow enabling dot notation when decodeDotInKeys is set to true and allowDots is undefined', function (st) {
  111. st.deepEqual(
  112. qs.parse(
  113. 'name%252Eobj%252Esubobject.first%252Egodly%252Ename=John&name%252Eobj%252Esubobject.last=Doe',
  114. { decodeDotInKeys: true }
  115. ),
  116. { 'name.obj.subobject': { 'first.godly.name': 'John', last: 'Doe' } },
  117. 'with allowDots undefined and decodeDotInKeys true'
  118. );
  119. st.end();
  120. });
  121. t.test('throws when decodeDotInKeys is not of type boolean', function (st) {
  122. st['throws'](
  123. function () { qs.parse('foo[]&bar=baz', { decodeDotInKeys: 'foobar' }); },
  124. TypeError
  125. );
  126. st['throws'](
  127. function () { qs.parse('foo[]&bar=baz', { decodeDotInKeys: 0 }); },
  128. TypeError
  129. );
  130. st['throws'](
  131. function () { qs.parse('foo[]&bar=baz', { decodeDotInKeys: NaN }); },
  132. TypeError
  133. );
  134. st['throws'](
  135. function () { qs.parse('foo[]&bar=baz', { decodeDotInKeys: null }); },
  136. TypeError
  137. );
  138. st.end();
  139. });
  140. t.test('allows empty arrays in obj values', function (st) {
  141. st.deepEqual(qs.parse('foo[]&bar=baz', { allowEmptyArrays: true }), { foo: [], bar: 'baz' });
  142. st.deepEqual(qs.parse('foo[]&bar=baz', { allowEmptyArrays: false }), { foo: [''], bar: 'baz' });
  143. st.end();
  144. });
  145. t.test('throws when allowEmptyArrays is not of type boolean', function (st) {
  146. st['throws'](
  147. function () { qs.parse('foo[]&bar=baz', { allowEmptyArrays: 'foobar' }); },
  148. TypeError
  149. );
  150. st['throws'](
  151. function () { qs.parse('foo[]&bar=baz', { allowEmptyArrays: 0 }); },
  152. TypeError
  153. );
  154. st['throws'](
  155. function () { qs.parse('foo[]&bar=baz', { allowEmptyArrays: NaN }); },
  156. TypeError
  157. );
  158. st['throws'](
  159. function () { qs.parse('foo[]&bar=baz', { allowEmptyArrays: null }); },
  160. TypeError
  161. );
  162. st.end();
  163. });
  164. t.test('allowEmptyArrays + strictNullHandling', function (st) {
  165. st.deepEqual(
  166. qs.parse('testEmptyArray[]', { strictNullHandling: true, allowEmptyArrays: true }),
  167. { testEmptyArray: [] }
  168. );
  169. st.end();
  170. });
  171. t.deepEqual(qs.parse('a[b]=c'), { a: { b: 'c' } }, 'parses a single nested string');
  172. t.deepEqual(qs.parse('a[b][c]=d'), { a: { b: { c: 'd' } } }, 'parses a double nested string');
  173. t.deepEqual(
  174. qs.parse('a[b][c][d][e][f][g][h]=i'),
  175. { a: { b: { c: { d: { e: { f: { '[g][h]': 'i' } } } } } } },
  176. 'defaults to a depth of 5'
  177. );
  178. t.test('only parses one level when depth = 1', function (st) {
  179. st.deepEqual(qs.parse('a[b][c]=d', { depth: 1 }), { a: { b: { '[c]': 'd' } } });
  180. st.deepEqual(qs.parse('a[b][c][d]=e', { depth: 1 }), { a: { b: { '[c][d]': 'e' } } });
  181. st.end();
  182. });
  183. t.test('uses original key when depth = 0', function (st) {
  184. st.deepEqual(qs.parse('a[0]=b&a[1]=c', { depth: 0 }), { 'a[0]': 'b', 'a[1]': 'c' });
  185. 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' });
  186. st.end();
  187. });
  188. t.test('uses original key when depth = false', function (st) {
  189. st.deepEqual(qs.parse('a[0]=b&a[1]=c', { depth: false }), { 'a[0]': 'b', 'a[1]': 'c' });
  190. 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' });
  191. st.end();
  192. });
  193. t.deepEqual(qs.parse('a=b&a=c'), { a: ['b', 'c'] }, 'parses a simple array');
  194. t.test('parses an explicit array', function (st) {
  195. st.deepEqual(qs.parse('a[]=b'), { a: ['b'] });
  196. st.deepEqual(qs.parse('a[]=b&a[]=c'), { a: ['b', 'c'] });
  197. st.deepEqual(qs.parse('a[]=b&a[]=c&a[]=d'), { a: ['b', 'c', 'd'] });
  198. st.end();
  199. });
  200. t.test('parses a mix of simple and explicit arrays', function (st) {
  201. st.deepEqual(qs.parse('a=b&a[]=c'), { a: ['b', 'c'] });
  202. st.deepEqual(qs.parse('a[]=b&a=c'), { a: ['b', 'c'] });
  203. st.deepEqual(qs.parse('a[0]=b&a=c'), { a: ['b', 'c'] });
  204. st.deepEqual(qs.parse('a=b&a[0]=c'), { a: ['b', 'c'] });
  205. st.deepEqual(qs.parse('a[1]=b&a=c', { arrayLimit: 20 }), { a: ['b', 'c'] });
  206. st.deepEqual(qs.parse('a[]=b&a=c', { arrayLimit: 0 }), { a: ['b', 'c'] });
  207. st.deepEqual(qs.parse('a[]=b&a=c'), { a: ['b', 'c'] });
  208. st.deepEqual(qs.parse('a=b&a[1]=c', { arrayLimit: 20 }), { a: ['b', 'c'] });
  209. st.deepEqual(qs.parse('a=b&a[]=c', { arrayLimit: 0 }), { a: ['b', 'c'] });
  210. st.deepEqual(qs.parse('a=b&a[]=c'), { a: ['b', 'c'] });
  211. st.end();
  212. });
  213. t.test('parses a nested array', function (st) {
  214. st.deepEqual(qs.parse('a[b][]=c&a[b][]=d'), { a: { b: ['c', 'd'] } });
  215. st.deepEqual(qs.parse('a[>=]=25'), { a: { '>=': '25' } });
  216. st.end();
  217. });
  218. t.test('allows to specify array indices', function (st) {
  219. st.deepEqual(qs.parse('a[1]=c&a[0]=b&a[2]=d'), { a: ['b', 'c', 'd'] });
  220. st.deepEqual(qs.parse('a[1]=c&a[0]=b'), { a: ['b', 'c'] });
  221. st.deepEqual(qs.parse('a[1]=c', { arrayLimit: 20 }), { a: ['c'] });
  222. st.deepEqual(qs.parse('a[1]=c', { arrayLimit: 0 }), { a: { 1: 'c' } });
  223. st.deepEqual(qs.parse('a[1]=c'), { a: ['c'] });
  224. st.end();
  225. });
  226. t.test('limits specific array indices to arrayLimit', function (st) {
  227. st.deepEqual(qs.parse('a[20]=a', { arrayLimit: 20 }), { a: ['a'] });
  228. st.deepEqual(qs.parse('a[21]=a', { arrayLimit: 20 }), { a: { 21: 'a' } });
  229. st.deepEqual(qs.parse('a[20]=a'), { a: ['a'] });
  230. st.deepEqual(qs.parse('a[21]=a'), { a: { 21: 'a' } });
  231. st.end();
  232. });
  233. t.deepEqual(qs.parse('a[12b]=c'), { a: { '12b': 'c' } }, 'supports keys that begin with a number');
  234. t.test('supports encoded = signs', function (st) {
  235. st.deepEqual(qs.parse('he%3Dllo=th%3Dere'), { 'he=llo': 'th=ere' });
  236. st.end();
  237. });
  238. t.test('is ok with url encoded strings', function (st) {
  239. st.deepEqual(qs.parse('a[b%20c]=d'), { a: { 'b c': 'd' } });
  240. st.deepEqual(qs.parse('a[b]=c%20d'), { a: { b: 'c d' } });
  241. st.end();
  242. });
  243. t.test('allows brackets in the value', function (st) {
  244. st.deepEqual(qs.parse('pets=["tobi"]'), { pets: '["tobi"]' });
  245. st.deepEqual(qs.parse('operators=[">=", "<="]'), { operators: '[">=", "<="]' });
  246. st.end();
  247. });
  248. t.test('allows empty values', function (st) {
  249. st.deepEqual(qs.parse(''), {});
  250. st.deepEqual(qs.parse(null), {});
  251. st.deepEqual(qs.parse(undefined), {});
  252. st.end();
  253. });
  254. t.test('transforms arrays to objects', function (st) {
  255. st.deepEqual(qs.parse('foo[0]=bar&foo[bad]=baz'), { foo: { 0: 'bar', bad: 'baz' } });
  256. st.deepEqual(qs.parse('foo[bad]=baz&foo[0]=bar'), { foo: { bad: 'baz', 0: 'bar' } });
  257. st.deepEqual(qs.parse('foo[bad]=baz&foo[]=bar'), { foo: { bad: 'baz', 0: 'bar' } });
  258. st.deepEqual(qs.parse('foo[]=bar&foo[bad]=baz'), { foo: { 0: 'bar', bad: 'baz' } });
  259. st.deepEqual(qs.parse('foo[bad]=baz&foo[]=bar&foo[]=foo'), { foo: { bad: 'baz', 0: 'bar', 1: 'foo' } });
  260. 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' }] });
  261. st.deepEqual(qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c', { allowPrototypes: false }), { a: { 0: 'b', t: 'u' } });
  262. st.deepEqual(qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c', { allowPrototypes: true }), { a: { 0: 'b', t: 'u', hasOwnProperty: 'c' } });
  263. st.deepEqual(qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y', { allowPrototypes: false }), { a: { 0: 'b', x: 'y' } });
  264. st.deepEqual(qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y', { allowPrototypes: true }), { a: { 0: 'b', hasOwnProperty: 'c', x: 'y' } });
  265. st.end();
  266. });
  267. t.test('transforms arrays to objects (dot notation)', function (st) {
  268. st.deepEqual(qs.parse('foo[0].baz=bar&fool.bad=baz', { allowDots: true }), { foo: [{ baz: 'bar' }], fool: { bad: 'baz' } });
  269. st.deepEqual(qs.parse('foo[0].baz=bar&fool.bad.boo=baz', { allowDots: true }), { foo: [{ baz: 'bar' }], fool: { bad: { boo: 'baz' } } });
  270. st.deepEqual(qs.parse('foo[0][0].baz=bar&fool.bad=baz', { allowDots: true }), { foo: [[{ baz: 'bar' }]], fool: { bad: 'baz' } });
  271. st.deepEqual(qs.parse('foo[0].baz[0]=15&foo[0].bar=2', { allowDots: true }), { foo: [{ baz: ['15'], bar: '2' }] });
  272. 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' }] });
  273. st.deepEqual(qs.parse('foo.bad=baz&foo[0]=bar', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar' } });
  274. st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar' } });
  275. st.deepEqual(qs.parse('foo[]=bar&foo.bad=baz', { allowDots: true }), { foo: { 0: 'bar', bad: 'baz' } });
  276. st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar&foo[]=foo', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar', 1: 'foo' } });
  277. 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' }] });
  278. st.end();
  279. });
  280. t.test('correctly prunes undefined values when converting an array to an object', function (st) {
  281. st.deepEqual(qs.parse('a[2]=b&a[99999999]=c'), { a: { 2: 'b', 99999999: 'c' } });
  282. st.end();
  283. });
  284. t.test('supports malformed uri characters', function (st) {
  285. st.deepEqual(qs.parse('{%:%}', { strictNullHandling: true }), { '{%:%}': null });
  286. st.deepEqual(qs.parse('{%:%}='), { '{%:%}': '' });
  287. st.deepEqual(qs.parse('foo=%:%}'), { foo: '%:%}' });
  288. st.end();
  289. });
  290. t.test('doesn\'t produce empty keys', function (st) {
  291. st.deepEqual(qs.parse('_r=1&'), { _r: '1' });
  292. st.end();
  293. });
  294. t.test('cannot access Object prototype', function (st) {
  295. qs.parse('constructor[prototype][bad]=bad');
  296. qs.parse('bad[constructor][prototype][bad]=bad');
  297. st.equal(typeof Object.prototype.bad, 'undefined');
  298. st.end();
  299. });
  300. t.test('parses arrays of objects', function (st) {
  301. st.deepEqual(qs.parse('a[][b]=c'), { a: [{ b: 'c' }] });
  302. st.deepEqual(qs.parse('a[0][b]=c'), { a: [{ b: 'c' }] });
  303. st.end();
  304. });
  305. t.test('allows for empty strings in arrays', function (st) {
  306. st.deepEqual(qs.parse('a[]=b&a[]=&a[]=c'), { a: ['b', '', 'c'] });
  307. st.deepEqual(
  308. qs.parse('a[0]=b&a[1]&a[2]=c&a[19]=', { strictNullHandling: true, arrayLimit: 20 }),
  309. { a: ['b', null, 'c', ''] },
  310. 'with arrayLimit 20 + array indices: null then empty string works'
  311. );
  312. st.deepEqual(
  313. qs.parse('a[]=b&a[]&a[]=c&a[]=', { strictNullHandling: true, arrayLimit: 0 }),
  314. { a: ['b', null, 'c', ''] },
  315. 'with arrayLimit 0 + array brackets: null then empty string works'
  316. );
  317. st.deepEqual(
  318. qs.parse('a[0]=b&a[1]=&a[2]=c&a[19]', { strictNullHandling: true, arrayLimit: 20 }),
  319. { a: ['b', '', 'c', null] },
  320. 'with arrayLimit 20 + array indices: empty string then null works'
  321. );
  322. st.deepEqual(
  323. qs.parse('a[]=b&a[]=&a[]=c&a[]', { strictNullHandling: true, arrayLimit: 0 }),
  324. { a: ['b', '', 'c', null] },
  325. 'with arrayLimit 0 + array brackets: empty string then null works'
  326. );
  327. st.deepEqual(
  328. qs.parse('a[]=&a[]=b&a[]=c'),
  329. { a: ['', 'b', 'c'] },
  330. 'array brackets: empty strings work'
  331. );
  332. st.end();
  333. });
  334. t.test('compacts sparse arrays', function (st) {
  335. st.deepEqual(qs.parse('a[10]=1&a[2]=2', { arrayLimit: 20 }), { a: ['2', '1'] });
  336. st.deepEqual(qs.parse('a[1][b][2][c]=1', { arrayLimit: 20 }), { a: [{ b: [{ c: '1' }] }] });
  337. st.deepEqual(qs.parse('a[1][2][3][c]=1', { arrayLimit: 20 }), { a: [[[{ c: '1' }]]] });
  338. st.deepEqual(qs.parse('a[1][2][3][c][1]=1', { arrayLimit: 20 }), { a: [[[{ c: ['1'] }]]] });
  339. st.end();
  340. });
  341. t.test('parses sparse arrays', function (st) {
  342. /* eslint no-sparse-arrays: 0 */
  343. st.deepEqual(qs.parse('a[4]=1&a[1]=2', { allowSparse: true }), { a: [, '2', , , '1'] });
  344. st.deepEqual(qs.parse('a[1][b][2][c]=1', { allowSparse: true }), { a: [, { b: [, , { c: '1' }] }] });
  345. st.deepEqual(qs.parse('a[1][2][3][c]=1', { allowSparse: true }), { a: [, [, , [, , , { c: '1' }]]] });
  346. st.deepEqual(qs.parse('a[1][2][3][c][1]=1', { allowSparse: true }), { a: [, [, , [, , , { c: [, '1'] }]]] });
  347. st.end();
  348. });
  349. t.test('parses semi-parsed strings', function (st) {
  350. st.deepEqual(qs.parse({ 'a[b]': 'c' }), { a: { b: 'c' } });
  351. st.deepEqual(qs.parse({ 'a[b]': 'c', 'a[d]': 'e' }), { a: { b: 'c', d: 'e' } });
  352. st.end();
  353. });
  354. t.test('parses buffers correctly', function (st) {
  355. var b = SaferBuffer.from('test');
  356. st.deepEqual(qs.parse({ a: b }), { a: b });
  357. st.end();
  358. });
  359. t.test('parses jquery-param strings', function (st) {
  360. // readable = 'filter[0][]=int1&filter[0][]==&filter[0][]=77&filter[]=and&filter[2][]=int2&filter[2][]==&filter[2][]=8'
  361. 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';
  362. var expected = { filter: [['int1', '=', '77'], 'and', ['int2', '=', '8']] };
  363. st.deepEqual(qs.parse(encoded), expected);
  364. st.end();
  365. });
  366. t.test('continues parsing when no parent is found', function (st) {
  367. st.deepEqual(qs.parse('[]=&a=b'), { 0: '', a: 'b' });
  368. st.deepEqual(qs.parse('[]&a=b', { strictNullHandling: true }), { 0: null, a: 'b' });
  369. st.deepEqual(qs.parse('[foo]=bar'), { foo: 'bar' });
  370. st.end();
  371. });
  372. t.test('does not error when parsing a very long array', function (st) {
  373. var str = 'a[]=a';
  374. while (Buffer.byteLength(str) < 128 * 1024) {
  375. str = str + '&' + str;
  376. }
  377. st.doesNotThrow(function () {
  378. qs.parse(str);
  379. });
  380. st.end();
  381. });
  382. t.test('does not throw when a native prototype has an enumerable property', function (st) {
  383. st.intercept(Object.prototype, 'crash', { value: '' });
  384. st.intercept(Array.prototype, 'crash', { value: '' });
  385. st.doesNotThrow(qs.parse.bind(null, 'a=b'));
  386. st.deepEqual(qs.parse('a=b'), { a: 'b' });
  387. st.doesNotThrow(qs.parse.bind(null, 'a[][b]=c'));
  388. st.deepEqual(qs.parse('a[][b]=c'), { a: [{ b: 'c' }] });
  389. st.end();
  390. });
  391. t.test('parses a string with an alternative string delimiter', function (st) {
  392. st.deepEqual(qs.parse('a=b;c=d', { delimiter: ';' }), { a: 'b', c: 'd' });
  393. st.end();
  394. });
  395. t.test('parses a string with an alternative RegExp delimiter', function (st) {
  396. st.deepEqual(qs.parse('a=b; c=d', { delimiter: /[;,] */ }), { a: 'b', c: 'd' });
  397. st.end();
  398. });
  399. t.test('does not use non-splittable objects as delimiters', function (st) {
  400. st.deepEqual(qs.parse('a=b&c=d', { delimiter: true }), { a: 'b', c: 'd' });
  401. st.end();
  402. });
  403. t.test('allows overriding parameter limit', function (st) {
  404. st.deepEqual(qs.parse('a=b&c=d', { parameterLimit: 1 }), { a: 'b' });
  405. st.end();
  406. });
  407. t.test('allows setting the parameter limit to Infinity', function (st) {
  408. st.deepEqual(qs.parse('a=b&c=d', { parameterLimit: Infinity }), { a: 'b', c: 'd' });
  409. st.end();
  410. });
  411. t.test('allows overriding array limit', function (st) {
  412. st.deepEqual(qs.parse('a[0]=b', { arrayLimit: -1 }), { a: { 0: 'b' } });
  413. st.deepEqual(qs.parse('a[0]=b', { arrayLimit: 0 }), { a: ['b'] });
  414. st.deepEqual(qs.parse('a[-1]=b', { arrayLimit: -1 }), { a: { '-1': 'b' } });
  415. st.deepEqual(qs.parse('a[-1]=b', { arrayLimit: 0 }), { a: { '-1': 'b' } });
  416. st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayLimit: -1 }), { a: { 0: 'b', 1: 'c' } });
  417. st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayLimit: 0 }), { a: { 0: 'b', 1: 'c' } });
  418. st.end();
  419. });
  420. t.test('allows disabling array parsing', function (st) {
  421. var indices = qs.parse('a[0]=b&a[1]=c', { parseArrays: false });
  422. st.deepEqual(indices, { a: { 0: 'b', 1: 'c' } });
  423. st.equal(Array.isArray(indices.a), false, 'parseArrays:false, indices case is not an array');
  424. var emptyBrackets = qs.parse('a[]=b', { parseArrays: false });
  425. st.deepEqual(emptyBrackets, { a: { 0: 'b' } });
  426. st.equal(Array.isArray(emptyBrackets.a), false, 'parseArrays:false, empty brackets case is not an array');
  427. st.end();
  428. });
  429. t.test('allows for query string prefix', function (st) {
  430. st.deepEqual(qs.parse('?foo=bar', { ignoreQueryPrefix: true }), { foo: 'bar' });
  431. st.deepEqual(qs.parse('foo=bar', { ignoreQueryPrefix: true }), { foo: 'bar' });
  432. st.deepEqual(qs.parse('?foo=bar', { ignoreQueryPrefix: false }), { '?foo': 'bar' });
  433. st.end();
  434. });
  435. t.test('parses an object', function (st) {
  436. var input = {
  437. 'user[name]': { 'pop[bob]': 3 },
  438. 'user[email]': null
  439. };
  440. var expected = {
  441. user: {
  442. name: { 'pop[bob]': 3 },
  443. email: null
  444. }
  445. };
  446. var result = qs.parse(input);
  447. st.deepEqual(result, expected);
  448. st.end();
  449. });
  450. t.test('parses string with comma as array divider', function (st) {
  451. st.deepEqual(qs.parse('foo=bar,tee', { comma: true }), { foo: ['bar', 'tee'] });
  452. st.deepEqual(qs.parse('foo[bar]=coffee,tee', { comma: true }), { foo: { bar: ['coffee', 'tee'] } });
  453. st.deepEqual(qs.parse('foo=', { comma: true }), { foo: '' });
  454. st.deepEqual(qs.parse('foo', { comma: true }), { foo: '' });
  455. st.deepEqual(qs.parse('foo', { comma: true, strictNullHandling: true }), { foo: null });
  456. // test cases inversed from from stringify tests
  457. st.deepEqual(qs.parse('a[0]=c'), { a: ['c'] });
  458. st.deepEqual(qs.parse('a[]=c'), { a: ['c'] });
  459. st.deepEqual(qs.parse('a[]=c', { comma: true }), { a: ['c'] });
  460. st.deepEqual(qs.parse('a[0]=c&a[1]=d'), { a: ['c', 'd'] });
  461. st.deepEqual(qs.parse('a[]=c&a[]=d'), { a: ['c', 'd'] });
  462. st.deepEqual(qs.parse('a=c,d', { comma: true }), { a: ['c', 'd'] });
  463. st.end();
  464. });
  465. t.test('parses values with comma as array divider', function (st) {
  466. st.deepEqual(qs.parse({ foo: 'bar,tee' }, { comma: false }), { foo: 'bar,tee' });
  467. st.deepEqual(qs.parse({ foo: 'bar,tee' }, { comma: true }), { foo: ['bar', 'tee'] });
  468. st.end();
  469. });
  470. t.test('use number decoder, parses string that has one number with comma option enabled', function (st) {
  471. var decoder = function (str, defaultDecoder, charset, type) {
  472. if (!isNaN(Number(str))) {
  473. return parseFloat(str);
  474. }
  475. return defaultDecoder(str, defaultDecoder, charset, type);
  476. };
  477. st.deepEqual(qs.parse('foo=1', { comma: true, decoder: decoder }), { foo: 1 });
  478. st.deepEqual(qs.parse('foo=0', { comma: true, decoder: decoder }), { foo: 0 });
  479. st.end();
  480. });
  481. t.test('parses brackets holds array of arrays when having two parts of strings with comma as array divider', function (st) {
  482. st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=4,5,6', { comma: true }), { foo: [['1', '2', '3'], ['4', '5', '6']] });
  483. st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=', { comma: true }), { foo: [['1', '2', '3'], ''] });
  484. st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=,', { comma: true }), { foo: [['1', '2', '3'], ['', '']] });
  485. st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=a', { comma: true }), { foo: [['1', '2', '3'], 'a'] });
  486. st.end();
  487. });
  488. t.test('parses url-encoded brackets holds array of arrays when having two parts of strings with comma as array divider', function (st) {
  489. st.deepEqual(qs.parse('foo%5B%5D=1,2,3&foo%5B%5D=4,5,6', { comma: true }), { foo: [['1', '2', '3'], ['4', '5', '6']] });
  490. st.deepEqual(qs.parse('foo%5B%5D=1,2,3&foo%5B%5D=', { comma: true }), { foo: [['1', '2', '3'], ''] });
  491. st.deepEqual(qs.parse('foo%5B%5D=1,2,3&foo%5B%5D=,', { comma: true }), { foo: [['1', '2', '3'], ['', '']] });
  492. st.deepEqual(qs.parse('foo%5B%5D=1,2,3&foo%5B%5D=a', { comma: true }), { foo: [['1', '2', '3'], 'a'] });
  493. st.end();
  494. });
  495. t.test('parses comma delimited array while having percent-encoded comma treated as normal text', function (st) {
  496. st.deepEqual(qs.parse('foo=a%2Cb', { comma: true }), { foo: 'a,b' });
  497. st.deepEqual(qs.parse('foo=a%2C%20b,d', { comma: true }), { foo: ['a, b', 'd'] });
  498. st.deepEqual(qs.parse('foo=a%2C%20b,c%2C%20d', { comma: true }), { foo: ['a, b', 'c, d'] });
  499. st.end();
  500. });
  501. t.test('parses an object in dot notation', function (st) {
  502. var input = {
  503. 'user.name': { 'pop[bob]': 3 },
  504. 'user.email.': null
  505. };
  506. var expected = {
  507. user: {
  508. name: { 'pop[bob]': 3 },
  509. email: null
  510. }
  511. };
  512. var result = qs.parse(input, { allowDots: true });
  513. st.deepEqual(result, expected);
  514. st.end();
  515. });
  516. t.test('parses an object and not child values', function (st) {
  517. var input = {
  518. 'user[name]': { 'pop[bob]': { test: 3 } },
  519. 'user[email]': null
  520. };
  521. var expected = {
  522. user: {
  523. name: { 'pop[bob]': { test: 3 } },
  524. email: null
  525. }
  526. };
  527. var result = qs.parse(input);
  528. st.deepEqual(result, expected);
  529. st.end();
  530. });
  531. t.test('does not blow up when Buffer global is missing', function (st) {
  532. var restore = mockProperty(global, 'Buffer', { 'delete': true });
  533. var result = qs.parse('a=b&c=d');
  534. restore();
  535. st.deepEqual(result, { a: 'b', c: 'd' });
  536. st.end();
  537. });
  538. t.test('does not crash when parsing circular references', function (st) {
  539. var a = {};
  540. a.b = a;
  541. var parsed;
  542. st.doesNotThrow(function () {
  543. parsed = qs.parse({ 'foo[bar]': 'baz', 'foo[baz]': a });
  544. });
  545. st.equal('foo' in parsed, true, 'parsed has "foo" property');
  546. st.equal('bar' in parsed.foo, true);
  547. st.equal('baz' in parsed.foo, true);
  548. st.equal(parsed.foo.bar, 'baz');
  549. st.deepEqual(parsed.foo.baz, a);
  550. st.end();
  551. });
  552. t.test('does not crash when parsing deep objects', function (st) {
  553. var parsed;
  554. var str = 'foo';
  555. for (var i = 0; i < 5000; i++) {
  556. str += '[p]';
  557. }
  558. str += '=bar';
  559. st.doesNotThrow(function () {
  560. parsed = qs.parse(str, { depth: 5000 });
  561. });
  562. st.equal('foo' in parsed, true, 'parsed has "foo" property');
  563. var depth = 0;
  564. var ref = parsed.foo;
  565. while ((ref = ref.p)) {
  566. depth += 1;
  567. }
  568. st.equal(depth, 5000, 'parsed is 5000 properties deep');
  569. st.end();
  570. });
  571. t.test('parses null objects correctly', { skip: !hasProto }, function (st) {
  572. var a = { __proto__: null, b: 'c' };
  573. st.deepEqual(qs.parse(a), { b: 'c' });
  574. var result = qs.parse({ a: a });
  575. st.equal('a' in result, true, 'result has "a" property');
  576. st.deepEqual(result.a, a);
  577. st.end();
  578. });
  579. t.test('parses dates correctly', function (st) {
  580. var now = new Date();
  581. st.deepEqual(qs.parse({ a: now }), { a: now });
  582. st.end();
  583. });
  584. t.test('parses regular expressions correctly', function (st) {
  585. var re = /^test$/;
  586. st.deepEqual(qs.parse({ a: re }), { a: re });
  587. st.end();
  588. });
  589. t.test('does not allow overwriting prototype properties', function (st) {
  590. st.deepEqual(qs.parse('a[hasOwnProperty]=b', { allowPrototypes: false }), {});
  591. st.deepEqual(qs.parse('hasOwnProperty=b', { allowPrototypes: false }), {});
  592. st.deepEqual(
  593. qs.parse('toString', { allowPrototypes: false }),
  594. {},
  595. 'bare "toString" results in {}'
  596. );
  597. st.end();
  598. });
  599. t.test('can allow overwriting prototype properties', function (st) {
  600. st.deepEqual(qs.parse('a[hasOwnProperty]=b', { allowPrototypes: true }), { a: { hasOwnProperty: 'b' } });
  601. st.deepEqual(qs.parse('hasOwnProperty=b', { allowPrototypes: true }), { hasOwnProperty: 'b' });
  602. st.deepEqual(
  603. qs.parse('toString', { allowPrototypes: true }),
  604. { toString: '' },
  605. 'bare "toString" results in { toString: "" }'
  606. );
  607. st.end();
  608. });
  609. t.test('does not crash when the global Object prototype is frozen', { skip: !hasPropertyDescriptors || !hasOverrideMistake }, function (st) {
  610. // We can't actually freeze the global Object prototype as that will interfere with other tests, and once an object is frozen, it
  611. // can't be unfrozen. Instead, we add a new non-writable property to simulate this.
  612. st.teardown(mockProperty(Object.prototype, 'frozenProp', { value: 'foo', nonWritable: true, nonEnumerable: true }));
  613. st['throws'](
  614. function () {
  615. var obj = {};
  616. obj.frozenProp = 'bar';
  617. },
  618. // node < 6 has a different error message
  619. /^TypeError: Cannot assign to read only property 'frozenProp' of (?:object '#<Object>'|#<Object>)/,
  620. 'regular assignment of an inherited non-writable property throws'
  621. );
  622. var parsed;
  623. st.doesNotThrow(
  624. function () {
  625. parsed = qs.parse('frozenProp', { allowPrototypes: false });
  626. },
  627. 'parsing a nonwritable Object.prototype property does not throw'
  628. );
  629. st.deepEqual(parsed, {}, 'bare "frozenProp" results in {}');
  630. st.end();
  631. });
  632. t.test('params starting with a closing bracket', function (st) {
  633. st.deepEqual(qs.parse(']=toString'), { ']': 'toString' });
  634. st.deepEqual(qs.parse(']]=toString'), { ']]': 'toString' });
  635. st.deepEqual(qs.parse(']hello]=toString'), { ']hello]': 'toString' });
  636. st.end();
  637. });
  638. t.test('params starting with a starting bracket', function (st) {
  639. st.deepEqual(qs.parse('[=toString'), { '[': 'toString' });
  640. st.deepEqual(qs.parse('[[=toString'), { '[[': 'toString' });
  641. st.deepEqual(qs.parse('[hello[=toString'), { '[hello[': 'toString' });
  642. st.end();
  643. });
  644. t.test('add keys to objects', function (st) {
  645. st.deepEqual(
  646. qs.parse('a[b]=c&a=d'),
  647. { a: { b: 'c', d: true } },
  648. 'can add keys to objects'
  649. );
  650. st.deepEqual(
  651. qs.parse('a[b]=c&a=toString'),
  652. { a: { b: 'c' } },
  653. 'can not overwrite prototype'
  654. );
  655. st.deepEqual(
  656. qs.parse('a[b]=c&a=toString', { allowPrototypes: true }),
  657. { a: { b: 'c', toString: true } },
  658. 'can overwrite prototype with allowPrototypes true'
  659. );
  660. st.deepEqual(
  661. qs.parse('a[b]=c&a=toString', { plainObjects: true }),
  662. { __proto__: null, a: { __proto__: null, b: 'c', toString: true } },
  663. 'can overwrite prototype with plainObjects true'
  664. );
  665. st.end();
  666. });
  667. t.test('dunder proto is ignored', function (st) {
  668. var payload = 'categories[__proto__]=login&categories[__proto__]&categories[length]=42';
  669. var result = qs.parse(payload, { allowPrototypes: true });
  670. st.deepEqual(
  671. result,
  672. {
  673. categories: {
  674. length: '42'
  675. }
  676. },
  677. 'silent [[Prototype]] payload'
  678. );
  679. var plainResult = qs.parse(payload, { allowPrototypes: true, plainObjects: true });
  680. st.deepEqual(
  681. plainResult,
  682. {
  683. __proto__: null,
  684. categories: {
  685. __proto__: null,
  686. length: '42'
  687. }
  688. },
  689. 'silent [[Prototype]] payload: plain objects'
  690. );
  691. var query = qs.parse('categories[__proto__]=cats&categories[__proto__]=dogs&categories[some][json]=toInject', { allowPrototypes: true });
  692. st.notOk(Array.isArray(query.categories), 'is not an array');
  693. st.notOk(query.categories instanceof Array, 'is not instanceof an array');
  694. st.deepEqual(query.categories, { some: { json: 'toInject' } });
  695. st.equal(JSON.stringify(query.categories), '{"some":{"json":"toInject"}}', 'stringifies as a non-array');
  696. st.deepEqual(
  697. qs.parse('foo[__proto__][hidden]=value&foo[bar]=stuffs', { allowPrototypes: true }),
  698. {
  699. foo: {
  700. bar: 'stuffs'
  701. }
  702. },
  703. 'hidden values'
  704. );
  705. st.deepEqual(
  706. qs.parse('foo[__proto__][hidden]=value&foo[bar]=stuffs', { allowPrototypes: true, plainObjects: true }),
  707. {
  708. __proto__: null,
  709. foo: {
  710. __proto__: null,
  711. bar: 'stuffs'
  712. }
  713. },
  714. 'hidden values: plain objects'
  715. );
  716. st.end();
  717. });
  718. t.test('can return null objects', { skip: !hasProto }, function (st) {
  719. var expected = {
  720. __proto__: null,
  721. a: {
  722. __proto__: null,
  723. b: 'c',
  724. hasOwnProperty: 'd'
  725. }
  726. };
  727. st.deepEqual(qs.parse('a[b]=c&a[hasOwnProperty]=d', { plainObjects: true }), expected);
  728. st.deepEqual(qs.parse(null, { plainObjects: true }), { __proto__: null });
  729. var expectedArray = {
  730. __proto__: null,
  731. a: {
  732. __proto__: null,
  733. 0: 'b',
  734. c: 'd'
  735. }
  736. };
  737. st.deepEqual(qs.parse('a[]=b&a[c]=d', { plainObjects: true }), expectedArray);
  738. st.end();
  739. });
  740. t.test('can parse with custom encoding', function (st) {
  741. st.deepEqual(qs.parse('%8c%a7=%91%e5%8d%e3%95%7b', {
  742. decoder: function (str) {
  743. var reg = /%([0-9A-F]{2})/ig;
  744. var result = [];
  745. var parts = reg.exec(str);
  746. while (parts) {
  747. result.push(parseInt(parts[1], 16));
  748. parts = reg.exec(str);
  749. }
  750. return String(iconv.decode(SaferBuffer.from(result), 'shift_jis'));
  751. }
  752. }), { : '大阪府' });
  753. st.end();
  754. });
  755. t.test('receives the default decoder as a second argument', function (st) {
  756. st.plan(1);
  757. qs.parse('a', {
  758. decoder: function (str, defaultDecoder) {
  759. st.equal(defaultDecoder, utils.decode);
  760. }
  761. });
  762. st.end();
  763. });
  764. t.test('throws error with wrong decoder', function (st) {
  765. st['throws'](function () {
  766. qs.parse({}, { decoder: 'string' });
  767. }, new TypeError('Decoder has to be a function.'));
  768. st.end();
  769. });
  770. t.test('does not mutate the options argument', function (st) {
  771. var options = {};
  772. qs.parse('a[b]=true', options);
  773. st.deepEqual(options, {});
  774. st.end();
  775. });
  776. t.test('throws if an invalid charset is specified', function (st) {
  777. st['throws'](function () {
  778. qs.parse('a=b', { charset: 'foobar' });
  779. }, new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined'));
  780. st.end();
  781. });
  782. t.test('parses an iso-8859-1 string if asked to', function (st) {
  783. st.deepEqual(qs.parse('%A2=%BD', { charset: 'iso-8859-1' }), { '¢': '½' });
  784. st.end();
  785. });
  786. var urlEncodedCheckmarkInUtf8 = '%E2%9C%93';
  787. var urlEncodedOSlashInUtf8 = '%C3%B8';
  788. var urlEncodedNumCheckmark = '%26%2310003%3B';
  789. var urlEncodedNumSmiley = '%26%239786%3B';
  790. t.test('prefers an utf-8 charset specified by the utf8 sentinel to a default charset of iso-8859-1', function (st) {
  791. st.deepEqual(qs.parse('utf8=' + urlEncodedCheckmarkInUtf8 + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'iso-8859-1' }), { ø: 'ø' });
  792. st.end();
  793. });
  794. t.test('prefers an iso-8859-1 charset specified by the utf8 sentinel to a default charset of utf-8', function (st) {
  795. st.deepEqual(qs.parse('utf8=' + urlEncodedNumCheckmark + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'utf-8' }), { 'ø': 'ø' });
  796. st.end();
  797. });
  798. t.test('does not require the utf8 sentinel to be defined before the parameters whose decoding it affects', function (st) {
  799. st.deepEqual(qs.parse('a=' + urlEncodedOSlashInUtf8 + '&utf8=' + urlEncodedNumCheckmark, { charsetSentinel: true, charset: 'utf-8' }), { a: 'ø' });
  800. st.end();
  801. });
  802. t.test('ignores an utf8 sentinel with an unknown value', function (st) {
  803. st.deepEqual(qs.parse('utf8=foo&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'utf-8' }), { ø: 'ø' });
  804. st.end();
  805. });
  806. t.test('uses the utf8 sentinel to switch to utf-8 when no default charset is given', function (st) {
  807. st.deepEqual(qs.parse('utf8=' + urlEncodedCheckmarkInUtf8 + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true }), { ø: 'ø' });
  808. st.end();
  809. });
  810. t.test('uses the utf8 sentinel to switch to iso-8859-1 when no default charset is given', function (st) {
  811. st.deepEqual(qs.parse('utf8=' + urlEncodedNumCheckmark + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true }), { 'ø': 'ø' });
  812. st.end();
  813. });
  814. t.test('interprets numeric entities in iso-8859-1 when `interpretNumericEntities`', function (st) {
  815. st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'iso-8859-1', interpretNumericEntities: true }), { foo: '☺' });
  816. st.end();
  817. });
  818. t.test('handles a custom decoder returning `null`, in the `iso-8859-1` charset, when `interpretNumericEntities`', function (st) {
  819. st.deepEqual(qs.parse('foo=&bar=' + urlEncodedNumSmiley, {
  820. charset: 'iso-8859-1',
  821. decoder: function (str, defaultDecoder, charset) {
  822. return str ? defaultDecoder(str, defaultDecoder, charset) : null;
  823. },
  824. interpretNumericEntities: true
  825. }), { foo: null, bar: '☺' });
  826. st.end();
  827. });
  828. t.test('does not interpret numeric entities in iso-8859-1 when `interpretNumericEntities` is absent', function (st) {
  829. st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'iso-8859-1' }), { foo: '&#9786;' });
  830. st.end();
  831. });
  832. t.test('does not interpret numeric entities when the charset is utf-8, even when `interpretNumericEntities`', function (st) {
  833. st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'utf-8', interpretNumericEntities: true }), { foo: '&#9786;' });
  834. st.end();
  835. });
  836. t.test('interpretNumericEntities with comma:true and iso charset does not crash', function (st) {
  837. st.deepEqual(
  838. qs.parse('b&a[]=1,' + urlEncodedNumSmiley, { comma: true, charset: 'iso-8859-1', interpretNumericEntities: true }),
  839. { b: '', a: ['1,☺'] }
  840. );
  841. st.end();
  842. });
  843. t.test('does not interpret %uXXXX syntax in iso-8859-1 mode', function (st) {
  844. st.deepEqual(qs.parse('%u263A=%u263A', { charset: 'iso-8859-1' }), { '%u263A': '%u263A' });
  845. st.end();
  846. });
  847. t.test('allows for decoding keys and values differently', function (st) {
  848. var decoder = function (str, defaultDecoder, charset, type) {
  849. if (type === 'key') {
  850. return defaultDecoder(str, defaultDecoder, charset, type).toLowerCase();
  851. }
  852. if (type === 'value') {
  853. return defaultDecoder(str, defaultDecoder, charset, type).toUpperCase();
  854. }
  855. throw 'this should never happen! type: ' + type;
  856. };
  857. st.deepEqual(qs.parse('KeY=vAlUe', { decoder: decoder }), { key: 'VALUE' });
  858. st.end();
  859. });
  860. t.test('parameter limit tests', function (st) {
  861. st.test('does not throw error when within parameter limit', function (sst) {
  862. var result = qs.parse('a=1&b=2&c=3', { parameterLimit: 5, throwOnLimitExceeded: true });
  863. sst.deepEqual(result, { a: '1', b: '2', c: '3' }, 'parses without errors');
  864. sst.end();
  865. });
  866. st.test('throws error when throwOnLimitExceeded is present but not boolean', function (sst) {
  867. sst['throws'](
  868. function () {
  869. qs.parse('a=1&b=2&c=3&d=4&e=5&f=6', { parameterLimit: 3, throwOnLimitExceeded: 'true' });
  870. },
  871. new TypeError('`throwOnLimitExceeded` option must be a boolean'),
  872. 'throws error when throwOnLimitExceeded is present and not boolean'
  873. );
  874. sst.end();
  875. });
  876. st.test('throws error when parameter limit exceeded', function (sst) {
  877. sst['throws'](
  878. function () {
  879. qs.parse('a=1&b=2&c=3&d=4&e=5&f=6', { parameterLimit: 3, throwOnLimitExceeded: true });
  880. },
  881. new RangeError('Parameter limit exceeded. Only 3 parameters allowed.'),
  882. 'throws error when parameter limit is exceeded'
  883. );
  884. sst.end();
  885. });
  886. st.test('silently truncates when throwOnLimitExceeded is not given', function (sst) {
  887. var result = qs.parse('a=1&b=2&c=3&d=4&e=5', { parameterLimit: 3 });
  888. sst.deepEqual(result, { a: '1', b: '2', c: '3' }, 'parses and truncates silently');
  889. sst.end();
  890. });
  891. st.test('silently truncates when parameter limit exceeded without error', function (sst) {
  892. var result = qs.parse('a=1&b=2&c=3&d=4&e=5', { parameterLimit: 3, throwOnLimitExceeded: false });
  893. sst.deepEqual(result, { a: '1', b: '2', c: '3' }, 'parses and truncates silently');
  894. sst.end();
  895. });
  896. st.test('allows unlimited parameters when parameterLimit set to Infinity', function (sst) {
  897. var result = qs.parse('a=1&b=2&c=3&d=4&e=5&f=6', { parameterLimit: Infinity });
  898. sst.deepEqual(result, { a: '1', b: '2', c: '3', d: '4', e: '5', f: '6' }, 'parses all parameters without truncation');
  899. sst.end();
  900. });
  901. st.end();
  902. });
  903. t.test('array limit tests', function (st) {
  904. st.test('does not throw error when array is within limit', function (sst) {
  905. var result = qs.parse('a[]=1&a[]=2&a[]=3', { arrayLimit: 5, throwOnLimitExceeded: true });
  906. sst.deepEqual(result, { a: ['1', '2', '3'] }, 'parses array without errors');
  907. sst.end();
  908. });
  909. st.test('throws error when throwOnLimitExceeded is present but not boolean for array limit', function (sst) {
  910. sst['throws'](
  911. function () {
  912. qs.parse('a[]=1&a[]=2&a[]=3&a[]=4', { arrayLimit: 3, throwOnLimitExceeded: 'true' });
  913. },
  914. new TypeError('`throwOnLimitExceeded` option must be a boolean'),
  915. 'throws error when throwOnLimitExceeded is present and not boolean for array limit'
  916. );
  917. sst.end();
  918. });
  919. st.test('throws error when array limit exceeded', function (sst) {
  920. sst['throws'](
  921. function () {
  922. qs.parse('a[]=1&a[]=2&a[]=3&a[]=4', { arrayLimit: 3, throwOnLimitExceeded: true });
  923. },
  924. new RangeError('Array limit exceeded. Only 3 elements allowed in an array.'),
  925. 'throws error when array limit is exceeded'
  926. );
  927. sst.end();
  928. });
  929. st.test('converts array to object if length is greater than limit', function (sst) {
  930. var result = qs.parse('a[1]=1&a[2]=2&a[3]=3&a[4]=4&a[5]=5&a[6]=6', { arrayLimit: 5 });
  931. sst.deepEqual(result, { a: { 1: '1', 2: '2', 3: '3', 4: '4', 5: '5', 6: '6' } }, 'parses into object if array length is greater than limit');
  932. sst.end();
  933. });
  934. st.end();
  935. });
  936. t.end();
  937. });
  938. test('parses empty keys', function (t) {
  939. emptyTestCases.forEach(function (testCase) {
  940. t.test('skips empty string key with ' + testCase.input, function (st) {
  941. st.deepEqual(qs.parse(testCase.input), testCase.noEmptyKeys);
  942. st.end();
  943. });
  944. });
  945. });
  946. test('`duplicates` option', function (t) {
  947. v.nonStrings.concat('not a valid option').forEach(function (invalidOption) {
  948. if (typeof invalidOption !== 'undefined') {
  949. t['throws'](
  950. function () { qs.parse('', { duplicates: invalidOption }); },
  951. TypeError,
  952. 'throws on invalid option: ' + inspect(invalidOption)
  953. );
  954. }
  955. });
  956. t.deepEqual(
  957. qs.parse('foo=bar&foo=baz'),
  958. { foo: ['bar', 'baz'] },
  959. 'duplicates: default, combine'
  960. );
  961. t.deepEqual(
  962. qs.parse('foo=bar&foo=baz', { duplicates: 'combine' }),
  963. { foo: ['bar', 'baz'] },
  964. 'duplicates: combine'
  965. );
  966. t.deepEqual(
  967. qs.parse('foo=bar&foo=baz', { duplicates: 'first' }),
  968. { foo: 'bar' },
  969. 'duplicates: first'
  970. );
  971. t.deepEqual(
  972. qs.parse('foo=bar&foo=baz', { duplicates: 'last' }),
  973. { foo: 'baz' },
  974. 'duplicates: last'
  975. );
  976. t.end();
  977. });
  978. test('qs strictDepth option - throw cases', function (t) {
  979. t.test('throws an exception when depth exceeds the limit with strictDepth: true', function (st) {
  980. st['throws'](
  981. function () {
  982. qs.parse('a[b][c][d][e][f][g][h][i]=j', { depth: 1, strictDepth: true });
  983. },
  984. RangeError,
  985. 'throws RangeError'
  986. );
  987. st.end();
  988. });
  989. t.test('throws an exception for multiple nested arrays with strictDepth: true', function (st) {
  990. st['throws'](
  991. function () {
  992. qs.parse('a[0][1][2][3][4]=b', { depth: 3, strictDepth: true });
  993. },
  994. RangeError,
  995. 'throws RangeError'
  996. );
  997. st.end();
  998. });
  999. t.test('throws an exception for nested objects and arrays with strictDepth: true', function (st) {
  1000. st['throws'](
  1001. function () {
  1002. qs.parse('a[b][c][0][d][e]=f', { depth: 3, strictDepth: true });
  1003. },
  1004. RangeError,
  1005. 'throws RangeError'
  1006. );
  1007. st.end();
  1008. });
  1009. t.test('throws an exception for different types of values with strictDepth: true', function (st) {
  1010. st['throws'](
  1011. function () {
  1012. qs.parse('a[b][c][d][e]=true&a[b][c][d][f]=42', { depth: 3, strictDepth: true });
  1013. },
  1014. RangeError,
  1015. 'throws RangeError'
  1016. );
  1017. st.end();
  1018. });
  1019. });
  1020. test('qs strictDepth option - non-throw cases', function (t) {
  1021. t.test('when depth is 0 and strictDepth true, do not throw', function (st) {
  1022. st.doesNotThrow(
  1023. function () {
  1024. qs.parse('a[b][c][d][e]=true&a[b][c][d][f]=42', { depth: 0, strictDepth: true });
  1025. },
  1026. RangeError,
  1027. 'does not throw RangeError'
  1028. );
  1029. st.end();
  1030. });
  1031. t.test('parses successfully when depth is within the limit with strictDepth: true', function (st) {
  1032. st.doesNotThrow(
  1033. function () {
  1034. var result = qs.parse('a[b]=c', { depth: 1, strictDepth: true });
  1035. st.deepEqual(result, { a: { b: 'c' } }, 'parses correctly');
  1036. }
  1037. );
  1038. st.end();
  1039. });
  1040. t.test('does not throw an exception when depth exceeds the limit with strictDepth: false', function (st) {
  1041. st.doesNotThrow(
  1042. function () {
  1043. var result = qs.parse('a[b][c][d][e][f][g][h][i]=j', { depth: 1 });
  1044. st.deepEqual(result, { a: { b: { '[c][d][e][f][g][h][i]': 'j' } } }, 'parses with depth limit');
  1045. }
  1046. );
  1047. st.end();
  1048. });
  1049. t.test('parses successfully when depth is within the limit with strictDepth: false', function (st) {
  1050. st.doesNotThrow(
  1051. function () {
  1052. var result = qs.parse('a[b]=c', { depth: 1 });
  1053. st.deepEqual(result, { a: { b: 'c' } }, 'parses correctly');
  1054. }
  1055. );
  1056. st.end();
  1057. });
  1058. t.test('does not throw when depth is exactly at the limit with strictDepth: true', function (st) {
  1059. st.doesNotThrow(
  1060. function () {
  1061. var result = qs.parse('a[b][c]=d', { depth: 2, strictDepth: true });
  1062. st.deepEqual(result, { a: { b: { c: 'd' } } }, 'parses correctly');
  1063. }
  1064. );
  1065. st.end();
  1066. });
  1067. });