储能智慧云小程序
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

parse.js 42KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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 url-encoded brackets holds array of arrays when having two parts of strings with comma as array divider', function (st) {
  481. 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']] });
  482. st.deepEqual(qs.parse('foo%5B%5D=1,2,3&foo%5B%5D=', { comma: true }), { foo: [['1', '2', '3'], ''] });
  483. st.deepEqual(qs.parse('foo%5B%5D=1,2,3&foo%5B%5D=,', { comma: true }), { foo: [['1', '2', '3'], ['', '']] });
  484. st.deepEqual(qs.parse('foo%5B%5D=1,2,3&foo%5B%5D=a', { comma: true }), { foo: [['1', '2', '3'], 'a'] });
  485. st.end();
  486. });
  487. t.test('parses comma delimited array while having percent-encoded comma treated as normal text', function (st) {
  488. st.deepEqual(qs.parse('foo=a%2Cb', { comma: true }), { foo: 'a,b' });
  489. st.deepEqual(qs.parse('foo=a%2C%20b,d', { comma: true }), { foo: ['a, b', 'd'] });
  490. st.deepEqual(qs.parse('foo=a%2C%20b,c%2C%20d', { comma: true }), { foo: ['a, b', 'c, d'] });
  491. st.end();
  492. });
  493. t.test('parses an object in dot notation', function (st) {
  494. var input = {
  495. 'user.name': { 'pop[bob]': 3 },
  496. 'user.email.': null
  497. };
  498. var expected = {
  499. user: {
  500. name: { 'pop[bob]': 3 },
  501. email: null
  502. }
  503. };
  504. var result = qs.parse(input, { allowDots: true });
  505. st.deepEqual(result, expected);
  506. st.end();
  507. });
  508. t.test('parses an object and not child values', function (st) {
  509. var input = {
  510. 'user[name]': { 'pop[bob]': { test: 3 } },
  511. 'user[email]': null
  512. };
  513. var expected = {
  514. user: {
  515. name: { 'pop[bob]': { test: 3 } },
  516. email: null
  517. }
  518. };
  519. var result = qs.parse(input);
  520. st.deepEqual(result, expected);
  521. st.end();
  522. });
  523. t.test('does not blow up when Buffer global is missing', function (st) {
  524. var restore = mockProperty(global, 'Buffer', { 'delete': true });
  525. var result = qs.parse('a=b&c=d');
  526. restore();
  527. st.deepEqual(result, { a: 'b', c: 'd' });
  528. st.end();
  529. });
  530. t.test('does not crash when parsing circular references', function (st) {
  531. var a = {};
  532. a.b = a;
  533. var parsed;
  534. st.doesNotThrow(function () {
  535. parsed = qs.parse({ 'foo[bar]': 'baz', 'foo[baz]': a });
  536. });
  537. st.equal('foo' in parsed, true, 'parsed has "foo" property');
  538. st.equal('bar' in parsed.foo, true);
  539. st.equal('baz' in parsed.foo, true);
  540. st.equal(parsed.foo.bar, 'baz');
  541. st.deepEqual(parsed.foo.baz, a);
  542. st.end();
  543. });
  544. t.test('does not crash when parsing deep objects', function (st) {
  545. var parsed;
  546. var str = 'foo';
  547. for (var i = 0; i < 5000; i++) {
  548. str += '[p]';
  549. }
  550. str += '=bar';
  551. st.doesNotThrow(function () {
  552. parsed = qs.parse(str, { depth: 5000 });
  553. });
  554. st.equal('foo' in parsed, true, 'parsed has "foo" property');
  555. var depth = 0;
  556. var ref = parsed.foo;
  557. while ((ref = ref.p)) {
  558. depth += 1;
  559. }
  560. st.equal(depth, 5000, 'parsed is 5000 properties deep');
  561. st.end();
  562. });
  563. t.test('parses null objects correctly', { skip: !Object.create }, function (st) {
  564. var a = Object.create(null);
  565. a.b = 'c';
  566. st.deepEqual(qs.parse(a), { b: 'c' });
  567. var result = qs.parse({ a: a });
  568. st.equal('a' in result, true, 'result has "a" property');
  569. st.deepEqual(result.a, a);
  570. st.end();
  571. });
  572. t.test('parses dates correctly', function (st) {
  573. var now = new Date();
  574. st.deepEqual(qs.parse({ a: now }), { a: now });
  575. st.end();
  576. });
  577. t.test('parses regular expressions correctly', function (st) {
  578. var re = /^test$/;
  579. st.deepEqual(qs.parse({ a: re }), { a: re });
  580. st.end();
  581. });
  582. t.test('does not allow overwriting prototype properties', function (st) {
  583. st.deepEqual(qs.parse('a[hasOwnProperty]=b', { allowPrototypes: false }), {});
  584. st.deepEqual(qs.parse('hasOwnProperty=b', { allowPrototypes: false }), {});
  585. st.deepEqual(
  586. qs.parse('toString', { allowPrototypes: false }),
  587. {},
  588. 'bare "toString" results in {}'
  589. );
  590. st.end();
  591. });
  592. t.test('can allow overwriting prototype properties', function (st) {
  593. st.deepEqual(qs.parse('a[hasOwnProperty]=b', { allowPrototypes: true }), { a: { hasOwnProperty: 'b' } });
  594. st.deepEqual(qs.parse('hasOwnProperty=b', { allowPrototypes: true }), { hasOwnProperty: 'b' });
  595. st.deepEqual(
  596. qs.parse('toString', { allowPrototypes: true }),
  597. { toString: '' },
  598. 'bare "toString" results in { toString: "" }'
  599. );
  600. st.end();
  601. });
  602. t.test('does not crash when the global Object prototype is frozen', { skip: !hasPropertyDescriptors || !hasOverrideMistake }, function (st) {
  603. // We can't actually freeze the global Object prototype as that will interfere with other tests, and once an object is frozen, it
  604. // can't be unfrozen. Instead, we add a new non-writable property to simulate this.
  605. st.teardown(mockProperty(Object.prototype, 'frozenProp', { value: 'foo', nonWritable: true, nonEnumerable: true }));
  606. st['throws'](
  607. function () {
  608. var obj = {};
  609. obj.frozenProp = 'bar';
  610. },
  611. // node < 6 has a different error message
  612. /^TypeError: Cannot assign to read only property 'frozenProp' of (?:object '#<Object>'|#<Object>)/,
  613. 'regular assignment of an inherited non-writable property throws'
  614. );
  615. var parsed;
  616. st.doesNotThrow(
  617. function () {
  618. parsed = qs.parse('frozenProp', { allowPrototypes: false });
  619. },
  620. 'parsing a nonwritable Object.prototype property does not throw'
  621. );
  622. st.deepEqual(parsed, {}, 'bare "frozenProp" results in {}');
  623. st.end();
  624. });
  625. t.test('params starting with a closing bracket', function (st) {
  626. st.deepEqual(qs.parse(']=toString'), { ']': 'toString' });
  627. st.deepEqual(qs.parse(']]=toString'), { ']]': 'toString' });
  628. st.deepEqual(qs.parse(']hello]=toString'), { ']hello]': 'toString' });
  629. st.end();
  630. });
  631. t.test('params starting with a starting bracket', function (st) {
  632. st.deepEqual(qs.parse('[=toString'), { '[': 'toString' });
  633. st.deepEqual(qs.parse('[[=toString'), { '[[': 'toString' });
  634. st.deepEqual(qs.parse('[hello[=toString'), { '[hello[': 'toString' });
  635. st.end();
  636. });
  637. t.test('add keys to objects', function (st) {
  638. st.deepEqual(
  639. qs.parse('a[b]=c&a=d'),
  640. { a: { b: 'c', d: true } },
  641. 'can add keys to objects'
  642. );
  643. st.deepEqual(
  644. qs.parse('a[b]=c&a=toString'),
  645. { a: { b: 'c' } },
  646. 'can not overwrite prototype'
  647. );
  648. st.deepEqual(
  649. qs.parse('a[b]=c&a=toString', { allowPrototypes: true }),
  650. { a: { b: 'c', toString: true } },
  651. 'can overwrite prototype with allowPrototypes true'
  652. );
  653. st.deepEqual(
  654. qs.parse('a[b]=c&a=toString', { plainObjects: true }),
  655. { __proto__: null, a: { __proto__: null, b: 'c', toString: true } },
  656. 'can overwrite prototype with plainObjects true'
  657. );
  658. st.end();
  659. });
  660. t.test('dunder proto is ignored', function (st) {
  661. var payload = 'categories[__proto__]=login&categories[__proto__]&categories[length]=42';
  662. var result = qs.parse(payload, { allowPrototypes: true });
  663. st.deepEqual(
  664. result,
  665. {
  666. categories: {
  667. length: '42'
  668. }
  669. },
  670. 'silent [[Prototype]] payload'
  671. );
  672. var plainResult = qs.parse(payload, { allowPrototypes: true, plainObjects: true });
  673. st.deepEqual(
  674. plainResult,
  675. {
  676. __proto__: null,
  677. categories: {
  678. __proto__: null,
  679. length: '42'
  680. }
  681. },
  682. 'silent [[Prototype]] payload: plain objects'
  683. );
  684. var query = qs.parse('categories[__proto__]=cats&categories[__proto__]=dogs&categories[some][json]=toInject', { allowPrototypes: true });
  685. st.notOk(Array.isArray(query.categories), 'is not an array');
  686. st.notOk(query.categories instanceof Array, 'is not instanceof an array');
  687. st.deepEqual(query.categories, { some: { json: 'toInject' } });
  688. st.equal(JSON.stringify(query.categories), '{"some":{"json":"toInject"}}', 'stringifies as a non-array');
  689. st.deepEqual(
  690. qs.parse('foo[__proto__][hidden]=value&foo[bar]=stuffs', { allowPrototypes: true }),
  691. {
  692. foo: {
  693. bar: 'stuffs'
  694. }
  695. },
  696. 'hidden values'
  697. );
  698. st.deepEqual(
  699. qs.parse('foo[__proto__][hidden]=value&foo[bar]=stuffs', { allowPrototypes: true, plainObjects: true }),
  700. {
  701. __proto__: null,
  702. foo: {
  703. __proto__: null,
  704. bar: 'stuffs'
  705. }
  706. },
  707. 'hidden values: plain objects'
  708. );
  709. st.end();
  710. });
  711. t.test('can return null objects', { skip: !Object.create }, function (st) {
  712. var expected = Object.create(null);
  713. expected.a = Object.create(null);
  714. expected.a.b = 'c';
  715. expected.a.hasOwnProperty = 'd';
  716. st.deepEqual(qs.parse('a[b]=c&a[hasOwnProperty]=d', { plainObjects: true }), expected);
  717. st.deepEqual(qs.parse(null, { plainObjects: true }), Object.create(null));
  718. var expectedArray = Object.create(null);
  719. expectedArray.a = Object.create(null);
  720. expectedArray.a[0] = 'b';
  721. expectedArray.a.c = 'd';
  722. st.deepEqual(qs.parse('a[]=b&a[c]=d', { plainObjects: true }), expectedArray);
  723. st.end();
  724. });
  725. t.test('can parse with custom encoding', function (st) {
  726. st.deepEqual(qs.parse('%8c%a7=%91%e5%8d%e3%95%7b', {
  727. decoder: function (str) {
  728. var reg = /%([0-9A-F]{2})/ig;
  729. var result = [];
  730. var parts = reg.exec(str);
  731. while (parts) {
  732. result.push(parseInt(parts[1], 16));
  733. parts = reg.exec(str);
  734. }
  735. return String(iconv.decode(SaferBuffer.from(result), 'shift_jis'));
  736. }
  737. }), { 県: '大阪府' });
  738. st.end();
  739. });
  740. t.test('receives the default decoder as a second argument', function (st) {
  741. st.plan(1);
  742. qs.parse('a', {
  743. decoder: function (str, defaultDecoder) {
  744. st.equal(defaultDecoder, utils.decode);
  745. }
  746. });
  747. st.end();
  748. });
  749. t.test('throws error with wrong decoder', function (st) {
  750. st['throws'](function () {
  751. qs.parse({}, { decoder: 'string' });
  752. }, new TypeError('Decoder has to be a function.'));
  753. st.end();
  754. });
  755. t.test('does not mutate the options argument', function (st) {
  756. var options = {};
  757. qs.parse('a[b]=true', options);
  758. st.deepEqual(options, {});
  759. st.end();
  760. });
  761. t.test('throws if an invalid charset is specified', function (st) {
  762. st['throws'](function () {
  763. qs.parse('a=b', { charset: 'foobar' });
  764. }, new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined'));
  765. st.end();
  766. });
  767. t.test('parses an iso-8859-1 string if asked to', function (st) {
  768. st.deepEqual(qs.parse('%A2=%BD', { charset: 'iso-8859-1' }), { '¢': '½' });
  769. st.end();
  770. });
  771. var urlEncodedCheckmarkInUtf8 = '%E2%9C%93';
  772. var urlEncodedOSlashInUtf8 = '%C3%B8';
  773. var urlEncodedNumCheckmark = '%26%2310003%3B';
  774. var urlEncodedNumSmiley = '%26%239786%3B';
  775. t.test('prefers an utf-8 charset specified by the utf8 sentinel to a default charset of iso-8859-1', function (st) {
  776. st.deepEqual(qs.parse('utf8=' + urlEncodedCheckmarkInUtf8 + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'iso-8859-1' }), { ø: 'ø' });
  777. st.end();
  778. });
  779. t.test('prefers an iso-8859-1 charset specified by the utf8 sentinel to a default charset of utf-8', function (st) {
  780. st.deepEqual(qs.parse('utf8=' + urlEncodedNumCheckmark + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'utf-8' }), { 'ø': 'ø' });
  781. st.end();
  782. });
  783. t.test('does not require the utf8 sentinel to be defined before the parameters whose decoding it affects', function (st) {
  784. st.deepEqual(qs.parse('a=' + urlEncodedOSlashInUtf8 + '&utf8=' + urlEncodedNumCheckmark, { charsetSentinel: true, charset: 'utf-8' }), { a: 'ø' });
  785. st.end();
  786. });
  787. t.test('should ignore an utf8 sentinel with an unknown value', function (st) {
  788. st.deepEqual(qs.parse('utf8=foo&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'utf-8' }), { ø: 'ø' });
  789. st.end();
  790. });
  791. t.test('uses the utf8 sentinel to switch to utf-8 when no default charset is given', function (st) {
  792. st.deepEqual(qs.parse('utf8=' + urlEncodedCheckmarkInUtf8 + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true }), { ø: 'ø' });
  793. st.end();
  794. });
  795. t.test('uses the utf8 sentinel to switch to iso-8859-1 when no default charset is given', function (st) {
  796. st.deepEqual(qs.parse('utf8=' + urlEncodedNumCheckmark + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true }), { 'ø': 'ø' });
  797. st.end();
  798. });
  799. t.test('interprets numeric entities in iso-8859-1 when `interpretNumericEntities`', function (st) {
  800. st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'iso-8859-1', interpretNumericEntities: true }), { foo: '☺' });
  801. st.end();
  802. });
  803. t.test('handles a custom decoder returning `null`, in the `iso-8859-1` charset, when `interpretNumericEntities`', function (st) {
  804. st.deepEqual(qs.parse('foo=&bar=' + urlEncodedNumSmiley, {
  805. charset: 'iso-8859-1',
  806. decoder: function (str, defaultDecoder, charset) {
  807. return str ? defaultDecoder(str, defaultDecoder, charset) : null;
  808. },
  809. interpretNumericEntities: true
  810. }), { foo: null, bar: '☺' });
  811. st.end();
  812. });
  813. t.test('does not interpret numeric entities in iso-8859-1 when `interpretNumericEntities` is absent', function (st) {
  814. st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'iso-8859-1' }), { foo: '&#9786;' });
  815. st.end();
  816. });
  817. t.test('does not interpret numeric entities when the charset is utf-8, even when `interpretNumericEntities`', function (st) {
  818. st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'utf-8', interpretNumericEntities: true }), { foo: '&#9786;' });
  819. st.end();
  820. });
  821. t.test('does not interpret %uXXXX syntax in iso-8859-1 mode', function (st) {
  822. st.deepEqual(qs.parse('%u263A=%u263A', { charset: 'iso-8859-1' }), { '%u263A': '%u263A' });
  823. st.end();
  824. });
  825. t.test('allows for decoding keys and values differently', function (st) {
  826. var decoder = function (str, defaultDecoder, charset, type) {
  827. if (type === 'key') {
  828. return defaultDecoder(str, defaultDecoder, charset, type).toLowerCase();
  829. }
  830. if (type === 'value') {
  831. return defaultDecoder(str, defaultDecoder, charset, type).toUpperCase();
  832. }
  833. throw 'this should never happen! type: ' + type;
  834. };
  835. st.deepEqual(qs.parse('KeY=vAlUe', { decoder: decoder }), { key: 'VALUE' });
  836. st.end();
  837. });
  838. t.end();
  839. });
  840. test('parses empty keys', function (t) {
  841. emptyTestCases.forEach(function (testCase) {
  842. t.test('skips empty string key with ' + testCase.input, function (st) {
  843. st.deepEqual(qs.parse(testCase.input), testCase.noEmptyKeys);
  844. st.end();
  845. });
  846. });
  847. });
  848. test('`duplicates` option', function (t) {
  849. v.nonStrings.concat('not a valid option').forEach(function (invalidOption) {
  850. if (typeof invalidOption !== 'undefined') {
  851. t['throws'](
  852. function () { qs.parse('', { duplicates: invalidOption }); },
  853. TypeError,
  854. 'throws on invalid option: ' + inspect(invalidOption)
  855. );
  856. }
  857. });
  858. t.deepEqual(
  859. qs.parse('foo=bar&foo=baz'),
  860. { foo: ['bar', 'baz'] },
  861. 'duplicates: default, combine'
  862. );
  863. t.deepEqual(
  864. qs.parse('foo=bar&foo=baz', { duplicates: 'combine' }),
  865. { foo: ['bar', 'baz'] },
  866. 'duplicates: combine'
  867. );
  868. t.deepEqual(
  869. qs.parse('foo=bar&foo=baz', { duplicates: 'first' }),
  870. { foo: 'bar' },
  871. 'duplicates: first'
  872. );
  873. t.deepEqual(
  874. qs.parse('foo=bar&foo=baz', { duplicates: 'last' }),
  875. { foo: 'baz' },
  876. 'duplicates: last'
  877. );
  878. t.end();
  879. });