Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stringify.js 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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. test('stringify()', function (t) {
  8. t.test('stringifies a querystring object', function (st) {
  9. st.equal(qs.stringify({ a: 'b' }), 'a=b');
  10. st.equal(qs.stringify({ a: 1 }), 'a=1');
  11. st.equal(qs.stringify({ a: 1, b: 2 }), 'a=1&b=2');
  12. st.equal(qs.stringify({ a: 'A_Z' }), 'a=A_Z');
  13. st.equal(qs.stringify({ a: '€' }), 'a=%E2%82%AC');
  14. st.equal(qs.stringify({ a: '' }), 'a=%EE%80%80');
  15. st.equal(qs.stringify({ a: 'א' }), 'a=%D7%90');
  16. st.equal(qs.stringify({ a: '𐐷' }), 'a=%F0%90%90%B7');
  17. st.end();
  18. });
  19. t.test('stringifies falsy values', function (st) {
  20. st.equal(qs.stringify(undefined), '');
  21. st.equal(qs.stringify(null), '');
  22. st.equal(qs.stringify(null, { strictNullHandling: true }), '');
  23. st.equal(qs.stringify(false), '');
  24. st.equal(qs.stringify(0), '');
  25. st.end();
  26. });
  27. t.test('adds query prefix', function (st) {
  28. st.equal(qs.stringify({ a: 'b' }, { addQueryPrefix: true }), '?a=b');
  29. st.end();
  30. });
  31. t.test('with query prefix, outputs blank string given an empty object', function (st) {
  32. st.equal(qs.stringify({}, { addQueryPrefix: true }), '');
  33. st.end();
  34. });
  35. t.test('stringifies nested falsy values', function (st) {
  36. st.equal(qs.stringify({ a: { b: { c: null } } }), 'a%5Bb%5D%5Bc%5D=');
  37. st.equal(qs.stringify({ a: { b: { c: null } } }, { strictNullHandling: true }), 'a%5Bb%5D%5Bc%5D');
  38. st.equal(qs.stringify({ a: { b: { c: false } } }), 'a%5Bb%5D%5Bc%5D=false');
  39. st.end();
  40. });
  41. t.test('stringifies a nested object', function (st) {
  42. st.equal(qs.stringify({ a: { b: 'c' } }), 'a%5Bb%5D=c');
  43. st.equal(qs.stringify({ a: { b: { c: { d: 'e' } } } }), 'a%5Bb%5D%5Bc%5D%5Bd%5D=e');
  44. st.end();
  45. });
  46. t.test('stringifies a nested object with dots notation', function (st) {
  47. st.equal(qs.stringify({ a: { b: 'c' } }, { allowDots: true }), 'a.b=c');
  48. st.equal(qs.stringify({ a: { b: { c: { d: 'e' } } } }, { allowDots: true }), 'a.b.c.d=e');
  49. st.end();
  50. });
  51. t.test('stringifies an array value', function (st) {
  52. st.equal(
  53. qs.stringify({ a: ['b', 'c', 'd'] }, { arrayFormat: 'indices' }),
  54. 'a%5B0%5D=b&a%5B1%5D=c&a%5B2%5D=d',
  55. 'indices => indices'
  56. );
  57. st.equal(
  58. qs.stringify({ a: ['b', 'c', 'd'] }, { arrayFormat: 'brackets' }),
  59. 'a%5B%5D=b&a%5B%5D=c&a%5B%5D=d',
  60. 'brackets => brackets'
  61. );
  62. st.equal(
  63. qs.stringify({ a: ['b', 'c', 'd'] }, { arrayFormat: 'comma' }),
  64. 'a=b%2Cc%2Cd',
  65. 'comma => comma'
  66. );
  67. st.equal(
  68. qs.stringify({ a: ['b', 'c', 'd'] }),
  69. 'a%5B0%5D=b&a%5B1%5D=c&a%5B2%5D=d',
  70. 'default => indices'
  71. );
  72. st.end();
  73. });
  74. t.test('omits nulls when asked', function (st) {
  75. st.equal(qs.stringify({ a: 'b', c: null }, { skipNulls: true }), 'a=b');
  76. st.end();
  77. });
  78. t.test('omits nested nulls when asked', function (st) {
  79. st.equal(qs.stringify({ a: { b: 'c', d: null } }, { skipNulls: true }), 'a%5Bb%5D=c');
  80. st.end();
  81. });
  82. t.test('omits array indices when asked', function (st) {
  83. st.equal(qs.stringify({ a: ['b', 'c', 'd'] }, { indices: false }), 'a=b&a=c&a=d');
  84. st.end();
  85. });
  86. t.test('stringifies a nested array value', function (st) {
  87. st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { arrayFormat: 'indices' }), 'a%5Bb%5D%5B0%5D=c&a%5Bb%5D%5B1%5D=d');
  88. st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { arrayFormat: 'brackets' }), 'a%5Bb%5D%5B%5D=c&a%5Bb%5D%5B%5D=d');
  89. st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { arrayFormat: 'comma' }), 'a%5Bb%5D=c%2Cd'); // a[b]=c,d
  90. st.equal(qs.stringify({ a: { b: ['c', 'd'] } }), 'a%5Bb%5D%5B0%5D=c&a%5Bb%5D%5B1%5D=d');
  91. st.end();
  92. });
  93. t.test('stringifies a nested array value with dots notation', function (st) {
  94. st.equal(
  95. qs.stringify(
  96. { a: { b: ['c', 'd'] } },
  97. { allowDots: true, encode: false, arrayFormat: 'indices' }
  98. ),
  99. 'a.b[0]=c&a.b[1]=d',
  100. 'indices: stringifies with dots + indices'
  101. );
  102. st.equal(
  103. qs.stringify(
  104. { a: { b: ['c', 'd'] } },
  105. { allowDots: true, encode: false, arrayFormat: 'brackets' }
  106. ),
  107. 'a.b[]=c&a.b[]=d',
  108. 'brackets: stringifies with dots + brackets'
  109. );
  110. st.equal(
  111. qs.stringify(
  112. { a: { b: ['c', 'd'] } },
  113. { allowDots: true, encode: false, arrayFormat: 'comma' }
  114. ),
  115. 'a.b=c,d',
  116. 'comma: stringifies with dots + comma'
  117. );
  118. st.equal(
  119. qs.stringify(
  120. { a: { b: ['c', 'd'] } },
  121. { allowDots: true, encode: false }
  122. ),
  123. 'a.b[0]=c&a.b[1]=d',
  124. 'default: stringifies with dots + indices'
  125. );
  126. st.end();
  127. });
  128. t.test('stringifies an object inside an array', function (st) {
  129. st.equal(
  130. qs.stringify({ a: [{ b: 'c' }] }, { arrayFormat: 'indices' }),
  131. 'a%5B0%5D%5Bb%5D=c', // a[0][b]=c
  132. 'indices => brackets'
  133. );
  134. st.equal(
  135. qs.stringify({ a: [{ b: 'c' }] }, { arrayFormat: 'brackets' }),
  136. 'a%5B%5D%5Bb%5D=c', // a[][b]=c
  137. 'brackets => brackets'
  138. );
  139. st.equal(
  140. qs.stringify({ a: [{ b: 'c' }] }),
  141. 'a%5B0%5D%5Bb%5D=c',
  142. 'default => indices'
  143. );
  144. st.equal(
  145. qs.stringify({ a: [{ b: { c: [1] } }] }, { arrayFormat: 'indices' }),
  146. 'a%5B0%5D%5Bb%5D%5Bc%5D%5B0%5D=1',
  147. 'indices => indices'
  148. );
  149. st.equal(
  150. qs.stringify({ a: [{ b: { c: [1] } }] }, { arrayFormat: 'brackets' }),
  151. 'a%5B%5D%5Bb%5D%5Bc%5D%5B%5D=1',
  152. 'brackets => brackets'
  153. );
  154. st.equal(
  155. qs.stringify({ a: [{ b: { c: [1] } }] }),
  156. 'a%5B0%5D%5Bb%5D%5Bc%5D%5B0%5D=1',
  157. 'default => indices'
  158. );
  159. st.end();
  160. });
  161. t.test('stringifies an array with mixed objects and primitives', function (st) {
  162. st.equal(
  163. qs.stringify({ a: [{ b: 1 }, 2, 3] }, { encode: false, arrayFormat: 'indices' }),
  164. 'a[0][b]=1&a[1]=2&a[2]=3',
  165. 'indices => indices'
  166. );
  167. st.equal(
  168. qs.stringify({ a: [{ b: 1 }, 2, 3] }, { encode: false, arrayFormat: 'brackets' }),
  169. 'a[][b]=1&a[]=2&a[]=3',
  170. 'brackets => brackets'
  171. );
  172. st.equal(
  173. qs.stringify({ a: [{ b: 1 }, 2, 3] }, { encode: false }),
  174. 'a[0][b]=1&a[1]=2&a[2]=3',
  175. 'default => indices'
  176. );
  177. st.end();
  178. });
  179. t.test('stringifies an object inside an array with dots notation', function (st) {
  180. st.equal(
  181. qs.stringify(
  182. { a: [{ b: 'c' }] },
  183. { allowDots: true, encode: false, arrayFormat: 'indices' }
  184. ),
  185. 'a[0].b=c',
  186. 'indices => indices'
  187. );
  188. st.equal(
  189. qs.stringify(
  190. { a: [{ b: 'c' }] },
  191. { allowDots: true, encode: false, arrayFormat: 'brackets' }
  192. ),
  193. 'a[].b=c',
  194. 'brackets => brackets'
  195. );
  196. st.equal(
  197. qs.stringify(
  198. { a: [{ b: 'c' }] },
  199. { allowDots: true, encode: false }
  200. ),
  201. 'a[0].b=c',
  202. 'default => indices'
  203. );
  204. st.equal(
  205. qs.stringify(
  206. { a: [{ b: { c: [1] } }] },
  207. { allowDots: true, encode: false, arrayFormat: 'indices' }
  208. ),
  209. 'a[0].b.c[0]=1',
  210. 'indices => indices'
  211. );
  212. st.equal(
  213. qs.stringify(
  214. { a: [{ b: { c: [1] } }] },
  215. { allowDots: true, encode: false, arrayFormat: 'brackets' }
  216. ),
  217. 'a[].b.c[]=1',
  218. 'brackets => brackets'
  219. );
  220. st.equal(
  221. qs.stringify(
  222. { a: [{ b: { c: [1] } }] },
  223. { allowDots: true, encode: false }
  224. ),
  225. 'a[0].b.c[0]=1',
  226. 'default => indices'
  227. );
  228. st.end();
  229. });
  230. t.test('does not omit object keys when indices = false', function (st) {
  231. st.equal(qs.stringify({ a: [{ b: 'c' }] }, { indices: false }), 'a%5Bb%5D=c');
  232. st.end();
  233. });
  234. t.test('uses indices notation for arrays when indices=true', function (st) {
  235. st.equal(qs.stringify({ a: ['b', 'c'] }, { indices: true }), 'a%5B0%5D=b&a%5B1%5D=c');
  236. st.end();
  237. });
  238. t.test('uses indices notation for arrays when no arrayFormat is specified', function (st) {
  239. st.equal(qs.stringify({ a: ['b', 'c'] }), 'a%5B0%5D=b&a%5B1%5D=c');
  240. st.end();
  241. });
  242. t.test('uses indices notation for arrays when no arrayFormat=indices', function (st) {
  243. st.equal(qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'indices' }), 'a%5B0%5D=b&a%5B1%5D=c');
  244. st.end();
  245. });
  246. t.test('uses repeat notation for arrays when no arrayFormat=repeat', function (st) {
  247. st.equal(qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'repeat' }), 'a=b&a=c');
  248. st.end();
  249. });
  250. t.test('uses brackets notation for arrays when no arrayFormat=brackets', function (st) {
  251. st.equal(qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'brackets' }), 'a%5B%5D=b&a%5B%5D=c');
  252. st.end();
  253. });
  254. t.test('stringifies a complicated object', function (st) {
  255. st.equal(qs.stringify({ a: { b: 'c', d: 'e' } }), 'a%5Bb%5D=c&a%5Bd%5D=e');
  256. st.end();
  257. });
  258. t.test('stringifies an empty value', function (st) {
  259. st.equal(qs.stringify({ a: '' }), 'a=');
  260. st.equal(qs.stringify({ a: null }, { strictNullHandling: true }), 'a');
  261. st.equal(qs.stringify({ a: '', b: '' }), 'a=&b=');
  262. st.equal(qs.stringify({ a: null, b: '' }, { strictNullHandling: true }), 'a&b=');
  263. st.equal(qs.stringify({ a: { b: '' } }), 'a%5Bb%5D=');
  264. st.equal(qs.stringify({ a: { b: null } }, { strictNullHandling: true }), 'a%5Bb%5D');
  265. st.equal(qs.stringify({ a: { b: null } }, { strictNullHandling: false }), 'a%5Bb%5D=');
  266. st.end();
  267. });
  268. t.test('stringifies a null object', { skip: !Object.create }, function (st) {
  269. var obj = Object.create(null);
  270. obj.a = 'b';
  271. st.equal(qs.stringify(obj), 'a=b');
  272. st.end();
  273. });
  274. t.test('returns an empty string for invalid input', function (st) {
  275. st.equal(qs.stringify(undefined), '');
  276. st.equal(qs.stringify(false), '');
  277. st.equal(qs.stringify(null), '');
  278. st.equal(qs.stringify(''), '');
  279. st.end();
  280. });
  281. t.test('stringifies an object with a null object as a child', { skip: !Object.create }, function (st) {
  282. var obj = { a: Object.create(null) };
  283. obj.a.b = 'c';
  284. st.equal(qs.stringify(obj), 'a%5Bb%5D=c');
  285. st.end();
  286. });
  287. t.test('drops keys with a value of undefined', function (st) {
  288. st.equal(qs.stringify({ a: undefined }), '');
  289. st.equal(qs.stringify({ a: { b: undefined, c: null } }, { strictNullHandling: true }), 'a%5Bc%5D');
  290. st.equal(qs.stringify({ a: { b: undefined, c: null } }, { strictNullHandling: false }), 'a%5Bc%5D=');
  291. st.equal(qs.stringify({ a: { b: undefined, c: '' } }), 'a%5Bc%5D=');
  292. st.end();
  293. });
  294. t.test('url encodes values', function (st) {
  295. st.equal(qs.stringify({ a: 'b c' }), 'a=b%20c');
  296. st.end();
  297. });
  298. t.test('stringifies a date', function (st) {
  299. var now = new Date();
  300. var str = 'a=' + encodeURIComponent(now.toISOString());
  301. st.equal(qs.stringify({ a: now }), str);
  302. st.end();
  303. });
  304. t.test('stringifies the weird object from qs', function (st) {
  305. 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');
  306. st.end();
  307. });
  308. t.test('skips properties that are part of the object prototype', function (st) {
  309. Object.prototype.crash = 'test';
  310. st.equal(qs.stringify({ a: 'b' }), 'a=b');
  311. st.equal(qs.stringify({ a: { b: 'c' } }), 'a%5Bb%5D=c');
  312. delete Object.prototype.crash;
  313. st.end();
  314. });
  315. t.test('stringifies boolean values', function (st) {
  316. st.equal(qs.stringify({ a: true }), 'a=true');
  317. st.equal(qs.stringify({ a: { b: true } }), 'a%5Bb%5D=true');
  318. st.equal(qs.stringify({ b: false }), 'b=false');
  319. st.equal(qs.stringify({ b: { c: false } }), 'b%5Bc%5D=false');
  320. st.end();
  321. });
  322. t.test('stringifies buffer values', function (st) {
  323. st.equal(qs.stringify({ a: SaferBuffer.from('test') }), 'a=test');
  324. st.equal(qs.stringify({ a: { b: SaferBuffer.from('test') } }), 'a%5Bb%5D=test');
  325. st.end();
  326. });
  327. t.test('stringifies an object using an alternative delimiter', function (st) {
  328. st.equal(qs.stringify({ a: 'b', c: 'd' }, { delimiter: ';' }), 'a=b;c=d');
  329. st.end();
  330. });
  331. t.test('doesn\'t blow up when Buffer global is missing', function (st) {
  332. var tempBuffer = global.Buffer;
  333. delete global.Buffer;
  334. var result = qs.stringify({ a: 'b', c: 'd' });
  335. global.Buffer = tempBuffer;
  336. st.equal(result, 'a=b&c=d');
  337. st.end();
  338. });
  339. t.test('selects properties when filter=array', function (st) {
  340. st.equal(qs.stringify({ a: 'b' }, { filter: ['a'] }), 'a=b');
  341. st.equal(qs.stringify({ a: 1 }, { filter: [] }), '');
  342. st.equal(
  343. qs.stringify(
  344. { a: { b: [1, 2, 3, 4], c: 'd' }, c: 'f' },
  345. { filter: ['a', 'b', 0, 2], arrayFormat: 'indices' }
  346. ),
  347. 'a%5Bb%5D%5B0%5D=1&a%5Bb%5D%5B2%5D=3',
  348. 'indices => indices'
  349. );
  350. st.equal(
  351. qs.stringify(
  352. { a: { b: [1, 2, 3, 4], c: 'd' }, c: 'f' },
  353. { filter: ['a', 'b', 0, 2], arrayFormat: 'brackets' }
  354. ),
  355. 'a%5Bb%5D%5B%5D=1&a%5Bb%5D%5B%5D=3',
  356. 'brackets => brackets'
  357. );
  358. st.equal(
  359. qs.stringify(
  360. { a: { b: [1, 2, 3, 4], c: 'd' }, c: 'f' },
  361. { filter: ['a', 'b', 0, 2] }
  362. ),
  363. 'a%5Bb%5D%5B0%5D=1&a%5Bb%5D%5B2%5D=3',
  364. 'default => indices'
  365. );
  366. st.end();
  367. });
  368. t.test('supports custom representations when filter=function', function (st) {
  369. var calls = 0;
  370. var obj = { a: 'b', c: 'd', e: { f: new Date(1257894000000) } };
  371. var filterFunc = function (prefix, value) {
  372. calls += 1;
  373. if (calls === 1) {
  374. st.equal(prefix, '', 'prefix is empty');
  375. st.equal(value, obj);
  376. } else if (prefix === 'c') {
  377. return void 0;
  378. } else if (value instanceof Date) {
  379. st.equal(prefix, 'e[f]');
  380. return value.getTime();
  381. }
  382. return value;
  383. };
  384. st.equal(qs.stringify(obj, { filter: filterFunc }), 'a=b&e%5Bf%5D=1257894000000');
  385. st.equal(calls, 5);
  386. st.end();
  387. });
  388. t.test('can disable uri encoding', function (st) {
  389. st.equal(qs.stringify({ a: 'b' }, { encode: false }), 'a=b');
  390. st.equal(qs.stringify({ a: { b: 'c' } }, { encode: false }), 'a[b]=c');
  391. st.equal(qs.stringify({ a: 'b', c: null }, { strictNullHandling: true, encode: false }), 'a=b&c');
  392. st.end();
  393. });
  394. t.test('can sort the keys', function (st) {
  395. var sort = function (a, b) {
  396. return a.localeCompare(b);
  397. };
  398. st.equal(qs.stringify({ a: 'c', z: 'y', b: 'f' }, { sort: sort }), 'a=c&b=f&z=y');
  399. 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');
  400. st.end();
  401. });
  402. t.test('can sort the keys at depth 3 or more too', function (st) {
  403. var sort = function (a, b) {
  404. return a.localeCompare(b);
  405. };
  406. st.equal(
  407. qs.stringify(
  408. { a: 'a', z: { zj: { zjb: 'zjb', zja: 'zja' }, zi: { zib: 'zib', zia: 'zia' } }, b: 'b' },
  409. { sort: sort, encode: false }
  410. ),
  411. 'a=a&b=b&z[zi][zia]=zia&z[zi][zib]=zib&z[zj][zja]=zja&z[zj][zjb]=zjb'
  412. );
  413. st.equal(
  414. qs.stringify(
  415. { a: 'a', z: { zj: { zjb: 'zjb', zja: 'zja' }, zi: { zib: 'zib', zia: 'zia' } }, b: 'b' },
  416. { sort: null, encode: false }
  417. ),
  418. 'a=a&z[zj][zjb]=zjb&z[zj][zja]=zja&z[zi][zib]=zib&z[zi][zia]=zia&b=b'
  419. );
  420. st.end();
  421. });
  422. t.test('can stringify with custom encoding', function (st) {
  423. st.equal(qs.stringify({ 県: '大阪府', '': '' }, {
  424. encoder: function (str) {
  425. if (str.length === 0) {
  426. return '';
  427. }
  428. var buf = iconv.encode(str, 'shiftjis');
  429. var result = [];
  430. for (var i = 0; i < buf.length; ++i) {
  431. result.push(buf.readUInt8(i).toString(16));
  432. }
  433. return '%' + result.join('%');
  434. }
  435. }), '%8c%a7=%91%e5%8d%e3%95%7b&=');
  436. st.end();
  437. });
  438. t.test('receives the default encoder as a second argument', function (st) {
  439. st.plan(2);
  440. qs.stringify({ a: 1 }, {
  441. encoder: function (str, defaultEncoder) {
  442. st.equal(defaultEncoder, utils.encode);
  443. }
  444. });
  445. st.end();
  446. });
  447. t.test('throws error with wrong encoder', function (st) {
  448. st['throws'](function () {
  449. qs.stringify({}, { encoder: 'string' });
  450. }, new TypeError('Encoder has to be a function.'));
  451. st.end();
  452. });
  453. t.test('can use custom encoder for a buffer object', { skip: typeof Buffer === 'undefined' }, function (st) {
  454. st.equal(qs.stringify({ a: SaferBuffer.from([1]) }, {
  455. encoder: function (buffer) {
  456. if (typeof buffer === 'string') {
  457. return buffer;
  458. }
  459. return String.fromCharCode(buffer.readUInt8(0) + 97);
  460. }
  461. }), 'a=b');
  462. st.end();
  463. });
  464. t.test('serializeDate option', function (st) {
  465. var date = new Date();
  466. st.equal(
  467. qs.stringify({ a: date }),
  468. 'a=' + date.toISOString().replace(/:/g, '%3A'),
  469. 'default is toISOString'
  470. );
  471. var mutatedDate = new Date();
  472. mutatedDate.toISOString = function () {
  473. throw new SyntaxError();
  474. };
  475. st['throws'](function () {
  476. mutatedDate.toISOString();
  477. }, SyntaxError);
  478. st.equal(
  479. qs.stringify({ a: mutatedDate }),
  480. 'a=' + Date.prototype.toISOString.call(mutatedDate).replace(/:/g, '%3A'),
  481. 'toISOString works even when method is not locally present'
  482. );
  483. var specificDate = new Date(6);
  484. st.equal(
  485. qs.stringify(
  486. { a: specificDate },
  487. { serializeDate: function (d) { return d.getTime() * 7; } }
  488. ),
  489. 'a=42',
  490. 'custom serializeDate function called'
  491. );
  492. st.end();
  493. });
  494. t.test('RFC 1738 spaces serialization', function (st) {
  495. st.equal(qs.stringify({ a: 'b c' }, { format: qs.formats.RFC1738 }), 'a=b+c');
  496. st.equal(qs.stringify({ 'a b': 'c d' }, { format: qs.formats.RFC1738 }), 'a+b=c+d');
  497. st.end();
  498. });
  499. t.test('RFC 3986 spaces serialization', function (st) {
  500. st.equal(qs.stringify({ a: 'b c' }, { format: qs.formats.RFC3986 }), 'a=b%20c');
  501. st.equal(qs.stringify({ 'a b': 'c d' }, { format: qs.formats.RFC3986 }), 'a%20b=c%20d');
  502. st.end();
  503. });
  504. t.test('Backward compatibility to RFC 3986', function (st) {
  505. st.equal(qs.stringify({ a: 'b c' }), 'a=b%20c');
  506. st.end();
  507. });
  508. t.test('Edge cases and unknown formats', function (st) {
  509. ['UFO1234', false, 1234, null, {}, []].forEach(
  510. function (format) {
  511. st['throws'](
  512. function () {
  513. qs.stringify({ a: 'b c' }, { format: format });
  514. },
  515. new TypeError('Unknown format option provided.')
  516. );
  517. }
  518. );
  519. st.end();
  520. });
  521. t.test('encodeValuesOnly', function (st) {
  522. st.equal(
  523. qs.stringify(
  524. { a: 'b', c: ['d', 'e=f'], f: [['g'], ['h']] },
  525. { encodeValuesOnly: true }
  526. ),
  527. 'a=b&c[0]=d&c[1]=e%3Df&f[0][0]=g&f[1][0]=h'
  528. );
  529. st.equal(
  530. qs.stringify(
  531. { a: 'b', c: ['d', 'e'], f: [['g'], ['h']] }
  532. ),
  533. 'a=b&c%5B0%5D=d&c%5B1%5D=e&f%5B0%5D%5B0%5D=g&f%5B1%5D%5B0%5D=h'
  534. );
  535. st.end();
  536. });
  537. t.test('encodeValuesOnly - strictNullHandling', function (st) {
  538. st.equal(
  539. qs.stringify(
  540. { a: { b: null } },
  541. { encodeValuesOnly: true, strictNullHandling: true }
  542. ),
  543. 'a[b]'
  544. );
  545. st.end();
  546. });
  547. t.test('throws if an invalid charset is specified', function (st) {
  548. st['throws'](function () {
  549. qs.stringify({ a: 'b' }, { charset: 'foobar' });
  550. }, new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined'));
  551. st.end();
  552. });
  553. t.test('respects a charset of iso-8859-1', function (st) {
  554. st.equal(qs.stringify({ æ: 'æ' }, { charset: 'iso-8859-1' }), '%E6=%E6');
  555. st.end();
  556. });
  557. t.test('encodes unrepresentable chars as numeric entities in iso-8859-1 mode', function (st) {
  558. st.equal(qs.stringify({ a: '☺' }, { charset: 'iso-8859-1' }), 'a=%26%239786%3B');
  559. st.end();
  560. });
  561. t.test('respects an explicit charset of utf-8 (the default)', function (st) {
  562. st.equal(qs.stringify({ a: 'æ' }, { charset: 'utf-8' }), 'a=%C3%A6');
  563. st.end();
  564. });
  565. t.test('adds the right sentinel when instructed to and the charset is utf-8', function (st) {
  566. st.equal(qs.stringify({ a: 'æ' }, { charsetSentinel: true, charset: 'utf-8' }), 'utf8=%E2%9C%93&a=%C3%A6');
  567. st.end();
  568. });
  569. t.test('adds the right sentinel when instructed to and the charset is iso-8859-1', function (st) {
  570. st.equal(qs.stringify({ a: 'æ' }, { charsetSentinel: true, charset: 'iso-8859-1' }), 'utf8=%26%2310003%3B&a=%E6');
  571. st.end();
  572. });
  573. t.test('does not mutate the options argument', function (st) {
  574. var options = {};
  575. qs.stringify({}, options);
  576. st.deepEqual(options, {});
  577. st.end();
  578. });
  579. t.test('strictNullHandling works with custom filter', function (st) {
  580. var filter = function (prefix, value) {
  581. return value;
  582. };
  583. var options = { strictNullHandling: true, filter: filter };
  584. st.equal(qs.stringify({ key: null }, options), 'key');
  585. st.end();
  586. });
  587. t.test('strictNullHandling works with null serializeDate', function (st) {
  588. var serializeDate = function () {
  589. return null;
  590. };
  591. var options = { strictNullHandling: true, serializeDate: serializeDate };
  592. var date = new Date();
  593. st.equal(qs.stringify({ key: date }, options), 'key');
  594. st.end();
  595. });
  596. t.end();
  597. });