/node_modules/grunt-contrib-watch/node_modules/tiny-lr/node_modules/qs/test/stringify.js

https://gitlab.com/RedDedVolvo/solidgoald · JavaScript · 281 lines · 216 code · 61 blank · 4 comment · 6 complexity · 5ac33f7ae443fb8c14998fd9ebe72534 MD5 · raw file

  1. /* eslint no-extend-native:0 */
  2. // Load modules
  3. var Code = require('code');
  4. var Lab = require('lab');
  5. var Qs = require('../');
  6. // Declare internals
  7. var internals = {};
  8. // Test shortcuts
  9. var lab = exports.lab = Lab.script();
  10. var expect = Code.expect;
  11. var describe = lab.experiment;
  12. var it = lab.test;
  13. describe('stringify()', function () {
  14. it('stringifies a querystring object', function (done) {
  15. expect(Qs.stringify({ a: 'b' })).to.equal('a=b');
  16. expect(Qs.stringify({ a: 1 })).to.equal('a=1');
  17. expect(Qs.stringify({ a: 1, b: 2 })).to.equal('a=1&b=2');
  18. expect(Qs.stringify({ a: 'A_Z' })).to.equal('a=A_Z');
  19. expect(Qs.stringify({ a: '€' })).to.equal('a=%E2%82%AC');
  20. expect(Qs.stringify({ a: '' })).to.equal('a=%EE%80%80');
  21. expect(Qs.stringify({ a: 'א' })).to.equal('a=%D7%90');
  22. expect(Qs.stringify({ a: '𐐷' })).to.equal('a=%F0%90%90%B7');
  23. done();
  24. });
  25. it('stringifies a nested object', function (done) {
  26. expect(Qs.stringify({ a: { b: 'c' } })).to.equal('a%5Bb%5D=c');
  27. expect(Qs.stringify({ a: { b: { c: { d: 'e' } } } })).to.equal('a%5Bb%5D%5Bc%5D%5Bd%5D=e');
  28. done();
  29. });
  30. it('stringifies an array value', function (done) {
  31. expect(Qs.stringify({ a: ['b', 'c', 'd'] })).to.equal('a%5B0%5D=b&a%5B1%5D=c&a%5B2%5D=d');
  32. done();
  33. });
  34. it('omits nulls when asked', function (done) {
  35. expect(Qs.stringify({ a: 'b', c: null }, { skipNulls: true })).to.equal('a=b');
  36. done();
  37. });
  38. it('omits nested nulls when asked', function (done) {
  39. expect(Qs.stringify({ a: { b: 'c', d: null } }, { skipNulls: true })).to.equal('a%5Bb%5D=c');
  40. done();
  41. });
  42. it('omits array indices when asked', function (done) {
  43. expect(Qs.stringify({ a: ['b', 'c', 'd'] }, { indices: false })).to.equal('a=b&a=c&a=d');
  44. done();
  45. });
  46. it('stringifies a nested array value', function (done) {
  47. expect(Qs.stringify({ a: { b: ['c', 'd'] } })).to.equal('a%5Bb%5D%5B0%5D=c&a%5Bb%5D%5B1%5D=d');
  48. done();
  49. });
  50. it('stringifies an object inside an array', function (done) {
  51. expect(Qs.stringify({ a: [{ b: 'c' }] })).to.equal('a%5B0%5D%5Bb%5D=c');
  52. expect(Qs.stringify({ a: [{ b: { c: [1] } }] })).to.equal('a%5B0%5D%5Bb%5D%5Bc%5D%5B0%5D=1');
  53. done();
  54. });
  55. it('does not omit object keys when indices = false', function (done) {
  56. expect(Qs.stringify({ a: [{ b: 'c' }] }, { indices: false })).to.equal('a%5Bb%5D=c');
  57. done();
  58. });
  59. it('uses indices notation for arrays when indices=true', function (done) {
  60. expect(Qs.stringify({ a: ['b', 'c'] }, { indices: true })).to.equal('a%5B0%5D=b&a%5B1%5D=c');
  61. done();
  62. });
  63. it('uses indices notation for arrays when no arrayFormat is specified', function (done) {
  64. expect(Qs.stringify({ a: ['b', 'c'] })).to.equal('a%5B0%5D=b&a%5B1%5D=c');
  65. done();
  66. });
  67. it('uses indices notation for arrays when no arrayFormat=indices', function (done) {
  68. expect(Qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'indices' })).to.equal('a%5B0%5D=b&a%5B1%5D=c');
  69. done();
  70. });
  71. it('uses repeat notation for arrays when no arrayFormat=repeat', function (done) {
  72. expect(Qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'repeat' })).to.equal('a=b&a=c');
  73. done();
  74. });
  75. it('uses brackets notation for arrays when no arrayFormat=brackets', function (done) {
  76. expect(Qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'brackets' })).to.equal('a%5B%5D=b&a%5B%5D=c');
  77. done();
  78. });
  79. it('stringifies a complicated object', function (done) {
  80. expect(Qs.stringify({ a: { b: 'c', d: 'e' } })).to.equal('a%5Bb%5D=c&a%5Bd%5D=e');
  81. done();
  82. });
  83. it('stringifies an empty value', function (done) {
  84. expect(Qs.stringify({ a: '' })).to.equal('a=');
  85. expect(Qs.stringify({ a: null }, { strictNullHandling: true })).to.equal('a');
  86. expect(Qs.stringify({ a: '', b: '' })).to.equal('a=&b=');
  87. expect(Qs.stringify({ a: null, b: '' }, { strictNullHandling: true })).to.equal('a&b=');
  88. expect(Qs.stringify({ a: { b: '' } })).to.equal('a%5Bb%5D=');
  89. expect(Qs.stringify({ a: { b: null } }, { strictNullHandling: true })).to.equal('a%5Bb%5D');
  90. expect(Qs.stringify({ a: { b: null } }, { strictNullHandling: false })).to.equal('a%5Bb%5D=');
  91. done();
  92. });
  93. it('stringifies an empty object', function (done) {
  94. var obj = Object.create(null);
  95. obj.a = 'b';
  96. expect(Qs.stringify(obj)).to.equal('a=b');
  97. done();
  98. });
  99. it('returns an empty string for invalid input', function (done) {
  100. expect(Qs.stringify(undefined)).to.equal('');
  101. expect(Qs.stringify(false)).to.equal('');
  102. expect(Qs.stringify(null)).to.equal('');
  103. expect(Qs.stringify('')).to.equal('');
  104. done();
  105. });
  106. it('stringifies an object with an empty object as a child', function (done) {
  107. var obj = {
  108. a: Object.create(null)
  109. };
  110. obj.a.b = 'c';
  111. expect(Qs.stringify(obj)).to.equal('a%5Bb%5D=c');
  112. done();
  113. });
  114. it('drops keys with a value of undefined', function (done) {
  115. expect(Qs.stringify({ a: undefined })).to.equal('');
  116. expect(Qs.stringify({ a: { b: undefined, c: null } }, { strictNullHandling: true })).to.equal('a%5Bc%5D');
  117. expect(Qs.stringify({ a: { b: undefined, c: null } }, { strictNullHandling: false })).to.equal('a%5Bc%5D=');
  118. expect(Qs.stringify({ a: { b: undefined, c: '' } })).to.equal('a%5Bc%5D=');
  119. done();
  120. });
  121. it('url encodes values', function (done) {
  122. expect(Qs.stringify({ a: 'b c' })).to.equal('a=b%20c');
  123. done();
  124. });
  125. it('stringifies a date', function (done) {
  126. var now = new Date();
  127. var str = 'a=' + encodeURIComponent(now.toISOString());
  128. expect(Qs.stringify({ a: now })).to.equal(str);
  129. done();
  130. });
  131. it('stringifies the weird object from qs', function (done) {
  132. expect(Qs.stringify({ 'my weird field': '~q1!2"\'w$5&7/z8)?' })).to.equal('my%20weird%20field=~q1%212%22%27w%245%267%2Fz8%29%3F');
  133. done();
  134. });
  135. it('skips properties that are part of the object prototype', function (done) {
  136. Object.prototype.crash = 'test';
  137. expect(Qs.stringify({ a: 'b' })).to.equal('a=b');
  138. expect(Qs.stringify({ a: { b: 'c' } })).to.equal('a%5Bb%5D=c');
  139. delete Object.prototype.crash;
  140. done();
  141. });
  142. it('stringifies boolean values', function (done) {
  143. expect(Qs.stringify({ a: true })).to.equal('a=true');
  144. expect(Qs.stringify({ a: { b: true } })).to.equal('a%5Bb%5D=true');
  145. expect(Qs.stringify({ b: false })).to.equal('b=false');
  146. expect(Qs.stringify({ b: { c: false } })).to.equal('b%5Bc%5D=false');
  147. done();
  148. });
  149. it('stringifies buffer values', function (done) {
  150. expect(Qs.stringify({ a: new Buffer('test') })).to.equal('a=test');
  151. expect(Qs.stringify({ a: { b: new Buffer('test') } })).to.equal('a%5Bb%5D=test');
  152. done();
  153. });
  154. it('stringifies an object using an alternative delimiter', function (done) {
  155. expect(Qs.stringify({ a: 'b', c: 'd' }, { delimiter: ';' })).to.equal('a=b;c=d');
  156. done();
  157. });
  158. it('doesn\'t blow up when Buffer global is missing', function (done) {
  159. var tempBuffer = global.Buffer;
  160. delete global.Buffer;
  161. var result = Qs.stringify({ a: 'b', c: 'd' });
  162. global.Buffer = tempBuffer;
  163. expect(result).to.equal('a=b&c=d');
  164. done();
  165. });
  166. it('selects properties when filter=array', function (done) {
  167. expect(Qs.stringify({ a: 'b' }, { filter: ['a'] })).to.equal('a=b');
  168. expect(Qs.stringify({ a: 1 }, { filter: [] })).to.equal('');
  169. expect(Qs.stringify({ a: { b: [1, 2, 3, 4], c: 'd' }, c: 'f' }, { filter: ['a', 'b', 0, 2] })).to.equal('a%5Bb%5D%5B0%5D=1&a%5Bb%5D%5B2%5D=3');
  170. done();
  171. });
  172. it('supports custom representations when filter=function', function (done) {
  173. var calls = 0;
  174. var obj = { a: 'b', c: 'd', e: { f: new Date(1257894000000) } };
  175. var filterFunc = function (prefix, value) {
  176. calls++;
  177. if (calls === 1) {
  178. expect(prefix).to.be.empty();
  179. expect(value).to.equal(obj);
  180. }
  181. else if (prefix === 'c') {
  182. return;
  183. }
  184. else if (value instanceof Date) {
  185. expect(prefix).to.equal('e[f]');
  186. return value.getTime();
  187. }
  188. return value;
  189. };
  190. expect(Qs.stringify(obj, { filter: filterFunc })).to.equal('a=b&e%5Bf%5D=1257894000000');
  191. expect(calls).to.equal(5);
  192. done();
  193. });
  194. it('can disable uri encoding', function (done) {
  195. expect(Qs.stringify({ a: 'b' }, { encode: false })).to.equal('a=b');
  196. expect(Qs.stringify({ a: { b: 'c' } }, { encode: false })).to.equal('a[b]=c');
  197. expect(Qs.stringify({ a: 'b', c: null }, { strictNullHandling: true, encode: false })).to.equal('a=b&c');
  198. done();
  199. });
  200. });