parse.js 46 KB

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