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.

158 lines
3.6 KiB

  1. var assert = require('assert')
  2. , expect = require('expect.js')
  3. , Receiver = require('../lib/Receiver.hixie');
  4. require('./hybi-common');
  5. describe('Receiver', function() {
  6. it('can parse text message', function() {
  7. var p = new Receiver();
  8. var packet = '00 48 65 6c 6c 6f ff';
  9. var gotData = false;
  10. p.ontext = function(data) {
  11. gotData = true;
  12. assert.equal('Hello', data);
  13. };
  14. p.add(getBufferFromHexString(packet));
  15. expect(gotData).to.equal(true);
  16. });
  17. it('can parse multiple text messages', function() {
  18. var p = new Receiver();
  19. var packet = '00 48 65 6c 6c 6f ff 00 48 65 6c 6c 6f ff';
  20. var gotData = false;
  21. var messages = [];
  22. p.ontext = function(data) {
  23. gotData = true;
  24. messages.push(data);
  25. };
  26. p.add(getBufferFromHexString(packet));
  27. expect(gotData).to.equal(true);
  28. for (var i = 0; i < 2; ++i) {
  29. expect(messages[i]).to.equal('Hello');
  30. }
  31. });
  32. it('can parse empty message', function() {
  33. var p = new Receiver();
  34. var packet = '00 ff';
  35. var gotData = false;
  36. p.ontext = function(data) {
  37. gotData = true;
  38. assert.equal('', data);
  39. };
  40. p.add(getBufferFromHexString(packet));
  41. expect(gotData).to.equal(true);
  42. });
  43. it('can parse text messages delivered over multiple frames', function() {
  44. var p = new Receiver();
  45. var packets = [
  46. '00 48',
  47. '65 6c 6c',
  48. '6f ff 00 48',
  49. '65',
  50. '6c 6c 6f',
  51. 'ff'
  52. ];
  53. var gotData = false;
  54. var messages = [];
  55. p.ontext = function(data) {
  56. gotData = true;
  57. messages.push(data);
  58. };
  59. for (var i = 0; i < packets.length; ++i) {
  60. p.add(getBufferFromHexString(packets[i]));
  61. }
  62. expect(gotData).to.equal(true);
  63. for (var i = 0; i < 2; ++i) {
  64. expect(messages[i]).to.equal('Hello');
  65. }
  66. });
  67. it('emits an error if a payload doesnt start with 0x00', function() {
  68. var p = new Receiver();
  69. var packets = [
  70. '00 6c ff',
  71. '00 6c ff ff',
  72. 'ff 00 6c ff 00 6c ff',
  73. '00',
  74. '6c 6c 6f',
  75. 'ff'
  76. ];
  77. var gotData = false;
  78. var gotError = false;
  79. var messages = [];
  80. p.ontext = function(data) {
  81. gotData = true;
  82. messages.push(data);
  83. };
  84. p.onerror = function(reason, code) {
  85. gotError = code == true;
  86. };
  87. for (var i = 0; i < packets.length && !gotError; ++i) {
  88. p.add(getBufferFromHexString(packets[i]));
  89. }
  90. expect(gotError).to.equal(true);
  91. expect(messages[0]).to.equal('l');
  92. expect(messages[1]).to.equal('l');
  93. expect(messages.length).to.equal(2);
  94. });
  95. it('can parse close messages', function() {
  96. var p = new Receiver();
  97. var packets = [
  98. 'ff 00'
  99. ];
  100. var gotClose = false;
  101. var gotError = false;
  102. p.onclose = function() {
  103. gotClose = true;
  104. };
  105. p.onerror = function(reason, code) {
  106. gotError = code == true;
  107. };
  108. for (var i = 0; i < packets.length && !gotError; ++i) {
  109. p.add(getBufferFromHexString(packets[i]));
  110. }
  111. expect(gotClose).to.equal(true);
  112. expect(gotError).to.equal(false);
  113. });
  114. it('can parse binary messages delivered over multiple frames', function() {
  115. var p = new Receiver();
  116. var packets = [
  117. '80 05 48',
  118. '65 6c 6c',
  119. '6f 80 80 05 48',
  120. '65',
  121. '6c 6c 6f'
  122. ];
  123. var gotData = false;
  124. var messages = [];
  125. p.ontext = function(data) {
  126. gotData = true;
  127. messages.push(data);
  128. };
  129. for (var i = 0; i < packets.length; ++i) {
  130. p.add(getBufferFromHexString(packets[i]));
  131. }
  132. expect(gotData).to.equal(true);
  133. for (var i = 0; i < 2; ++i) {
  134. expect(messages[i]).to.equal('Hello');
  135. }
  136. });
  137. });