Merge pull request #476 from nodejitsu/caronte-tests

[merge] caronte tests
This commit is contained in:
yawnt 2013-09-17 01:23:55 -07:00
commit 9efa40a9d2
11 changed files with 444 additions and 363 deletions

9
.travis.yml Normal file
View File

@ -0,0 +1,9 @@
language: node_js
node_js:
- "0.10"
- "0.11"
notifications:
email:
- travis@nodejitsu.com
irc: "irc.freenode.org#nodejitsu"

View File

@ -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'
};

View File

@ -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" : {

View File

@ -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,
{
@ -17,7 +17,7 @@ describe('lib/caronte/common.js', function() {
},
{
method : 'i',
path : 'am',
url : 'am',
headers : 'proxy'
});
@ -25,11 +25,67 @@ describe('lib/caronte/common.js', function() {
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);
});
});
});

View File

@ -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() {

View File

@ -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');
});
});
});

View File

@ -1,87 +0,0 @@
var caronte = require('../lib/caronte/passes/ws'),
expect = require('expect.js');
describe('lib/caronte/passes/ws.js', function () {
describe('#checkMethodAndHeader', function () {
it('should drop non-GET connections', function () {
var endCalled = false,
stubRequest = {
method: 'DELETE',
headers: {},
end: function () {
// Simulate Stream.end() method when call
endCalled = true;
}
},
returnValue = caronte.checkMethodAndHeader(stubRequest, {}, {});
expect(returnValue).to.be(true);
expect(endCalled).to.be(true);
})
it('should drop connections when no upgrade header', function () {
var endCalled = false,
stubRequest = {
method: 'GET',
headers: {},
end: function () {
// Simulate Stream.end() method when call
endCalled = true;
}
},
returnValue = caronte.checkMethodAndHeader(stubRequest, {}, {});
expect(returnValue).to.be(true);
expect(endCalled).to.be(true);
})
it('should drop connections when upgrade header is different of `websocket`', function () {
var endCalled = false,
stubRequest = {
method: 'GET',
headers: {
upgrade: 'anotherprotocol'
},
end: function () {
// Simulate Stream.end() method when call
endCalled = true;
}
},
returnValue = caronte.checkMethodAndHeader(stubRequest, {}, {});
expect(returnValue).to.be(true);
expect(endCalled).to.be(true);
})
it('should return nothing when all is ok', function () {
var endCalled = false,
stubRequest = {
method: 'GET',
headers: {
upgrade: 'websocket'
},
end: function () {
// Simulate Stream.end() method when call
endCalled = true;
}
},
returnValue = caronte.checkMethodAndHeader(stubRequest, {}, {});
expect(returnValue).to.be(undefined);
expect(endCalled).to.be(false);
})
});
describe('#XHeaders', function () {
// var stubRequest = {
// connection: {
// remoteAddress: '192.168.1.2',
// remotePort: '8080'
// },
// headers: {}
// }
// it('set the correct x-forwarded-* headers', function () {
// 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('http');
// });
});
});

View File

@ -1,45 +0,0 @@
var caronte = require('../'),
ForwardStream = require('../lib/caronte/streams/forward');
expect = require('expect.js'),
Writable = require('stream').Writable,
http = require('http');
describe('lib/caronte/streams/forward.js', function () {
describe('forward stream constructor', function () {
it('should be an instance of Writable stream and get the correct options and methods', function () {
var stubOptions = {
key: 'value'
};
var forwardProxy = new ForwardStream(stubOptions);
expect(forwardProxy).to.be.a(Writable);
expect(forwardProxy.options).to.eql({ key: 'value' });
expect(forwardProxy.onPipe).to.be.a('function');
expect(forwardProxy.onFinish).to.be.a('function');
expect(forwardProxy._events).to.have.property('pipe');
expect(forwardProxy._events).to.have.property('finish');
});
});
describe('should pipe the request and finish it', function () {
it('should make the request on pipe and finish it', function(done) {
var result;
var p = caronte.createProxyServer({
forward: 'http://127.0.0.1:8080'
}).listen('8081')
var s = http.createServer(function(req, res) {
expect(req.method).to.eql('GET');
s.close();
p.close();
done();
});
s.listen('8080');
http.request('http://127.0.0.1:8081', function() {}).end();
});
});
});

View File

@ -1,109 +0,0 @@
var caronte = require('../'),
ProxyStream = require('../lib/caronte/streams/proxy');
expect = require('expect.js'),
Duplex = require('stream').Duplex,
http = require('http');
describe('lib/caronte/streams/proxy.js', function () {
describe('proxy stream constructor', function () {
it('should be an instance of Duplex stream and get the correct options and methods', function () {
var stubOptions = {
key: 'value'
};
var proxyStream = new ProxyStream(stubOptions);
expect(proxyStream).to.be.a(Duplex);
expect(proxyStream.options).to.eql({ key: 'value' });
expect(proxyStream.onPipe).to.be.a('function');
expect(proxyStream.onFinish).to.be.a('function');
expect(proxyStream._events).to.have.property('pipe');
expect(proxyStream._events).to.have.property('finish');
});
});
describe('caronte createProxyServer() method', function () {
it('should make the request on pipe and finish it', function(done) {
var proxy = caronte.createProxyServer({
target: 'http://127.0.0.1:8080'
}).listen('8081');
var source = http.createServer(function(req, res) {
expect(req.headers['x-forwarded-for']).to.eql('127.0.0.1');
source.close();
proxy.close();
done();
});
source.listen('8080');
http.request({
hostname: '127.0.0.1',
port: '8081',
method: 'POST',
headers: {
'x-forwarded-for': '127.0.0.1'
}
}, function() {}).end();
});
});
describe('caronte createProxyServer() method with response', function () {
it('should make the request, handle response and finish it', function(done) {
var proxy = caronte.createProxyServer({
target: 'http://127.0.0.1:8080'
}).listen('8081');
var source = http.createServer(function(req, res) {
expect(req.method).to.eql('GET');
res.writeHead(200, {'Content-Type': 'text/plain'})
res.end('Hello from ' + source.address().port);
});
source.listen('8080');
http.request({
hostname: '127.0.0.1',
port: '8081',
method: 'GET',
}, function(res) {
expect(res.statusCode).to.eql(200);
res.on('data', function (data) {
expect(data.toString()).to.eql('Hello from 8080');
});
res.on('end', function () {
source.close();
proxy.close();
done();
});
}).end();
});
});
describe('caronte createProxyServer() method with error response', function () {
it('should make the request and response with error', function(done) {
var proxy = caronte.createProxyServer({
target: 'http://127.0.0.1:8080'
}).listen('8081');
http.request({
hostname: '127.0.0.1',
port: '8081',
method: 'GET',
}, function(res) {
expect(res.statusCode).to.eql(500);
res.on('data', function (data) {
expect(data.toString()).to.eql('Internal Server Error');
});
res.on('end', function () {
proxy.close();
done();
});
}).end();
});
});
});

View File

@ -1,109 +0,0 @@
var caronte = require('../'),
WebSocket = require('../lib/caronte/streams/websocket');
expect = require('expect.js'),
Duplex = require('stream').Duplex,
http = require('http');
describe('lib/caronte/streams/websocket.js', function () {
describe('WebSocket stream constructor', function () {
it('should be an instance of Duplex stream and get the correct options and methods', function () {
var stubOptions = {
key: 'value'
};
var WebSocketStream = new WebSocket(stubOptions);
expect(WebSocketStream).to.be.a(Duplex);
expect(WebSocketStream.options).to.eql({ key: 'value' });
expect(WebSocketStream.onPipe).to.be.a('function');
expect(WebSocketStream.onFinish).to.be.a('function');
expect(WebSocketStream._events).to.have.property('pipe');
expect(WebSocketStream._events).to.have.property('finish');
});
});
describe('caronte createWebSocketServer() method', function () {
// it('should make the request on pipe and finish it', function(done) {
// var proxy = caronte.createProxyServer({
// target: 'http://127.0.0.1:8080'
// }).listen('8081');
// var source = http.createServer(function(req, res) {
// expect(req.headers['x-forwarded-for']).to.eql('127.0.0.1');
// source.close();
// proxy.close();
// done();
// });
// source.listen('8080');
// http.request({
// hostname: '127.0.0.1',
// port: '8081',
// method: 'POST',
// headers: {
// 'x-forwarded-for': '127.0.0.1'
// }
// }, function() {}).end();
// });
});
describe('caronte createProxyServer() method with response', function () {
// it('should make the request, handle response and finish it', function(done) {
// var proxy = caronte.createProxyServer({
// target: 'http://127.0.0.1:8080'
// }).listen('8081');
// var source = http.createServer(function(req, res) {
// expect(req.method).to.eql('GET');
// res.writeHead(200, {'Content-Type': 'text/plain'})
// res.end('Hello from ' + source.address().port);
// });
// source.listen('8080');
// http.request({
// hostname: '127.0.0.1',
// port: '8081',
// method: 'GET',
// }, function(res) {
// expect(res.statusCode).to.eql(200);
// res.on('data', function (data) {
// expect(data.toString()).to.eql('Hello from 8080');
// });
// res.on('end', function () {
// source.close();
// proxy.close();
// done();
// });
// }).end();
// });
});
describe('caronte createProxyServer() method with error response', function () {
// it('should make the request and response with error', function(done) {
// var proxy = caronte.createProxyServer({
// target: 'http://127.0.0.1:8080'
// }).listen('8081');
// http.request({
// hostname: '127.0.0.1',
// port: '8081',
// method: 'GET',
// }, function(res) {
// expect(res.statusCode).to.eql(500);
// res.on('data', function (data) {
// expect(data.toString()).to.eql('Internal Server Error');
// });
// res.on('end', function () {
// proxy.close();
// done();
// });
// }).end();
// });
});
});

View File

@ -1,5 +1,10 @@
var caronte = require('../lib/caronte'),
expect = require('expect.js');
var caronte = require('../lib/caronte'),
expect = require('expect.js'),
http = require('http'),
ws = require('ws')
io = require('socket.io'),
ioClient = require('socket.io-client');
describe('lib/caronte.js', function() {
describe('#createProxyServer', function() {
@ -24,4 +29,218 @@ describe('lib/caronte.js', function() {
expect(obj.listen).to.be.a(Function);
});
});
describe('#createProxyServer with forward options and using web-incoming passes', function () {
it('should pipe the request using web-incoming#stream method', function (done) {
var proxy = caronte.createProxyServer({
forward: 'http://127.0.0.1:8080'
}).listen('8081')
var source = http.createServer(function(req, res) {
expect(req.method).to.eql('GET');
expect(req.headers.host.split(':')[1]).to.eql('8081');
source.close();
proxy.close();
done();
});
source.listen('8080');
http.request('http://127.0.0.1:8081', function() {}).end();
})
});
describe('#createProxyServer using the web-incoming passes', function () {
it('should make the request on pipe and finish it', function(done) {
var proxy = caronte.createProxyServer({
target: 'http://127.0.0.1:8080'
}).listen('8081');
var source = http.createServer(function(req, res) {
expect(req.method).to.eql('POST');
expect(req.headers['x-forwarded-for']).to.eql('127.0.0.1');
expect(req.headers.host.split(':')[1]).to.eql('8081');
source.close();
proxy.close();
done();
});
source.listen('8080');
http.request({
hostname: '127.0.0.1',
port: '8081',
method: 'POST',
headers: {
'x-forwarded-for': '127.0.0.1'
}
}, function() {}).end();
});
});
describe('#createProxyServer using the web-incoming passes', function () {
it('should make the request, handle response and finish it', function(done) {
var proxy = caronte.createProxyServer({
target: 'http://127.0.0.1:8080'
}).listen('8081');
var source = http.createServer(function(req, res) {
expect(req.method).to.eql('GET');
expect(req.headers.host.split(':')[1]).to.eql('8081');
res.writeHead(200, {'Content-Type': 'text/plain'})
res.end('Hello from ' + source.address().port);
});
source.listen('8080');
http.request({
hostname: '127.0.0.1',
port: '8081',
method: 'GET',
}, function(res) {
expect(res.statusCode).to.eql(200);
res.on('data', function (data) {
expect(data.toString()).to.eql('Hello from 8080');
});
res.on('end', function () {
source.close();
proxy.close();
done();
});
}).end();
});
});
describe('#createProxyServer() method with error response', function () {
it('should make the request and emit the error event', function(done) {
var proxy = caronte.createProxyServer({
target: 'http://127.0.0.1:8080'
});
proxy.ee.on('caronte:outgoing:web:error', function (err) {
expect(err).to.be.an(Error);
expect(err.code).to.be('ECONNREFUSED');
proxyServer.close();
done();
})
var proxyServer = proxy.listen('8081');
http.request({
hostname: '127.0.0.1',
port: '8081',
method: 'GET',
}, function() {}).end();
});
});
describe('#createProxyServer using the web-incoming passes', function () {
it('should emit events correclty', function(done) {
var proxy = caronte.createProxyServer({
target: 'http://127.0.0.1:8080'
}),
proxyServer = proxy.listen('8081'),
source = http.createServer(function(req, res) {
expect(req.method).to.eql('GET');
expect(req.headers.host.split(':')[1]).to.eql('8081');
res.writeHead(200, {'Content-Type': 'text/plain'})
res.end('Hello from ' + source.address().port);
}),
events = [];
source.listen('8080');
proxy.ee.on('caronte:**', function (uno, dos, tres) {
events.push(this.event);
})
http.request({
hostname: '127.0.0.1',
port: '8081',
method: 'GET',
}, function(res) {
expect(res.statusCode).to.eql(200);
res.on('data', function (data) {
expect(data.toString()).to.eql('Hello from 8080');
});
res.on('end', function () {
expect(events).to.contain('caronte:outgoing:web:begin');
expect(events).to.contain('caronte:outgoing:web:end');
source.close();
proxyServer.close();
done();
});
}).end();
});
});
describe('#createProxyServer using the ws-incoming passes', function () {
it('should proxy the websockets stream', function (done) {
var proxy = caronte.createProxyServer({
target: 'ws://127.0.0.1:8080',
ws: true
}),
proxyServer = proxy.listen('8081'),
destiny = new ws.Server({ port: 8080 }, function () {
var client = new ws('ws://127.0.0.1:8081');
client.on('open', function () {
client.send('hello there');
});
client.on('message', function (msg) {
expect(msg).to.be('Hello over websockets');
client.close();
proxyServer.close();
destiny.close();
done();
});
});
destiny.on('connection', function (socket) {
socket.on('message', function (msg) {
expect(msg).to.be('hello there');
socket.send('Hello over websockets');
});
});
});
});
describe('#createProxyServer using the ws-incoming passes', function () {
it('should proxy a socket.io stream', function (done) {
var proxy = caronte.createProxyServer({
target: 'ws://127.0.0.1:8080',
ws: true
}),
proxyServer = proxy.listen('8081'),
destiny = io.listen(8080, function () {
var client = ioClient.connect('ws://127.0.0.1:8081');
client.on('connect', function () {
client.emit('incoming', 'hello there');
});
client.on('outgoing', function (data) {
expect(data).to.be('Hello over websockets');
proxyServer.close();
destiny.server.close();
done();
});
});
destiny.sockets.on('connection', function (socket) {
socket.on('incoming', function (msg) {
expect(msg).to.be('hello there');
socket.emit('outgoing', 'Hello over websockets');
});
})
});
})
});