stringify.js 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448
  1. 'use strict';
  2. var test = require('tape');
  3. var qs = require('../');
  4. var utils = require('../lib/utils');
  5. var iconv = require('iconv-lite');
  6. var SaferBuffer = require('safer-buffer').Buffer;
  7. var hasSymbols = require('has-symbols');
  8. var mockProperty = require('mock-property');
  9. var emptyTestCases = require('./empty-keys-cases').emptyTestCases;
  10. var hasProto = require('has-proto')();
  11. var hasBigInt = require('has-bigints')();
  12. test('stringify()', function (t) {
  13. t.test('stringifies a querystring object', function (st) {
  14. st.equal(qs.stringify({ a: 'b' }), 'a=b');
  15. st.equal(qs.stringify({ a: 1 }), 'a=1');
  16. st.equal(qs.stringify({ a: 1, b: 2 }), 'a=1&b=2');
  17. st.equal(qs.stringify({ a: 'A_Z' }), 'a=A_Z');
  18. st.equal(qs.stringify({ a: '€' }), 'a=%E2%82%AC');
  19. st.equal(qs.stringify({ a: '' }), 'a=%EE%80%80');
  20. st.equal(qs.stringify({ a: 'א' }), 'a=%D7%90');
  21. st.equal(qs.stringify({ a: '𐐷' }), 'a=%F0%90%90%B7');
  22. st.end();
  23. });
  24. t.test('stringifies falsy values', function (st) {
  25. st.equal(qs.stringify(undefined), '');
  26. st.equal(qs.stringify(null), '');
  27. st.equal(qs.stringify(null, { strictNullHandling: true }), '');
  28. st.equal(qs.stringify(false), '');
  29. st.equal(qs.stringify(0), '');
  30. st.end();
  31. });
  32. t.test('stringifies symbols', { skip: !hasSymbols() }, function (st) {
  33. st.equal(qs.stringify(Symbol.iterator), '');
  34. st.equal(qs.stringify([Symbol.iterator]), '0=Symbol%28Symbol.iterator%29');
  35. st.equal(qs.stringify({ a: Symbol.iterator }), 'a=Symbol%28Symbol.iterator%29');
  36. st.equal(
  37. qs.stringify({ a: [Symbol.iterator] }, { encodeValuesOnly: true, arrayFormat: 'brackets' }),
  38. 'a[]=Symbol%28Symbol.iterator%29'
  39. );
  40. st.end();
  41. });
  42. t.test('stringifies bigints', { skip: !hasBigInt }, function (st) {
  43. var three = BigInt(3);
  44. var encodeWithN = function (value, defaultEncoder, charset) {
  45. var result = defaultEncoder(value, defaultEncoder, charset);
  46. return typeof value === 'bigint' ? result + 'n' : result;
  47. };
  48. st.equal(qs.stringify(three), '');
  49. st.equal(qs.stringify([three]), '0=3');
  50. st.equal(qs.stringify([three], { encoder: encodeWithN }), '0=3n');
  51. st.equal(qs.stringify({ a: three }), 'a=3');
  52. st.equal(qs.stringify({ a: three }, { encoder: encodeWithN }), 'a=3n');
  53. st.equal(
  54. qs.stringify({ a: [three] }, { encodeValuesOnly: true, arrayFormat: 'brackets' }),
  55. 'a[]=3'
  56. );
  57. st.equal(
  58. qs.stringify({ a: [three] }, { encodeValuesOnly: true, encoder: encodeWithN, arrayFormat: 'brackets' }),
  59. 'a[]=3n'
  60. );
  61. st.end();
  62. });
  63. t.test('encodes dot in key of object when encodeDotInKeys and allowDots is provided', function (st) {
  64. st.equal(
  65. qs.stringify(
  66. { 'name.obj': { first: 'John', last: 'Doe' } },
  67. { allowDots: false, encodeDotInKeys: false }
  68. ),
  69. 'name.obj%5Bfirst%5D=John&name.obj%5Blast%5D=Doe',
  70. 'with allowDots false and encodeDotInKeys false'
  71. );
  72. st.equal(
  73. qs.stringify(
  74. { 'name.obj': { first: 'John', last: 'Doe' } },
  75. { allowDots: true, encodeDotInKeys: false }
  76. ),
  77. 'name.obj.first=John&name.obj.last=Doe',
  78. 'with allowDots true and encodeDotInKeys false'
  79. );
  80. st.equal(
  81. qs.stringify(
  82. { 'name.obj': { first: 'John', last: 'Doe' } },
  83. { allowDots: false, encodeDotInKeys: true }
  84. ),
  85. 'name%252Eobj%5Bfirst%5D=John&name%252Eobj%5Blast%5D=Doe',
  86. 'with allowDots false and encodeDotInKeys true'
  87. );
  88. st.equal(
  89. qs.stringify(
  90. { 'name.obj': { first: 'John', last: 'Doe' } },
  91. { allowDots: true, encodeDotInKeys: true }
  92. ),
  93. 'name%252Eobj.first=John&name%252Eobj.last=Doe',
  94. 'with allowDots true and encodeDotInKeys true'
  95. );
  96. st.equal(
  97. qs.stringify(
  98. { 'name.obj.subobject': { 'first.godly.name': 'John', last: 'Doe' } },
  99. { allowDots: false, encodeDotInKeys: false }
  100. ),
  101. 'name.obj.subobject%5Bfirst.godly.name%5D=John&name.obj.subobject%5Blast%5D=Doe',
  102. 'with allowDots false and encodeDotInKeys false'
  103. );
  104. st.equal(
  105. qs.stringify(
  106. { 'name.obj.subobject': { 'first.godly.name': 'John', last: 'Doe' } },
  107. { allowDots: true, encodeDotInKeys: false }
  108. ),
  109. 'name.obj.subobject.first.godly.name=John&name.obj.subobject.last=Doe',
  110. 'with allowDots false and encodeDotInKeys false'
  111. );
  112. st.equal(
  113. qs.stringify(
  114. { 'name.obj.subobject': { 'first.godly.name': 'John', last: 'Doe' } },
  115. { allowDots: false, encodeDotInKeys: true }
  116. ),
  117. 'name%252Eobj%252Esubobject%5Bfirst.godly.name%5D=John&name%252Eobj%252Esubobject%5Blast%5D=Doe',
  118. 'with allowDots false and encodeDotInKeys true'
  119. );
  120. st.equal(
  121. qs.stringify(
  122. { 'name.obj.subobject': { 'first.godly.name': 'John', last: 'Doe' } },
  123. { allowDots: true, encodeDotInKeys: true }
  124. ),
  125. 'name%252Eobj%252Esubobject.first%252Egodly%252Ename=John&name%252Eobj%252Esubobject.last=Doe',
  126. 'with allowDots true and encodeDotInKeys true'
  127. );
  128. st.end();
  129. });
  130. t.test('should encode dot in key of object, and automatically set allowDots to `true` when encodeDotInKeys is true and allowDots in undefined', function (st) {
  131. st.equal(
  132. qs.stringify(
  133. { 'name.obj.subobject': { 'first.godly.name': 'John', last: 'Doe' } },
  134. { encodeDotInKeys: true }
  135. ),
  136. 'name%252Eobj%252Esubobject.first%252Egodly%252Ename=John&name%252Eobj%252Esubobject.last=Doe',
  137. 'with allowDots undefined and encodeDotInKeys true'
  138. );
  139. st.end();
  140. });
  141. t.test('should encode dot in key of object when encodeDotInKeys and allowDots is provided, and nothing else when encodeValuesOnly is provided', function (st) {
  142. st.equal(
  143. qs.stringify({ 'name.obj': { first: 'John', last: 'Doe' } }, {
  144. encodeDotInKeys: true, allowDots: true, encodeValuesOnly: true
  145. }),
  146. 'name%2Eobj.first=John&name%2Eobj.last=Doe'
  147. );
  148. st.equal(
  149. qs.stringify({ 'name.obj.subobject': { 'first.godly.name': 'John', last: 'Doe' } }, { allowDots: true, encodeDotInKeys: true, encodeValuesOnly: true }),
  150. 'name%2Eobj%2Esubobject.first%2Egodly%2Ename=John&name%2Eobj%2Esubobject.last=Doe'
  151. );
  152. st.end();
  153. });
  154. t.test('throws when `commaRoundTrip` is not a boolean', function (st) {
  155. st['throws'](
  156. function () { qs.stringify({}, { commaRoundTrip: 'not a boolean' }); },
  157. TypeError,
  158. 'throws when `commaRoundTrip` is not a boolean'
  159. );
  160. st.end();
  161. });
  162. t.test('throws when `encodeDotInKeys` is not a boolean', function (st) {
  163. st['throws'](
  164. function () { qs.stringify({ a: [], b: 'zz' }, { encodeDotInKeys: 'foobar' }); },
  165. TypeError
  166. );
  167. st['throws'](
  168. function () { qs.stringify({ a: [], b: 'zz' }, { encodeDotInKeys: 0 }); },
  169. TypeError
  170. );
  171. st['throws'](
  172. function () { qs.stringify({ a: [], b: 'zz' }, { encodeDotInKeys: NaN }); },
  173. TypeError
  174. );
  175. st['throws'](
  176. function () { qs.stringify({ a: [], b: 'zz' }, { encodeDotInKeys: null }); },
  177. TypeError
  178. );
  179. st.end();
  180. });
  181. t.test('adds query prefix', function (st) {
  182. st.equal(qs.stringify({ a: 'b' }, { addQueryPrefix: true }), '?a=b');
  183. st.end();
  184. });
  185. t.test('with query prefix, outputs blank string given an empty object', function (st) {
  186. st.equal(qs.stringify({}, { addQueryPrefix: true }), '');
  187. st.end();
  188. });
  189. t.test('stringifies nested falsy values', function (st) {
  190. st.equal(qs.stringify({ a: { b: { c: null } } }), 'a%5Bb%5D%5Bc%5D=');
  191. st.equal(qs.stringify({ a: { b: { c: null } } }, { strictNullHandling: true }), 'a%5Bb%5D%5Bc%5D');
  192. st.equal(qs.stringify({ a: { b: { c: false } } }), 'a%5Bb%5D%5Bc%5D=false');
  193. st.end();
  194. });
  195. t.test('stringifies a nested object', function (st) {
  196. st.equal(qs.stringify({ a: { b: 'c' } }), 'a%5Bb%5D=c');
  197. st.equal(qs.stringify({ a: { b: { c: { d: 'e' } } } }), 'a%5Bb%5D%5Bc%5D%5Bd%5D=e');
  198. st.end();
  199. });
  200. t.test('`allowDots` option: stringifies a nested object with dots notation', function (st) {
  201. st.equal(qs.stringify({ a: { b: 'c' } }, { allowDots: true }), 'a.b=c');
  202. st.equal(qs.stringify({ a: { b: { c: { d: 'e' } } } }, { allowDots: true }), 'a.b.c.d=e');
  203. st.end();
  204. });
  205. t.test('stringifies an array value', function (st) {
  206. st.equal(
  207. qs.stringify({ a: ['b', 'c', 'd'] }, { arrayFormat: 'indices' }),
  208. 'a%5B0%5D=b&a%5B1%5D=c&a%5B2%5D=d',
  209. 'indices => indices'
  210. );
  211. st.equal(
  212. qs.stringify({ a: ['b', 'c', 'd'] }, { arrayFormat: 'brackets' }),
  213. 'a%5B%5D=b&a%5B%5D=c&a%5B%5D=d',
  214. 'brackets => brackets'
  215. );
  216. st.equal(
  217. qs.stringify({ a: ['b', 'c', 'd'] }, { arrayFormat: 'comma' }),
  218. 'a=b%2Cc%2Cd',
  219. 'comma => comma'
  220. );
  221. st.equal(
  222. qs.stringify({ a: ['b', 'c', 'd'] }, { arrayFormat: 'comma', commaRoundTrip: true }),
  223. 'a=b%2Cc%2Cd',
  224. 'comma round trip => comma'
  225. );
  226. st.equal(
  227. qs.stringify({ a: ['b', 'c', 'd'] }),
  228. 'a%5B0%5D=b&a%5B1%5D=c&a%5B2%5D=d',
  229. 'default => indices'
  230. );
  231. st.end();
  232. });
  233. t.test('`skipNulls` option', function (st) {
  234. st.equal(
  235. qs.stringify({ a: 'b', c: null }, { skipNulls: true }),
  236. 'a=b',
  237. 'omits nulls when asked'
  238. );
  239. st.equal(
  240. qs.stringify({ a: { b: 'c', d: null } }, { skipNulls: true }),
  241. 'a%5Bb%5D=c',
  242. 'omits nested nulls when asked'
  243. );
  244. st.end();
  245. });
  246. t.test('omits array indices when asked', function (st) {
  247. st.equal(qs.stringify({ a: ['b', 'c', 'd'] }, { indices: false }), 'a=b&a=c&a=d');
  248. st.end();
  249. });
  250. t.test('omits object key/value pair when value is empty array', function (st) {
  251. st.equal(qs.stringify({ a: [], b: 'zz' }), 'b=zz');
  252. st.end();
  253. });
  254. t.test('should not omit object key/value pair when value is empty array and when asked', function (st) {
  255. st.equal(qs.stringify({ a: [], b: 'zz' }), 'b=zz');
  256. st.equal(qs.stringify({ a: [], b: 'zz' }, { allowEmptyArrays: false }), 'b=zz');
  257. st.equal(qs.stringify({ a: [], b: 'zz' }, { allowEmptyArrays: true }), 'a[]&b=zz');
  258. st.end();
  259. });
  260. t.test('should throw when allowEmptyArrays is not of type boolean', function (st) {
  261. st['throws'](
  262. function () { qs.stringify({ a: [], b: 'zz' }, { allowEmptyArrays: 'foobar' }); },
  263. TypeError
  264. );
  265. st['throws'](
  266. function () { qs.stringify({ a: [], b: 'zz' }, { allowEmptyArrays: 0 }); },
  267. TypeError
  268. );
  269. st['throws'](
  270. function () { qs.stringify({ a: [], b: 'zz' }, { allowEmptyArrays: NaN }); },
  271. TypeError
  272. );
  273. st['throws'](
  274. function () { qs.stringify({ a: [], b: 'zz' }, { allowEmptyArrays: null }); },
  275. TypeError
  276. );
  277. st.end();
  278. });
  279. t.test('allowEmptyArrays + strictNullHandling', function (st) {
  280. st.equal(
  281. qs.stringify(
  282. { testEmptyArray: [] },
  283. { strictNullHandling: true, allowEmptyArrays: true }
  284. ),
  285. 'testEmptyArray[]'
  286. );
  287. st.end();
  288. });
  289. t.test('stringifies an array value with one item vs multiple items', function (st) {
  290. st.test('non-array item', function (s2t) {
  291. s2t.equal(qs.stringify({ a: 'c' }, { encodeValuesOnly: true, arrayFormat: 'indices' }), 'a=c');
  292. s2t.equal(qs.stringify({ a: 'c' }, { encodeValuesOnly: true, arrayFormat: 'brackets' }), 'a=c');
  293. s2t.equal(qs.stringify({ a: 'c' }, { encodeValuesOnly: true, arrayFormat: 'comma' }), 'a=c');
  294. s2t.equal(qs.stringify({ a: 'c' }, { encodeValuesOnly: true }), 'a=c');
  295. s2t.end();
  296. });
  297. st.test('array with a single item', function (s2t) {
  298. s2t.equal(qs.stringify({ a: ['c'] }, { encodeValuesOnly: true, arrayFormat: 'indices' }), 'a[0]=c');
  299. s2t.equal(qs.stringify({ a: ['c'] }, { encodeValuesOnly: true, arrayFormat: 'brackets' }), 'a[]=c');
  300. s2t.equal(qs.stringify({ a: ['c'] }, { encodeValuesOnly: true, arrayFormat: 'comma' }), 'a=c');
  301. s2t.equal(qs.stringify({ a: ['c'] }, { encodeValuesOnly: true, arrayFormat: 'comma', commaRoundTrip: true }), 'a[]=c'); // so it parses back as an array
  302. s2t.equal(qs.stringify({ a: ['c'] }, { encodeValuesOnly: true }), 'a[0]=c');
  303. s2t.end();
  304. });
  305. st.test('array with multiple items', function (s2t) {
  306. s2t.equal(qs.stringify({ a: ['c', 'd'] }, { encodeValuesOnly: true, arrayFormat: 'indices' }), 'a[0]=c&a[1]=d');
  307. s2t.equal(qs.stringify({ a: ['c', 'd'] }, { encodeValuesOnly: true, arrayFormat: 'brackets' }), 'a[]=c&a[]=d');
  308. s2t.equal(qs.stringify({ a: ['c', 'd'] }, { encodeValuesOnly: true, arrayFormat: 'comma' }), 'a=c,d');
  309. s2t.equal(qs.stringify({ a: ['c', 'd'] }, { encodeValuesOnly: true, arrayFormat: 'comma', commaRoundTrip: true }), 'a=c,d');
  310. s2t.equal(qs.stringify({ a: ['c', 'd'] }, { encodeValuesOnly: true }), 'a[0]=c&a[1]=d');
  311. s2t.end();
  312. });
  313. st.test('array with multiple items with a comma inside', function (s2t) {
  314. s2t.equal(qs.stringify({ a: ['c,d', 'e'] }, { encodeValuesOnly: true, arrayFormat: 'comma' }), 'a=c%2Cd,e');
  315. s2t.equal(qs.stringify({ a: ['c,d', 'e'] }, { arrayFormat: 'comma' }), 'a=c%2Cd%2Ce');
  316. s2t.equal(qs.stringify({ a: ['c,d', 'e'] }, { encodeValuesOnly: true, arrayFormat: 'comma', commaRoundTrip: true }), 'a=c%2Cd,e');
  317. s2t.equal(qs.stringify({ a: ['c,d', 'e'] }, { arrayFormat: 'comma', commaRoundTrip: true }), 'a=c%2Cd%2Ce');
  318. s2t.end();
  319. });
  320. st.end();
  321. });
  322. t.test('stringifies a nested array value', function (st) {
  323. st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { encodeValuesOnly: true, arrayFormat: 'indices' }), 'a[b][0]=c&a[b][1]=d');
  324. st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { encodeValuesOnly: true, arrayFormat: 'brackets' }), 'a[b][]=c&a[b][]=d');
  325. st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { encodeValuesOnly: true, arrayFormat: 'comma' }), 'a[b]=c,d');
  326. st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { encodeValuesOnly: true }), 'a[b][0]=c&a[b][1]=d');
  327. st.end();
  328. });
  329. t.test('stringifies comma and empty array values', function (st) {
  330. st.equal(qs.stringify({ a: [',', '', 'c,d%'] }, { encode: false, arrayFormat: 'indices' }), 'a[0]=,&a[1]=&a[2]=c,d%');
  331. st.equal(qs.stringify({ a: [',', '', 'c,d%'] }, { encode: false, arrayFormat: 'brackets' }), 'a[]=,&a[]=&a[]=c,d%');
  332. st.equal(qs.stringify({ a: [',', '', 'c,d%'] }, { encode: false, arrayFormat: 'comma' }), 'a=,,,c,d%');
  333. st.equal(qs.stringify({ a: [',', '', 'c,d%'] }, { encode: false, arrayFormat: 'repeat' }), 'a=,&a=&a=c,d%');
  334. st.equal(qs.stringify({ a: [',', '', 'c,d%'] }, { encode: true, encodeValuesOnly: true, arrayFormat: 'indices' }), 'a[0]=%2C&a[1]=&a[2]=c%2Cd%25');
  335. st.equal(qs.stringify({ a: [',', '', 'c,d%'] }, { encode: true, encodeValuesOnly: true, arrayFormat: 'brackets' }), 'a[]=%2C&a[]=&a[]=c%2Cd%25');
  336. st.equal(qs.stringify({ a: [',', '', 'c,d%'] }, { encode: true, encodeValuesOnly: true, arrayFormat: 'comma' }), 'a=%2C,,c%2Cd%25');
  337. st.equal(qs.stringify({ a: [',', '', 'c,d%'] }, { encode: true, encodeValuesOnly: true, arrayFormat: 'repeat' }), 'a=%2C&a=&a=c%2Cd%25');
  338. st.equal(qs.stringify({ a: [',', '', 'c,d%'] }, { encode: true, encodeValuesOnly: false, arrayFormat: 'indices' }), 'a%5B0%5D=%2C&a%5B1%5D=&a%5B2%5D=c%2Cd%25');
  339. st.equal(qs.stringify({ a: [',', '', 'c,d%'] }, { encode: true, encodeValuesOnly: false, arrayFormat: 'brackets' }), 'a%5B%5D=%2C&a%5B%5D=&a%5B%5D=c%2Cd%25');
  340. st.equal(qs.stringify({ a: [',', '', 'c,d%'] }, { encode: true, encodeValuesOnly: false, arrayFormat: 'comma' }), 'a=%2C%2C%2Cc%2Cd%25');
  341. st.equal(qs.stringify({ a: [',', '', 'c,d%'] }, { encode: true, encodeValuesOnly: false, arrayFormat: 'repeat' }), 'a=%2C&a=&a=c%2Cd%25');
  342. st.end();
  343. });
  344. t.test('stringifies comma and empty non-array values', function (st) {
  345. st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: false, arrayFormat: 'indices' }), 'a=,&b=&c=c,d%');
  346. st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: false, arrayFormat: 'brackets' }), 'a=,&b=&c=c,d%');
  347. st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: false, arrayFormat: 'comma' }), 'a=,&b=&c=c,d%');
  348. st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: false, arrayFormat: 'repeat' }), 'a=,&b=&c=c,d%');
  349. st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: true, encodeValuesOnly: true, arrayFormat: 'indices' }), 'a=%2C&b=&c=c%2Cd%25');
  350. st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: true, encodeValuesOnly: true, arrayFormat: 'brackets' }), 'a=%2C&b=&c=c%2Cd%25');
  351. st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: true, encodeValuesOnly: true, arrayFormat: 'comma' }), 'a=%2C&b=&c=c%2Cd%25');
  352. st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: true, encodeValuesOnly: true, arrayFormat: 'repeat' }), 'a=%2C&b=&c=c%2Cd%25');
  353. st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: true, encodeValuesOnly: false, arrayFormat: 'indices' }), 'a=%2C&b=&c=c%2Cd%25');
  354. st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: true, encodeValuesOnly: false, arrayFormat: 'brackets' }), 'a=%2C&b=&c=c%2Cd%25');
  355. st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: true, encodeValuesOnly: false, arrayFormat: 'comma' }), 'a=%2C&b=&c=c%2Cd%25');
  356. st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: true, encodeValuesOnly: false, arrayFormat: 'repeat' }), 'a=%2C&b=&c=c%2Cd%25');
  357. st.end();
  358. });
  359. t.test('stringifies a nested array value with dots notation', function (st) {
  360. st.equal(
  361. qs.stringify(
  362. { a: { b: ['c', 'd'] } },
  363. { allowDots: true, encodeValuesOnly: true, arrayFormat: 'indices' }
  364. ),
  365. 'a.b[0]=c&a.b[1]=d',
  366. 'indices: stringifies with dots + indices'
  367. );
  368. st.equal(
  369. qs.stringify(
  370. { a: { b: ['c', 'd'] } },
  371. { allowDots: true, encodeValuesOnly: true, arrayFormat: 'brackets' }
  372. ),
  373. 'a.b[]=c&a.b[]=d',
  374. 'brackets: stringifies with dots + brackets'
  375. );
  376. st.equal(
  377. qs.stringify(
  378. { a: { b: ['c', 'd'] } },
  379. { allowDots: true, encodeValuesOnly: true, arrayFormat: 'comma' }
  380. ),
  381. 'a.b=c,d',
  382. 'comma: stringifies with dots + comma'
  383. );
  384. st.equal(
  385. qs.stringify(
  386. { a: { b: ['c', 'd'] } },
  387. { allowDots: true, encodeValuesOnly: true }
  388. ),
  389. 'a.b[0]=c&a.b[1]=d',
  390. 'default: stringifies with dots + indices'
  391. );
  392. st.end();
  393. });
  394. t.test('stringifies an object inside an array', function (st) {
  395. st.equal(
  396. qs.stringify({ a: [{ b: 'c' }] }, { arrayFormat: 'indices', encodeValuesOnly: true }),
  397. 'a[0][b]=c',
  398. 'indices => indices'
  399. );
  400. st.equal(
  401. qs.stringify({ a: [{ b: 'c' }] }, { arrayFormat: 'repeat', encodeValuesOnly: true }),
  402. 'a[b]=c',
  403. 'repeat => repeat'
  404. );
  405. st.equal(
  406. qs.stringify({ a: [{ b: 'c' }] }, { arrayFormat: 'brackets', encodeValuesOnly: true }),
  407. 'a[][b]=c',
  408. 'brackets => brackets'
  409. );
  410. st.equal(
  411. qs.stringify({ a: [{ b: 'c' }] }, { encodeValuesOnly: true }),
  412. 'a[0][b]=c',
  413. 'default => indices'
  414. );
  415. st.equal(
  416. qs.stringify({ a: [{ b: { c: [1] } }] }, { arrayFormat: 'indices', encodeValuesOnly: true }),
  417. 'a[0][b][c][0]=1',
  418. 'indices => indices'
  419. );
  420. st.equal(
  421. qs.stringify({ a: [{ b: { c: [1] } }] }, { arrayFormat: 'repeat', encodeValuesOnly: true }),
  422. 'a[b][c]=1',
  423. 'repeat => repeat'
  424. );
  425. st.equal(
  426. qs.stringify({ a: [{ b: { c: [1] } }] }, { arrayFormat: 'brackets', encodeValuesOnly: true }),
  427. 'a[][b][c][]=1',
  428. 'brackets => brackets'
  429. );
  430. st.equal(
  431. qs.stringify({ a: [{ b: { c: [1] } }] }, { encodeValuesOnly: true }),
  432. 'a[0][b][c][0]=1',
  433. 'default => indices'
  434. );
  435. st.end();
  436. });
  437. t.test('stringifies an array with mixed objects and primitives', function (st) {
  438. st.equal(
  439. qs.stringify({ a: [{ b: 1 }, 2, 3] }, { encodeValuesOnly: true, arrayFormat: 'indices' }),
  440. 'a[0][b]=1&a[1]=2&a[2]=3',
  441. 'indices => indices'
  442. );
  443. st.equal(
  444. qs.stringify({ a: [{ b: 1 }, 2, 3] }, { encodeValuesOnly: true, arrayFormat: 'brackets' }),
  445. 'a[][b]=1&a[]=2&a[]=3',
  446. 'brackets => brackets'
  447. );
  448. st.equal(
  449. qs.stringify({ a: [{ b: 1 }, 2, 3] }, { encodeValuesOnly: true, arrayFormat: 'comma' }),
  450. '???',
  451. 'brackets => brackets',
  452. { skip: 'TODO: figure out what this should do' }
  453. );
  454. st.equal(
  455. qs.stringify({ a: [{ b: 1 }, 2, 3] }, { encodeValuesOnly: true }),
  456. 'a[0][b]=1&a[1]=2&a[2]=3',
  457. 'default => indices'
  458. );
  459. st.end();
  460. });
  461. t.test('stringifies an object inside an array with dots notation', function (st) {
  462. st.equal(
  463. qs.stringify(
  464. { a: [{ b: 'c' }] },
  465. { allowDots: true, encode: false, arrayFormat: 'indices' }
  466. ),
  467. 'a[0].b=c',
  468. 'indices => indices'
  469. );
  470. st.equal(
  471. qs.stringify(
  472. { a: [{ b: 'c' }] },
  473. { allowDots: true, encode: false, arrayFormat: 'brackets' }
  474. ),
  475. 'a[].b=c',
  476. 'brackets => brackets'
  477. );
  478. st.equal(
  479. qs.stringify(
  480. { a: [{ b: 'c' }] },
  481. { allowDots: true, encode: false }
  482. ),
  483. 'a[0].b=c',
  484. 'default => indices'
  485. );
  486. st.equal(
  487. qs.stringify(
  488. { a: [{ b: { c: [1] } }] },
  489. { allowDots: true, encode: false, arrayFormat: 'indices' }
  490. ),
  491. 'a[0].b.c[0]=1',
  492. 'indices => indices'
  493. );
  494. st.equal(
  495. qs.stringify(
  496. { a: [{ b: { c: [1] } }] },
  497. { allowDots: true, encode: false, arrayFormat: 'brackets' }
  498. ),
  499. 'a[].b.c[]=1',
  500. 'brackets => brackets'
  501. );
  502. st.equal(
  503. qs.stringify(
  504. { a: [{ b: { c: [1] } }] },
  505. { allowDots: true, encode: false }
  506. ),
  507. 'a[0].b.c[0]=1',
  508. 'default => indices'
  509. );
  510. st.end();
  511. });
  512. t.test('does not omit object keys when indices = false', function (st) {
  513. st.equal(qs.stringify({ a: [{ b: 'c' }] }, { indices: false }), 'a%5Bb%5D=c');
  514. st.end();
  515. });
  516. t.test('uses indices notation for arrays when indices=true', function (st) {
  517. st.equal(qs.stringify({ a: ['b', 'c'] }, { indices: true }), 'a%5B0%5D=b&a%5B1%5D=c');
  518. st.end();
  519. });
  520. t.test('uses indices notation for arrays when no arrayFormat is specified', function (st) {
  521. st.equal(qs.stringify({ a: ['b', 'c'] }), 'a%5B0%5D=b&a%5B1%5D=c');
  522. st.end();
  523. });
  524. t.test('uses indices notation for arrays when arrayFormat=indices', function (st) {
  525. st.equal(qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'indices' }), 'a%5B0%5D=b&a%5B1%5D=c');
  526. st.end();
  527. });
  528. t.test('uses repeat notation for arrays when arrayFormat=repeat', function (st) {
  529. st.equal(qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'repeat' }), 'a=b&a=c');
  530. st.end();
  531. });
  532. t.test('uses brackets notation for arrays when arrayFormat=brackets', function (st) {
  533. st.equal(qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'brackets' }), 'a%5B%5D=b&a%5B%5D=c');
  534. st.end();
  535. });
  536. t.test('stringifies a complicated object', function (st) {
  537. st.equal(qs.stringify({ a: { b: 'c', d: 'e' } }), 'a%5Bb%5D=c&a%5Bd%5D=e');
  538. st.end();
  539. });
  540. t.test('stringifies an empty value', function (st) {
  541. st.equal(qs.stringify({ a: '' }), 'a=');
  542. st.equal(qs.stringify({ a: null }, { strictNullHandling: true }), 'a');
  543. st.equal(qs.stringify({ a: '', b: '' }), 'a=&b=');
  544. st.equal(qs.stringify({ a: null, b: '' }, { strictNullHandling: true }), 'a&b=');
  545. st.equal(qs.stringify({ a: { b: '' } }), 'a%5Bb%5D=');
  546. st.equal(qs.stringify({ a: { b: null } }, { strictNullHandling: true }), 'a%5Bb%5D');
  547. st.equal(qs.stringify({ a: { b: null } }, { strictNullHandling: false }), 'a%5Bb%5D=');
  548. st.end();
  549. });
  550. t.test('stringifies an empty array in different arrayFormat', function (st) {
  551. st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false }), 'b[0]=&c=c');
  552. // arrayFormat default
  553. st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'indices' }), 'b[0]=&c=c');
  554. st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'brackets' }), 'b[]=&c=c');
  555. st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'repeat' }), 'b=&c=c');
  556. st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'comma' }), 'b=&c=c');
  557. st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'comma', commaRoundTrip: true }), 'b[]=&c=c');
  558. // with strictNullHandling
  559. st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'indices', strictNullHandling: true }), 'b[0]&c=c');
  560. st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'brackets', strictNullHandling: true }), 'b[]&c=c');
  561. st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'repeat', strictNullHandling: true }), 'b&c=c');
  562. st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'comma', strictNullHandling: true }), 'b&c=c');
  563. st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'comma', strictNullHandling: true, commaRoundTrip: true }), 'b[]&c=c');
  564. // with skipNulls
  565. st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'indices', skipNulls: true }), 'c=c');
  566. st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'brackets', skipNulls: true }), 'c=c');
  567. st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'repeat', skipNulls: true }), 'c=c');
  568. st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'comma', skipNulls: true }), 'c=c');
  569. st.end();
  570. });
  571. t.test('does not crash on null/undefined entries in arrayFormat=comma with encodeValuesOnly', function (st) {
  572. st.doesNotThrow(
  573. function () { qs.stringify({ a: [null, 'b'] }, { arrayFormat: 'comma', encodeValuesOnly: true }); },
  574. 'does not pass a raw null array entry to the encoder'
  575. );
  576. st.doesNotThrow(
  577. function () { qs.stringify({ a: [undefined, 'b'] }, { arrayFormat: 'comma', encodeValuesOnly: true }); },
  578. 'does not pass a raw undefined array entry to the encoder'
  579. );
  580. st.doesNotThrow(
  581. function () { qs.stringify({ a: [null] }, { arrayFormat: 'comma', encodeValuesOnly: true }); },
  582. 'does not crash on a single-null array'
  583. );
  584. st.equal(
  585. qs.stringify({ a: [null, 'b'] }, { arrayFormat: 'comma', encodeValuesOnly: true }),
  586. 'a=,b',
  587. 'null entry joins as empty, comma stays unencoded under encodeValuesOnly'
  588. );
  589. st.equal(
  590. qs.stringify({ a: [undefined, 'b'] }, { arrayFormat: 'comma', encodeValuesOnly: true }),
  591. 'a=,b',
  592. 'undefined entry joins as empty, comma stays unencoded under encodeValuesOnly'
  593. );
  594. st.equal(
  595. qs.stringify({ a: [null] }, { arrayFormat: 'comma', encodeValuesOnly: true }),
  596. 'a=',
  597. 'single-null array stringifies as empty value'
  598. );
  599. st.equal(
  600. qs.stringify({ a: [null] }, { arrayFormat: 'comma', encodeValuesOnly: true, strictNullHandling: true }),
  601. 'a',
  602. 'strictNullHandling drops the equals sign for a single-null array'
  603. );
  604. st.equal(
  605. qs.stringify({ a: [null] }, { arrayFormat: 'comma', encodeValuesOnly: true, skipNulls: true }),
  606. '',
  607. 'skipNulls drops a single-null array entirely'
  608. );
  609. st.end();
  610. });
  611. t.test('stringifies a null object', { skip: !hasProto }, function (st) {
  612. st.equal(qs.stringify({ __proto__: null, a: 'b' }), 'a=b');
  613. st.end();
  614. });
  615. t.test('returns an empty string for invalid input', function (st) {
  616. st.equal(qs.stringify(undefined), '');
  617. st.equal(qs.stringify(false), '');
  618. st.equal(qs.stringify(null), '');
  619. st.equal(qs.stringify(''), '');
  620. st.end();
  621. });
  622. t.test('stringifies an object with a null object as a child', { skip: !hasProto }, function (st) {
  623. st.equal(qs.stringify({ a: { __proto__: null, b: 'c' } }), 'a%5Bb%5D=c');
  624. st.end();
  625. });
  626. t.test('drops keys with a value of undefined', function (st) {
  627. st.equal(qs.stringify({ a: undefined }), '');
  628. st.equal(qs.stringify({ a: { b: undefined, c: null } }, { strictNullHandling: true }), 'a%5Bc%5D');
  629. st.equal(qs.stringify({ a: { b: undefined, c: null } }, { strictNullHandling: false }), 'a%5Bc%5D=');
  630. st.equal(qs.stringify({ a: { b: undefined, c: '' } }), 'a%5Bc%5D=');
  631. st.end();
  632. });
  633. t.test('url encodes values', function (st) {
  634. st.equal(qs.stringify({ a: 'b c' }), 'a=b%20c');
  635. st.end();
  636. });
  637. t.test('stringifies a date', function (st) {
  638. var now = new Date();
  639. var str = 'a=' + encodeURIComponent(now.toISOString());
  640. st.equal(qs.stringify({ a: now }), str);
  641. st.end();
  642. });
  643. t.test('stringifies the weird object from qs', function (st) {
  644. st.equal(qs.stringify({ 'my weird field': '~q1!2"\'w$5&7/z8)?' }), 'my%20weird%20field=~q1%212%22%27w%245%267%2Fz8%29%3F');
  645. st.end();
  646. });
  647. t.test('skips properties that are part of the object prototype', function (st) {
  648. st.intercept(Object.prototype, 'crash', { value: 'test' });
  649. st.equal(qs.stringify({ a: 'b' }), 'a=b');
  650. st.equal(qs.stringify({ a: { b: 'c' } }), 'a%5Bb%5D=c');
  651. st.end();
  652. });
  653. t.test('stringifies boolean values', function (st) {
  654. st.equal(qs.stringify({ a: true }), 'a=true');
  655. st.equal(qs.stringify({ a: { b: true } }), 'a%5Bb%5D=true');
  656. st.equal(qs.stringify({ b: false }), 'b=false');
  657. st.equal(qs.stringify({ b: { c: false } }), 'b%5Bc%5D=false');
  658. st.end();
  659. });
  660. t.test('stringifies buffer values', function (st) {
  661. st.equal(qs.stringify({ a: SaferBuffer.from('test') }), 'a=test');
  662. st.equal(qs.stringify({ a: { b: SaferBuffer.from('test') } }), 'a%5Bb%5D=test');
  663. st.end();
  664. });
  665. t.test('stringifies an object using an alternative delimiter', function (st) {
  666. st.equal(qs.stringify({ a: 'b', c: 'd' }, { delimiter: ';' }), 'a=b;c=d');
  667. st.end();
  668. });
  669. t.test('does not blow up when Buffer global is missing', function (st) {
  670. var restore = mockProperty(global, 'Buffer', { 'delete': true });
  671. var result = qs.stringify({ a: 'b', c: 'd' });
  672. restore();
  673. st.equal(result, 'a=b&c=d');
  674. st.end();
  675. });
  676. t.test('does not crash when parsing circular references', function (st) {
  677. var a = {};
  678. a.b = a;
  679. st['throws'](
  680. function () { qs.stringify({ 'foo[bar]': 'baz', 'foo[baz]': a }); },
  681. /RangeError: Cyclic object value/,
  682. 'cyclic values throw'
  683. );
  684. var circular = {
  685. a: 'value'
  686. };
  687. circular.a = circular;
  688. st['throws'](
  689. function () { qs.stringify(circular); },
  690. /RangeError: Cyclic object value/,
  691. 'cyclic values throw'
  692. );
  693. var arr = ['a'];
  694. st.doesNotThrow(
  695. function () { qs.stringify({ x: arr, y: arr }); },
  696. 'non-cyclic values do not throw'
  697. );
  698. st.end();
  699. });
  700. t.test('non-circular duplicated references can still work', function (st) {
  701. var hourOfDay = {
  702. 'function': 'hour_of_day'
  703. };
  704. var p1 = {
  705. 'function': 'gte',
  706. arguments: [hourOfDay, 0]
  707. };
  708. var p2 = {
  709. 'function': 'lte',
  710. arguments: [hourOfDay, 23]
  711. };
  712. st.equal(
  713. qs.stringify({ filters: { $and: [p1, p2] } }, { encodeValuesOnly: true, arrayFormat: 'indices' }),
  714. 'filters[$and][0][function]=gte&filters[$and][0][arguments][0][function]=hour_of_day&filters[$and][0][arguments][1]=0&filters[$and][1][function]=lte&filters[$and][1][arguments][0][function]=hour_of_day&filters[$and][1][arguments][1]=23'
  715. );
  716. st.equal(
  717. qs.stringify({ filters: { $and: [p1, p2] } }, { encodeValuesOnly: true, arrayFormat: 'brackets' }),
  718. 'filters[$and][][function]=gte&filters[$and][][arguments][][function]=hour_of_day&filters[$and][][arguments][]=0&filters[$and][][function]=lte&filters[$and][][arguments][][function]=hour_of_day&filters[$and][][arguments][]=23'
  719. );
  720. st.equal(
  721. qs.stringify({ filters: { $and: [p1, p2] } }, { encodeValuesOnly: true, arrayFormat: 'repeat' }),
  722. 'filters[$and][function]=gte&filters[$and][arguments][function]=hour_of_day&filters[$and][arguments]=0&filters[$and][function]=lte&filters[$and][arguments][function]=hour_of_day&filters[$and][arguments]=23'
  723. );
  724. st.end();
  725. });
  726. t.test('selects properties when filter=array', function (st) {
  727. st.equal(qs.stringify({ a: 'b' }, { filter: ['a'] }), 'a=b');
  728. st.equal(qs.stringify({ a: 1 }, { filter: [] }), '');
  729. st.equal(
  730. qs.stringify(
  731. { a: { b: [1, 2, 3, 4], c: 'd' }, c: 'f' },
  732. { filter: ['a', 'b', 0, 2], arrayFormat: 'indices' }
  733. ),
  734. 'a%5Bb%5D%5B0%5D=1&a%5Bb%5D%5B2%5D=3',
  735. 'indices => indices'
  736. );
  737. st.equal(
  738. qs.stringify(
  739. { a: { b: [1, 2, 3, 4], c: 'd' }, c: 'f' },
  740. { filter: ['a', 'b', 0, 2], arrayFormat: 'brackets' }
  741. ),
  742. 'a%5Bb%5D%5B%5D=1&a%5Bb%5D%5B%5D=3',
  743. 'brackets => brackets'
  744. );
  745. st.equal(
  746. qs.stringify(
  747. { a: { b: [1, 2, 3, 4], c: 'd' }, c: 'f' },
  748. { filter: ['a', 'b', 0, 2] }
  749. ),
  750. 'a%5Bb%5D%5B0%5D=1&a%5Bb%5D%5B2%5D=3',
  751. 'default => indices'
  752. );
  753. st.end();
  754. });
  755. t.test('skips null/undefined entries in filter=array', function (st) {
  756. st.doesNotThrow(
  757. function () { qs.stringify({ a: 'b', undefined: 'x' }, { filter: ['a', undefined] }); },
  758. 'does not pass a raw undefined filter entry to the encoder'
  759. );
  760. st.doesNotThrow(
  761. function () { qs.stringify({ a: 'b', 'null': 'x' }, { filter: ['a', null] }); },
  762. 'does not pass a raw null filter entry to the encoder'
  763. );
  764. st.equal(
  765. qs.stringify({ a: 'b', undefined: 'x', c: 'd' }, { filter: ['a', undefined, 'c'] }),
  766. 'a=b&c=d',
  767. 'undefined filter entry is skipped, remaining keys are kept'
  768. );
  769. st.equal(
  770. qs.stringify({ a: 'b', 'null': 'x', c: 'd' }, { filter: ['a', null, 'c'] }),
  771. 'a=b&c=d',
  772. 'null filter entry is skipped, remaining keys are kept'
  773. );
  774. st.equal(
  775. qs.stringify({ a: 'b', 'null': 'x' }, { filter: [null] }),
  776. '',
  777. 'filter array containing only null yields empty string'
  778. );
  779. st.end();
  780. });
  781. t.test('supports custom representations when filter=function', function (st) {
  782. var calls = 0;
  783. var obj = { a: 'b', c: 'd', e: { f: new Date(1257894000000) } };
  784. var filterFunc = function (prefix, value) {
  785. calls += 1;
  786. if (calls === 1) {
  787. st.equal(prefix, '', 'prefix is empty');
  788. st.equal(value, obj);
  789. } else if (prefix === 'c') {
  790. return void 0;
  791. } else if (value instanceof Date) {
  792. st.equal(prefix, 'e[f]');
  793. return value.getTime();
  794. }
  795. return value;
  796. };
  797. st.equal(qs.stringify(obj, { filter: filterFunc }), 'a=b&e%5Bf%5D=1257894000000');
  798. st.equal(calls, 5);
  799. st.end();
  800. });
  801. t.test('can disable uri encoding', function (st) {
  802. st.equal(qs.stringify({ a: 'b' }, { encode: false }), 'a=b');
  803. st.equal(qs.stringify({ a: { b: 'c' } }, { encode: false }), 'a[b]=c');
  804. st.equal(qs.stringify({ a: 'b', c: null }, { strictNullHandling: true, encode: false }), 'a=b&c');
  805. st.end();
  806. });
  807. t.test('can sort the keys', function (st) {
  808. var sort = function (a, b) {
  809. return a.localeCompare(b);
  810. };
  811. st.equal(qs.stringify({ a: 'c', z: 'y', b: 'f' }, { sort: sort }), 'a=c&b=f&z=y');
  812. st.equal(qs.stringify({ a: 'c', z: { j: 'a', i: 'b' }, b: 'f' }, { sort: sort }), 'a=c&b=f&z%5Bi%5D=b&z%5Bj%5D=a');
  813. st.end();
  814. });
  815. t.test('can sort the keys at depth 3 or more too', function (st) {
  816. var sort = function (a, b) {
  817. return a.localeCompare(b);
  818. };
  819. st.equal(
  820. qs.stringify(
  821. { a: 'a', z: { zj: { zjb: 'zjb', zja: 'zja' }, zi: { zib: 'zib', zia: 'zia' } }, b: 'b' },
  822. { sort: sort, encode: false }
  823. ),
  824. 'a=a&b=b&z[zi][zia]=zia&z[zi][zib]=zib&z[zj][zja]=zja&z[zj][zjb]=zjb'
  825. );
  826. st.equal(
  827. qs.stringify(
  828. { a: 'a', z: { zj: { zjb: 'zjb', zja: 'zja' }, zi: { zib: 'zib', zia: 'zia' } }, b: 'b' },
  829. { sort: null, encode: false }
  830. ),
  831. 'a=a&z[zj][zjb]=zjb&z[zj][zja]=zja&z[zi][zib]=zib&z[zi][zia]=zia&b=b'
  832. );
  833. st.end();
  834. });
  835. t.test('can stringify with custom encoding', function (st) {
  836. st.equal(qs.stringify({ 県: '大阪府', '': '' }, {
  837. encoder: function (str) {
  838. if (str.length === 0) {
  839. return '';
  840. }
  841. var buf = iconv.encode(str, 'shiftjis');
  842. var result = [];
  843. for (var i = 0; i < buf.length; ++i) {
  844. result.push(buf.readUInt8(i).toString(16));
  845. }
  846. return '%' + result.join('%');
  847. }
  848. }), '%8c%a7=%91%e5%8d%e3%95%7b&=');
  849. st.end();
  850. });
  851. t.test('receives the default encoder as a second argument', function (st) {
  852. st.plan(8);
  853. qs.stringify({ a: 1, b: new Date(), c: true, d: [1] }, {
  854. encoder: function (str) {
  855. st.match(typeof str, /^(?:string|number|boolean)$/);
  856. return '';
  857. }
  858. });
  859. st.end();
  860. });
  861. t.test('receives the default encoder as a second argument', function (st) {
  862. st.plan(2);
  863. qs.stringify({ a: 1 }, {
  864. encoder: function (str, defaultEncoder) {
  865. st.equal(defaultEncoder, utils.encode);
  866. }
  867. });
  868. st.end();
  869. });
  870. t.test('throws error with wrong encoder', function (st) {
  871. st['throws'](function () {
  872. qs.stringify({}, { encoder: 'string' });
  873. }, new TypeError('Encoder has to be a function.'));
  874. st.end();
  875. });
  876. t.test('can use custom encoder for a buffer object', { skip: typeof Buffer === 'undefined' }, function (st) {
  877. st.equal(qs.stringify({ a: SaferBuffer.from([1]) }, {
  878. encoder: function (buffer) {
  879. if (typeof buffer === 'string') {
  880. return buffer;
  881. }
  882. return String.fromCharCode(buffer.readUInt8(0) + 97);
  883. }
  884. }), 'a=b');
  885. st.equal(qs.stringify({ a: SaferBuffer.from('a b') }, {
  886. encoder: function (buffer) {
  887. return buffer;
  888. }
  889. }), 'a=a b');
  890. st.end();
  891. });
  892. t.test('serializeDate option', function (st) {
  893. var date = new Date();
  894. st.equal(
  895. qs.stringify({ a: date }),
  896. 'a=' + date.toISOString().replace(/:/g, '%3A'),
  897. 'default is toISOString'
  898. );
  899. var mutatedDate = new Date();
  900. mutatedDate.toISOString = function () {
  901. throw new SyntaxError();
  902. };
  903. st['throws'](function () {
  904. mutatedDate.toISOString();
  905. }, SyntaxError);
  906. st.equal(
  907. qs.stringify({ a: mutatedDate }),
  908. 'a=' + Date.prototype.toISOString.call(mutatedDate).replace(/:/g, '%3A'),
  909. 'toISOString works even when method is not locally present'
  910. );
  911. var specificDate = new Date(6);
  912. st.equal(
  913. qs.stringify(
  914. { a: specificDate },
  915. { serializeDate: function (d) { return d.getTime() * 7; } }
  916. ),
  917. 'a=42',
  918. 'custom serializeDate function called'
  919. );
  920. st.equal(
  921. qs.stringify(
  922. { a: [date] },
  923. {
  924. serializeDate: function (d) { return d.getTime(); },
  925. arrayFormat: 'comma'
  926. }
  927. ),
  928. 'a=' + date.getTime(),
  929. 'works with arrayFormat comma'
  930. );
  931. st.equal(
  932. qs.stringify(
  933. { a: [date] },
  934. {
  935. serializeDate: function (d) { return d.getTime(); },
  936. arrayFormat: 'comma',
  937. commaRoundTrip: true
  938. }
  939. ),
  940. 'a%5B%5D=' + date.getTime(),
  941. 'works with arrayFormat comma'
  942. );
  943. st.end();
  944. });
  945. t.test('RFC 1738 serialization', function (st) {
  946. st.equal(qs.stringify({ a: 'b c' }, { format: qs.formats.RFC1738 }), 'a=b+c');
  947. st.equal(qs.stringify({ 'a b': 'c d' }, { format: qs.formats.RFC1738 }), 'a+b=c+d');
  948. st.equal(qs.stringify({ 'a b': SaferBuffer.from('a b') }, { format: qs.formats.RFC1738 }), 'a+b=a+b');
  949. st.equal(qs.stringify({ 'foo(ref)': 'bar' }, { format: qs.formats.RFC1738 }), 'foo(ref)=bar');
  950. st.end();
  951. });
  952. t.test('RFC 3986 spaces serialization', function (st) {
  953. st.equal(qs.stringify({ a: 'b c' }, { format: qs.formats.RFC3986 }), 'a=b%20c');
  954. st.equal(qs.stringify({ 'a b': 'c d' }, { format: qs.formats.RFC3986 }), 'a%20b=c%20d');
  955. st.equal(qs.stringify({ 'a b': SaferBuffer.from('a b') }, { format: qs.formats.RFC3986 }), 'a%20b=a%20b');
  956. st.end();
  957. });
  958. t.test('Backward compatibility to RFC 3986', function (st) {
  959. st.equal(qs.stringify({ a: 'b c' }), 'a=b%20c');
  960. st.equal(qs.stringify({ 'a b': SaferBuffer.from('a b') }), 'a%20b=a%20b');
  961. st.end();
  962. });
  963. t.test('Edge cases and unknown formats', function (st) {
  964. ['UFO1234', false, 1234, null, {}, []].forEach(function (format) {
  965. st['throws'](
  966. function () {
  967. qs.stringify({ a: 'b c' }, { format: format });
  968. },
  969. new TypeError('Unknown format option provided.')
  970. );
  971. });
  972. st.end();
  973. });
  974. t.test('encodeValuesOnly', function (st) {
  975. st.equal(
  976. qs.stringify(
  977. { a: 'b', c: ['d', 'e=f'], f: [['g'], ['h']] },
  978. { encodeValuesOnly: true, arrayFormat: 'indices' }
  979. ),
  980. 'a=b&c[0]=d&c[1]=e%3Df&f[0][0]=g&f[1][0]=h',
  981. 'encodeValuesOnly + indices'
  982. );
  983. st.equal(
  984. qs.stringify(
  985. { a: 'b', c: ['d', 'e=f'], f: [['g'], ['h']] },
  986. { encodeValuesOnly: true, arrayFormat: 'brackets' }
  987. ),
  988. 'a=b&c[]=d&c[]=e%3Df&f[][]=g&f[][]=h',
  989. 'encodeValuesOnly + brackets'
  990. );
  991. st.equal(
  992. qs.stringify(
  993. { a: 'b', c: ['d', 'e=f'], f: [['g'], ['h']] },
  994. { encodeValuesOnly: true, arrayFormat: 'repeat' }
  995. ),
  996. 'a=b&c=d&c=e%3Df&f=g&f=h',
  997. 'encodeValuesOnly + repeat'
  998. );
  999. st.equal(
  1000. qs.stringify(
  1001. { a: 'b', c: ['d', 'e'], f: [['g'], ['h']] },
  1002. { arrayFormat: 'indices' }
  1003. ),
  1004. 'a=b&c%5B0%5D=d&c%5B1%5D=e&f%5B0%5D%5B0%5D=g&f%5B1%5D%5B0%5D=h',
  1005. 'no encodeValuesOnly + indices'
  1006. );
  1007. st.equal(
  1008. qs.stringify(
  1009. { a: 'b', c: ['d', 'e'], f: [['g'], ['h']] },
  1010. { arrayFormat: 'brackets' }
  1011. ),
  1012. 'a=b&c%5B%5D=d&c%5B%5D=e&f%5B%5D%5B%5D=g&f%5B%5D%5B%5D=h',
  1013. 'no encodeValuesOnly + brackets'
  1014. );
  1015. st.equal(
  1016. qs.stringify(
  1017. { a: 'b', c: ['d', 'e'], f: [['g'], ['h']] },
  1018. { arrayFormat: 'repeat' }
  1019. ),
  1020. 'a=b&c=d&c=e&f=g&f=h',
  1021. 'no encodeValuesOnly + repeat'
  1022. );
  1023. st.end();
  1024. });
  1025. t.test('encodeValuesOnly - strictNullHandling', function (st) {
  1026. st.equal(
  1027. qs.stringify(
  1028. { a: { b: null } },
  1029. { encodeValuesOnly: true, strictNullHandling: true }
  1030. ),
  1031. 'a[b]'
  1032. );
  1033. st.end();
  1034. });
  1035. t.test('strictNullHandling: applies the formatter to the encoded key (RFC1738)', function (st) {
  1036. st.equal(
  1037. qs.stringify(
  1038. { 'a b': null, 'c d': 'e f' },
  1039. { strictNullHandling: false, format: 'RFC1738' }
  1040. ),
  1041. 'a+b=&c+d=e+f',
  1042. 'without: as expected'
  1043. );
  1044. st.equal(
  1045. qs.stringify(
  1046. { 'a b': null, 'c d': 'e f' },
  1047. { strictNullHandling: true, format: 'RFC1738' }
  1048. ),
  1049. 'a+b&c+d=e+f',
  1050. 'with: as expected'
  1051. );
  1052. st.end();
  1053. });
  1054. t.test('throws if an invalid charset is specified', function (st) {
  1055. st['throws'](function () {
  1056. qs.stringify({ a: 'b' }, { charset: 'foobar' });
  1057. }, new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined'));
  1058. st.end();
  1059. });
  1060. t.test('respects a charset of iso-8859-1', function (st) {
  1061. st.equal(qs.stringify({ æ: 'æ' }, { charset: 'iso-8859-1' }), '%E6=%E6');
  1062. st.end();
  1063. });
  1064. t.test('encodes unrepresentable chars as numeric entities in iso-8859-1 mode', function (st) {
  1065. st.equal(qs.stringify({ a: '☺' }, { charset: 'iso-8859-1' }), 'a=%26%239786%3B');
  1066. st.end();
  1067. });
  1068. t.test('respects an explicit charset of utf-8 (the default)', function (st) {
  1069. st.equal(qs.stringify({ a: 'æ' }, { charset: 'utf-8' }), 'a=%C3%A6');
  1070. st.end();
  1071. });
  1072. t.test('`charsetSentinel` option', function (st) {
  1073. st.equal(
  1074. qs.stringify({ a: 'æ' }, { charsetSentinel: true, charset: 'utf-8' }),
  1075. 'utf8=%E2%9C%93&a=%C3%A6',
  1076. 'adds the right sentinel when instructed to and the charset is utf-8'
  1077. );
  1078. st.equal(
  1079. qs.stringify({ a: 'æ' }, { charsetSentinel: true, charset: 'iso-8859-1' }),
  1080. 'utf8=%26%2310003%3B&a=%E6',
  1081. 'adds the right sentinel when instructed to and the charset is iso-8859-1'
  1082. );
  1083. st.equal(
  1084. qs.stringify({ a: 1, b: 2 }, { charsetSentinel: true, delimiter: ';' }),
  1085. 'utf8=%E2%9C%93;a=1;b=2',
  1086. 'uses the configured delimiter after the sentinel'
  1087. );
  1088. st.end();
  1089. });
  1090. t.test('does not mutate the options argument', function (st) {
  1091. var options = {};
  1092. qs.stringify({}, options);
  1093. st.deepEqual(options, {});
  1094. st.end();
  1095. });
  1096. t.test('strictNullHandling works with custom filter', function (st) {
  1097. var filter = function (prefix, value) {
  1098. return value;
  1099. };
  1100. var options = { strictNullHandling: true, filter: filter };
  1101. st.equal(qs.stringify({ key: null }, options), 'key');
  1102. st.end();
  1103. });
  1104. t.test('strictNullHandling works with null serializeDate', function (st) {
  1105. var serializeDate = function () {
  1106. return null;
  1107. };
  1108. var options = { strictNullHandling: true, serializeDate: serializeDate };
  1109. var date = new Date();
  1110. st.equal(qs.stringify({ key: date }, options), 'key');
  1111. st.end();
  1112. });
  1113. t.test('allows for encoding keys and values differently', function (st) {
  1114. var encoder = function (str, defaultEncoder, charset, type) {
  1115. if (type === 'key') {
  1116. return defaultEncoder(str, defaultEncoder, charset, type).toLowerCase();
  1117. }
  1118. if (type === 'value') {
  1119. return defaultEncoder(str, defaultEncoder, charset, type).toUpperCase();
  1120. }
  1121. throw 'this should never happen! type: ' + type;
  1122. };
  1123. st.deepEqual(qs.stringify({ KeY: 'vAlUe' }, { encoder: encoder }), 'key=VALUE');
  1124. var noopEncoder = function () { return 'x'; };
  1125. noopEncoder();
  1126. st['throws'](
  1127. function () { encoder('x', noopEncoder, 'utf-8', 'unknown'); },
  1128. 'this should never happen! type: unknown',
  1129. 'encoder throws for unexpected type'
  1130. );
  1131. st.end();
  1132. });
  1133. t.test('objects inside arrays', function (st) {
  1134. var obj = { a: { b: { c: 'd', e: 'f' } } };
  1135. var withArray = { a: { b: [{ c: 'd', e: 'f' }] } };
  1136. st.equal(qs.stringify(obj, { encode: false }), 'a[b][c]=d&a[b][e]=f', 'no array, no arrayFormat');
  1137. st.equal(qs.stringify(obj, { encode: false, arrayFormat: 'brackets' }), 'a[b][c]=d&a[b][e]=f', 'no array, bracket');
  1138. st.equal(qs.stringify(obj, { encode: false, arrayFormat: 'indices' }), 'a[b][c]=d&a[b][e]=f', 'no array, indices');
  1139. st.equal(qs.stringify(obj, { encode: false, arrayFormat: 'repeat' }), 'a[b][c]=d&a[b][e]=f', 'no array, repeat');
  1140. st.equal(qs.stringify(obj, { encode: false, arrayFormat: 'comma' }), 'a[b][c]=d&a[b][e]=f', 'no array, comma');
  1141. st.equal(qs.stringify(withArray, { encode: false }), 'a[b][0][c]=d&a[b][0][e]=f', 'array, no arrayFormat');
  1142. st.equal(qs.stringify(withArray, { encode: false, arrayFormat: 'brackets' }), 'a[b][][c]=d&a[b][][e]=f', 'array, bracket');
  1143. st.equal(qs.stringify(withArray, { encode: false, arrayFormat: 'indices' }), 'a[b][0][c]=d&a[b][0][e]=f', 'array, indices');
  1144. st.equal(qs.stringify(withArray, { encode: false, arrayFormat: 'repeat' }), 'a[b][c]=d&a[b][e]=f', 'array, repeat');
  1145. st.equal(
  1146. qs.stringify(withArray, { encode: false, arrayFormat: 'comma' }),
  1147. '???',
  1148. 'array, comma',
  1149. { skip: 'TODO: figure out what this should do' }
  1150. );
  1151. st.end();
  1152. });
  1153. t.test('stringifies sparse arrays', function (st) {
  1154. /* eslint no-sparse-arrays: 0 */
  1155. st.equal(qs.stringify({ a: [, '2', , , '1'] }, { encodeValuesOnly: true, arrayFormat: 'indices' }), 'a[1]=2&a[4]=1');
  1156. st.equal(qs.stringify({ a: [, '2', , , '1'] }, { encodeValuesOnly: true, arrayFormat: 'brackets' }), 'a[]=2&a[]=1');
  1157. st.equal(qs.stringify({ a: [, '2', , , '1'] }, { encodeValuesOnly: true, arrayFormat: 'repeat' }), 'a=2&a=1');
  1158. st.equal(qs.stringify({ a: [, { b: [, , { c: '1' }] }] }, { encodeValuesOnly: true, arrayFormat: 'indices' }), 'a[1][b][2][c]=1');
  1159. st.equal(qs.stringify({ a: [, { b: [, , { c: '1' }] }] }, { encodeValuesOnly: true, arrayFormat: 'brackets' }), 'a[][b][][c]=1');
  1160. st.equal(qs.stringify({ a: [, { b: [, , { c: '1' }] }] }, { encodeValuesOnly: true, arrayFormat: 'repeat' }), 'a[b][c]=1');
  1161. st.equal(qs.stringify({ a: [, [, , [, , , { c: '1' }]]] }, { encodeValuesOnly: true, arrayFormat: 'indices' }), 'a[1][2][3][c]=1');
  1162. st.equal(qs.stringify({ a: [, [, , [, , , { c: '1' }]]] }, { encodeValuesOnly: true, arrayFormat: 'brackets' }), 'a[][][][c]=1');
  1163. st.equal(qs.stringify({ a: [, [, , [, , , { c: '1' }]]] }, { encodeValuesOnly: true, arrayFormat: 'repeat' }), 'a[c]=1');
  1164. st.equal(qs.stringify({ a: [, [, , [, , , { c: [, '1'] }]]] }, { encodeValuesOnly: true, arrayFormat: 'indices' }), 'a[1][2][3][c][1]=1');
  1165. st.equal(qs.stringify({ a: [, [, , [, , , { c: [, '1'] }]]] }, { encodeValuesOnly: true, arrayFormat: 'brackets' }), 'a[][][][c][]=1');
  1166. st.equal(qs.stringify({ a: [, [, , [, , , { c: [, '1'] }]]] }, { encodeValuesOnly: true, arrayFormat: 'repeat' }), 'a[c]=1');
  1167. st.end();
  1168. });
  1169. t.test('encodes a very long string', function (st) {
  1170. var chars = [];
  1171. var expected = [];
  1172. for (var i = 0; i < 5e3; i++) {
  1173. chars.push(' ' + i);
  1174. expected.push('%20' + i);
  1175. }
  1176. var obj = {
  1177. foo: chars.join('')
  1178. };
  1179. st.equal(
  1180. qs.stringify(obj, { arrayFormat: 'brackets', charset: 'utf-8' }),
  1181. 'foo=' + expected.join('')
  1182. );
  1183. st.end();
  1184. });
  1185. t.end();
  1186. });
  1187. test('stringifies empty keys', function (t) {
  1188. emptyTestCases.forEach(function (testCase) {
  1189. t.test('stringifies an object with empty string key with ' + testCase.input, function (st) {
  1190. st.deepEqual(
  1191. qs.stringify(testCase.withEmptyKeys, { encode: false, arrayFormat: 'indices' }),
  1192. testCase.stringifyOutput.indices,
  1193. 'test case: ' + testCase.input + ', indices'
  1194. );
  1195. st.deepEqual(
  1196. qs.stringify(testCase.withEmptyKeys, { encode: false, arrayFormat: 'brackets' }),
  1197. testCase.stringifyOutput.brackets,
  1198. 'test case: ' + testCase.input + ', brackets'
  1199. );
  1200. st.deepEqual(
  1201. qs.stringify(testCase.withEmptyKeys, { encode: false, arrayFormat: 'repeat' }),
  1202. testCase.stringifyOutput.repeat,
  1203. 'test case: ' + testCase.input + ', repeat'
  1204. );
  1205. st.end();
  1206. });
  1207. });
  1208. t.test('edge case with object/arrays', function (st) {
  1209. st.deepEqual(qs.stringify({ '': { '': [2, 3] } }, { encode: false }), '[][0]=2&[][1]=3');
  1210. st.deepEqual(qs.stringify({ '': { '': [2, 3], a: 2 } }, { encode: false }), '[][0]=2&[][1]=3&[a]=2');
  1211. st.deepEqual(qs.stringify({ '': { '': [2, 3] } }, { encode: false, arrayFormat: 'indices' }), '[][0]=2&[][1]=3');
  1212. st.deepEqual(qs.stringify({ '': { '': [2, 3], a: 2 } }, { encode: false, arrayFormat: 'indices' }), '[][0]=2&[][1]=3&[a]=2');
  1213. st.end();
  1214. });
  1215. t.test('stringifies non-string keys', function (st) {
  1216. var S = Object('abc');
  1217. S.toString = function () {
  1218. return 'd';
  1219. };
  1220. var actual = qs.stringify({ a: 'b', 'false': {}, 1e+22: 'c', d: 'e' }, {
  1221. filter: ['a', false, null, 10000000000000000000000, S],
  1222. allowDots: true,
  1223. encodeDotInKeys: true
  1224. });
  1225. st.equal(actual, 'a=b&1e%2B22=c&d=e', 'stringifies correctly');
  1226. st.end();
  1227. });
  1228. t.test('round-trips keys containing percent-encoded bracket text', function (st) {
  1229. var cases = [
  1230. { 'a%5Bb': 'c' },
  1231. { 'a%5Db': 'c' },
  1232. { 'a%255Bb': 'c' },
  1233. { 'a%255Db': 'c' },
  1234. { a: { 'b%5Bc': 'd' } },
  1235. { a: { 'b%255Bc': 'd' } },
  1236. { 'a%5B%255Bb': 'c' }
  1237. ];
  1238. for (var i = 0; i < cases.length; i++) {
  1239. st.deepEqual(
  1240. qs.parse(qs.stringify(cases[i])),
  1241. cases[i],
  1242. 'round-trips ' + JSON.stringify(cases[i])
  1243. );
  1244. }
  1245. st.end();
  1246. });
  1247. t.test('parses input containing percent-encoded bracket text without mangling', function (st) {
  1248. st.deepEqual(qs.parse('a%25255Bb=c'), { 'a%255Bb': 'c' }, 'a%25255Bb decodes to a%255Bb, not a%5Bb');
  1249. st.deepEqual(qs.parse('a%25255Db=c'), { 'a%255Db': 'c' }, 'a%25255Db decodes to a%255Db, not a%5Db');
  1250. st.deepEqual(qs.parse('a%5Bb%25255Bc%5D=d'), { a: { 'b%255Bc': 'd' } }, 'nested %25255B decodes to %255B inside segment, not %5B');
  1251. st.end();
  1252. });
  1253. });