Fix tests

This commit is contained in:
lmb 2014-10-20 09:44:09 +01:00
parent 9b9c6f168a
commit d1a24d5711
5 changed files with 140 additions and 201 deletions

View File

@ -1,21 +1,15 @@
var express = require('express');
var mixin = require('utils-merge');
var proto = require('./server');
var util = require('./util');
var http = require('http');
var https = require('https');
function ExpressPeerServer(server, options) {
if (!server) {
throw new Error('Server is not passed to constructor - '+
'can\'t start PeerServer');
}
var app = express();
mixin(app, proto);
util.extend(app, proto);
options = app.options = mixin({
options = app._options = util.extend({
debug: false,
timeout: 5000,
key: 'peerjs',
@ -34,6 +28,11 @@ function ExpressPeerServer(server, options) {
app._ips = {};
app.on('mount', function() {
if (!server) {
throw new Error('Server is not passed to constructor - '+
'can\'t start PeerServer');
}
// Initialize HTTP routes. This is only used for the first few milliseconds
// before a socket is opened for a Peer.
app._initializeHTTP();
@ -47,11 +46,11 @@ function ExpressPeerServer(server, options) {
function PeerServer(options, callback) {
var app = express();
options = options || {};
var path = options.path || '/';
var port = options.port;
var port = options.port || 80;
delete options.path;
delete options.port;
if (path[0] !== '/') {
path = '/' + path;

View File

@ -74,7 +74,7 @@ app._configureWS = function(socket, key, id, token) {
// Cleanup after a socket closes.
socket.on('close', function() {
util.log('Socket closed:', id);
self._log('Socket closed:', id);
if (client.socket == socket) {
self._removePeer(key, id);
}
@ -96,7 +96,7 @@ app._configureWS = function(socket, key, id, token) {
util.prettyError('Message unrecognized');
}
} catch(e) {
util.log('Invalid message', data);
self._log('Invalid message', data);
throw e;
}
});
@ -107,11 +107,11 @@ app._configureWS = function(socket, key, id, token) {
};
app._checkAllowsDiscovery = function(key, cb) {
cb(this.options.allow_discovery);
cb(this._options.allow_discovery);
};
app._checkKey = function(key, ip, cb) {
if (key == this.options.key) {
if (key == this._options.key) {
if (!this._clients[key]) {
this._clients[key] = {};
}
@ -122,11 +122,11 @@ app._checkKey = function(key, ip, cb) {
this._ips[ip] = 0;
}
// Check concurrent limit
if (Object.keys(this._clients[key]).length >= this.options.concurrent_limit) {
if (Object.keys(this._clients[key]).length >= this._options.concurrent_limit) {
cb('Server has reached its concurrent user limit');
return;
}
if (this._ips[ip] >= this.options.ip_limit) {
if (this._ips[ip] >= this._options.ip_limit) {
cb(ip + ' has reached its concurrent user limit');
return;
}
@ -339,7 +339,7 @@ app._handleTransmission = function(key, message) {
// User is connected!
if (destination) {
try {
util.log(type, 'from', src, 'to', dst);
this._log(type, 'from', src, 'to', dst);
if (destination.socket) {
destination.socket.send(data);
} else if (destination.res) {
@ -389,3 +389,9 @@ app._generateClientId = function(key) {
}
return clientId;
};
app._log = function() {
if (this._options.debug) {
console.log.apply(console, arguments);
}
};

View File

@ -1,4 +1,3 @@
var util = {
debug: false,
inherits: function(ctor, superCtor) {
@ -27,15 +26,6 @@ var util = {
prettyError: function (msg) {
console.log('ERROR PeerServer: ', msg);
},
log: function() {
if (util.debug) {
var copy = [];
for (var i = 0; i < arguments.length; i += 1) {
copy[i] = arguments[i];
}
console.log.apply(console, copy);
}
},
allowCrossDomain: function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
@ -45,6 +35,4 @@ var util = {
}
};
// if node
module.exports = util;
// end node

View File

@ -16,7 +16,6 @@
"body-parser": "^1.9.0",
"express": "^4.9.8",
"optimist": "~0.6.1",
"utils-merge": "~1.0.0",
"ws": "~0.4.25"
},
"devDependencies": {

View File

@ -1,30 +1,16 @@
var PeerServer = require('../').PeerServer;
var ExpressPeerServer = require('../').ExpressPeerServer;
var expect = require('expect.js');
var sinon = require('sinon');
describe('PeerServer', function() {
describe('#_initializeWSS', function() {
WebSocketServer = sinon.stub();
});
describe('#_configureWS', function() {
describe('ExpressPeerServer', function() {
describe('method', function() {
var p;
before(function() {
p = ExpressPeerServer(undefined, {port: 8000});
});
describe('#_checkKey', function() {
var p;
before(function(done) {
PeerServer.prototype._initializeHTTP = sinon.stub();
p = PeerServer({ port: 8000 }, done);
p._checkKey('peerjs', 'myip', function() {});
});
after(function(done) {
p._server.close(done);
});
it('should reject keys that are not the default', function(done) {
p._checkKey('bad key', null, function(response) {
expect(response).to.be('Invalid key provided');
@ -40,7 +26,7 @@ describe('PeerServer', function() {
});
it('should reject ips that are at their limit', function(done) {
p.options.ip_limit = 0;
p._options.ip_limit = 0;
p._checkKey('peerjs', 'myip', function(response) {
expect(response).to.be('myip has reached its concurrent user limit');
done();
@ -48,47 +34,22 @@ describe('PeerServer', function() {
});
it('should reject when the server is at its limit', function(done) {
p.options.concurrent_limit = 0;
p._options.concurrent_limit = 0;
p._checkKey('peerjs', 'myip', function(response) {
expect(response).to.be('Server has reached its concurrent user limit');
done();
});
});
});
describe('#_initializeHTTP', function() {
});
describe('#_startStreaming', function() {
});
describe('#_pruneOutstanding', function() {
});
describe('#_processOutstanding', function() {
});
describe('#_removePeer', function() {
var p;
before(function(done) {
PeerServer.prototype._initializeHTTP = sinon.stub();
p = new PeerServer({ port: 8000 }, done);
before(function() {
var fake = {ip: '0.0.0.0'};
p._ips[fake.ip] = 1;
p._clients['peerjs'] = {};
p._clients['peerjs']['test'] = fake;
});
after(function(done) {
p._server.close(done);
});
it('should decrement the number of ips being used and remove the connection', function() {
expect(p._ips['0.0.0.0']).to.be(1);
p._removePeer('peerjs', 'test');
@ -98,17 +59,11 @@ describe('PeerServer', function() {
});
describe('#_handleTransmission', function() {
var p;
var KEY = 'peerjs';
var ID = 'test';
before(function(done) {
PeerServer.prototype._initializeHTTP = sinon.stub();
p = PeerServer({ port: 8000 }, done);
p._clients[KEY] = {};
});
after(function(done) {
p._server.close(done);
before(function() {
p._clients[KEY] = {};
});
it('should send to the socket when appropriate', function() {
@ -189,17 +144,9 @@ describe('PeerServer', function() {
});
describe('#_generateClientId', function() {
var p;
before(function(done) {
p = new PeerServer({ port: 8000 }, done);
});
after(function(done) {
p._server.close(done);
});
it('should generate a 16-character ID', function() {
expect(p._generateClientId('anykey').length).to.be.within(15, 16);
expect(p._generateClientId('anykey').length).to.be(16);
});
});
});
});