1 | var util = require('util');
|
2 | var EventEmitter = require('events').EventEmitter;
|
3 | var PeerClient = require('../lib/peer_client');
|
4 | var assert = require('assert');
|
5 |
|
6 |
|
7 | var MockServer = { _name: '1234', httpServer: { spdyServer: {}}, log: {
|
8 | emit: function() {}
|
9 | }};
|
10 | var MockSocket = function() {
|
11 | EventEmitter.call(this);
|
12 | this.setAddress = function() {};
|
13 | this.start = function() {};
|
14 | };
|
15 | util.inherits(MockSocket, EventEmitter);
|
16 |
|
17 | var urlEndingWithSlash = 'http://cloud.zettajs.io/';
|
18 | var urlEndingWithNoSlash = 'http://cloud.zettajs.io';
|
19 |
|
20 | describe('Peer Client', function() {
|
21 | describe('url parsing', function() {
|
22 | it('should calculate the proper url with a trailing slash', function() {
|
23 | var client = new PeerClient(urlEndingWithSlash, MockServer);
|
24 | assert.equal(client.url, 'ws://cloud.zettajs.io/peers/1234');
|
25 | });
|
26 |
|
27 | it('should calculate the proper url without a trailing slash', function() {
|
28 | var client = new PeerClient(urlEndingWithNoSlash, MockServer);
|
29 | assert.equal(client.url, 'ws://cloud.zettajs.io/peers/1234');
|
30 | });
|
31 | });
|
32 |
|
33 | it('should emit error when underlying ws does', function(done) {
|
34 | var client = new PeerClient(urlEndingWithNoSlash, MockServer);
|
35 | client.ws = new MockSocket();
|
36 | client._createSocket();
|
37 | client.once('error', function(err) {
|
38 | assert.equal(err.message, 'some message');
|
39 | done();
|
40 | });
|
41 |
|
42 | client.once('closed', function() {
|
43 | done(new Error('Should not have emitted closed'));
|
44 | });
|
45 |
|
46 | setTimeout(function() {
|
47 | client.ws.emit('error', new Error('some message'));
|
48 | }, 2);
|
49 | })
|
50 |
|
51 | it('should emit closed when underlying ws does', function(done) {
|
52 | var client = new PeerClient(urlEndingWithNoSlash, MockServer);
|
53 | client.ws = new MockSocket();
|
54 | client._createSocket();
|
55 | client.once('error', function(err) {
|
56 | done(new Error('Should not have emitted error'));
|
57 | });
|
58 |
|
59 | client.once('closed', function() {
|
60 | done();
|
61 | });
|
62 |
|
63 | setTimeout(function() {
|
64 | client.ws.emit('close');
|
65 | }, 2);
|
66 | })
|
67 | });
|