Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caronte tests #476

Merged
merged 11 commits into from
Sep 17, 2013
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: node_js
node_js:
- "0.10"
- "0.11"

notifications:
email:
- [email protected]
irc: "irc.freenode.org#nodejitsu"
2 changes: 1 addition & 1 deletion lib/caronte/passes/ws-incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function XHeaders(req, socket, options) {
if(!options.xfwd) return;

var values = {
for : req.connection.remoteAddsockets || req.socket.remoteAddsockets,
for : req.connection.remoteAddress || req.socket.remoteAddress,
port : req.connection.remotePort || req.socket.remotePort,
proto: req.connection.pair ? 'wss' : 'ws'
};
Expand Down
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
"coveralls" : "*",
"mocha-lcov-reporter": "*",
"blanket" : "*",
"ws" : "*"
"ws" : "*",
"socket.io" : "*",
"socket.io-client" : "*"
},
"scripts" : {
"blanket" : { "pattern": "caronte/lib" },
"test" : "mocha -R landing test/*-test.js",
"test-cov" : "mocha --require blanket -R html-cov > cov/coverage.html"
"blanket" : { "pattern": "lib/caronte" },
"test" : "./node_modules/.bin/mocha -R landing test/*-test.js",
"test-cov" : "./node_modules/.bin/mocha --require blanket -R html-cov > cov/coverage.html"
},

"engines" : {
Expand Down
66 changes: 61 additions & 5 deletions test/lib-caronte-common-test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
var common = require('../lib/caronte/common'),
expect = require('expect.js');

describe('lib/caronte/common.js', function() {
describe('#setupOutgoing', function() {
it('should setup the right headers', function() {
describe('lib/caronte/common.js', function () {
describe('#setupOutgoing', function () {
it('should setup the correct headers', function () {
var outgoing = {};
common.setupOutgoing(outgoing,
{
Expand All @@ -17,19 +17,75 @@ describe('lib/caronte/common.js', function() {
},
{
method : 'i',
path : 'am',
url : 'am',
headers : 'proxy'
});

expect(outgoing.host).to.eql('hey');
expect(outgoing.hostname).to.eql('how');
expect(outgoing.socketPath).to.eql('are');
expect(outgoing.port).to.eql('you');
//expect(outgoing.agent).to.eql('?');
expect(outgoing.agent).to.eql('?');

expect(outgoing.method).to.eql('i');
expect(outgoing.path).to.eql('am');
expect(outgoing.headers).to.eql('proxy')
});

it('set the port according to the protocol', function () {
var outgoing = {};
common.setupOutgoing(outgoing,
{
target: {
host : 'how',
hostname : 'are',
socketPath: 'you',
agent : '?',
protocol: 'https:'
}
},
{
method : 'i',
url : 'am',
headers : 'proxy'
});

expect(outgoing.host).to.eql('how');
expect(outgoing.hostname).to.eql('are');
expect(outgoing.socketPath).to.eql('you');
expect(outgoing.agent).to.eql('?');

expect(outgoing.method).to.eql('i');
expect(outgoing.path).to.eql('am');
expect(outgoing.headers).to.eql('proxy')

expect(outgoing.port).to.eql(443);
});
});

describe('#setupSocket', function () {
it('should setup a socket', function () {
var socketConfig = {
timeout: null,
nodelay: false,
keepalive: false
},
stubSocket = {
setTimeout: function (num) {
socketConfig.timeout = num;
},
setNoDelay: function (bol) {
socketConfig.nodelay = bol;
},
setKeepAlive: function (bol) {
socketConfig.keepalive = bol;
}
}
returnValue = common.setupSocket(stubSocket);

expect(socketConfig.timeout).to.eql(0);
expect(socketConfig.nodelay).to.eql(true);
expect(socketConfig.keepalive).to.eql(true);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var caronte = require('../lib/caronte/passes/web'),
var caronte = require('../lib/caronte/passes/web-incoming'),
expect = require('expect.js');

describe('lib/caronte/passes/web.js', function() {
Expand Down
145 changes: 145 additions & 0 deletions test/lib-caronte-passes-ws-incoming-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
var caronte = require('../lib/caronte/passes/ws-incoming'),
expect = require('expect.js');

describe('lib/caronte/passes/ws-incoming.js', function () {
describe('#checkMethodAndHeader', function () {
it('should drop non-GET connections', function () {
var destroyCalled = false,
stubRequest = {
method: 'DELETE',
headers: {}
},
stubSocket = {
destroy: function () {
// Simulate Socket.destroy() method when call
destroyCalled = true;
}
}
returnValue = caronte.checkMethodAndHeader(stubRequest, stubSocket);
expect(returnValue).to.be(true);
expect(destroyCalled).to.be(true);
})

it('should drop connections when no upgrade header', function () {
var destroyCalled = false,
stubRequest = {
method: 'GET',
headers: {}
},
stubSocket = {
destroy: function () {
// Simulate Socket.destroy() method when call
destroyCalled = true;
}
}
returnValue = caronte.checkMethodAndHeader(stubRequest, stubSocket);
expect(returnValue).to.be(true);
expect(destroyCalled).to.be(true);
})

it('should drop connections when upgrade header is different of `websocket`', function () {
var destroyCalled = false,
stubRequest = {
method: 'GET',
headers: {
upgrade: 'anotherprotocol'
}
},
stubSocket = {
destroy: function () {
// Simulate Socket.destroy() method when call
destroyCalled = true;
}
}
returnValue = caronte.checkMethodAndHeader(stubRequest, stubSocket);
expect(returnValue).to.be(true);
expect(destroyCalled).to.be(true);
})

it('should return nothing when all is ok', function () {
var destroyCalled = false,
stubRequest = {
method: 'GET',
headers: {
upgrade: 'websocket'
}
},
stubSocket = {
destroy: function () {
// Simulate Socket.destroy() method when call
destroyCalled = true;
}
}
returnValue = caronte.checkMethodAndHeader(stubRequest, stubSocket);
expect(returnValue).to.be(undefined);
expect(destroyCalled).to.be(false);
})
});

describe('#setupSocket', function () {
it('Set the correct config to the socket', function () {
var stubSocket = {
setTimeout: function (num) {
// Simulate Socket.setTimeout()
socketConfig.timeout = num;
},
setNoDelay: function (bol) {
// Simulate Socket.setNoDelay()
socketConfig.nodelay = bol;
},
setKeepAlive: function (bol) {
// Simulate Socket.setKeepAlive()
socketConfig.keepalive = bol;
}
},
socketConfig = {
timeout: null,
nodelay: false,
keepalive: false
},
returnValue = caronte.setupSocket({}, stubSocket);
expect(returnValue).to.be(undefined);
expect(socketConfig.timeout).to.eql(0);
expect(socketConfig.nodelay).to.eql(true);
expect(socketConfig.keepalive).to.eql(true);
});
});

describe('#XHeaders', function () {
it('return if no forward request', function () {
var returnValue = caronte.XHeaders({}, {}, {});
expect(returnValue).to.be(undefined);
});

it('set the correct x-forwarded-* headers from req.connection', function () {
var stubRequest = {
connection: {
remoteAddress: '192.168.1.2',
remotePort: '8080'
},
headers: {}
}
caronte.XHeaders(stubRequest, {}, { xfwd: true });
expect(stubRequest.headers['x-forwarded-for']).to.be('192.168.1.2');
expect(stubRequest.headers['x-forwarded-port']).to.be('8080');
expect(stubRequest.headers['x-forwarded-proto']).to.be('ws');
});

it('set the correct x-forwarded-* headers from req.socket', function () {
var stubRequest = {
socket: {
remoteAddress: '192.168.1.3',
remotePort: '8181'
},
connection: {
pair: true
},
headers: {}
};
caronte.XHeaders(stubRequest, {}, { xfwd: true });
expect(stubRequest.headers['x-forwarded-for']).to.be('192.168.1.3');
expect(stubRequest.headers['x-forwarded-port']).to.be('8181');
expect(stubRequest.headers['x-forwarded-proto']).to.be('wss');
});
});
});
87 changes: 0 additions & 87 deletions test/lib-caronte-passes-ws-test.js

This file was deleted.

Loading