server.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. var PeerServer = require('../').PeerServer;
  2. var expect = require('expect.js');
  3. var sinon = require('sinon');
  4. describe('PeerServer', function() {
  5. describe('constructor', function() {
  6. before(function() {
  7. PeerServer.prototype._initializeWSS = sinon.stub();
  8. PeerServer.prototype._initializeHTTP = sinon.stub();
  9. });
  10. it('should be able to be created without the `new` keyword', function() {
  11. var p = PeerServer();
  12. expect(p.constructor).to.be(PeerServer);
  13. });
  14. it('should default to port 80, key `peerjs`', function() {
  15. var p = new PeerServer();
  16. expect(p._options.key).to.be('peerjs');
  17. expect(p._options.port).to.be(80);
  18. });
  19. it('should accept a custom port', function() {
  20. var p = new PeerServer({ port: 8000 });
  21. expect(p._options.port).to.be(8000);
  22. });
  23. });
  24. describe('#_initializeWSS', function() {
  25. WebSocketServer = sinon.stub();
  26. });
  27. describe('#_configureWS', function() {
  28. });
  29. describe('#_checkKey', function() {
  30. var p;
  31. before(function() {
  32. PeerServer.prototype._initializeHTTP = sinon.stub();
  33. p = new PeerServer({ port: 8000 });
  34. p._checkKey('peerjs', 'myip', function() {});
  35. });
  36. it('should reject keys that are not the default', function(done) {
  37. p._checkKey('bad key', null, function(response) {
  38. expect(response).to.be('Invalid key provided');
  39. done();
  40. });
  41. });
  42. it('should accept valid key/ip pairs', function(done) {
  43. p._checkKey('peerjs', 'myip', function(response) {
  44. expect(response).to.be(null);
  45. done();
  46. });
  47. });
  48. it('should reject ips that are at their limit', function(done) {
  49. p._options.ip_limit = 0;
  50. p._checkKey('peerjs', 'myip', function(response) {
  51. expect(response).to.be('myip has reached its concurrent user limit');
  52. done();
  53. });
  54. });
  55. it('should reject when the server is at its limit', function(done) {
  56. p._options.concurrent_limit = 0;
  57. p._checkKey('peerjs', 'myip', function(response) {
  58. expect(response).to.be('Server has reached its concurrent user limit');
  59. done();
  60. });
  61. });
  62. });
  63. describe('#_initializeHTTP', function() {
  64. });
  65. describe('#_startStreaming', function() {
  66. });
  67. describe('#_pruneOutstanding', function() {
  68. });
  69. describe('#_processOutstanding', function() {
  70. });
  71. describe('#_removePeer', function() {
  72. var p;
  73. before(function() {
  74. PeerServer.prototype._initializeHTTP = sinon.stub();
  75. p = new PeerServer({ port: 8000 });
  76. var fake = {ip: '0.0.0.0'};
  77. p._ips[fake.ip] = 1;
  78. p._clients['peerjs'] = {};
  79. p._clients['peerjs']['test'] = fake;
  80. });
  81. it('should decrement the number of ips being used and remove the connection', function() {
  82. expect(p._ips['0.0.0.0']).to.be(1);
  83. p._removePeer('peerjs', 'test');
  84. expect(p._ips['0.0.0.0']).to.be(0);
  85. expect(p._clients['peerjs']['test']).to.be(undefined);
  86. });
  87. });
  88. describe('#_handleTransmission', function() {
  89. var p;
  90. var KEY = 'peerjs';
  91. var ID = 'test';
  92. before(function() {
  93. PeerServer.prototype._initializeHTTP = sinon.stub();
  94. p = new PeerServer({ port: 8000 });
  95. p._clients[KEY] = {};
  96. });
  97. it('should send to the socket when appropriate', function() {
  98. var send = sinon.spy();
  99. var write = sinon.spy();
  100. var message = {dst: ID};
  101. p._clients[KEY][ID] = {
  102. socket: {
  103. send: send
  104. },
  105. res: {
  106. write: write
  107. }
  108. }
  109. p._handleTransmission(KEY, message);
  110. expect(send.calledWith(JSON.stringify(message))).to.be(true);
  111. expect(write.calledWith(JSON.stringify(message))).to.be(false);
  112. });
  113. it('should write to the response with a newline when appropriate', function() {
  114. var write = sinon.spy();
  115. var message = {dst: ID};
  116. p._clients[KEY][ID] = {
  117. res: {
  118. write: write
  119. }
  120. }
  121. p._handleTransmission(KEY, message);
  122. expect(write.calledWith(JSON.stringify(message) + '\n')).to.be(true);
  123. });
  124. // no destination.
  125. it('should push to outstanding messages if the destination is not found', function() {
  126. var message = {dst: ID};
  127. p._outstanding[KEY] = {};
  128. p._clients[KEY] = {};
  129. p._handleTransmission(KEY, message);
  130. expect(p._outstanding[KEY][ID][0]).to.be(message);
  131. });
  132. it('should not push to outstanding messages if the message is a LEAVE or EXPIRE', function() {
  133. var message = {dst: ID, type: 'LEAVE'};
  134. p._outstanding[KEY] = {};
  135. p._clients[KEY] = {};
  136. p._handleTransmission(KEY, message);
  137. expect(p._outstanding[KEY][ID]).to.be(undefined);
  138. message = {dst: ID, type: 'EXPIRE'};
  139. p._handleTransmission(KEY, message);
  140. expect(p._outstanding[KEY][ID]).to.be(undefined);
  141. });
  142. it('should remove the peer if there is no dst in the message', function() {
  143. var message = {type: 'LEAVE'};
  144. p._removePeer = sinon.spy();
  145. p._outstanding[KEY] = {};
  146. p._handleTransmission(KEY, message);
  147. expect(p._removePeer.calledWith(KEY, undefined)).to.be(true);
  148. });
  149. it('should remove the peer and send a LEAVE message if the socket appears to be closed', function() {
  150. var send = sinon.stub().throws();
  151. var message = {dst: ID};
  152. var leaveMessage = {type: 'LEAVE', dst: undefined, src: ID};
  153. var oldHandleTransmission = p._handleTransmission;
  154. p._removePeer = function() {
  155. // Hacks!
  156. p._handleTransmission = sinon.spy();
  157. };
  158. p._clients[KEY][ID] = {
  159. socket: {
  160. send: send
  161. }
  162. }
  163. p._handleTransmission(KEY, message);
  164. expect(p._handleTransmission.calledWith(KEY, leaveMessage)).to.be(true);
  165. });
  166. });
  167. describe('#_generateClientId', function() {
  168. var p;
  169. before(function() {
  170. PeerServer.prototype._initializeHTTP = sinon.stub();
  171. p = new PeerServer({ port: 8000 });
  172. });
  173. it('should generate a 16-character ID', function() {
  174. expect(p._generateClientId('anykey').length).to.be(16);
  175. });
  176. });
  177. });