parse.js 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  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 qs = require('../');
  12. var utils = require('../lib/utils');
  13. test('parse()', function (t) {
  14. t.test('parses a simple string', function (st) {
  15. st.deepEqual(qs.parse('0=foo'), { 0: 'foo' });
  16. st.deepEqual(qs.parse('foo=c++'), { foo: 'c ' });
  17. st.deepEqual(qs.parse('a[>=]=23'), { a: { '>=': '23' } });
  18. st.deepEqual(qs.parse('a[<=>]==23'), { a: { '<=>': '=23' } });
  19. st.deepEqual(qs.parse('a[==]=23'), { a: { '==': '23' } });
  20. st.deepEqual(qs.parse('foo', { strictNullHandling: true }), { foo: null });
  21. st.deepEqual(qs.parse('foo'), { foo: '' });
  22. st.deepEqual(qs.parse('foo='), { foo: '' });
  23. st.deepEqual(qs.parse('foo=bar'), { foo: 'bar' });
  24. st.deepEqual(qs.parse(' foo = bar = baz '), { ' foo ': ' bar = baz ' });
  25. st.deepEqual(qs.parse('foo=bar=baz'), { foo: 'bar=baz' });
  26. st.deepEqual(qs.parse('foo=bar&bar=baz'), { foo: 'bar', bar: 'baz' });
  27. st.deepEqual(qs.parse('foo2=bar2&baz2='), { foo2: 'bar2', baz2: '' });
  28. st.deepEqual(qs.parse('foo=bar&baz', { strictNullHandling: true }), { foo: 'bar', baz: null });
  29. st.deepEqual(qs.parse('foo=bar&baz'), { foo: 'bar', baz: '' });
  30. st.deepEqual(qs.parse('cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World'), {
  31. cht: 'p3',
  32. chd: 't:60,40',
  33. chs: '250x100',
  34. chl: 'Hello|World'
  35. });
  36. st.end();
  37. });
  38. t.test('comma: false', function (st) {
  39. st.deepEqual(qs.parse('a[]=b&a[]=c'), { a: ['b', 'c'] });
  40. st.deepEqual(qs.parse('a[0]=b&a[1]=c'), { a: ['b', 'c'] });
  41. st.deepEqual(qs.parse('a=b,c'), { a: 'b,c' });
  42. st.deepEqual(qs.parse('a=b&a=c'), { a: ['b', 'c'] });
  43. st.end();
  44. });
  45. t.test('comma: true', function (st) {
  46. st.deepEqual(qs.parse('a[]=b&a[]=c', { comma: true }), { a: ['b', 'c'] });
  47. st.deepEqual(qs.parse('a[0]=b&a[1]=c', { comma: true }), { a: ['b', 'c'] });
  48. st.deepEqual(qs.parse('a=b,c', { comma: true }), { a: ['b', 'c'] });
  49. st.deepEqual(qs.parse('a=b&a=c', { comma: true }), { a: ['b', 'c'] });
  50. st.end();
  51. });
  52. t.test('allows enabling dot notation', function (st) {
  53. st.deepEqual(qs.parse('a.b=c'), { 'a.b': 'c' });
  54. st.deepEqual(qs.parse('a.b=c', { allowDots: true }), { a: { b: 'c' } });
  55. st.end();
  56. });
  57. t.test('decode dot keys correctly', function (st) {
  58. st.deepEqual(
  59. qs.parse('name%252Eobj.first=John&name%252Eobj.last=Doe', { allowDots: false, decodeDotInKeys: false }),
  60. { 'name%2Eobj.first': 'John', 'name%2Eobj.last': 'Doe' },
  61. 'with allowDots false and decodeDotInKeys false'
  62. );
  63. st.deepEqual(
  64. qs.parse('name.obj.first=John&name.obj.last=Doe', { allowDots: true, decodeDotInKeys: false }),
  65. { name: { obj: { first: 'John', last: 'Doe' } } },
  66. 'with allowDots false and decodeDotInKeys false'
  67. );
  68. st.deepEqual(
  69. qs.parse('name%252Eobj.first=John&name%252Eobj.last=Doe', { allowDots: true, decodeDotInKeys: false }),
  70. { 'name%2Eobj': { first: 'John', last: 'Doe' } },
  71. 'with allowDots true and decodeDotInKeys false'
  72. );
  73. st.deepEqual(
  74. qs.parse('name%252Eobj.first=John&name%252Eobj.last=Doe', { allowDots: true, decodeDotInKeys: true }),
  75. { 'name.obj': { first: 'John', last: 'Doe' } },
  76. 'with allowDots true and decodeDotInKeys true'
  77. );
  78. st.deepEqual(
  79. qs.parse(
  80. 'name%252Eobj%252Esubobject.first%252Egodly%252Ename=John&name%252Eobj%252Esubobject.last=Doe',
  81. { allowDots: false, decodeDotInKeys: false }
  82. ),
  83. { 'name%2Eobj%2Esubobject.first%2Egodly%2Ename': 'John', 'name%2Eobj%2Esubobject.last': 'Doe' },
  84. 'with allowDots false and decodeDotInKeys false'
  85. );
  86. st.deepEqual(
  87. qs.parse(
  88. 'name.obj.subobject.first.godly.name=John&name.obj.subobject.last=Doe',
  89. { allowDots: true, decodeDotInKeys: false }
  90. ),
  91. { name: { obj: { subobject: { first: { godly: { name: 'John' } }, last: 'Doe' } } } },
  92. 'with allowDots true and decodeDotInKeys false'
  93. );
  94. st.deepEqual(
  95. qs.parse(
  96. 'name%252Eobj%252Esubobject.first%252Egodly%252Ename=John&name%252Eobj%252Esubobject.last=Doe',
  97. { allowDots: true, decodeDotInKeys: true }
  98. ),
  99. { 'name.obj.subobject': { 'first.godly.name': 'John', last: 'Doe' } },
  100. 'with allowDots true and decodeDotInKeys true'
  101. );
  102. st.deepEqual(
  103. qs.parse('name%252Eobj.first=John&name%252Eobj.last=Doe'),
  104. { 'name%2Eobj.first': 'John', 'name%2Eobj.last': 'Doe' },
  105. 'with allowDots and decodeDotInKeys undefined'
  106. );
  107. st.end();
  108. });
  109. t.test('should decode dot in key of object, and allow enabling dot notation when decodeDotInKeys is set to true and allowDots is undefined', function (st) {
  110. st.deepEqual(
  111. qs.parse(
  112. 'name%252Eobj%252Esubobject.first%252Egodly%252Ename=John&name%252Eobj%252Esubobject.last=Doe',
  113. { decodeDotInKeys: true }
  114. ),
  115. { 'name.obj.subobject': { 'first.godly.name': 'John', last: 'Doe' } },
  116. 'with allowDots undefined and decodeDotInKeys true'
  117. );
  118. st.end();
  119. });
  120. t.test('should throw when decodeDotInKeys is not of type boolean', function (st) {
  121. st['throws'](
  122. function () { qs.parse('foo[]&bar=baz', { decodeDotInKeys: 'foobar' }); },
  123. TypeError
  124. );
  125. st['throws'](
  126. function () { qs.parse('foo[]&bar=baz', { decodeDotInKeys: 0 }); },
  127. TypeError
  128. );
  129. st['throws'](
  130. function () { qs.parse('foo[]&bar=baz', { decodeDotInKeys: NaN }); },
  131. TypeError
  132. );
  133. st['throws'](
  134. function () { qs.parse('foo[]&bar=baz', { decodeDotInKeys: null }); },
  135. TypeError
  136. );
  137. st.end();
  138. });
  139. t.test('allows empty arrays in obj values', function (st) {
  140. st.deepEqual(qs.parse('foo[]&bar=baz', { allowEmptyArrays: true }), { foo: [], bar: 'baz' });
  141. st.deepEqual(qs.parse('foo[]&bar=baz', { allowEmptyArrays: false }), { foo: [''], bar: 'baz' });
  142. st.end();
  143. });
  144. t.test('should throw when allowEmptyArrays is not of type boolean', function (st) {
  145. st['throws'](
  146. function () { qs.parse('foo[]&bar=baz', { allowEmptyArrays: 'foobar' }); },
  147. TypeError
  148. );
  149. st['throws'](
  150. function () { qs.parse('foo[]&bar=baz', { allowEmptyArrays: 0 }); },
  151. TypeError
  152. );
  153. st['throws'](
  154. function () { qs.parse('foo[]&bar=baz', { allowEmptyArrays: NaN }); },
  155. TypeError
  156. );
  157. st['throws'](
  158. function () { qs.parse('foo[]&bar=baz', { allowEmptyArrays: null }); },
  159. TypeError
  160. );
  161. st.end();
  162. });
  163. t.test('allowEmptyArrays + strictNullHandling', function (st) {
  164. st.deepEqual(
  165. qs.parse('testEmptyArray[]', { strictNullHandling: true, allowEmptyArrays: true }),
  166. { testEmptyArray: [] }
  167. );
  168. st.end();
  169. });
  170. t.deepEqual(qs.parse('a[b]=c'), { a: { b: 'c' } }, 'parses a single nested string');
  171. t.deepEqual(qs.parse('a[b][c]=d'), { a: { b: { c: 'd' } } }, 'parses a double nested string');
  172. t.deepEqual(
  173. qs.parse('a[b][c][d][e][f][g][h]=i'),
  174. { a: { b: { c: { d: { e: { f: { '[g][h]': 'i' } } } } } } },
  175. 'defaults to a depth of 5'
  176. );
  177. t.test('only parses one level when depth = 1', function (st) {
  178. st.deepEqual(qs.parse('a[b][c]=d', { depth: 1 }), { a: { b: { '[c]': 'd' } } });
  179. st.deepEqual(qs.parse('a[b][c][d]=e', { depth: 1 }), { a: { b: { '[c][d]': 'e' } } });
  180. st.end();
  181. });
  182. t.test('uses original key when depth = 0', function (st) {
  183. st.deepEqual(qs.parse('a[0]=b&a[1]=c', { depth: 0 }), { 'a[0]': 'b', 'a[1]': 'c' });
  184. 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' });
  185. st.end();
  186. });
  187. t.test('uses original key when depth = false', function (st) {
  188. st.deepEqual(qs.parse('a[0]=b&a[1]=c', { depth: false }), { 'a[0]': 'b', 'a[1]': 'c' });
  189. 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' });
  190. st.end();
  191. });
  192. t.deepEqual(qs.parse('a=b&a=c'), { a: ['b', 'c'] }, 'parses a simple array');
  193. t.test('parses an explicit array', function (st) {
  194. st.deepEqual(qs.parse('a[]=b'), { a: ['b'] });
  195. st.deepEqual(qs.parse('a[]=b&a[]=c'), { a: ['b', 'c'] });
  196. st.deepEqual(qs.parse('a[]=b&a[]=c&a[]=d'), { a: ['b', 'c', 'd'] });
  197. st.end();
  198. });
  199. t.test('parses a mix of simple and explicit arrays', function (st) {
  200. st.deepEqual(qs.parse('a=b&a[]=c'), { a: ['b', 'c'] });
  201. st.deepEqual(qs.parse('a[]=b&a=c'), { a: ['b', 'c'] });
  202. st.deepEqual(qs.parse('a[0]=b&a=c'), { a: ['b', 'c'] });
  203. st.deepEqual(qs.parse('a=b&a[0]=c'), { a: ['b', 'c'] });
  204. st.deepEqual(qs.parse('a[1]=b&a=c', { arrayLimit: 20 }), { a: ['b', 'c'] });
  205. st.deepEqual(qs.parse('a[]=b&a=c', { arrayLimit: 0 }), { a: ['b', 'c'] });
  206. st.deepEqual(qs.parse('a[]=b&a=c'), { a: ['b', 'c'] });
  207. st.deepEqual(qs.parse('a=b&a[1]=c', { arrayLimit: 20 }), { a: ['b', 'c'] });
  208. st.deepEqual(qs.parse('a=b&a[]=c', { arrayLimit: 0 }), { a: ['b', 'c'] });
  209. st.deepEqual(qs.parse('a=b&a[]=c'), { a: ['b', 'c'] });
  210. st.end();
  211. });
  212. t.test('parses a nested array', function (st) {
  213. st.deepEqual(qs.parse('a[b][]=c&a[b][]=d'), { a: { b: ['c', 'd'] } });
  214. st.deepEqual(qs.parse('a[>=]=25'), { a: { '>=': '25' } });
  215. st.end();
  216. });
  217. t.test('allows to specify array indices', function (st) {
  218. st.deepEqual(qs.parse('a[1]=c&a[0]=b&a[2]=d'), { a: ['b', 'c', 'd'] });
  219. st.deepEqual(qs.parse('a[1]=c&a[0]=b'), { a: ['b', 'c'] });
  220. st.deepEqual(qs.parse('a[1]=c', { arrayLimit: 20 }), { a: ['c'] });
  221. st.deepEqual(qs.parse('a[1]=c', { arrayLimit: 0 }), { a: { 1: 'c' } });
  222. st.deepEqual(qs.parse('a[1]=c'), { a: ['c'] });
  223. st.end();
  224. });
  225. t.test('limits specific array indices to arrayLimit', function (st) {
  226. st.deepEqual(qs.parse('a[20]=a', { arrayLimit: 20 }), { a: ['a'] });
  227. st.deepEqual(qs.parse('a[21]=a', { arrayLimit: 20 }), { a: { 21: 'a' } });
  228. st.deepEqual(qs.parse('a[20]=a'), { a: ['a'] });
  229. st.deepEqual(qs.parse('a[21]=a'), { a: { 21: 'a' } });
  230. st.end();
  231. });
  232. t.deepEqual(qs.parse('a[12b]=c'), { a: { '12b': 'c' } }, 'supports keys that begin with a number');
  233. t.test('supports encoded = signs', function (st) {
  234. st.deepEqual(qs.parse('he%3Dllo=th%3Dere'), { 'he=llo': 'th=ere' });
  235. st.end();
  236. });
  237. t.test('is ok with url encoded strings', function (st) {
  238. st.deepEqual(qs.parse('a[b%20c]=d'), { a: { 'b c': 'd' } });
  239. st.deepEqual(qs.parse('a[b]=c%20d'), { a: { b: 'c d' } });
  240. st.end();
  241. });
  242. t.test('allows brackets in the value', function (st) {
  243. st.deepEqual(qs.parse('pets=["tobi"]'), { pets: '["tobi"]' });
  244. st.deepEqual(qs.parse('operators=[">=", "<="]'), { operators: '[">=", "<="]' });
  245. st.end();
  246. });
  247. t.test('allows empty values', function (st) {
  248. st.deepEqual(qs.parse(''), {});
  249. st.deepEqual(qs.parse(null), {});
  250. st.deepEqual(qs.parse(undefined), {});
  251. st.end();
  252. });
  253. t.test('transforms arrays to objects', function (st) {
  254. st.deepEqual(qs.parse('foo[0]=bar&foo[bad]=baz'), { foo: { 0: 'bar', bad: 'baz' } });
  255. st.deepEqual(qs.parse('foo[bad]=baz&foo[0]=bar'), { foo: { bad: 'baz', 0: 'bar' } });
  256. st.deepEqual(qs.parse('foo[bad]=baz&foo[]=bar'), { foo: { bad: 'baz', 0: 'bar' } });
  257. st.deepEqual(qs.parse('foo[]=bar&foo[bad]=baz'), { foo: { 0: 'bar', bad: 'baz' } });
  258. st.deepEqual(qs.parse('foo[bad]=baz&foo[]=bar&foo[]=foo'), { foo: { bad: 'baz', 0: 'bar', 1: 'foo' } });
  259. 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' }] });
  260. st.deepEqual(qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c', { allowPrototypes: false }), { a: { 0: 'b', t: 'u' } });
  261. st.deepEqual(qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c', { allowPrototypes: true }), { a: { 0: 'b', t: 'u', hasOwnProperty: 'c' } });
  262. st.deepEqual(qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y', { allowPrototypes: false }), { a: { 0: 'b', x: 'y' } });
  263. st.deepEqual(qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y', { allowPrototypes: true }), { a: { 0: 'b', hasOwnProperty: 'c', x: 'y' } });
  264. st.end();
  265. });
  266. t.test('transforms arrays to objects (dot notation)', function (st) {
  267. st.deepEqual(qs.parse('foo[0].baz=bar&fool.bad=baz', { allowDots: true }), { foo: [{ baz: 'bar' }], fool: { bad: 'baz' } });
  268. st.deepEqual(qs.parse('foo[0].baz=bar&fool.bad.boo=baz', { allowDots: true }), { foo: [{ baz: 'bar' }], fool: { bad: { boo: 'baz' } } });
  269. st.deepEqual(qs.parse('foo[0][0].baz=bar&fool.bad=baz', { allowDots: true }), { foo: [[{ baz: 'bar' }]], fool: { bad: 'baz' } });
  270. st.deepEqual(qs.parse('foo[0].baz[0]=15&foo[0].bar=2', { allowDots: true }), { foo: [{ baz: ['15'], bar: '2' }] });
  271. 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' }] });
  272. st.deepEqual(qs.parse('foo.bad=baz&foo[0]=bar', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar' } });
  273. st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar' } });
  274. st.deepEqual(qs.parse('foo[]=bar&foo.bad=baz', { allowDots: true }), { foo: { 0: 'bar', bad: 'baz' } });
  275. st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar&foo[]=foo', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar', 1: 'foo' } });
  276. 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' }] });
  277. st.end();
  278. });
  279. t.test('correctly prunes undefined values when converting an array to an object', function (st) {
  280. st.deepEqual(qs.parse('a[2]=b&a[99999999]=c'), { a: { 2: 'b', 99999999: 'c' } });
  281. st.end();
  282. });
  283. t.test('supports malformed uri characters', function (st) {
  284. st.deepEqual(qs.parse('{%:%}', { strictNullHandling: true }), { '{%:%}': null });
  285. st.deepEqual(qs.parse('{%:%}='), { '{%:%}': '' });
  286. st.deepEqual(qs.parse('foo=%:%}'), { foo: '%:%}' });
  287. st.end();
  288. });
  289. t.test('doesn\'t produce empty keys', function (st) {
  290. st.deepEqual(qs.parse('_r=1&'), { _r: '1' });
  291. st.end();
  292. });
  293. t.test('cannot access Object prototype', function (st) {
  294. qs.parse('constructor[prototype][bad]=bad');
  295. qs.parse('bad[constructor][prototype][bad]=bad');
  296. st.equal(typeof Object.prototype.bad, 'undefined');
  297. st.end();
  298. });
  299. t.test('parses arrays of objects', function (st) {
  300. st.deepEqual(qs.parse('a[][b]=c'), { a: [{ b: 'c' }] });
  301. st.deepEqual(qs.parse('a[0][b]=c'), { a: [{ b: 'c' }] });
  302. st.end();
  303. });
  304. t.test('allows for empty strings in arrays', function (st) {
  305. st.deepEqual(qs.parse('a[]=b&a[]=&a[]=c'), { a: ['b', '', 'c'] });
  306. st.deepEqual(
  307. qs.parse('a[0]=b&a[1]&a[2]=c&a[19]=', { strictNullHandling: true, arrayLimit: 20 }),
  308. { a: ['b', null, 'c', ''] },
  309. 'with arrayLimit 20 + array indices: null then empty string works'
  310. );
  311. st.deepEqual(
  312. qs.parse('a[]=b&a[]&a[]=c&a[]=', { strictNullHandling: true, arrayLimit: 0 }),
  313. { a: ['b', null, 'c', ''] },
  314. 'with arrayLimit 0 + array brackets: null then empty string works'
  315. );
  316. st.deepEqual(
  317. qs.parse('a[0]=b&a[1]=&a[2]=c&a[19]', { strictNullHandling: true, arrayLimit: 20 }),
  318. { a: ['b', '', 'c', null] },
  319. 'with arrayLimit 20 + array indices: empty string then null works'
  320. );
  321. st.deepEqual(
  322. qs.parse('a[]=b&a[]=&a[]=c&a[]', { strictNullHandling: true, arrayLimit: 0 }),
  323. { a: ['b', '', 'c', null] },
  324. 'with arrayLimit 0 + array brackets: empty string then null works'
  325. );
  326. st.deepEqual(
  327. qs.parse('a[]=&a[]=b&a[]=c'),
  328. { a: ['', 'b', 'c'] },
  329. 'array brackets: empty strings work'
  330. );
  331. st.end();
  332. });
  333. t.test('compacts sparse arrays', function (st) {
  334. st.deepEqual(qs.parse('a[10]=1&a[2]=2', { arrayLimit: 20 }), { a: ['2', '1'] });
  335. st.deepEqual(qs.parse('a[1][b][2][c]=1', { arrayLimit: 20 }), { a: [{ b: [{ c: '1' }] }] });
  336. st.deepEqual(qs.parse('a[1][2][3][c]=1', { arrayLimit: 20 }), { a: [[[{ c: '1' }]]] });
  337. st.deepEqual(qs.parse('a[1][2][3][c][1]=1', { arrayLimit: 20 }), { a: [[[{ c: ['1'] }]]] });
  338. st.end();
  339. });
  340. t.test('parses sparse arrays', function (st) {
  341. /* eslint no-sparse-arrays: 0 */
  342. st.deepEqual(qs.parse('a[4]=1&a[1]=2', { allowSparse: true }), { a: [, '2', , , '1'] });
  343. st.deepEqual(qs.parse('a[1][b][2][c]=1', { allowSparse: true }), { a: [, { b: [, , { c: '1' }] }] });
  344. st.deepEqual(qs.parse('a[1][2][3][c]=1', { allowSparse: true }), { a: [, [, , [, , , { c: '1' }]]] });
  345. st.deepEqual(qs.parse('a[1][2][3][c][1]=1', { allowSparse: true }), { a: [, [, , [, , , { c: [, '1'] }]]] });
  346. st.end();
  347. });
  348. t.test('parses semi-parsed strings', function (st) {
  349. st.deepEqual(qs.parse({ 'a[b]': 'c' }), { a: { b: 'c' } });
  350. st.deepEqual(qs.parse({ 'a[b]': 'c', 'a[d]': 'e' }), { a: { b: 'c', d: 'e' } });
  351. st.end();
  352. });
  353. t.test('parses buffers correctly', function (st) {
  354. var b = SaferBuffer.from('test');
  355. st.deepEqual(qs.parse({ a: b }), { a: b });
  356. st.end();
  357. });
  358. t.test('parses jquery-param strings', function (st) {
  359. // readable = 'filter[0][]=int1&filter[0][]==&filter[0][]=77&filter[]=and&filter[2][]=int2&filter[2][]==&filter[2][]=8'
  360. 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';
  361. var expected = { filter: [['int1', '=', '77'], 'and', ['int2', '=', '8']] };
  362. st.deepEqual(qs.parse(encoded), expected);
  363. st.end();
  364. });
  365. t.test('continues parsing when no parent is found', function (st) {
  366. st.deepEqual(qs.parse('[]=&a=b'), { 0: '', a: 'b' });
  367. st.deepEqual(qs.parse('[]&a=b', { strictNullHandling: true }), { 0: null, a: 'b' });
  368. st.deepEqual(qs.parse('[foo]=bar'), { foo: 'bar' });
  369. st.end();
  370. });
  371. t.test('does not error when parsing a very long array', function (st) {
  372. var str = 'a[]=a';
  373. while (Buffer.byteLength(str) < 128 * 1024) {
  374. str = str + '&' + str;
  375. }
  376. st.doesNotThrow(function () {
  377. qs.parse(str);
  378. });
  379. st.end();
  380. });
  381. t.test('should not throw when a native prototype has an enumerable property', function (st) {
  382. st.intercept(Object.prototype, 'crash', { value: '' });
  383. st.intercept(Array.prototype, 'crash', { value: '' });
  384. st.doesNotThrow(qs.parse.bind(null, 'a=b'));
  385. st.deepEqual(qs.parse('a=b'), { a: 'b' });
  386. st.doesNotThrow(qs.parse.bind(null, 'a[][b]=c'));
  387. st.deepEqual(qs.parse('a[][b]=c'), { a: [{ b: 'c' }] });
  388. st.end();
  389. });
  390. t.test('parses a string with an alternative string delimiter', function (st) {
  391. st.deepEqual(qs.parse('a=b;c=d', { delimiter: ';' }), { a: 'b', c: 'd' });
  392. st.end();
  393. });
  394. t.test('parses a string with an alternative RegExp delimiter', function (st) {
  395. st.deepEqual(qs.parse('a=b; c=d', { delimiter: /[;,] */ }), { a: 'b', c: 'd' });
  396. st.end();
  397. });
  398. t.test('does not use non-splittable objects as delimiters', function (st) {
  399. st.deepEqual(qs.parse('a=b&c=d', { delimiter: true }), { a: 'b', c: 'd' });
  400. st.end();
  401. });
  402. t.test('allows overriding parameter limit', function (st) {
  403. st.deepEqual(qs.parse('a=b&c=d', { parameterLimit: 1 }), { a: 'b' });
  404. st.end();
  405. });
  406. t.test('allows setting the parameter limit to Infinity', function (st) {
  407. st.deepEqual(qs.parse('a=b&c=d', { parameterLimit: Infinity }), { a: 'b', c: 'd' });
  408. st.end();
  409. });
  410. t.test('allows overriding array limit', function (st) {
  411. st.deepEqual(qs.parse('a[0]=b', { arrayLimit: -1 }), { a: { 0: 'b' } });
  412. st.deepEqual(qs.parse('a[0]=b', { arrayLimit: 0 }), { a: ['b'] });
  413. st.deepEqual(qs.parse('a[-1]=b', { arrayLimit: -1 }), { a: { '-1': 'b' } });
  414. st.deepEqual(qs.parse('a[-1]=b', { arrayLimit: 0 }), { a: { '-1': 'b' } });
  415. st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayLimit: -1 }), { a: { 0: 'b', 1: 'c' } });
  416. st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayLimit: 0 }), { a: { 0: 'b', 1: 'c' } });
  417. st.end();
  418. });
  419. t.test('allows disabling array parsing', function (st) {
  420. var indices = qs.parse('a[0]=b&a[1]=c', { parseArrays: false });
  421. st.deepEqual(indices, { a: { 0: 'b', 1: 'c' } });
  422. st.equal(Array.isArray(indices.a), false, 'parseArrays:false, indices case is not an array');
  423. var emptyBrackets = qs.parse('a[]=b', { parseArrays: false });
  424. st.deepEqual(emptyBrackets, { a: { 0: 'b' } });
  425. st.equal(Array.isArray(emptyBrackets.a), false, 'parseArrays:false, empty brackets case is not an array');
  426. st.end();
  427. });
  428. t.test('allows for query string prefix', function (st) {
  429. st.deepEqual(qs.parse('?foo=bar', { ignoreQueryPrefix: true }), { foo: 'bar' });
  430. st.deepEqual(qs.parse('foo=bar', { ignoreQueryPrefix: true }), { foo: 'bar' });
  431. st.deepEqual(qs.parse('?foo=bar', { ignoreQueryPrefix: false }), { '?foo': 'bar' });
  432. st.end();
  433. });
  434. t.test('parses an object', function (st) {
  435. var input = {
  436. 'user[name]': { 'pop[bob]': 3 },
  437. 'user[email]': null
  438. };
  439. var expected = {
  440. user: {
  441. name: { 'pop[bob]': 3 },
  442. email: null
  443. }
  444. };
  445. var result = qs.parse(input);
  446. st.deepEqual(result, expected);
  447. st.end();
  448. });
  449. t.test('parses string with comma as array divider', function (st) {
  450. st.deepEqual(qs.parse('foo=bar,tee', { comma: true }), { foo: ['bar', 'tee'] });
  451. st.deepEqual(qs.parse('foo[bar]=coffee,tee', { comma: true }), { foo: { bar: ['coffee', 'tee'] } });
  452. st.deepEqual(qs.parse('foo=', { comma: true }), { foo: '' });
  453. st.deepEqual(qs.parse('foo', { comma: true }), { foo: '' });
  454. st.deepEqual(qs.parse('foo', { comma: true, strictNullHandling: true }), { foo: null });
  455. // test cases inversed from from stringify tests
  456. st.deepEqual(qs.parse('a[0]=c'), { a: ['c'] });
  457. st.deepEqual(qs.parse('a[]=c'), { a: ['c'] });
  458. st.deepEqual(qs.parse('a[]=c', { comma: true }), { a: ['c'] });
  459. st.deepEqual(qs.parse('a[0]=c&a[1]=d'), { a: ['c', 'd'] });
  460. st.deepEqual(qs.parse('a[]=c&a[]=d'), { a: ['c', 'd'] });
  461. st.deepEqual(qs.parse('a=c,d', { comma: true }), { a: ['c', 'd'] });
  462. st.end();
  463. });
  464. t.test('parses values with comma as array divider', function (st) {
  465. st.deepEqual(qs.parse({ foo: 'bar,tee' }, { comma: false }), { foo: 'bar,tee' });
  466. st.deepEqual(qs.parse({ foo: 'bar,tee' }, { comma: true }), { foo: ['bar', 'tee'] });
  467. st.end();
  468. });
  469. t.test('use number decoder, parses string that has one number with comma option enabled', function (st) {
  470. var decoder = function (str, defaultDecoder, charset, type) {
  471. if (!isNaN(Number(str))) {
  472. return parseFloat(str);
  473. }
  474. return defaultDecoder(str, defaultDecoder, charset, type);
  475. };
  476. st.deepEqual(qs.parse('foo=1', { comma: true, decoder: decoder }), { foo: 1 });
  477. st.deepEqual(qs.parse('foo=0', { comma: true, decoder: decoder }), { foo: 0 });
  478. st.end();
  479. });
  480. t.test('parses brackets holds array of arrays when having two parts of strings with comma as array divider', function (st) {
  481. st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=4,5,6', { comma: true }), { foo: [['1', '2', '3'], ['4', '5', '6']] });
  482. st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=', { comma: true }), { foo: [['1', '2', '3'], ''] });
  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[]=a', { comma: true }), { foo: [['1', '2', '3'], 'a'] });
  485. st.end();
  486. });
  487. t.test('parses url-encoded brackets holds array of arrays when having two parts of strings with comma as array divider', function (st) {
  488. 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']] });
  489. st.deepEqual(qs.parse('foo%5B%5D=1,2,3&foo%5B%5D=', { comma: true }), { foo: [['1', '2', '3'], ''] });
  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=a', { comma: true }), { foo: [['1', '2', '3'], 'a'] });
  492. st.end();
  493. });
  494. t.test('parses comma delimited array while having percent-encoded comma treated as normal text', function (st) {
  495. st.deepEqual(qs.parse('foo=a%2Cb', { comma: true }), { foo: 'a,b' });
  496. st.deepEqual(qs.parse('foo=a%2C%20b,d', { comma: true }), { foo: ['a, b', 'd'] });
  497. st.deepEqual(qs.parse('foo=a%2C%20b,c%2C%20d', { comma: true }), { foo: ['a, b', 'c, d'] });
  498. st.end();
  499. });
  500. t.test('parses an object in dot notation', function (st) {
  501. var input = {
  502. 'user.name': { 'pop[bob]': 3 },
  503. 'user.email.': null
  504. };
  505. var expected = {
  506. user: {
  507. name: { 'pop[bob]': 3 },
  508. email: null
  509. }
  510. };
  511. var result = qs.parse(input, { allowDots: true });
  512. st.deepEqual(result, expected);
  513. st.end();
  514. });
  515. t.test('parses an object and not child values', function (st) {
  516. var input = {
  517. 'user[name]': { 'pop[bob]': { test: 3 } },
  518. 'user[email]': null
  519. };
  520. var expected = {
  521. user: {
  522. name: { 'pop[bob]': { test: 3 } },
  523. email: null
  524. }
  525. };
  526. var result = qs.parse(input);
  527. st.deepEqual(result, expected);
  528. st.end();
  529. });
  530. t.test('does not blow up when Buffer global is missing', function (st) {
  531. var restore = mockProperty(global, 'Buffer', { 'delete': true });
  532. var result = qs.parse('a=b&c=d');
  533. restore();
  534. st.deepEqual(result, { a: 'b', c: 'd' });
  535. st.end();
  536. });
  537. t.test('does not crash when parsing circular references', function (st) {
  538. var a = {};
  539. a.b = a;
  540. var parsed;
  541. st.doesNotThrow(function () {
  542. parsed = qs.parse({ 'foo[bar]': 'baz', 'foo[baz]': a });
  543. });
  544. st.equal('foo' in parsed, true, 'parsed has "foo" property');
  545. st.equal('bar' in parsed.foo, true);
  546. st.equal('baz' in parsed.foo, true);
  547. st.equal(parsed.foo.bar, 'baz');
  548. st.deepEqual(parsed.foo.baz, a);
  549. st.end();
  550. });
  551. t.test('does not crash when parsing deep objects', function (st) {
  552. var parsed;
  553. var str = 'foo';
  554. for (var i = 0; i < 5000; i++) {
  555. str += '[p]';
  556. }
  557. str += '=bar';
  558. st.doesNotThrow(function () {
  559. parsed = qs.parse(str, { depth: 5000 });
  560. });
  561. st.equal('foo' in parsed, true, 'parsed has "foo" property');
  562. var depth = 0;
  563. var ref = parsed.foo;
  564. while ((ref = ref.p)) {
  565. depth += 1;
  566. }
  567. st.equal(depth, 5000, 'parsed is 5000 properties deep');
  568. st.end();
  569. });
  570. t.test('parses null objects correctly', { skip: !Object.create }, function (st) {
  571. var a = Object.create(null);
  572. a.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: !Object.create }, function (st) {
  719. var expected = Object.create(null);
  720. expected.a = Object.create(null);
  721. expected.a.b = 'c';
  722. expected.a.hasOwnProperty = 'd';
  723. st.deepEqual(qs.parse('a[b]=c&a[hasOwnProperty]=d', { plainObjects: true }), expected);
  724. st.deepEqual(qs.parse(null, { plainObjects: true }), Object.create(null));
  725. var expectedArray = Object.create(null);
  726. expectedArray.a = Object.create(null);
  727. expectedArray.a[0] = 'b';
  728. expectedArray.a.c = 'd';
  729. st.deepEqual(qs.parse('a[]=b&a[c]=d', { plainObjects: true }), expectedArray);
  730. st.end();
  731. });
  732. t.test('can parse with custom encoding', function (st) {
  733. st.deepEqual(qs.parse('%8c%a7=%91%e5%8d%e3%95%7b', {
  734. decoder: function (str) {
  735. var reg = /%([0-9A-F]{2})/ig;
  736. var result = [];
  737. var parts = reg.exec(str);
  738. while (parts) {
  739. result.push(parseInt(parts[1], 16));
  740. parts = reg.exec(str);
  741. }
  742. return String(iconv.decode(SaferBuffer.from(result), 'shift_jis'));
  743. }
  744. }), { 県: '大阪府' });
  745. st.end();
  746. });
  747. t.test('receives the default decoder as a second argument', function (st) {
  748. st.plan(1);
  749. qs.parse('a', {
  750. decoder: function (str, defaultDecoder) {
  751. st.equal(defaultDecoder, utils.decode);
  752. }
  753. });
  754. st.end();
  755. });
  756. t.test('throws error with wrong decoder', function (st) {
  757. st['throws'](function () {
  758. qs.parse({}, { decoder: 'string' });
  759. }, new TypeError('Decoder has to be a function.'));
  760. st.end();
  761. });
  762. t.test('does not mutate the options argument', function (st) {
  763. var options = {};
  764. qs.parse('a[b]=true', options);
  765. st.deepEqual(options, {});
  766. st.end();
  767. });
  768. t.test('throws if an invalid charset is specified', function (st) {
  769. st['throws'](function () {
  770. qs.parse('a=b', { charset: 'foobar' });
  771. }, new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined'));
  772. st.end();
  773. });
  774. t.test('parses an iso-8859-1 string if asked to', function (st) {
  775. st.deepEqual(qs.parse('%A2=%BD', { charset: 'iso-8859-1' }), { '¢': '½' });
  776. st.end();
  777. });
  778. var urlEncodedCheckmarkInUtf8 = '%E2%9C%93';
  779. var urlEncodedOSlashInUtf8 = '%C3%B8';
  780. var urlEncodedNumCheckmark = '%26%2310003%3B';
  781. var urlEncodedNumSmiley = '%26%239786%3B';
  782. t.test('prefers an utf-8 charset specified by the utf8 sentinel to a default charset of iso-8859-1', function (st) {
  783. st.deepEqual(qs.parse('utf8=' + urlEncodedCheckmarkInUtf8 + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'iso-8859-1' }), { ø: 'ø' });
  784. st.end();
  785. });
  786. t.test('prefers an iso-8859-1 charset specified by the utf8 sentinel to a default charset of utf-8', function (st) {
  787. st.deepEqual(qs.parse('utf8=' + urlEncodedNumCheckmark + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'utf-8' }), { 'ø': 'ø' });
  788. st.end();
  789. });
  790. t.test('does not require the utf8 sentinel to be defined before the parameters whose decoding it affects', function (st) {
  791. st.deepEqual(qs.parse('a=' + urlEncodedOSlashInUtf8 + '&utf8=' + urlEncodedNumCheckmark, { charsetSentinel: true, charset: 'utf-8' }), { a: 'ø' });
  792. st.end();
  793. });
  794. t.test('should ignore an utf8 sentinel with an unknown value', function (st) {
  795. st.deepEqual(qs.parse('utf8=foo&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'utf-8' }), { ø: 'ø' });
  796. st.end();
  797. });
  798. t.test('uses the utf8 sentinel to switch to utf-8 when no default charset is given', function (st) {
  799. st.deepEqual(qs.parse('utf8=' + urlEncodedCheckmarkInUtf8 + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true }), { ø: 'ø' });
  800. st.end();
  801. });
  802. t.test('uses the utf8 sentinel to switch to iso-8859-1 when no default charset is given', function (st) {
  803. st.deepEqual(qs.parse('utf8=' + urlEncodedNumCheckmark + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true }), { 'ø': 'ø' });
  804. st.end();
  805. });
  806. t.test('interprets numeric entities in iso-8859-1 when `interpretNumericEntities`', function (st) {
  807. st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'iso-8859-1', interpretNumericEntities: true }), { foo: '☺' });
  808. st.end();
  809. });
  810. t.test('handles a custom decoder returning `null`, in the `iso-8859-1` charset, when `interpretNumericEntities`', function (st) {
  811. st.deepEqual(qs.parse('foo=&bar=' + urlEncodedNumSmiley, {
  812. charset: 'iso-8859-1',
  813. decoder: function (str, defaultDecoder, charset) {
  814. return str ? defaultDecoder(str, defaultDecoder, charset) : null;
  815. },
  816. interpretNumericEntities: true
  817. }), { foo: null, bar: '☺' });
  818. st.end();
  819. });
  820. t.test('does not interpret numeric entities in iso-8859-1 when `interpretNumericEntities` is absent', function (st) {
  821. st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'iso-8859-1' }), { foo: '&#9786;' });
  822. st.end();
  823. });
  824. t.test('does not interpret numeric entities when the charset is utf-8, even when `interpretNumericEntities`', function (st) {
  825. st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'utf-8', interpretNumericEntities: true }), { foo: '&#9786;' });
  826. st.end();
  827. });
  828. t.test('does not interpret %uXXXX syntax in iso-8859-1 mode', function (st) {
  829. st.deepEqual(qs.parse('%u263A=%u263A', { charset: 'iso-8859-1' }), { '%u263A': '%u263A' });
  830. st.end();
  831. });
  832. t.test('allows for decoding keys and values differently', function (st) {
  833. var decoder = function (str, defaultDecoder, charset, type) {
  834. if (type === 'key') {
  835. return defaultDecoder(str, defaultDecoder, charset, type).toLowerCase();
  836. }
  837. if (type === 'value') {
  838. return defaultDecoder(str, defaultDecoder, charset, type).toUpperCase();
  839. }
  840. throw 'this should never happen! type: ' + type;
  841. };
  842. st.deepEqual(qs.parse('KeY=vAlUe', { decoder: decoder }), { key: 'VALUE' });
  843. st.end();
  844. });
  845. t.end();
  846. });
  847. test('parses empty keys', function (t) {
  848. emptyTestCases.forEach(function (testCase) {
  849. t.test('skips empty string key with ' + testCase.input, function (st) {
  850. st.deepEqual(qs.parse(testCase.input), testCase.noEmptyKeys);
  851. st.end();
  852. });
  853. });
  854. });
  855. test('`duplicates` option', function (t) {
  856. v.nonStrings.concat('not a valid option').forEach(function (invalidOption) {
  857. if (typeof invalidOption !== 'undefined') {
  858. t['throws'](
  859. function () { qs.parse('', { duplicates: invalidOption }); },
  860. TypeError,
  861. 'throws on invalid option: ' + inspect(invalidOption)
  862. );
  863. }
  864. });
  865. t.deepEqual(
  866. qs.parse('foo=bar&foo=baz'),
  867. { foo: ['bar', 'baz'] },
  868. 'duplicates: default, combine'
  869. );
  870. t.deepEqual(
  871. qs.parse('foo=bar&foo=baz', { duplicates: 'combine' }),
  872. { foo: ['bar', 'baz'] },
  873. 'duplicates: combine'
  874. );
  875. t.deepEqual(
  876. qs.parse('foo=bar&foo=baz', { duplicates: 'first' }),
  877. { foo: 'bar' },
  878. 'duplicates: first'
  879. );
  880. t.deepEqual(
  881. qs.parse('foo=bar&foo=baz', { duplicates: 'last' }),
  882. { foo: 'baz' },
  883. 'duplicates: last'
  884. );
  885. t.end();
  886. });
  887. test('qs strictDepth option - throw cases', function (t) {
  888. t.test('throws an exception when depth exceeds the limit with strictDepth: true', function (st) {
  889. st['throws'](
  890. function () {
  891. qs.parse('a[b][c][d][e][f][g][h][i]=j', { depth: 1, strictDepth: true });
  892. },
  893. RangeError,
  894. 'Should throw RangeError'
  895. );
  896. st.end();
  897. });
  898. t.test('throws an exception for multiple nested arrays with strictDepth: true', function (st) {
  899. st['throws'](
  900. function () {
  901. qs.parse('a[0][1][2][3][4]=b', { depth: 3, strictDepth: true });
  902. },
  903. RangeError,
  904. 'Should throw RangeError'
  905. );
  906. st.end();
  907. });
  908. t.test('throws an exception for nested objects and arrays with strictDepth: true', function (st) {
  909. st['throws'](
  910. function () {
  911. qs.parse('a[b][c][0][d][e]=f', { depth: 3, strictDepth: true });
  912. },
  913. RangeError,
  914. 'Should throw RangeError'
  915. );
  916. st.end();
  917. });
  918. t.test('throws an exception for different types of values with strictDepth: true', function (st) {
  919. st['throws'](
  920. function () {
  921. qs.parse('a[b][c][d][e]=true&a[b][c][d][f]=42', { depth: 3, strictDepth: true });
  922. },
  923. RangeError,
  924. 'Should throw RangeError'
  925. );
  926. st.end();
  927. });
  928. });
  929. test('qs strictDepth option - non-throw cases', function (t) {
  930. t.test('when depth is 0 and strictDepth true, do not throw', function (st) {
  931. st.doesNotThrow(
  932. function () {
  933. qs.parse('a[b][c][d][e]=true&a[b][c][d][f]=42', { depth: 0, strictDepth: true });
  934. },
  935. RangeError,
  936. 'Should not throw RangeError'
  937. );
  938. st.end();
  939. });
  940. t.test('parses successfully when depth is within the limit with strictDepth: true', function (st) {
  941. st.doesNotThrow(
  942. function () {
  943. var result = qs.parse('a[b]=c', { depth: 1, strictDepth: true });
  944. st.deepEqual(result, { a: { b: 'c' } }, 'Should parse correctly');
  945. }
  946. );
  947. st.end();
  948. });
  949. t.test('does not throw an exception when depth exceeds the limit with strictDepth: false', function (st) {
  950. st.doesNotThrow(
  951. function () {
  952. var result = qs.parse('a[b][c][d][e][f][g][h][i]=j', { depth: 1 });
  953. st.deepEqual(result, { a: { b: { '[c][d][e][f][g][h][i]': 'j' } } }, 'Should parse with depth limit');
  954. }
  955. );
  956. st.end();
  957. });
  958. t.test('parses successfully when depth is within the limit with strictDepth: false', function (st) {
  959. st.doesNotThrow(
  960. function () {
  961. var result = qs.parse('a[b]=c', { depth: 1 });
  962. st.deepEqual(result, { a: { b: 'c' } }, 'Should parse correctly');
  963. }
  964. );
  965. st.end();
  966. });
  967. t.test('does not throw when depth is exactly at the limit with strictDepth: true', function (st) {
  968. st.doesNotThrow(
  969. function () {
  970. var result = qs.parse('a[b][c]=d', { depth: 2, strictDepth: true });
  971. st.deepEqual(result, { a: { b: { c: 'd' } } }, 'Should parse correctly');
  972. }
  973. );
  974. st.end();
  975. });
  976. });