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.

179 lines
5.2 KiB

7 years ago
  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. done();
  19. });
  20. it('stringifies a nested object', function (done) {
  21. expect(Qs.stringify({ a: { b: 'c' } })).to.equal('a%5Bb%5D=c');
  22. expect(Qs.stringify({ a: { b: { c: { d: 'e' } } } })).to.equal('a%5Bb%5D%5Bc%5D%5Bd%5D=e');
  23. done();
  24. });
  25. it('stringifies an array value', function (done) {
  26. expect(Qs.stringify({ a: ['b', 'c', 'd'] })).to.equal('a%5B0%5D=b&a%5B1%5D=c&a%5B2%5D=d');
  27. done();
  28. });
  29. it('omits array indices when asked', function (done) {
  30. expect(Qs.stringify({ a: ['b', 'c', 'd'] }, { indices: false })).to.equal('a=b&a=c&a=d');
  31. done();
  32. });
  33. it('stringifies a nested array value', function (done) {
  34. expect(Qs.stringify({ a: { b: ['c', 'd'] } })).to.equal('a%5Bb%5D%5B0%5D=c&a%5Bb%5D%5B1%5D=d');
  35. done();
  36. });
  37. it('stringifies an object inside an array', function (done) {
  38. expect(Qs.stringify({ a: [{ b: 'c' }] })).to.equal('a%5B0%5D%5Bb%5D=c');
  39. expect(Qs.stringify({ a: [{ b: { c: [1] } }] })).to.equal('a%5B0%5D%5Bb%5D%5Bc%5D%5B0%5D=1');
  40. done();
  41. });
  42. it('does not omit object keys when indices = false', function (done) {
  43. expect(Qs.stringify({ a: [{ b: 'c' }] }, { indices: false })).to.equal('a%5Bb%5D=c');
  44. done();
  45. });
  46. it('stringifies a complicated object', function (done) {
  47. expect(Qs.stringify({ a: { b: 'c', d: 'e' } })).to.equal('a%5Bb%5D=c&a%5Bd%5D=e');
  48. done();
  49. });
  50. it('stringifies an empty value', function (done) {
  51. expect(Qs.stringify({ a: '' })).to.equal('a=');
  52. expect(Qs.stringify({ a: '', b: '' })).to.equal('a=&b=');
  53. expect(Qs.stringify({ a: null })).to.equal('a=');
  54. expect(Qs.stringify({ a: { b: null } })).to.equal('a%5Bb%5D=');
  55. done();
  56. });
  57. it('stringifies an empty object', function (done) {
  58. var obj = Object.create(null);
  59. obj.a = 'b';
  60. expect(Qs.stringify(obj)).to.equal('a=b');
  61. done();
  62. });
  63. it('returns an empty string for invalid input', function (done) {
  64. expect(Qs.stringify(undefined)).to.equal('');
  65. expect(Qs.stringify(false)).to.equal('');
  66. expect(Qs.stringify(null)).to.equal('');
  67. expect(Qs.stringify('')).to.equal('');
  68. done();
  69. });
  70. it('stringifies an object with an empty object as a child', function (done) {
  71. var obj = {
  72. a: Object.create(null)
  73. };
  74. obj.a.b = 'c';
  75. expect(Qs.stringify(obj)).to.equal('a%5Bb%5D=c');
  76. done();
  77. });
  78. it('drops keys with a value of undefined', function (done) {
  79. expect(Qs.stringify({ a: undefined })).to.equal('');
  80. expect(Qs.stringify({ a: { b: undefined, c: null } })).to.equal('a%5Bc%5D=');
  81. done();
  82. });
  83. it('url encodes values', function (done) {
  84. expect(Qs.stringify({ a: 'b c' })).to.equal('a=b%20c');
  85. done();
  86. });
  87. it('stringifies a date', function (done) {
  88. var now = new Date();
  89. var str = 'a=' + encodeURIComponent(now.toISOString());
  90. expect(Qs.stringify({ a: now })).to.equal(str);
  91. done();
  92. });
  93. it('stringifies the weird object from qs', function (done) {
  94. expect(Qs.stringify({ 'my weird field': 'q1!2"\'w$5&7/z8)?' })).to.equal('my%20weird%20field=q1!2%22\'w%245%267%2Fz8)%3F');
  95. done();
  96. });
  97. it('skips properties that are part of the object prototype', function (done) {
  98. Object.prototype.crash = 'test';
  99. expect(Qs.stringify({ a: 'b'})).to.equal('a=b');
  100. expect(Qs.stringify({ a: { b: 'c' } })).to.equal('a%5Bb%5D=c');
  101. delete Object.prototype.crash;
  102. done();
  103. });
  104. it('stringifies boolean values', function (done) {
  105. expect(Qs.stringify({ a: true })).to.equal('a=true');
  106. expect(Qs.stringify({ a: { b: true } })).to.equal('a%5Bb%5D=true');
  107. expect(Qs.stringify({ b: false })).to.equal('b=false');
  108. expect(Qs.stringify({ b: { c: false } })).to.equal('b%5Bc%5D=false');
  109. done();
  110. });
  111. it('stringifies buffer values', function (done) {
  112. expect(Qs.stringify({ a: new Buffer('test') })).to.equal('a=test');
  113. expect(Qs.stringify({ a: { b: new Buffer('test') } })).to.equal('a%5Bb%5D=test');
  114. done();
  115. });
  116. it('stringifies an object using an alternative delimiter', function (done) {
  117. expect(Qs.stringify({ a: 'b', c: 'd' }, { delimiter: ';' })).to.equal('a=b;c=d');
  118. done();
  119. });
  120. it('doesn\'t blow up when Buffer global is missing', function (done) {
  121. var tempBuffer = global.Buffer;
  122. delete global.Buffer;
  123. expect(Qs.stringify({ a: 'b', c: 'd' })).to.equal('a=b&c=d');
  124. global.Buffer = tempBuffer;
  125. done();
  126. });
  127. });