server.js 6.4 KB

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