parse.js 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568
  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: { 0: 'b', 1: '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: { 0: 'b', 1: '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[19]=a', { arrayLimit: 20 }), { a: ['a'] });
  228. st.deepEqual(qs.parse('a[20]=a', { arrayLimit: 20 }), { a: { 20: 'a' } });
  229. st.deepEqual(qs.parse('a[19]=a'), { a: ['a'] });
  230. st.deepEqual(qs.parse('a[20]=a'), { a: { 20: '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: { 0: 'b', 1: null, 2: 'c', 3: '' } },
  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: { 0: 'b', 1: '', 2: 'c', 3: 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: { 0: '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', { strictMerge: false }),
  647. { a: { b: 'c', d: true } },
  648. 'can add keys to objects'
  649. );
  650. st.deepEqual(
  651. qs.parse('a[b]=c&a=toString', { strictMerge: false }),
  652. { a: { b: 'c' } },
  653. 'can not overwrite prototype'
  654. );
  655. st.deepEqual(
  656. qs.parse('a[b]=c&a=toString', { strictMerge: false, 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', { strictMerge: false, 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('strictMerge wraps object and primitive into an array', function (st) {
  668. st.deepEqual(
  669. qs.parse('a[b]=c&a=d'),
  670. { a: [{ b: 'c' }, 'd'] },
  671. 'object then primitive produces array'
  672. );
  673. st.deepEqual(
  674. qs.parse('a=d&a[b]=c'),
  675. { a: ['d', { b: 'c' }] },
  676. 'primitive then object produces array'
  677. );
  678. st.deepEqual(
  679. qs.parse('a[b]=c&a=toString'),
  680. { a: [{ b: 'c' }, 'toString'] },
  681. 'prototype-colliding value is preserved in array'
  682. );
  683. st.deepEqual(
  684. qs.parse('a[b]=c&a=toString', { plainObjects: true }),
  685. { __proto__: null, a: [{ __proto__: null, b: 'c' }, 'toString'] },
  686. 'plainObjects preserved in array wrapping'
  687. );
  688. st.end();
  689. });
  690. t.test('dunder proto is ignored', function (st) {
  691. var payload = 'categories[__proto__]=login&categories[__proto__]&categories[length]=42';
  692. var result = qs.parse(payload, { allowPrototypes: true });
  693. st.deepEqual(
  694. result,
  695. {
  696. categories: {
  697. length: '42'
  698. }
  699. },
  700. 'silent [[Prototype]] payload'
  701. );
  702. var plainResult = qs.parse(payload, { allowPrototypes: true, plainObjects: true });
  703. st.deepEqual(
  704. plainResult,
  705. {
  706. __proto__: null,
  707. categories: {
  708. __proto__: null,
  709. length: '42'
  710. }
  711. },
  712. 'silent [[Prototype]] payload: plain objects'
  713. );
  714. var query = qs.parse('categories[__proto__]=cats&categories[__proto__]=dogs&categories[some][json]=toInject', { allowPrototypes: true });
  715. st.notOk(Array.isArray(query.categories), 'is not an array');
  716. st.notOk(query.categories instanceof Array, 'is not instanceof an array');
  717. st.deepEqual(query.categories, { some: { json: 'toInject' } });
  718. st.equal(JSON.stringify(query.categories), '{"some":{"json":"toInject"}}', 'stringifies as a non-array');
  719. st.deepEqual(
  720. qs.parse('foo[__proto__][hidden]=value&foo[bar]=stuffs', { allowPrototypes: true }),
  721. {
  722. foo: {
  723. bar: 'stuffs'
  724. }
  725. },
  726. 'hidden values'
  727. );
  728. st.deepEqual(
  729. qs.parse('foo[__proto__][hidden]=value&foo[bar]=stuffs', { allowPrototypes: true, plainObjects: true }),
  730. {
  731. __proto__: null,
  732. foo: {
  733. __proto__: null,
  734. bar: 'stuffs'
  735. }
  736. },
  737. 'hidden values: plain objects'
  738. );
  739. st.end();
  740. });
  741. t.test('can return null objects', { skip: !hasProto }, function (st) {
  742. var expected = {
  743. __proto__: null,
  744. a: {
  745. __proto__: null,
  746. b: 'c',
  747. hasOwnProperty: 'd'
  748. }
  749. };
  750. st.deepEqual(qs.parse('a[b]=c&a[hasOwnProperty]=d', { plainObjects: true }), expected);
  751. st.deepEqual(qs.parse(null, { plainObjects: true }), { __proto__: null });
  752. var expectedArray = {
  753. __proto__: null,
  754. a: {
  755. __proto__: null,
  756. 0: 'b',
  757. c: 'd'
  758. }
  759. };
  760. st.deepEqual(qs.parse('a[]=b&a[c]=d', { plainObjects: true }), expectedArray);
  761. st.end();
  762. });
  763. t.test('can parse with custom encoding', function (st) {
  764. st.deepEqual(qs.parse('%8c%a7=%91%e5%8d%e3%95%7b', {
  765. decoder: function (str) {
  766. var reg = /%([0-9A-F]{2})/ig;
  767. var result = [];
  768. var parts = reg.exec(str);
  769. while (parts) {
  770. result.push(parseInt(parts[1], 16));
  771. parts = reg.exec(str);
  772. }
  773. return String(iconv.decode(SaferBuffer.from(result), 'shift_jis'));
  774. }
  775. }), { 県: '大阪府' });
  776. st.end();
  777. });
  778. t.test('receives the default decoder as a second argument', function (st) {
  779. st.plan(1);
  780. qs.parse('a', {
  781. decoder: function (str, defaultDecoder) {
  782. st.equal(defaultDecoder, utils.decode);
  783. }
  784. });
  785. st.end();
  786. });
  787. t.test('throws error with wrong decoder', function (st) {
  788. st['throws'](function () {
  789. qs.parse({}, { decoder: 'string' });
  790. }, new TypeError('Decoder has to be a function.'));
  791. st.end();
  792. });
  793. t.test('does not mutate the options argument', function (st) {
  794. var options = {};
  795. qs.parse('a[b]=true', options);
  796. st.deepEqual(options, {});
  797. st.end();
  798. });
  799. t.test('throws if an invalid charset is specified', function (st) {
  800. st['throws'](function () {
  801. qs.parse('a=b', { charset: 'foobar' });
  802. }, new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined'));
  803. st.end();
  804. });
  805. t.test('parses an iso-8859-1 string if asked to', function (st) {
  806. st.deepEqual(qs.parse('%A2=%BD', { charset: 'iso-8859-1' }), { '¢': '½' });
  807. st.end();
  808. });
  809. var urlEncodedCheckmarkInUtf8 = '%E2%9C%93';
  810. var urlEncodedOSlashInUtf8 = '%C3%B8';
  811. var urlEncodedNumCheckmark = '%26%2310003%3B';
  812. var urlEncodedNumSmiley = '%26%239786%3B';
  813. t.test('prefers an utf-8 charset specified by the utf8 sentinel to a default charset of iso-8859-1', function (st) {
  814. st.deepEqual(qs.parse('utf8=' + urlEncodedCheckmarkInUtf8 + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'iso-8859-1' }), { ø: 'ø' });
  815. st.end();
  816. });
  817. t.test('prefers an iso-8859-1 charset specified by the utf8 sentinel to a default charset of utf-8', function (st) {
  818. st.deepEqual(qs.parse('utf8=' + urlEncodedNumCheckmark + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'utf-8' }), { 'ø': 'ø' });
  819. st.end();
  820. });
  821. t.test('does not require the utf8 sentinel to be defined before the parameters whose decoding it affects', function (st) {
  822. st.deepEqual(qs.parse('a=' + urlEncodedOSlashInUtf8 + '&utf8=' + urlEncodedNumCheckmark, { charsetSentinel: true, charset: 'utf-8' }), { a: 'ø' });
  823. st.end();
  824. });
  825. t.test('ignores an utf8 sentinel with an unknown value', function (st) {
  826. st.deepEqual(qs.parse('utf8=foo&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'utf-8' }), { ø: 'ø' });
  827. st.end();
  828. });
  829. t.test('uses the utf8 sentinel to switch to utf-8 when no default charset is given', function (st) {
  830. st.deepEqual(qs.parse('utf8=' + urlEncodedCheckmarkInUtf8 + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true }), { ø: 'ø' });
  831. st.end();
  832. });
  833. t.test('uses the utf8 sentinel to switch to iso-8859-1 when no default charset is given', function (st) {
  834. st.deepEqual(qs.parse('utf8=' + urlEncodedNumCheckmark + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true }), { 'ø': 'ø' });
  835. st.end();
  836. });
  837. t.test('interprets numeric entities in iso-8859-1 when `interpretNumericEntities`', function (st) {
  838. st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'iso-8859-1', interpretNumericEntities: true }), { foo: '☺' });
  839. st.end();
  840. });
  841. t.test('handles a custom decoder returning `null`, in the `iso-8859-1` charset, when `interpretNumericEntities`', function (st) {
  842. st.deepEqual(qs.parse('foo=&bar=' + urlEncodedNumSmiley, {
  843. charset: 'iso-8859-1',
  844. decoder: function (str, defaultDecoder, charset) {
  845. return str ? defaultDecoder(str, defaultDecoder, charset) : null;
  846. },
  847. interpretNumericEntities: true
  848. }), { foo: null, bar: '☺' });
  849. st.end();
  850. });
  851. t.test('handles a custom decoder returning `null`, with a string key of `null`', function (st) {
  852. st.deepEqual(
  853. qs.parse('null=1&ToNull=2', {
  854. decoder: function (str, defaultDecoder, charset) {
  855. return str === 'ToNull' ? null : defaultDecoder(str, defaultDecoder, charset);
  856. }
  857. }),
  858. { 'null': '1' },
  859. '"null" key is not overridden by `null` decoder result'
  860. );
  861. st.end();
  862. });
  863. t.test('does not interpret numeric entities in iso-8859-1 when `interpretNumericEntities` is absent', function (st) {
  864. st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'iso-8859-1' }), { foo: '&#9786;' });
  865. st.end();
  866. });
  867. t.test('does not interpret numeric entities when the charset is utf-8, even when `interpretNumericEntities`', function (st) {
  868. st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'utf-8', interpretNumericEntities: true }), { foo: '&#9786;' });
  869. st.end();
  870. });
  871. t.test('interpretNumericEntities with comma:true and iso charset does not crash', function (st) {
  872. st.deepEqual(
  873. qs.parse('b&a[]=1,' + urlEncodedNumSmiley, { comma: true, charset: 'iso-8859-1', interpretNumericEntities: true }),
  874. { b: '', a: ['1,☺'] }
  875. );
  876. st.end();
  877. });
  878. t.test('does not interpret %uXXXX syntax in iso-8859-1 mode', function (st) {
  879. st.deepEqual(qs.parse('%u263A=%u263A', { charset: 'iso-8859-1' }), { '%u263A': '%u263A' });
  880. st.end();
  881. });
  882. t.test('allows for decoding keys and values differently', function (st) {
  883. var decoder = function (str, defaultDecoder, charset, type) {
  884. if (type === 'key') {
  885. return defaultDecoder(str, defaultDecoder, charset, type).toLowerCase();
  886. }
  887. if (type === 'value') {
  888. return defaultDecoder(str, defaultDecoder, charset, type).toUpperCase();
  889. }
  890. throw 'this should never happen! type: ' + type;
  891. };
  892. st.deepEqual(qs.parse('KeY=vAlUe', { decoder: decoder }), { key: 'VALUE' });
  893. st.end();
  894. });
  895. t.test('parameter limit tests', function (st) {
  896. st.test('does not throw error when within parameter limit', function (sst) {
  897. var result = qs.parse('a=1&b=2&c=3', { parameterLimit: 5, throwOnLimitExceeded: true });
  898. sst.deepEqual(result, { a: '1', b: '2', c: '3' }, 'parses without errors');
  899. sst.end();
  900. });
  901. st.test('throws error when throwOnLimitExceeded is present but not boolean', function (sst) {
  902. sst['throws'](
  903. function () {
  904. qs.parse('a=1&b=2&c=3&d=4&e=5&f=6', { parameterLimit: 3, throwOnLimitExceeded: 'true' });
  905. },
  906. new TypeError('`throwOnLimitExceeded` option must be a boolean'),
  907. 'throws error when throwOnLimitExceeded is present and not boolean'
  908. );
  909. sst.end();
  910. });
  911. st.test('throws error when parameter limit exceeded', function (sst) {
  912. sst['throws'](
  913. function () {
  914. qs.parse('a=1&b=2&c=3&d=4&e=5&f=6', { parameterLimit: 3, throwOnLimitExceeded: true });
  915. },
  916. new RangeError('Parameter limit exceeded. Only 3 parameters allowed.'),
  917. 'throws error when parameter limit is exceeded'
  918. );
  919. sst.end();
  920. });
  921. st.test('silently truncates when throwOnLimitExceeded is not given', function (sst) {
  922. var result = qs.parse('a=1&b=2&c=3&d=4&e=5', { parameterLimit: 3 });
  923. sst.deepEqual(result, { a: '1', b: '2', c: '3' }, 'parses and truncates silently');
  924. sst.end();
  925. });
  926. st.test('silently truncates when parameter limit exceeded without error', function (sst) {
  927. var result = qs.parse('a=1&b=2&c=3&d=4&e=5', { parameterLimit: 3, throwOnLimitExceeded: false });
  928. sst.deepEqual(result, { a: '1', b: '2', c: '3' }, 'parses and truncates silently');
  929. sst.end();
  930. });
  931. st.test('allows unlimited parameters when parameterLimit set to Infinity', function (sst) {
  932. var result = qs.parse('a=1&b=2&c=3&d=4&e=5&f=6', { parameterLimit: Infinity });
  933. sst.deepEqual(result, { a: '1', b: '2', c: '3', d: '4', e: '5', f: '6' }, 'parses all parameters without truncation');
  934. sst.end();
  935. });
  936. st.end();
  937. });
  938. t.test('array limit tests', function (st) {
  939. st.test('does not throw error when array is within limit', function (sst) {
  940. var result = qs.parse('a[]=1&a[]=2&a[]=3', { arrayLimit: 5, throwOnLimitExceeded: true });
  941. sst.deepEqual(result, { a: ['1', '2', '3'] }, 'parses array without errors');
  942. sst.end();
  943. });
  944. st.test('throws error when throwOnLimitExceeded is present but not boolean for array limit', function (sst) {
  945. sst['throws'](
  946. function () {
  947. qs.parse('a[]=1&a[]=2&a[]=3&a[]=4', { arrayLimit: 3, throwOnLimitExceeded: 'true' });
  948. },
  949. new TypeError('`throwOnLimitExceeded` option must be a boolean'),
  950. 'throws error when throwOnLimitExceeded is present and not boolean for array limit'
  951. );
  952. sst.end();
  953. });
  954. st.test('throws error when array limit exceeded', function (sst) {
  955. // 4 elements exceeds limit of 3
  956. sst['throws'](
  957. function () {
  958. qs.parse('a[]=1&a[]=2&a[]=3&a[]=4', { arrayLimit: 3, throwOnLimitExceeded: true });
  959. },
  960. new RangeError('Array limit exceeded. Only 3 elements allowed in an array.'),
  961. 'throws error when array limit is exceeded'
  962. );
  963. sst.end();
  964. });
  965. st.test('does not throw when at limit', function (sst) {
  966. // 3 elements = limit of 3, should not throw
  967. var result = qs.parse('a[]=1&a[]=2&a[]=3', { arrayLimit: 3, throwOnLimitExceeded: true });
  968. sst.ok(Array.isArray(result.a), 'result is an array');
  969. sst.deepEqual(result.a, ['1', '2', '3'], 'all values present');
  970. sst.end();
  971. });
  972. st.test('converts array to object if length is greater than limit', function (sst) {
  973. var result = qs.parse('a[1]=1&a[2]=2&a[3]=3&a[4]=4&a[5]=5&a[6]=6', { arrayLimit: 5 });
  974. 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');
  975. sst.end();
  976. });
  977. st.test('throws error when indexed notation exceeds arrayLimit with throwOnLimitExceeded', function (sst) {
  978. sst['throws'](
  979. function () {
  980. qs.parse('a[1001]=b', { arrayLimit: 1000, throwOnLimitExceeded: true });
  981. },
  982. new RangeError('Array limit exceeded. Only 1000 elements allowed in an array.'),
  983. 'throws error for a single index exceeding arrayLimit'
  984. );
  985. sst['throws'](
  986. function () {
  987. qs.parse('a[0]=1&a[1]=2&a[2]=3&a[10]=4', { arrayLimit: 6, throwOnLimitExceeded: true, allowSparse: true });
  988. },
  989. new RangeError('Array limit exceeded. Only 6 elements allowed in an array.'),
  990. 'throws error when a sparse index exceeds arrayLimit'
  991. );
  992. sst.end();
  993. });
  994. st.test('does not throw for indexed notation within arrayLimit with throwOnLimitExceeded', function (sst) {
  995. var result = qs.parse('a[4]=b', { arrayLimit: 5, throwOnLimitExceeded: true, allowSparse: true });
  996. sst.ok(Array.isArray(result.a), 'result is an array');
  997. sst.equal(result.a.length, 5, 'array has correct length');
  998. sst.equal(result.a[4], 'b', 'value at index 4 is correct');
  999. sst.end();
  1000. });
  1001. st.test('silently converts to object for indexed notation exceeding arrayLimit without throwOnLimitExceeded', function (sst) {
  1002. var result = qs.parse('a[1001]=b', { arrayLimit: 1000 });
  1003. sst.deepEqual(result, { a: { 1001: 'b' } }, 'converts to object without throwing');
  1004. sst.end();
  1005. });
  1006. st.end();
  1007. });
  1008. t.end();
  1009. });
  1010. test('parses empty keys', function (t) {
  1011. emptyTestCases.forEach(function (testCase) {
  1012. t.test('skips empty string key with ' + testCase.input, function (st) {
  1013. st.deepEqual(qs.parse(testCase.input), testCase.noEmptyKeys);
  1014. st.end();
  1015. });
  1016. });
  1017. });
  1018. test('`duplicates` option', function (t) {
  1019. v.nonStrings.concat('not a valid option').forEach(function (invalidOption) {
  1020. if (typeof invalidOption !== 'undefined') {
  1021. t['throws'](
  1022. function () { qs.parse('', { duplicates: invalidOption }); },
  1023. TypeError,
  1024. 'throws on invalid option: ' + inspect(invalidOption)
  1025. );
  1026. }
  1027. });
  1028. t.deepEqual(
  1029. qs.parse('foo=bar&foo=baz'),
  1030. { foo: ['bar', 'baz'] },
  1031. 'duplicates: default, combine'
  1032. );
  1033. t.deepEqual(
  1034. qs.parse('foo=bar&foo=baz', { duplicates: 'combine' }),
  1035. { foo: ['bar', 'baz'] },
  1036. 'duplicates: combine'
  1037. );
  1038. t.deepEqual(
  1039. qs.parse('foo=bar&foo=baz', { duplicates: 'first' }),
  1040. { foo: 'bar' },
  1041. 'duplicates: first'
  1042. );
  1043. t.deepEqual(
  1044. qs.parse('foo=bar&foo=baz', { duplicates: 'last' }),
  1045. { foo: 'baz' },
  1046. 'duplicates: last'
  1047. );
  1048. t.test('bracket notation always combines regardless of duplicates', function (st) {
  1049. st.deepEqual(
  1050. qs.parse('a=1&a=2&b[]=1&b[]=2', { duplicates: 'last' }),
  1051. { a: '2', b: ['1', '2'] },
  1052. 'duplicates last: unbracketed takes last, bracketed combines'
  1053. );
  1054. st.deepEqual(
  1055. qs.parse('b[]=1&b[]=2', { duplicates: 'last' }),
  1056. { b: ['1', '2'] },
  1057. 'duplicates last: bracketed always combines'
  1058. );
  1059. st.deepEqual(
  1060. qs.parse('b[]=1&b[]=2', { duplicates: 'first' }),
  1061. { b: ['1', '2'] },
  1062. 'duplicates first: bracketed always combines'
  1063. );
  1064. st.deepEqual(
  1065. qs.parse('a=1&a=2&b[]=1&b[]=2', { duplicates: 'first' }),
  1066. { a: '1', b: ['1', '2'] },
  1067. 'duplicates first: unbracketed takes first, bracketed combines'
  1068. );
  1069. st.end();
  1070. });
  1071. t.end();
  1072. });
  1073. test('qs strictDepth option - throw cases', function (t) {
  1074. t.test('throws an exception when depth exceeds the limit with strictDepth: true', function (st) {
  1075. st['throws'](
  1076. function () {
  1077. qs.parse('a[b][c][d][e][f][g][h][i]=j', { depth: 1, strictDepth: true });
  1078. },
  1079. RangeError,
  1080. 'throws RangeError'
  1081. );
  1082. st.end();
  1083. });
  1084. t.test('throws an exception for multiple nested arrays with strictDepth: true', function (st) {
  1085. st['throws'](
  1086. function () {
  1087. qs.parse('a[0][1][2][3][4]=b', { depth: 3, strictDepth: true });
  1088. },
  1089. RangeError,
  1090. 'throws RangeError'
  1091. );
  1092. st.end();
  1093. });
  1094. t.test('throws an exception for nested objects and arrays with strictDepth: true', function (st) {
  1095. st['throws'](
  1096. function () {
  1097. qs.parse('a[b][c][0][d][e]=f', { depth: 3, strictDepth: true });
  1098. },
  1099. RangeError,
  1100. 'throws RangeError'
  1101. );
  1102. st.end();
  1103. });
  1104. t.test('throws an exception for different types of values with strictDepth: true', function (st) {
  1105. st['throws'](
  1106. function () {
  1107. qs.parse('a[b][c][d][e]=true&a[b][c][d][f]=42', { depth: 3, strictDepth: true });
  1108. },
  1109. RangeError,
  1110. 'throws RangeError'
  1111. );
  1112. st.end();
  1113. });
  1114. });
  1115. test('qs strictDepth option - non-throw cases', function (t) {
  1116. t.test('when depth is 0 and strictDepth true, do not throw', function (st) {
  1117. st.doesNotThrow(
  1118. function () {
  1119. qs.parse('a[b][c][d][e]=true&a[b][c][d][f]=42', { depth: 0, strictDepth: true });
  1120. },
  1121. RangeError,
  1122. 'does not throw RangeError'
  1123. );
  1124. st.end();
  1125. });
  1126. t.test('parses successfully when depth is within the limit with strictDepth: true', function (st) {
  1127. st.doesNotThrow(
  1128. function () {
  1129. var result = qs.parse('a[b]=c', { depth: 1, strictDepth: true });
  1130. st.deepEqual(result, { a: { b: 'c' } }, 'parses correctly');
  1131. }
  1132. );
  1133. st.end();
  1134. });
  1135. t.test('does not throw an exception when depth exceeds the limit with strictDepth: false', function (st) {
  1136. st.doesNotThrow(
  1137. function () {
  1138. var result = qs.parse('a[b][c][d][e][f][g][h][i]=j', { depth: 1 });
  1139. st.deepEqual(result, { a: { b: { '[c][d][e][f][g][h][i]': 'j' } } }, 'parses with depth limit');
  1140. }
  1141. );
  1142. st.end();
  1143. });
  1144. t.test('parses successfully when depth is within the limit with strictDepth: false', function (st) {
  1145. st.doesNotThrow(
  1146. function () {
  1147. var result = qs.parse('a[b]=c', { depth: 1 });
  1148. st.deepEqual(result, { a: { b: 'c' } }, 'parses correctly');
  1149. }
  1150. );
  1151. st.end();
  1152. });
  1153. t.test('does not throw when depth is exactly at the limit with strictDepth: true', function (st) {
  1154. st.doesNotThrow(
  1155. function () {
  1156. var result = qs.parse('a[b][c]=d', { depth: 2, strictDepth: true });
  1157. st.deepEqual(result, { a: { b: { c: 'd' } } }, 'parses correctly');
  1158. }
  1159. );
  1160. st.end();
  1161. });
  1162. });
  1163. test('DOS', function (t) {
  1164. var arr = [];
  1165. for (var i = 0; i < 105; i++) {
  1166. arr[arr.length] = 'x';
  1167. }
  1168. var attack = 'a[]=' + arr.join('&a[]=');
  1169. var result = qs.parse(attack, { arrayLimit: 100 });
  1170. t.notOk(Array.isArray(result.a), 'arrayLimit is respected: result is an object, not an array');
  1171. t.equal(Object.keys(result.a).length, 105, 'all values are preserved');
  1172. t.end();
  1173. });
  1174. test('arrayLimit boundary conditions', function (t) {
  1175. // arrayLimit is the max number of elements allowed in an array
  1176. t.test('exactly at the limit stays as array', function (st) {
  1177. // 3 elements = limit of 3
  1178. var result = qs.parse('a[]=1&a[]=2&a[]=3', { arrayLimit: 3 });
  1179. st.ok(Array.isArray(result.a), 'result is an array when count equals limit');
  1180. st.deepEqual(result.a, ['1', '2', '3'], 'all values present');
  1181. st.end();
  1182. });
  1183. t.test('one over the limit converts to object', function (st) {
  1184. // 4 elements exceeds limit of 3
  1185. var result = qs.parse('a[]=1&a[]=2&a[]=3&a[]=4', { arrayLimit: 3 });
  1186. st.notOk(Array.isArray(result.a), 'result is not an array when over limit');
  1187. st.deepEqual(result.a, { 0: '1', 1: '2', 2: '3', 3: '4' }, 'all values preserved as object');
  1188. st.end();
  1189. });
  1190. t.test('arrayLimit 1 with one value', function (st) {
  1191. // 1 element = limit of 1
  1192. var result = qs.parse('a[]=1', { arrayLimit: 1 });
  1193. st.ok(Array.isArray(result.a), 'result is an array when count equals limit');
  1194. st.deepEqual(result.a, ['1'], 'value preserved as array');
  1195. st.end();
  1196. });
  1197. t.test('arrayLimit 1 with two values converts to object', function (st) {
  1198. // 2 elements exceeds limit of 1
  1199. var result = qs.parse('a[]=1&a[]=2', { arrayLimit: 1 });
  1200. st.notOk(Array.isArray(result.a), 'result is not an array');
  1201. st.deepEqual(result.a, { 0: '1', 1: '2' }, 'all values preserved as object');
  1202. st.end();
  1203. });
  1204. t.end();
  1205. });
  1206. test('comma + arrayLimit', function (t) {
  1207. t.test('comma-separated values within arrayLimit stay as array', function (st) {
  1208. var result = qs.parse('a=1,2,3', { comma: true, arrayLimit: 5 });
  1209. st.ok(Array.isArray(result.a), 'result is an array');
  1210. st.deepEqual(result.a, ['1', '2', '3'], 'all values present');
  1211. st.end();
  1212. });
  1213. t.test('comma-separated values exceeding arrayLimit convert to object', function (st) {
  1214. var result = qs.parse('a=1,2,3,4', { comma: true, arrayLimit: 3 });
  1215. st.notOk(Array.isArray(result.a), 'result is not an array when over limit');
  1216. st.deepEqual(result.a, { 0: '1', 1: '2', 2: '3', 3: '4' }, 'all values preserved as object');
  1217. st.end();
  1218. });
  1219. t.test('comma-separated values exceeding arrayLimit with throwOnLimitExceeded throws', function (st) {
  1220. st['throws'](
  1221. function () {
  1222. qs.parse('a=1,2,3,4', { comma: true, arrayLimit: 3, throwOnLimitExceeded: true });
  1223. },
  1224. new RangeError('Array limit exceeded. Only 3 elements allowed in an array.'),
  1225. 'throws error when comma-split exceeds array limit'
  1226. );
  1227. st.end();
  1228. });
  1229. t.test('comma-separated values at exactly arrayLimit stay as array', function (st) {
  1230. var result = qs.parse('a=1,2,3', { comma: true, arrayLimit: 3 });
  1231. st.ok(Array.isArray(result.a), 'result is an array when exactly at limit');
  1232. st.deepEqual(result.a, ['1', '2', '3'], 'all values present');
  1233. st.end();
  1234. });
  1235. t.end();
  1236. });
  1237. test('mixed array and object notation', function (t) {
  1238. t.test('array brackets with object key - under limit', function (st) {
  1239. st.deepEqual(
  1240. qs.parse('a[]=b&a[c]=d'),
  1241. { a: { 0: 'b', c: 'd' } },
  1242. 'mixing [] and [key] converts to object'
  1243. );
  1244. st.end();
  1245. });
  1246. t.test('array index with object key - under limit', function (st) {
  1247. st.deepEqual(
  1248. qs.parse('a[0]=b&a[c]=d'),
  1249. { a: { 0: 'b', c: 'd' } },
  1250. 'mixing [0] and [key] produces object'
  1251. );
  1252. st.end();
  1253. });
  1254. t.test('plain value with array brackets - under limit', function (st) {
  1255. st.deepEqual(
  1256. qs.parse('a=b&a[]=c', { arrayLimit: 20 }),
  1257. { a: ['b', 'c'] },
  1258. 'plain value combined with [] stays as array under limit'
  1259. );
  1260. st.end();
  1261. });
  1262. t.test('array brackets with plain value - under limit', function (st) {
  1263. st.deepEqual(
  1264. qs.parse('a[]=b&a=c', { arrayLimit: 20 }),
  1265. { a: ['b', 'c'] },
  1266. '[] combined with plain value stays as array under limit'
  1267. );
  1268. st.end();
  1269. });
  1270. t.test('plain value with array index - under limit', function (st) {
  1271. st.deepEqual(
  1272. qs.parse('a=b&a[0]=c', { arrayLimit: 20 }),
  1273. { a: ['b', 'c'] },
  1274. 'plain value combined with [0] stays as array under limit'
  1275. );
  1276. st.end();
  1277. });
  1278. t.test('multiple plain values with duplicates combine', function (st) {
  1279. st.deepEqual(
  1280. qs.parse('a=b&a=c&a=d', { arrayLimit: 20 }),
  1281. { a: ['b', 'c', 'd'] },
  1282. 'duplicate plain keys combine into array'
  1283. );
  1284. st.end();
  1285. });
  1286. t.test('multiple plain values exceeding limit', function (st) {
  1287. // 3 elements (indices 0-2), max index 2 > limit 1
  1288. st.deepEqual(
  1289. qs.parse('a=b&a=c&a=d', { arrayLimit: 1 }),
  1290. { a: { 0: 'b', 1: 'c', 2: 'd' } },
  1291. 'duplicate plain keys convert to object when exceeding limit'
  1292. );
  1293. st.end();
  1294. });
  1295. t.test('mixed notation produces consistent results when arrayLimit is exceeded', function (st) {
  1296. var expected = { a: { 0: 'b', 1: 'c', 2: 'd' } };
  1297. st.deepEqual(
  1298. qs.parse('a[]=b&a[1]=c&a=d', { arrayLimit: -1 }),
  1299. expected,
  1300. 'arrayLimit -1'
  1301. );
  1302. st.deepEqual(
  1303. qs.parse('a[]=b&a[1]=c&a=d', { arrayLimit: 0 }),
  1304. expected,
  1305. 'arrayLimit 0'
  1306. );
  1307. st.deepEqual(
  1308. qs.parse('a[]=b&a[1]=c&a=d', { arrayLimit: 1 }),
  1309. expected,
  1310. 'arrayLimit 1'
  1311. );
  1312. st.end();
  1313. });
  1314. t.end();
  1315. });