stringify.js 52 KB

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