|
| 1 | +var assert = require('assert'), |
| 2 | + sinon = require('sinon'), |
| 3 | + rewire = require('rewire'), |
| 4 | + events = require('events'); |
| 5 | + |
| 6 | +var DDPClient = rewire("../lib/ddp-client"); |
| 7 | + |
| 8 | +var wsConstructor, wsMock; |
| 9 | + |
| 10 | +function prepareMocks() { |
| 11 | + wsMock = new events.EventEmitter(); |
| 12 | + |
| 13 | + wsConstructor = sinon.stub(); |
| 14 | + wsConstructor.returns(wsMock); |
| 15 | + DDPClient.__set__('WebSocket', wsConstructor); |
| 16 | +} |
| 17 | + |
| 18 | + |
| 19 | +describe("Connect to remote server", function() { |
| 20 | + beforeEach(function() { |
| 21 | + prepareMocks(); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should connect to localhost by default', function() { |
| 25 | + new DDPClient().connect(); |
| 26 | + |
| 27 | + assert(wsConstructor.calledOnce); |
| 28 | + assert(wsConstructor.calledWithNew()); |
| 29 | + assert(wsConstructor.call) |
| 30 | + assert.deepEqual(wsConstructor.args, [['ws://localhost:3000/websocket']]); |
| 31 | + }); |
| 32 | + it('should connect to the provided host', function() { |
| 33 | + new DDPClient({'host': 'myserver.com'}).connect(); |
| 34 | + assert.deepEqual(wsConstructor.args, [['ws://myserver.com:3000/websocket']]); |
| 35 | + }); |
| 36 | + it('should connect to the provided host and port', function() { |
| 37 | + new DDPClient({'host': 'myserver.com', 'port': 42}).connect(); |
| 38 | + assert.deepEqual(wsConstructor.args, [['ws://myserver.com:42/websocket']]); |
| 39 | + }); |
| 40 | + it('should use ssl if the port is 443', function() { |
| 41 | + new DDPClient({'host': 'myserver.com', 'port': 443}).connect(); |
| 42 | + assert.deepEqual(wsConstructor.args, [['wss://myserver.com:443/websocket']]); |
| 43 | + }); |
| 44 | +}); |
| 45 | + |
| 46 | +describe('Automatic reconnection', function() { |
| 47 | + beforeEach(function() { |
| 48 | + prepareMocks(); |
| 49 | + }); |
| 50 | + |
| 51 | + |
| 52 | + /* We should be able to get this test to work with clock.tick() but for some weird |
| 53 | + reasons it does not work. See: https://github.com/cjohansen/Sinon.JS/issues/283 |
| 54 | + */ |
| 55 | + it('should reconnect when the connection fails', function(done) { |
| 56 | + var ddpclient = new DDPClient({ auto_reconnect_timer: 10 }); |
| 57 | + |
| 58 | + ddpclient.connect(); |
| 59 | + wsMock.emit('error', {}); |
| 60 | + |
| 61 | + // At this point, the constructor should have been called only once. |
| 62 | + assert(wsConstructor.calledOnce); |
| 63 | + |
| 64 | + setTimeout(function() { |
| 65 | + // Now the constructor should have been called twice |
| 66 | + assert(wsConstructor.calledTwice); |
| 67 | + done(); |
| 68 | + }, 15); |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +describe("Network errors", function() { |
| 73 | + beforeEach(function() { |
| 74 | + prepareMocks(); |
| 75 | + }) |
| 76 | + |
| 77 | + // For some weird reasons (hard to reproduce) it happens that we try to send a message and |
| 78 | + // get an exception throws at us because the connection is not opened anymore. |
| 79 | + it ('should not crash when WebSocket throws a "not-opened" error', function() { |
| 80 | + // Simulates the error generated by WebSocket if the connection is not opened: |
| 81 | + // WebSocket.send (ws/lib/WebSocket.js:175:16) |
| 82 | + wsMock.send = function(data, options, cb) { |
| 83 | + if (typeof options == 'function') { |
| 84 | + cb = options; |
| 85 | + options = {}; |
| 86 | + } |
| 87 | + // if (this.readyState != WebSocket.OPEN) |
| 88 | + if (typeof cb == 'function') cb(new Error('not opened')); |
| 89 | + else throw new Error('not opened'); |
| 90 | + }; |
| 91 | + |
| 92 | + var ddpclient = new DDPClient(); |
| 93 | + ddpclient.connect(); |
| 94 | + |
| 95 | + var errorCB = sinon.spy(); |
| 96 | + ddpclient.on('socket-error', errorCB); |
| 97 | + var callCB = sinon.spy(); |
| 98 | + |
| 99 | + ddpclient.call('aServerMethod', [42], callCB); |
| 100 | + |
| 101 | + // First of all, the previous call should not throw anything. |
| 102 | + // The method callback should not be called |
| 103 | + // The socket-error event should not be triggered now |
| 104 | + // (but it will be triggered later by the socket) |
| 105 | + assert(!callCB.calledOnce); |
| 106 | + assert(!errorCB.calledOnce); |
| 107 | + }); |
| 108 | +}); |
0 commit comments