stringify.js 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  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('stringifies an array value with one item vs multiple items', function (st) {
  279. st.test('non-array item', function (s2t) {
  280. s2t.equal(qs.stringify({ a: 'c' }, { encodeValuesOnly: true, arrayFormat: 'indices' }), 'a=c');
  281. s2t.equal(qs.stringify({ a: 'c' }, { encodeValuesOnly: true, arrayFormat: 'brackets' }), 'a=c');
  282. s2t.equal(qs.stringify({ a: 'c' }, { encodeValuesOnly: true, arrayFormat: 'comma' }), 'a=c');
  283. s2t.equal(qs.stringify({ a: 'c' }, { encodeValuesOnly: true }), 'a=c');
  284. s2t.end();
  285. });
  286. st.test('array with a single item', function (s2t) {
  287. s2t.equal(qs.stringify({ a: ['c'] }, { encodeValuesOnly: true, arrayFormat: 'indices' }), 'a[0]=c');
  288. s2t.equal(qs.stringify({ a: ['c'] }, { encodeValuesOnly: true, arrayFormat: 'brackets' }), 'a[]=c');
  289. s2t.equal(qs.stringify({ a: ['c'] }, { encodeValuesOnly: true, arrayFormat: 'comma' }), 'a=c');
  290. s2t.equal(qs.stringify({ a: ['c'] }, { encodeValuesOnly: true, arrayFormat: 'comma', commaRoundTrip: true }), 'a[]=c'); // so it parses back as an array
  291. s2t.equal(qs.stringify({ a: ['c'] }, { encodeValuesOnly: true }), 'a[0]=c');
  292. s2t.end();
  293. });
  294. st.test('array with multiple items', function (s2t) {
  295. s2t.equal(qs.stringify({ a: ['c', 'd'] }, { encodeValuesOnly: true, arrayFormat: 'indices' }), 'a[0]=c&a[1]=d');
  296. s2t.equal(qs.stringify({ a: ['c', 'd'] }, { encodeValuesOnly: true, arrayFormat: 'brackets' }), 'a[]=c&a[]=d');
  297. s2t.equal(qs.stringify({ a: ['c', 'd'] }, { encodeValuesOnly: true, arrayFormat: 'comma' }), 'a=c,d');
  298. s2t.equal(qs.stringify({ a: ['c', 'd'] }, { encodeValuesOnly: true, arrayFormat: 'comma', commaRoundTrip: true }), 'a=c,d');
  299. s2t.equal(qs.stringify({ a: ['c', 'd'] }, { encodeValuesOnly: true }), 'a[0]=c&a[1]=d');
  300. s2t.end();
  301. });
  302. st.test('array with multiple items with a comma inside', function (s2t) {
  303. s2t.equal(qs.stringify({ a: ['c,d', 'e'] }, { encodeValuesOnly: true, arrayFormat: 'comma' }), 'a=c%2Cd,e');
  304. s2t.equal(qs.stringify({ a: ['c,d', 'e'] }, { arrayFormat: 'comma' }), 'a=c%2Cd%2Ce');
  305. s2t.equal(qs.stringify({ a: ['c,d', 'e'] }, { encodeValuesOnly: true, arrayFormat: 'comma', commaRoundTrip: true }), 'a=c%2Cd,e');
  306. s2t.equal(qs.stringify({ a: ['c,d', 'e'] }, { arrayFormat: 'comma', commaRoundTrip: true }), 'a=c%2Cd%2Ce');
  307. s2t.end();
  308. });
  309. st.end();
  310. });
  311. t.test('stringifies a nested array value', function (st) {
  312. st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { encodeValuesOnly: true, arrayFormat: 'indices' }), 'a[b][0]=c&a[b][1]=d');
  313. st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { encodeValuesOnly: true, arrayFormat: 'brackets' }), 'a[b][]=c&a[b][]=d');
  314. st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { encodeValuesOnly: true, arrayFormat: 'comma' }), 'a[b]=c,d');
  315. st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { encodeValuesOnly: true }), 'a[b][0]=c&a[b][1]=d');
  316. st.end();
  317. });
  318. t.test('stringifies comma and empty array values', function (st) {
  319. st.equal(qs.stringify({ a: [',', '', 'c,d%'] }, { encode: false, arrayFormat: 'indices' }), 'a[0]=,&a[1]=&a[2]=c,d%');
  320. st.equal(qs.stringify({ a: [',', '', 'c,d%'] }, { encode: false, arrayFormat: 'brackets' }), 'a[]=,&a[]=&a[]=c,d%');
  321. st.equal(qs.stringify({ a: [',', '', 'c,d%'] }, { encode: false, arrayFormat: 'comma' }), 'a=,,,c,d%');
  322. st.equal(qs.stringify({ a: [',', '', 'c,d%'] }, { encode: false, arrayFormat: 'repeat' }), 'a=,&a=&a=c,d%');
  323. st.equal(qs.stringify({ a: [',', '', 'c,d%'] }, { encode: true, encodeValuesOnly: true, arrayFormat: 'indices' }), 'a[0]=%2C&a[1]=&a[2]=c%2Cd%25');
  324. st.equal(qs.stringify({ a: [',', '', 'c,d%'] }, { encode: true, encodeValuesOnly: true, arrayFormat: 'brackets' }), 'a[]=%2C&a[]=&a[]=c%2Cd%25');
  325. st.equal(qs.stringify({ a: [',', '', 'c,d%'] }, { encode: true, encodeValuesOnly: true, arrayFormat: 'comma' }), 'a=%2C,,c%2Cd%25');
  326. st.equal(qs.stringify({ a: [',', '', 'c,d%'] }, { encode: true, encodeValuesOnly: true, arrayFormat: 'repeat' }), 'a=%2C&a=&a=c%2Cd%25');
  327. 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');
  328. 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');
  329. st.equal(qs.stringify({ a: [',', '', 'c,d%'] }, { encode: true, encodeValuesOnly: false, arrayFormat: 'comma' }), 'a=%2C%2C%2Cc%2Cd%25');
  330. st.equal(qs.stringify({ a: [',', '', 'c,d%'] }, { encode: true, encodeValuesOnly: false, arrayFormat: 'repeat' }), 'a=%2C&a=&a=c%2Cd%25');
  331. st.end();
  332. });
  333. t.test('stringifies comma and empty non-array values', function (st) {
  334. st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: false, arrayFormat: 'indices' }), 'a=,&b=&c=c,d%');
  335. st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: false, arrayFormat: 'brackets' }), 'a=,&b=&c=c,d%');
  336. st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: false, arrayFormat: 'comma' }), 'a=,&b=&c=c,d%');
  337. st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: false, arrayFormat: 'repeat' }), 'a=,&b=&c=c,d%');
  338. st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: true, encodeValuesOnly: true, arrayFormat: 'indices' }), 'a=%2C&b=&c=c%2Cd%25');
  339. st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: true, encodeValuesOnly: true, arrayFormat: 'brackets' }), 'a=%2C&b=&c=c%2Cd%25');
  340. st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: true, encodeValuesOnly: true, arrayFormat: 'comma' }), 'a=%2C&b=&c=c%2Cd%25');
  341. st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: true, encodeValuesOnly: true, arrayFormat: 'repeat' }), 'a=%2C&b=&c=c%2Cd%25');
  342. st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: true, encodeValuesOnly: false, arrayFormat: 'indices' }), 'a=%2C&b=&c=c%2Cd%25');
  343. st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: true, encodeValuesOnly: false, arrayFormat: 'brackets' }), 'a=%2C&b=&c=c%2Cd%25');
  344. st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: true, encodeValuesOnly: false, arrayFormat: 'comma' }), 'a=%2C&b=&c=c%2Cd%25');
  345. st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: true, encodeValuesOnly: false, arrayFormat: 'repeat' }), 'a=%2C&b=&c=c%2Cd%25');
  346. st.end();
  347. });
  348. t.test('stringifies a nested array value with dots notation', function (st) {
  349. st.equal(
  350. qs.stringify(
  351. { a: { b: ['c', 'd'] } },
  352. { allowDots: true, encodeValuesOnly: true, arrayFormat: 'indices' }
  353. ),
  354. 'a.b[0]=c&a.b[1]=d',
  355. 'indices: stringifies with dots + indices'
  356. );
  357. st.equal(
  358. qs.stringify(
  359. { a: { b: ['c', 'd'] } },
  360. { allowDots: true, encodeValuesOnly: true, arrayFormat: 'brackets' }
  361. ),
  362. 'a.b[]=c&a.b[]=d',
  363. 'brackets: stringifies with dots + brackets'
  364. );
  365. st.equal(
  366. qs.stringify(
  367. { a: { b: ['c', 'd'] } },
  368. { allowDots: true, encodeValuesOnly: true, arrayFormat: 'comma' }
  369. ),
  370. 'a.b=c,d',
  371. 'comma: stringifies with dots + comma'
  372. );
  373. st.equal(
  374. qs.stringify(
  375. { a: { b: ['c', 'd'] } },
  376. { allowDots: true, encodeValuesOnly: true }
  377. ),
  378. 'a.b[0]=c&a.b[1]=d',
  379. 'default: stringifies with dots + indices'
  380. );
  381. st.end();
  382. });
  383. t.test('stringifies an object inside an array', function (st) {
  384. st.equal(
  385. qs.stringify({ a: [{ b: 'c' }] }, { arrayFormat: 'indices', encodeValuesOnly: true }),
  386. 'a[0][b]=c',
  387. 'indices => indices'
  388. );
  389. st.equal(
  390. qs.stringify({ a: [{ b: 'c' }] }, { arrayFormat: 'repeat', encodeValuesOnly: true }),
  391. 'a[b]=c',
  392. 'repeat => repeat'
  393. );
  394. st.equal(
  395. qs.stringify({ a: [{ b: 'c' }] }, { arrayFormat: 'brackets', encodeValuesOnly: true }),
  396. 'a[][b]=c',
  397. 'brackets => brackets'
  398. );
  399. st.equal(
  400. qs.stringify({ a: [{ b: 'c' }] }, { encodeValuesOnly: true }),
  401. 'a[0][b]=c',
  402. 'default => indices'
  403. );
  404. st.equal(
  405. qs.stringify({ a: [{ b: { c: [1] } }] }, { arrayFormat: 'indices', encodeValuesOnly: true }),
  406. 'a[0][b][c][0]=1',
  407. 'indices => indices'
  408. );
  409. st.equal(
  410. qs.stringify({ a: [{ b: { c: [1] } }] }, { arrayFormat: 'repeat', encodeValuesOnly: true }),
  411. 'a[b][c]=1',
  412. 'repeat => repeat'
  413. );
  414. st.equal(
  415. qs.stringify({ a: [{ b: { c: [1] } }] }, { arrayFormat: 'brackets', encodeValuesOnly: true }),
  416. 'a[][b][c][]=1',
  417. 'brackets => brackets'
  418. );
  419. st.equal(
  420. qs.stringify({ a: [{ b: { c: [1] } }] }, { encodeValuesOnly: true }),
  421. 'a[0][b][c][0]=1',
  422. 'default => indices'
  423. );
  424. st.end();
  425. });
  426. t.test('stringifies an array with mixed objects and primitives', function (st) {
  427. st.equal(
  428. qs.stringify({ a: [{ b: 1 }, 2, 3] }, { encodeValuesOnly: true, arrayFormat: 'indices' }),
  429. 'a[0][b]=1&a[1]=2&a[2]=3',
  430. 'indices => indices'
  431. );
  432. st.equal(
  433. qs.stringify({ a: [{ b: 1 }, 2, 3] }, { encodeValuesOnly: true, arrayFormat: 'brackets' }),
  434. 'a[][b]=1&a[]=2&a[]=3',
  435. 'brackets => brackets'
  436. );
  437. st.equal(
  438. qs.stringify({ a: [{ b: 1 }, 2, 3] }, { encodeValuesOnly: true, arrayFormat: 'comma' }),
  439. '???',
  440. 'brackets => brackets',
  441. { skip: 'TODO: figure out what this should do' }
  442. );
  443. st.equal(
  444. qs.stringify({ a: [{ b: 1 }, 2, 3] }, { encodeValuesOnly: true }),
  445. 'a[0][b]=1&a[1]=2&a[2]=3',
  446. 'default => indices'
  447. );
  448. st.end();
  449. });
  450. t.test('stringifies an object inside an array with dots notation', function (st) {
  451. st.equal(
  452. qs.stringify(
  453. { a: [{ b: 'c' }] },
  454. { allowDots: true, encode: false, arrayFormat: 'indices' }
  455. ),
  456. 'a[0].b=c',
  457. 'indices => indices'
  458. );
  459. st.equal(
  460. qs.stringify(
  461. { a: [{ b: 'c' }] },
  462. { allowDots: true, encode: false, arrayFormat: 'brackets' }
  463. ),
  464. 'a[].b=c',
  465. 'brackets => brackets'
  466. );
  467. st.equal(
  468. qs.stringify(
  469. { a: [{ b: 'c' }] },
  470. { allowDots: true, encode: false }
  471. ),
  472. 'a[0].b=c',
  473. 'default => indices'
  474. );
  475. st.equal(
  476. qs.stringify(
  477. { a: [{ b: { c: [1] } }] },
  478. { allowDots: true, encode: false, arrayFormat: 'indices' }
  479. ),
  480. 'a[0].b.c[0]=1',
  481. 'indices => indices'
  482. );
  483. st.equal(
  484. qs.stringify(
  485. { a: [{ b: { c: [1] } }] },
  486. { allowDots: true, encode: false, arrayFormat: 'brackets' }
  487. ),
  488. 'a[].b.c[]=1',
  489. 'brackets => brackets'
  490. );
  491. st.equal(
  492. qs.stringify(
  493. { a: [{ b: { c: [1] } }] },
  494. { allowDots: true, encode: false }
  495. ),
  496. 'a[0].b.c[0]=1',
  497. 'default => indices'
  498. );
  499. st.end();
  500. });
  501. t.test('does not omit object keys when indices = false', function (st) {
  502. st.equal(qs.stringify({ a: [{ b: 'c' }] }, { indices: false }), 'a%5Bb%5D=c');
  503. st.end();
  504. });
  505. t.test('uses indices notation for arrays when indices=true', function (st) {
  506. st.equal(qs.stringify({ a: ['b', 'c'] }, { indices: true }), 'a%5B0%5D=b&a%5B1%5D=c');
  507. st.end();
  508. });
  509. t.test('uses indices notation for arrays when no arrayFormat is specified', function (st) {
  510. st.equal(qs.stringify({ a: ['b', 'c'] }), 'a%5B0%5D=b&a%5B1%5D=c');
  511. st.end();
  512. });
  513. t.test('uses indices notation for arrays when arrayFormat=indices', function (st) {
  514. st.equal(qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'indices' }), 'a%5B0%5D=b&a%5B1%5D=c');
  515. st.end();
  516. });
  517. t.test('uses repeat notation for arrays when arrayFormat=repeat', function (st) {
  518. st.equal(qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'repeat' }), 'a=b&a=c');
  519. st.end();
  520. });
  521. t.test('uses brackets notation for arrays when arrayFormat=brackets', function (st) {
  522. st.equal(qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'brackets' }), 'a%5B%5D=b&a%5B%5D=c');
  523. st.end();
  524. });
  525. t.test('stringifies a complicated object', function (st) {
  526. st.equal(qs.stringify({ a: { b: 'c', d: 'e' } }), 'a%5Bb%5D=c&a%5Bd%5D=e');
  527. st.end();
  528. });
  529. t.test('stringifies an empty value', function (st) {
  530. st.equal(qs.stringify({ a: '' }), 'a=');
  531. st.equal(qs.stringify({ a: null }, { strictNullHandling: true }), 'a');
  532. st.equal(qs.stringify({ a: '', b: '' }), 'a=&b=');
  533. st.equal(qs.stringify({ a: null, b: '' }, { strictNullHandling: true }), 'a&b=');
  534. st.equal(qs.stringify({ a: { b: '' } }), 'a%5Bb%5D=');
  535. st.equal(qs.stringify({ a: { b: null } }, { strictNullHandling: true }), 'a%5Bb%5D');
  536. st.equal(qs.stringify({ a: { b: null } }, { strictNullHandling: false }), 'a%5Bb%5D=');
  537. st.end();
  538. });
  539. t.test('stringifies an empty array in different arrayFormat', function (st) {
  540. st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false }), 'b[0]=&c=c');
  541. // arrayFormat default
  542. st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'indices' }), 'b[0]=&c=c');
  543. st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'brackets' }), 'b[]=&c=c');
  544. st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'repeat' }), 'b=&c=c');
  545. st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'comma' }), 'b=&c=c');
  546. st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'comma', commaRoundTrip: true }), 'b[]=&c=c');
  547. // with strictNullHandling
  548. st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'indices', strictNullHandling: true }), 'b[0]&c=c');
  549. st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'brackets', strictNullHandling: true }), 'b[]&c=c');
  550. st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'repeat', strictNullHandling: true }), 'b&c=c');
  551. st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'comma', strictNullHandling: true }), 'b&c=c');
  552. st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'comma', strictNullHandling: true, commaRoundTrip: true }), 'b[]&c=c');
  553. // with skipNulls
  554. st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'indices', skipNulls: true }), 'c=c');
  555. st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'brackets', skipNulls: true }), 'c=c');
  556. st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'repeat', skipNulls: true }), 'c=c');
  557. st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'comma', skipNulls: true }), 'c=c');
  558. st.end();
  559. });
  560. t.test('stringifies a null object', { skip: !Object.create }, function (st) {
  561. var obj = Object.create(null);
  562. obj.a = 'b';
  563. st.equal(qs.stringify(obj), 'a=b');
  564. st.end();
  565. });
  566. t.test('returns an empty string for invalid input', function (st) {
  567. st.equal(qs.stringify(undefined), '');
  568. st.equal(qs.stringify(false), '');
  569. st.equal(qs.stringify(null), '');
  570. st.equal(qs.stringify(''), '');
  571. st.end();
  572. });
  573. t.test('stringifies an object with a null object as a child', { skip: !Object.create }, function (st) {
  574. var obj = { a: Object.create(null) };
  575. obj.a.b = 'c';
  576. st.equal(qs.stringify(obj), 'a%5Bb%5D=c');
  577. st.end();
  578. });
  579. t.test('drops keys with a value of undefined', function (st) {
  580. st.equal(qs.stringify({ a: undefined }), '');
  581. st.equal(qs.stringify({ a: { b: undefined, c: null } }, { strictNullHandling: true }), 'a%5Bc%5D');
  582. st.equal(qs.stringify({ a: { b: undefined, c: null } }, { strictNullHandling: false }), 'a%5Bc%5D=');
  583. st.equal(qs.stringify({ a: { b: undefined, c: '' } }), 'a%5Bc%5D=');
  584. st.end();
  585. });
  586. t.test('url encodes values', function (st) {
  587. st.equal(qs.stringify({ a: 'b c' }), 'a=b%20c');
  588. st.end();
  589. });
  590. t.test('stringifies a date', function (st) {
  591. var now = new Date();
  592. var str = 'a=' + encodeURIComponent(now.toISOString());
  593. st.equal(qs.stringify({ a: now }), str);
  594. st.end();
  595. });
  596. t.test('stringifies the weird object from qs', function (st) {
  597. 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');
  598. st.end();
  599. });
  600. t.test('skips properties that are part of the object prototype', function (st) {
  601. st.intercept(Object.prototype, 'crash', { value: 'test' });
  602. st.equal(qs.stringify({ a: 'b' }), 'a=b');
  603. st.equal(qs.stringify({ a: { b: 'c' } }), 'a%5Bb%5D=c');
  604. st.end();
  605. });
  606. t.test('stringifies boolean values', function (st) {
  607. st.equal(qs.stringify({ a: true }), 'a=true');
  608. st.equal(qs.stringify({ a: { b: true } }), 'a%5Bb%5D=true');
  609. st.equal(qs.stringify({ b: false }), 'b=false');
  610. st.equal(qs.stringify({ b: { c: false } }), 'b%5Bc%5D=false');
  611. st.end();
  612. });
  613. t.test('stringifies buffer values', function (st) {
  614. st.equal(qs.stringify({ a: SaferBuffer.from('test') }), 'a=test');
  615. st.equal(qs.stringify({ a: { b: SaferBuffer.from('test') } }), 'a%5Bb%5D=test');
  616. st.end();
  617. });
  618. t.test('stringifies an object using an alternative delimiter', function (st) {
  619. st.equal(qs.stringify({ a: 'b', c: 'd' }, { delimiter: ';' }), 'a=b;c=d');
  620. st.end();
  621. });
  622. t.test('does not blow up when Buffer global is missing', function (st) {
  623. var restore = mockProperty(global, 'Buffer', { 'delete': true });
  624. var result = qs.stringify({ a: 'b', c: 'd' });
  625. restore();
  626. st.equal(result, 'a=b&c=d');
  627. st.end();
  628. });
  629. t.test('does not crash when parsing circular references', function (st) {
  630. var a = {};
  631. a.b = a;
  632. st['throws'](
  633. function () { qs.stringify({ 'foo[bar]': 'baz', 'foo[baz]': a }); },
  634. /RangeError: Cyclic object value/,
  635. 'cyclic values throw'
  636. );
  637. var circular = {
  638. a: 'value'
  639. };
  640. circular.a = circular;
  641. st['throws'](
  642. function () { qs.stringify(circular); },
  643. /RangeError: Cyclic object value/,
  644. 'cyclic values throw'
  645. );
  646. var arr = ['a'];
  647. st.doesNotThrow(
  648. function () { qs.stringify({ x: arr, y: arr }); },
  649. 'non-cyclic values do not throw'
  650. );
  651. st.end();
  652. });
  653. t.test('non-circular duplicated references can still work', function (st) {
  654. var hourOfDay = {
  655. 'function': 'hour_of_day'
  656. };
  657. var p1 = {
  658. 'function': 'gte',
  659. arguments: [hourOfDay, 0]
  660. };
  661. var p2 = {
  662. 'function': 'lte',
  663. arguments: [hourOfDay, 23]
  664. };
  665. st.equal(
  666. qs.stringify({ filters: { $and: [p1, p2] } }, { encodeValuesOnly: true, arrayFormat: 'indices' }),
  667. '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'
  668. );
  669. st.equal(
  670. qs.stringify({ filters: { $and: [p1, p2] } }, { encodeValuesOnly: true, arrayFormat: 'brackets' }),
  671. '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'
  672. );
  673. st.equal(
  674. qs.stringify({ filters: { $and: [p1, p2] } }, { encodeValuesOnly: true, arrayFormat: 'repeat' }),
  675. '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'
  676. );
  677. st.end();
  678. });
  679. t.test('selects properties when filter=array', function (st) {
  680. st.equal(qs.stringify({ a: 'b' }, { filter: ['a'] }), 'a=b');
  681. st.equal(qs.stringify({ a: 1 }, { filter: [] }), '');
  682. st.equal(
  683. qs.stringify(
  684. { a: { b: [1, 2, 3, 4], c: 'd' }, c: 'f' },
  685. { filter: ['a', 'b', 0, 2], arrayFormat: 'indices' }
  686. ),
  687. 'a%5Bb%5D%5B0%5D=1&a%5Bb%5D%5B2%5D=3',
  688. 'indices => indices'
  689. );
  690. st.equal(
  691. qs.stringify(
  692. { a: { b: [1, 2, 3, 4], c: 'd' }, c: 'f' },
  693. { filter: ['a', 'b', 0, 2], arrayFormat: 'brackets' }
  694. ),
  695. 'a%5Bb%5D%5B%5D=1&a%5Bb%5D%5B%5D=3',
  696. 'brackets => brackets'
  697. );
  698. st.equal(
  699. qs.stringify(
  700. { a: { b: [1, 2, 3, 4], c: 'd' }, c: 'f' },
  701. { filter: ['a', 'b', 0, 2] }
  702. ),
  703. 'a%5Bb%5D%5B0%5D=1&a%5Bb%5D%5B2%5D=3',
  704. 'default => indices'
  705. );
  706. st.end();
  707. });
  708. t.test('supports custom representations when filter=function', function (st) {
  709. var calls = 0;
  710. var obj = { a: 'b', c: 'd', e: { f: new Date(1257894000000) } };
  711. var filterFunc = function (prefix, value) {
  712. calls += 1;
  713. if (calls === 1) {
  714. st.equal(prefix, '', 'prefix is empty');
  715. st.equal(value, obj);
  716. } else if (prefix === 'c') {
  717. return void 0;
  718. } else if (value instanceof Date) {
  719. st.equal(prefix, 'e[f]');
  720. return value.getTime();
  721. }
  722. return value;
  723. };
  724. st.equal(qs.stringify(obj, { filter: filterFunc }), 'a=b&e%5Bf%5D=1257894000000');
  725. st.equal(calls, 5);
  726. st.end();
  727. });
  728. t.test('can disable uri encoding', function (st) {
  729. st.equal(qs.stringify({ a: 'b' }, { encode: false }), 'a=b');
  730. st.equal(qs.stringify({ a: { b: 'c' } }, { encode: false }), 'a[b]=c');
  731. st.equal(qs.stringify({ a: 'b', c: null }, { strictNullHandling: true, encode: false }), 'a=b&c');
  732. st.end();
  733. });
  734. t.test('can sort the keys', function (st) {
  735. var sort = function (a, b) {
  736. return a.localeCompare(b);
  737. };
  738. st.equal(qs.stringify({ a: 'c', z: 'y', b: 'f' }, { sort: sort }), 'a=c&b=f&z=y');
  739. 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');
  740. st.end();
  741. });
  742. t.test('can sort the keys at depth 3 or more too', function (st) {
  743. var sort = function (a, b) {
  744. return a.localeCompare(b);
  745. };
  746. st.equal(
  747. qs.stringify(
  748. { a: 'a', z: { zj: { zjb: 'zjb', zja: 'zja' }, zi: { zib: 'zib', zia: 'zia' } }, b: 'b' },
  749. { sort: sort, encode: false }
  750. ),
  751. 'a=a&b=b&z[zi][zia]=zia&z[zi][zib]=zib&z[zj][zja]=zja&z[zj][zjb]=zjb'
  752. );
  753. st.equal(
  754. qs.stringify(
  755. { a: 'a', z: { zj: { zjb: 'zjb', zja: 'zja' }, zi: { zib: 'zib', zia: 'zia' } }, b: 'b' },
  756. { sort: null, encode: false }
  757. ),
  758. 'a=a&z[zj][zjb]=zjb&z[zj][zja]=zja&z[zi][zib]=zib&z[zi][zia]=zia&b=b'
  759. );
  760. st.end();
  761. });
  762. t.test('can stringify with custom encoding', function (st) {
  763. st.equal(qs.stringify({ 県: '大阪府', '': '' }, {
  764. encoder: function (str) {
  765. if (str.length === 0) {
  766. return '';
  767. }
  768. var buf = iconv.encode(str, 'shiftjis');
  769. var result = [];
  770. for (var i = 0; i < buf.length; ++i) {
  771. result.push(buf.readUInt8(i).toString(16));
  772. }
  773. return '%' + result.join('%');
  774. }
  775. }), '%8c%a7=%91%e5%8d%e3%95%7b&=');
  776. st.end();
  777. });
  778. t.test('receives the default encoder as a second argument', function (st) {
  779. st.plan(8);
  780. qs.stringify({ a: 1, b: new Date(), c: true, d: [1] }, {
  781. encoder: function (str) {
  782. st.match(typeof str, /^(?:string|number|boolean)$/);
  783. return '';
  784. }
  785. });
  786. st.end();
  787. });
  788. t.test('receives the default encoder as a second argument', function (st) {
  789. st.plan(2);
  790. qs.stringify({ a: 1 }, {
  791. encoder: function (str, defaultEncoder) {
  792. st.equal(defaultEncoder, utils.encode);
  793. }
  794. });
  795. st.end();
  796. });
  797. t.test('throws error with wrong encoder', function (st) {
  798. st['throws'](function () {
  799. qs.stringify({}, { encoder: 'string' });
  800. }, new TypeError('Encoder has to be a function.'));
  801. st.end();
  802. });
  803. t.test('can use custom encoder for a buffer object', { skip: typeof Buffer === 'undefined' }, function (st) {
  804. st.equal(qs.stringify({ a: SaferBuffer.from([1]) }, {
  805. encoder: function (buffer) {
  806. if (typeof buffer === 'string') {
  807. return buffer;
  808. }
  809. return String.fromCharCode(buffer.readUInt8(0) + 97);
  810. }
  811. }), 'a=b');
  812. st.equal(qs.stringify({ a: SaferBuffer.from('a b') }, {
  813. encoder: function (buffer) {
  814. return buffer;
  815. }
  816. }), 'a=a b');
  817. st.end();
  818. });
  819. t.test('serializeDate option', function (st) {
  820. var date = new Date();
  821. st.equal(
  822. qs.stringify({ a: date }),
  823. 'a=' + date.toISOString().replace(/:/g, '%3A'),
  824. 'default is toISOString'
  825. );
  826. var mutatedDate = new Date();
  827. mutatedDate.toISOString = function () {
  828. throw new SyntaxError();
  829. };
  830. st['throws'](function () {
  831. mutatedDate.toISOString();
  832. }, SyntaxError);
  833. st.equal(
  834. qs.stringify({ a: mutatedDate }),
  835. 'a=' + Date.prototype.toISOString.call(mutatedDate).replace(/:/g, '%3A'),
  836. 'toISOString works even when method is not locally present'
  837. );
  838. var specificDate = new Date(6);
  839. st.equal(
  840. qs.stringify(
  841. { a: specificDate },
  842. { serializeDate: function (d) { return d.getTime() * 7; } }
  843. ),
  844. 'a=42',
  845. 'custom serializeDate function called'
  846. );
  847. st.equal(
  848. qs.stringify(
  849. { a: [date] },
  850. {
  851. serializeDate: function (d) { return d.getTime(); },
  852. arrayFormat: 'comma'
  853. }
  854. ),
  855. 'a=' + date.getTime(),
  856. 'works with arrayFormat comma'
  857. );
  858. st.equal(
  859. qs.stringify(
  860. { a: [date] },
  861. {
  862. serializeDate: function (d) { return d.getTime(); },
  863. arrayFormat: 'comma',
  864. commaRoundTrip: true
  865. }
  866. ),
  867. 'a%5B%5D=' + date.getTime(),
  868. 'works with arrayFormat comma'
  869. );
  870. st.end();
  871. });
  872. t.test('RFC 1738 serialization', function (st) {
  873. st.equal(qs.stringify({ a: 'b c' }, { format: qs.formats.RFC1738 }), 'a=b+c');
  874. st.equal(qs.stringify({ 'a b': 'c d' }, { format: qs.formats.RFC1738 }), 'a+b=c+d');
  875. st.equal(qs.stringify({ 'a b': SaferBuffer.from('a b') }, { format: qs.formats.RFC1738 }), 'a+b=a+b');
  876. st.equal(qs.stringify({ 'foo(ref)': 'bar' }, { format: qs.formats.RFC1738 }), 'foo(ref)=bar');
  877. st.end();
  878. });
  879. t.test('RFC 3986 spaces serialization', function (st) {
  880. st.equal(qs.stringify({ a: 'b c' }, { format: qs.formats.RFC3986 }), 'a=b%20c');
  881. st.equal(qs.stringify({ 'a b': 'c d' }, { format: qs.formats.RFC3986 }), 'a%20b=c%20d');
  882. st.equal(qs.stringify({ 'a b': SaferBuffer.from('a b') }, { format: qs.formats.RFC3986 }), 'a%20b=a%20b');
  883. st.end();
  884. });
  885. t.test('Backward compatibility to RFC 3986', function (st) {
  886. st.equal(qs.stringify({ a: 'b c' }), 'a=b%20c');
  887. st.equal(qs.stringify({ 'a b': SaferBuffer.from('a b') }), 'a%20b=a%20b');
  888. st.end();
  889. });
  890. t.test('Edge cases and unknown formats', function (st) {
  891. ['UFO1234', false, 1234, null, {}, []].forEach(function (format) {
  892. st['throws'](
  893. function () {
  894. qs.stringify({ a: 'b c' }, { format: format });
  895. },
  896. new TypeError('Unknown format option provided.')
  897. );
  898. });
  899. st.end();
  900. });
  901. t.test('encodeValuesOnly', function (st) {
  902. st.equal(
  903. qs.stringify(
  904. { a: 'b', c: ['d', 'e=f'], f: [['g'], ['h']] },
  905. { encodeValuesOnly: true, arrayFormat: 'indices' }
  906. ),
  907. 'a=b&c[0]=d&c[1]=e%3Df&f[0][0]=g&f[1][0]=h',
  908. 'encodeValuesOnly + indices'
  909. );
  910. st.equal(
  911. qs.stringify(
  912. { a: 'b', c: ['d', 'e=f'], f: [['g'], ['h']] },
  913. { encodeValuesOnly: true, arrayFormat: 'brackets' }
  914. ),
  915. 'a=b&c[]=d&c[]=e%3Df&f[][]=g&f[][]=h',
  916. 'encodeValuesOnly + brackets'
  917. );
  918. st.equal(
  919. qs.stringify(
  920. { a: 'b', c: ['d', 'e=f'], f: [['g'], ['h']] },
  921. { encodeValuesOnly: true, arrayFormat: 'repeat' }
  922. ),
  923. 'a=b&c=d&c=e%3Df&f=g&f=h',
  924. 'encodeValuesOnly + repeat'
  925. );
  926. st.equal(
  927. qs.stringify(
  928. { a: 'b', c: ['d', 'e'], f: [['g'], ['h']] },
  929. { arrayFormat: 'indices' }
  930. ),
  931. 'a=b&c%5B0%5D=d&c%5B1%5D=e&f%5B0%5D%5B0%5D=g&f%5B1%5D%5B0%5D=h',
  932. 'no encodeValuesOnly + indices'
  933. );
  934. st.equal(
  935. qs.stringify(
  936. { a: 'b', c: ['d', 'e'], f: [['g'], ['h']] },
  937. { arrayFormat: 'brackets' }
  938. ),
  939. 'a=b&c%5B%5D=d&c%5B%5D=e&f%5B%5D%5B%5D=g&f%5B%5D%5B%5D=h',
  940. 'no encodeValuesOnly + brackets'
  941. );
  942. st.equal(
  943. qs.stringify(
  944. { a: 'b', c: ['d', 'e'], f: [['g'], ['h']] },
  945. { arrayFormat: 'repeat' }
  946. ),
  947. 'a=b&c=d&c=e&f=g&f=h',
  948. 'no encodeValuesOnly + repeat'
  949. );
  950. st.end();
  951. });
  952. t.test('encodeValuesOnly - strictNullHandling', function (st) {
  953. st.equal(
  954. qs.stringify(
  955. { a: { b: null } },
  956. { encodeValuesOnly: true, strictNullHandling: true }
  957. ),
  958. 'a[b]'
  959. );
  960. st.end();
  961. });
  962. t.test('throws if an invalid charset is specified', function (st) {
  963. st['throws'](function () {
  964. qs.stringify({ a: 'b' }, { charset: 'foobar' });
  965. }, new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined'));
  966. st.end();
  967. });
  968. t.test('respects a charset of iso-8859-1', function (st) {
  969. st.equal(qs.stringify({ æ: 'æ' }, { charset: 'iso-8859-1' }), '%E6=%E6');
  970. st.end();
  971. });
  972. t.test('encodes unrepresentable chars as numeric entities in iso-8859-1 mode', function (st) {
  973. st.equal(qs.stringify({ a: '☺' }, { charset: 'iso-8859-1' }), 'a=%26%239786%3B');
  974. st.end();
  975. });
  976. t.test('respects an explicit charset of utf-8 (the default)', function (st) {
  977. st.equal(qs.stringify({ a: 'æ' }, { charset: 'utf-8' }), 'a=%C3%A6');
  978. st.end();
  979. });
  980. t.test('`charsetSentinel` option', function (st) {
  981. st.equal(
  982. qs.stringify({ a: 'æ' }, { charsetSentinel: true, charset: 'utf-8' }),
  983. 'utf8=%E2%9C%93&a=%C3%A6',
  984. 'adds the right sentinel when instructed to and the charset is utf-8'
  985. );
  986. st.equal(
  987. qs.stringify({ a: 'æ' }, { charsetSentinel: true, charset: 'iso-8859-1' }),
  988. 'utf8=%26%2310003%3B&a=%E6',
  989. 'adds the right sentinel when instructed to and the charset is iso-8859-1'
  990. );
  991. st.end();
  992. });
  993. t.test('does not mutate the options argument', function (st) {
  994. var options = {};
  995. qs.stringify({}, options);
  996. st.deepEqual(options, {});
  997. st.end();
  998. });
  999. t.test('strictNullHandling works with custom filter', function (st) {
  1000. var filter = function (prefix, value) {
  1001. return value;
  1002. };
  1003. var options = { strictNullHandling: true, filter: filter };
  1004. st.equal(qs.stringify({ key: null }, options), 'key');
  1005. st.end();
  1006. });
  1007. t.test('strictNullHandling works with null serializeDate', function (st) {
  1008. var serializeDate = function () {
  1009. return null;
  1010. };
  1011. var options = { strictNullHandling: true, serializeDate: serializeDate };
  1012. var date = new Date();
  1013. st.equal(qs.stringify({ key: date }, options), 'key');
  1014. st.end();
  1015. });
  1016. t.test('allows for encoding keys and values differently', function (st) {
  1017. var encoder = function (str, defaultEncoder, charset, type) {
  1018. if (type === 'key') {
  1019. return defaultEncoder(str, defaultEncoder, charset, type).toLowerCase();
  1020. }
  1021. if (type === 'value') {
  1022. return defaultEncoder(str, defaultEncoder, charset, type).toUpperCase();
  1023. }
  1024. throw 'this should never happen! type: ' + type;
  1025. };
  1026. st.deepEqual(qs.stringify({ KeY: 'vAlUe' }, { encoder: encoder }), 'key=VALUE');
  1027. st.end();
  1028. });
  1029. t.test('objects inside arrays', function (st) {
  1030. var obj = { a: { b: { c: 'd', e: 'f' } } };
  1031. var withArray = { a: { b: [{ c: 'd', e: 'f' }] } };
  1032. st.equal(qs.stringify(obj, { encode: false }), 'a[b][c]=d&a[b][e]=f', 'no array, no arrayFormat');
  1033. st.equal(qs.stringify(obj, { encode: false, arrayFormat: 'brackets' }), 'a[b][c]=d&a[b][e]=f', 'no array, bracket');
  1034. st.equal(qs.stringify(obj, { encode: false, arrayFormat: 'indices' }), 'a[b][c]=d&a[b][e]=f', 'no array, indices');
  1035. st.equal(qs.stringify(obj, { encode: false, arrayFormat: 'repeat' }), 'a[b][c]=d&a[b][e]=f', 'no array, repeat');
  1036. st.equal(qs.stringify(obj, { encode: false, arrayFormat: 'comma' }), 'a[b][c]=d&a[b][e]=f', 'no array, comma');
  1037. st.equal(qs.stringify(withArray, { encode: false }), 'a[b][0][c]=d&a[b][0][e]=f', 'array, no arrayFormat');
  1038. st.equal(qs.stringify(withArray, { encode: false, arrayFormat: 'brackets' }), 'a[b][][c]=d&a[b][][e]=f', 'array, bracket');
  1039. st.equal(qs.stringify(withArray, { encode: false, arrayFormat: 'indices' }), 'a[b][0][c]=d&a[b][0][e]=f', 'array, indices');
  1040. st.equal(qs.stringify(withArray, { encode: false, arrayFormat: 'repeat' }), 'a[b][c]=d&a[b][e]=f', 'array, repeat');
  1041. st.equal(
  1042. qs.stringify(withArray, { encode: false, arrayFormat: 'comma' }),
  1043. '???',
  1044. 'array, comma',
  1045. { skip: 'TODO: figure out what this should do' }
  1046. );
  1047. st.end();
  1048. });
  1049. t.test('stringifies sparse arrays', function (st) {
  1050. /* eslint no-sparse-arrays: 0 */
  1051. st.equal(qs.stringify({ a: [, '2', , , '1'] }, { encodeValuesOnly: true, arrayFormat: 'indices' }), 'a[1]=2&a[4]=1');
  1052. st.equal(qs.stringify({ a: [, '2', , , '1'] }, { encodeValuesOnly: true, arrayFormat: 'brackets' }), 'a[]=2&a[]=1');
  1053. st.equal(qs.stringify({ a: [, '2', , , '1'] }, { encodeValuesOnly: true, arrayFormat: 'repeat' }), 'a=2&a=1');
  1054. st.equal(qs.stringify({ a: [, { b: [, , { c: '1' }] }] }, { encodeValuesOnly: true, arrayFormat: 'indices' }), 'a[1][b][2][c]=1');
  1055. st.equal(qs.stringify({ a: [, { b: [, , { c: '1' }] }] }, { encodeValuesOnly: true, arrayFormat: 'brackets' }), 'a[][b][][c]=1');
  1056. st.equal(qs.stringify({ a: [, { b: [, , { c: '1' }] }] }, { encodeValuesOnly: true, arrayFormat: 'repeat' }), 'a[b][c]=1');
  1057. st.equal(qs.stringify({ a: [, [, , [, , , { c: '1' }]]] }, { encodeValuesOnly: true, arrayFormat: 'indices' }), 'a[1][2][3][c]=1');
  1058. st.equal(qs.stringify({ a: [, [, , [, , , { c: '1' }]]] }, { encodeValuesOnly: true, arrayFormat: 'brackets' }), 'a[][][][c]=1');
  1059. st.equal(qs.stringify({ a: [, [, , [, , , { c: '1' }]]] }, { encodeValuesOnly: true, arrayFormat: 'repeat' }), 'a[c]=1');
  1060. st.equal(qs.stringify({ a: [, [, , [, , , { c: [, '1'] }]]] }, { encodeValuesOnly: true, arrayFormat: 'indices' }), 'a[1][2][3][c][1]=1');
  1061. st.equal(qs.stringify({ a: [, [, , [, , , { c: [, '1'] }]]] }, { encodeValuesOnly: true, arrayFormat: 'brackets' }), 'a[][][][c][]=1');
  1062. st.equal(qs.stringify({ a: [, [, , [, , , { c: [, '1'] }]]] }, { encodeValuesOnly: true, arrayFormat: 'repeat' }), 'a[c]=1');
  1063. st.end();
  1064. });
  1065. t.test('encodes a very long string', function (st) {
  1066. var chars = [];
  1067. var expected = [];
  1068. for (var i = 0; i < 5e3; i++) {
  1069. chars.push(' ' + i);
  1070. expected.push('%20' + i);
  1071. }
  1072. var obj = {
  1073. foo: chars.join('')
  1074. };
  1075. st.equal(
  1076. qs.stringify(obj, { arrayFormat: 'bracket', charset: 'utf-8' }),
  1077. 'foo=' + expected.join('')
  1078. );
  1079. st.end();
  1080. });
  1081. t.end();
  1082. });
  1083. test('stringifies empty keys', function (t) {
  1084. emptyTestCases.forEach(function (testCase) {
  1085. t.test('stringifies an object with empty string key with ' + testCase.input, function (st) {
  1086. st.deepEqual(
  1087. qs.stringify(testCase.withEmptyKeys, { encode: false, arrayFormat: 'indices' }),
  1088. testCase.stringifyOutput.indices,
  1089. 'test case: ' + testCase.input + ', indices'
  1090. );
  1091. st.deepEqual(
  1092. qs.stringify(testCase.withEmptyKeys, { encode: false, arrayFormat: 'brackets' }),
  1093. testCase.stringifyOutput.brackets,
  1094. 'test case: ' + testCase.input + ', brackets'
  1095. );
  1096. st.deepEqual(
  1097. qs.stringify(testCase.withEmptyKeys, { encode: false, arrayFormat: 'repeat' }),
  1098. testCase.stringifyOutput.repeat,
  1099. 'test case: ' + testCase.input + ', repeat'
  1100. );
  1101. st.end();
  1102. });
  1103. });
  1104. t.test('edge case with object/arrays', function (st) {
  1105. st.deepEqual(qs.stringify({ '': { '': [2, 3] } }, { encode: false }), '[][0]=2&[][1]=3');
  1106. st.deepEqual(qs.stringify({ '': { '': [2, 3], a: 2 } }, { encode: false }), '[][0]=2&[][1]=3&[a]=2');
  1107. st.deepEqual(qs.stringify({ '': { '': [2, 3] } }, { encode: false, arrayFormat: 'indices' }), '[][0]=2&[][1]=3');
  1108. st.deepEqual(qs.stringify({ '': { '': [2, 3], a: 2 } }, { encode: false, arrayFormat: 'indices' }), '[][0]=2&[][1]=3&[a]=2');
  1109. st.end();
  1110. });
  1111. });