querystring_spec.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. var should = require('should'),
  2. stringify = require('../lib/querystring').build;
  3. describe('stringify', function() {
  4. describe('with null', function() {
  5. it('throws', function() {
  6. (function() {
  7. var res = stringify(null);
  8. }).should.throw();
  9. })
  10. })
  11. describe('with a number', function() {
  12. it('throws', function() {
  13. (function() {
  14. var res = stringify(100);
  15. }).should.throw();
  16. })
  17. })
  18. describe('with a string', function() {
  19. describe('that is empty', function() {
  20. it('throws', function() {
  21. (function() {
  22. var res = stringify('');
  23. }).should.throw();
  24. })
  25. })
  26. describe('that doesnt contain an equal sign', function() {
  27. it('throws', function() {
  28. (function() {
  29. var res = stringify('boomshagalaga');
  30. }).should.throw();
  31. })
  32. })
  33. describe('that contains an equal sign', function() {
  34. it('works', function() {
  35. var res = stringify('hello=123');
  36. res.should.eql('hello=123');
  37. })
  38. })
  39. })
  40. describe('with an array', function() {
  41. describe('with key val objects', function() {
  42. it('works', function() {
  43. var res = stringify([ {foo: 'bar'} ]);
  44. res.should.eql('foo=bar');
  45. })
  46. })
  47. describe('where all elements are strings with an equal sign', function() {
  48. it('works', function() {
  49. var res = stringify([ 'bar=123', 'quux=' ]);
  50. res.should.eql('bar=123&quux=');
  51. })
  52. })
  53. describe('with random words', function() {
  54. it('throws', function() {
  55. (function() {
  56. var res = stringify(['hello', 'there']);
  57. }).should.throw();
  58. })
  59. })
  60. describe('with integers', function() {
  61. it('throws', function() {
  62. (function() {
  63. var res = stringify([123, 432]);
  64. }).should.throw();
  65. })
  66. })
  67. })
  68. describe('with an object', function() {
  69. it('works', function() {
  70. var res = stringify({ test: 100 });
  71. res.should.eql('test=100');
  72. })
  73. describe('with object where val is an array', function() {
  74. it('works', function() {
  75. var res = stringify({ foo: ['bar', 'baz'] });
  76. res.should.eql('foo[]=bar&foo[]=baz');
  77. })
  78. })
  79. describe('with object where val is an array of key val objects', function() {
  80. it('works', function() {
  81. var res = stringify({ foo: [{'1': 'bar'}, {'2': 'baz'}] });
  82. res.should.eql('foo[][1]=bar&foo[][2]=baz');
  83. })
  84. })
  85. })
  86. })