parse.js 42 KB

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