server.js 4.8 KB

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