Skip to content

Commit

Permalink
create tests for configurable listening IP
Browse files Browse the repository at this point in the history
  • Loading branch information
cscade committed Aug 29, 2012
1 parent ad6babe commit cc7ed4a
Showing 1 changed file with 149 additions and 14 deletions.
163 changes: 149 additions & 14 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,56 @@ var Ears = require('../'),
assert = require('assert'),
http = require('http');

var getNetworkIPs = (function () {
var ignoreRE = /^(127\.0\.0\.1|::1|fe80(:1)?::1(%.*)?)$/i;

var exec = require('child_process').exec;
var cached;
var command;
var filterRE;

switch (process.platform) {
case 'win32':
//case 'win64': // TODO: test
command = 'ipconfig';
filterRE = /\bIP(v[46])?-?[^:\r\n]+:\s*([^\s]+)/g;
// TODO: find IPv6 RegEx
break;
case 'darwin':
command = 'ifconfig';
filterRE = /\binet\s+([^\s]+)/g;
// filterRE = /\binet6\s+([^\s]+)/g; // IPv6
break;
default:
command = 'ifconfig';
filterRE = /\binet\b[^:]+:\s*([^\s]+)/g;
// filterRE = /\binet6[^:]+:\s*([^\s]+)/g; // IPv6
break;
}

return function (callback, bypassCache) {
if (cached && !bypassCache) {
callback(null, cached);
return;
}
// system call
exec(command, function (error, stdout, sterr) {
cached = [];
var ip;
var matches = stdout.match(filterRE) || [];
for (var i = 0; i < matches.length; i++) {
ip = matches[i].replace(filterRE, '$1')
if (!ignoreRE.test(ip)) {
cached.push(ip);
}
}
callback(error, cached);
});
};
})();

describe('Ears', function () {
var ears = new Ears({ port: 3000 });
var ears = new Ears({ port: 3000, verbose: false });

describe('core methods and initialization', function () {
it('should be an instance of Ears', function () {
Expand Down Expand Up @@ -54,21 +102,108 @@ describe('Ears', function () {
});

describe('#listen()', function () {
it('should listen on port 3000', function (done) {
ears.listen(function () {
var req = http.request({
host: 'localhost',
port: 3000,
path: '/',
method: 'GET'
}, function (res) {
res.on('end', function () {
assert.equal(res.statusCode, 405/* Method Not Allowed */);
done();
it('should listen on port 3000 on all', function (done) {
/*
listen
test for listen on ipAddresses[0]
test for listen on localhost
stop listening
*/
getNetworkIPs(function (e, ipAddresses) {
ears.listen(function () {
var req = http.request({
host: ipAddresses[0],
port: 3000,
path: '/',
method: 'GET'
}, function (res) {
res.on('end', function () {
assert.equal(res.statusCode, 405/* Method Not Allowed */);
var req = http.request({
host: 'localhost',
port: 3000,
path: '/',
method: 'GET'
}, function (res) {
res.on('end', function () {
assert.equal(res.statusCode, 405/* Method Not Allowed */);
ears.muffs(done);
});
});
req.on('error', function(e) { throw e; });
req.end();
});
});
req.on('error', function(e) { throw e; });
req.end();
});
});
});
it('should listen on port 3000 on a machine IP address', function (done) {
/*
listen
test for listen on ipAddresses[0]
stop listening
*/
getNetworkIPs(function (e, ipAddresses) {
ears.listen({
host: ipAddresses[0]
}, function () {
var req = http.request({
host: ipAddresses[0],
port: 3000,
path: '/',
method: 'GET'
}, function (res) {
res.on('end', function () {
assert.equal(res.statusCode, 405/* Method Not Allowed */);
ears.muffs(done);
});
});
req.on('error', function(e) { throw e; });
req.end();
});
});
});
it('should listen on port 3000 on 127.0.0.1 only', function (done) {
/*
listen
test for listen on localhost
verify no listen on ipAddresses[0]
leave listening for later tests
*/
getNetworkIPs(function (e, ipAddresses) {
ears.listen({
host: '127.0.0.1'
}, function () {
var req = http.request({
host: 'localhost',
port: 3000,
path: '/',
method: 'GET'
}, function (res) {
res.on('end', function () {
assert.equal(res.statusCode, 405/* Method Not Allowed */);
var req = http.request({
host: ipAddresses[0],
port: 3000,
path: '/',
method: 'GET'
}, function (res) {
res.on('end', function () {
throw new Error('response received: ' + res.statusCode);
});
});
req.on('error', function (e) {
assert.equal(e.code, 'ECONNREFUSED');
done();
});
req.end();
});
});
req.on('error', function(e) { throw e; });
req.end();
});
req.on('error', function(e) { throw e; });
req.end();
});
});
});
Expand Down

0 comments on commit cc7ed4a

Please sign in to comment.