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.

134 lines
3.9 KiB

  1. var assert = require('assert')
  2. , Sender = require('../lib/Sender.hixie');
  3. require('should');
  4. require('./hybi-common');
  5. describe('Sender', function() {
  6. describe('#send', function() {
  7. it('frames and sends a text message', function(done) {
  8. var message = 'Hello world';
  9. var received;
  10. var socket = {
  11. write: function(data, encoding, cb) {
  12. received = data;
  13. process.nextTick(cb);
  14. }
  15. };
  16. var sender = new Sender(socket, {});
  17. sender.send(message, {}, function() {
  18. received.toString('utf8').should.eql('\u0000' + message + '\ufffd');
  19. done();
  20. });
  21. });
  22. it('frames and sends an empty message', function(done) {
  23. var socket = {
  24. write: function(data, encoding, cb) {
  25. done();
  26. }
  27. };
  28. var sender = new Sender(socket, {});
  29. sender.send('', {}, function() {});
  30. });
  31. it('frames and sends a buffer', function(done) {
  32. var received;
  33. var socket = {
  34. write: function(data, encoding, cb) {
  35. received = data;
  36. process.nextTick(cb);
  37. }
  38. };
  39. var sender = new Sender(socket, {});
  40. sender.send(new Buffer('foobar'), {}, function() {
  41. received.toString('utf8').should.eql('\u0000foobar\ufffd');
  42. done();
  43. });
  44. });
  45. it('frames and sends a binary message', function(done) {
  46. var message = 'Hello world';
  47. var received;
  48. var socket = {
  49. write: function(data, encoding, cb) {
  50. received = data;
  51. process.nextTick(cb);
  52. }
  53. };
  54. var sender = new Sender(socket, {});
  55. sender.send(message, {binary: true}, function() {
  56. received.toString('hex').should.eql(
  57. // 0x80 0x0b H e l l o <sp> w o r l d
  58. '800b48656c6c6f20776f726c64');
  59. done();
  60. });
  61. });
  62. /*
  63. it('throws an exception for binary data', function(done) {
  64. var socket = {
  65. write: function(data, encoding, cb) {
  66. process.nextTick(cb);
  67. }
  68. };
  69. var sender = new Sender(socket, {});
  70. sender.on('error', function() {
  71. done();
  72. });
  73. sender.send(new Buffer(100), {binary: true}, function() {});
  74. });
  75. */
  76. it('can fauxe stream data', function(done) {
  77. var received = [];
  78. var socket = {
  79. write: function(data, encoding, cb) {
  80. received.push(data);
  81. process.nextTick(cb);
  82. }
  83. };
  84. var sender = new Sender(socket, {});
  85. sender.send(new Buffer('foobar'), { fin: false }, function() {});
  86. sender.send('bazbar', { fin: false }, function() {});
  87. sender.send(new Buffer('end'), { fin: true }, function() {
  88. received[0].toString('utf8').should.eql('\u0000foobar');
  89. received[1].toString('utf8').should.eql('bazbar');
  90. received[2].toString('utf8').should.eql('end\ufffd');
  91. done();
  92. });
  93. });
  94. });
  95. describe('#close', function() {
  96. it('sends a hixie close frame', function(done) {
  97. var received;
  98. var socket = {
  99. write: function(data, encoding, cb) {
  100. received = data;
  101. process.nextTick(cb);
  102. }
  103. };
  104. var sender = new Sender(socket, {});
  105. sender.close(null, null, null, function() {
  106. received.toString('utf8').should.eql('\ufffd\u0000');
  107. done();
  108. });
  109. });
  110. it('sends a message end marker if fauxe streaming has started, before hixie close frame', function(done) {
  111. var received = [];
  112. var socket = {
  113. write: function(data, encoding, cb) {
  114. received.push(data);
  115. if (cb) process.nextTick(cb);
  116. }
  117. };
  118. var sender = new Sender(socket, {});
  119. sender.send(new Buffer('foobar'), { fin: false }, function() {});
  120. sender.close(null, null, null, function() {
  121. received[0].toString('utf8').should.eql('\u0000foobar');
  122. received[1].toString('utf8').should.eql('\ufffd');
  123. received[2].toString('utf8').should.eql('\ufffd\u0000');
  124. done();
  125. });
  126. });
  127. });
  128. });