external-ip
Advanced tools
Comparing version 0.1.4 to 0.1.5
25
index.js
@@ -66,2 +66,11 @@ 'use strict'; | ||
var abort = function (requests) { | ||
process.nextTick(function () { | ||
requests.forEach(function (request) { | ||
request.abort(); | ||
}); | ||
}); | ||
}; | ||
var onResponse = function (err, ip) { | ||
@@ -75,12 +84,12 @@ | ||
} | ||
if (ip || errors.length === services.length) { | ||
if(ip) { | ||
done = true; | ||
// Make sure requests has been initialized | ||
process.nextTick(function () { | ||
requests.forEach(function (request) { | ||
request.abort(); | ||
}); | ||
}); | ||
cb.apply(null, ip ? [null, ip] : [errors, null]); | ||
abort(requests); //async | ||
return cb(null, ip); | ||
} | ||
if (errors.length === services.length) { | ||
done = true; | ||
abort(requests); //async | ||
return cb(errors, null); | ||
} | ||
}; | ||
@@ -87,0 +96,0 @@ |
@@ -15,2 +15,3 @@ 'use strict'; | ||
} | ||
// if the body is null use an empty string | ||
body = (body || '').toString().replace('\n', ''); | ||
@@ -17,0 +18,0 @@ return cb.apply(null, utils.isIP(body) ? [null, body] : [new Error('Invalid IP'), null]); |
{ | ||
"name": "external-ip", | ||
"version": "0.1.4", | ||
"description": "Get your external IP, with fallbacks", | ||
"version": "0.1.5", | ||
"description": "A node.js library to get your external ip from multiple services", | ||
"main": "index.js", | ||
@@ -31,5 +31,5 @@ "scripts": { | ||
"devDependencies": { | ||
"mocha": "^1.21.4", | ||
"should": "^4.0.4" | ||
"chai": "^1.9.1", | ||
"mocha": "^1.21.4" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
#external-ip [![Build Status](https://travis-ci.org/J-Chaniotis/external-ip.svg?branch=master)](https://travis-ci.org/J-Chaniotis/external-ip) ![Dependencies](https://david-dm.org/J-chaniotis/external-ip.svg) | ||
#external-ip [![Build Status](https://travis-ci.org/J-Chaniotis/external-ip.svg?branch=master)](https://travis-ci.org/J-Chaniotis/external-ip) [![Dependency Status](https://david-dm.org/J-Chaniotis/external-ip.svg)](https://david-dm.org/J-Chaniotis/external-ip) | ||
@@ -3,0 +3,0 @@ ![XKCD 865](http://imgs.xkcd.com/comics/nanobots.png) |
'use strict'; | ||
/*globals describe, it*/ | ||
var expect = require('chai').expect; | ||
var extIP = require('../lib/extIP').setup({ | ||
@@ -11,35 +11,31 @@ replace: false, | ||
var should = require('should'); | ||
var successRequest = { | ||
get: function (opts, cb) { | ||
opts.should.have.property('url', 'batman'); | ||
opts.should.have.property('timeout', 500); | ||
opts.should.have.property('headers').with.property('User-Agent', 'curl/'); | ||
cb(null, null, '94.65.128.173'); | ||
// Request mocks | ||
var request = { | ||
success: { | ||
get: function (opts, cb) { | ||
expect(opts).to.have.property('url', 'batman'); | ||
expect(opts).to.have.property('timeout', 500); | ||
expect(opts).to.have.property('headers').with.property('User-Agent', 'curl/'); | ||
cb(null, null, '94.65.128.173'); | ||
} | ||
}, | ||
fail: { | ||
get: function (opts, cb) { | ||
cb('booom', null, null); | ||
} | ||
}, | ||
invalid: { | ||
get: function (opts, cb) { | ||
cb(null, null, 11111); | ||
} | ||
} | ||
}; | ||
var failedRequest = { | ||
get: function (opts, cb) { | ||
cb('booom', null, null); | ||
} | ||
}; | ||
var invalidRequest = { | ||
get: function (opts, cb) { | ||
cb(null, null, 11111); | ||
} | ||
}; | ||
describe('extIP.js test', function () { | ||
it('Should have correct request config and return without errors', function () { | ||
var req = extIP.requestFactory(successRequest, 'batman'); | ||
var req = extIP.requestFactory(request.success, 'batman'); | ||
req(function (err, ip) { | ||
(err === null).should.be.true; | ||
ip.should.equal('94.65.128.173'); | ||
expect(err).to.equal(null); | ||
expect(ip).to.equal('94.65.128.173'); | ||
}); | ||
@@ -49,6 +45,6 @@ }); | ||
it('Should return with an error', function () { | ||
var req = extIP.requestFactory(failedRequest, 'batman'); | ||
var req = extIP.requestFactory(request.fail, 'batman'); | ||
req(function (err, ip) { | ||
err.should.equal('booom'); | ||
(ip === null).should.be.true; | ||
expect(err).to.equal('booom'); | ||
expect(ip).to.equal(null); | ||
}); | ||
@@ -58,7 +54,7 @@ }); | ||
it('Should validate a correct ip', function () { | ||
var req = extIP.requestFactory(successRequest, 'batman'); | ||
var req = extIP.requestFactory(request.success, 'batman'); | ||
req = extIP.addValidation(req); | ||
req(function (err, ip) { | ||
(err === null).should.be.true; | ||
ip.should.equal('94.65.128.173'); | ||
expect(err).to.equal(null); | ||
expect(ip).to.equal('94.65.128.173'); | ||
}); | ||
@@ -68,7 +64,7 @@ }); | ||
it('Should return an error with an invalid ip', function () { | ||
var req = extIP.requestFactory(invalidRequest, 'batman'); | ||
var req = extIP.requestFactory(request.invalid, 'batman'); | ||
req = extIP.addValidation(req); | ||
req(function (err, ip) { | ||
err.should.be.an.Error; | ||
(ip === null).should.be.true; | ||
expect(err).to.be.instanceof(Error); | ||
expect(ip).to.equal(null); | ||
}); | ||
@@ -75,0 +71,0 @@ }); |
@@ -8,4 +8,5 @@ 'use strict'; | ||
var utils = require('../lib/utils'); | ||
var should = require('should'); | ||
var expect = require('chai').expect; | ||
describe('index.js test', function () { | ||
@@ -16,4 +17,4 @@ it('Should return an IP with default configuration', function (done) { | ||
getIP(function (err, ip) { | ||
(err === null).should.be.true; | ||
utils.isIP(ip).should.be.true; | ||
expect(err).to.equal(null); | ||
expect(utils.isIP(ip)).to.equal(true); | ||
done(); | ||
@@ -34,4 +35,4 @@ }); | ||
getIP(function (err, ip) { | ||
(err === null).should.be.true; | ||
utils.isIP(ip).should.be.true; | ||
expect(err).to.equal(null); | ||
expect(utils.isIP(ip)).to.equal(true); | ||
done(); | ||
@@ -38,0 +39,0 @@ }); |
@@ -6,3 +6,3 @@ 'use strict'; | ||
var utils = require('../lib/utils'); | ||
var sould = require('should'); | ||
var expect = require('chai').expect; | ||
@@ -13,13 +13,14 @@ | ||
it('should be able to validate IPv4, IPv6 and hostnames', function () { | ||
utils.isIP('192.168.1.1').should.be.true; | ||
utils.isIP('94.65.128.173').should.be.true; | ||
utils.isIP('FE80:0000:0000:0000:0202:B3FF:FE1E:8329').should.be.true; | ||
utils.isIP('FE80::0202:B3FF:FE1E:8329').should.be.true; | ||
utils.isIP('batman.local').should.be.true; | ||
expect(utils.isIP('192.168.1.1')).to.equal(true); | ||
expect(utils.isIP('94.65.128.173')).to.equal(true); | ||
expect(utils.isIP('FE80:0000:0000:0000:0202:B3FF:FE1E:8329')).to.equal(true); | ||
expect(utils.isIP('FE80::0202:B3FF:FE1E:8329')).to.equal(true); | ||
expect(utils.isIP('batman.local')).to.equal(true); | ||
utils.isIP(111111).should.be.false; | ||
utils.isIP('192..1.1').should.be.false; | ||
utils.isIP('94.65.128.1A3').should.be.false; | ||
utils.isIP('FE80:0000:0000:0000:0202:B3FF:FE1E:').should.be.false; | ||
expect(utils.isIP(111111)).to.equal(false); | ||
expect(utils.isIP('192..1.1')).to.equal(false); | ||
expect(utils.isIP('94.65.128.1A3')).to.equal(false); | ||
expect(utils.isIP('FE80:0000:0000:0000:0202:B3FF:FE1E:')).to.equal(false); | ||
}); | ||
@@ -33,7 +34,7 @@ | ||
exec: function (i, stop, next) { | ||
next(i, 'man'); | ||
next(i, 'someArg'); | ||
}, | ||
done: function (result, bat) { | ||
result.should.equal(i - 1); | ||
bat.should.equal('man'); | ||
done: function (result, anotherArg) { | ||
expect(result).to.equal(i - 1); | ||
expect(anotherArg).to.equal('someArg'); | ||
cb(); | ||
@@ -53,9 +54,9 @@ } | ||
if (i === n) { | ||
stop(i, 'tab'); | ||
stop(i, 'stopValue'); | ||
} | ||
next(i, 'man'); | ||
next(i, 'someArg'); | ||
}, | ||
done: function (result, bat) { | ||
result.should.equal(n); | ||
bat.should.equal('tab'); | ||
done: function (result, anotherArg) { | ||
expect(result).to.equal(n); | ||
expect(anotherArg).to.equal('stopValue'); | ||
cb(); | ||
@@ -67,89 +68,90 @@ } | ||
it('should validate the config object', function () { | ||
var validCfg1 = { | ||
replace: false, | ||
services: ['http://ifconfig.co/x-real-ip','http://ifconfig.me/ip'], | ||
timeout: 500, | ||
gerIP: 'sequential' | ||
}; | ||
utils.validateConfig(validCfg1).valid.should.be.true; | ||
var validCfg2 = { | ||
services: ['http://ifconfig.co/x-real-ip','http://ifconfig.me/ip'], | ||
timeout: 500, | ||
getIP: 'parallel' | ||
it('should allow valid config', function () { | ||
var config = { | ||
a: { | ||
replace: false, | ||
services: ['http://ifconfig.co/x-real-ip', 'http://ifconfig.me/ip'], | ||
timeout: 500, | ||
gerIP: 'sequential' | ||
}, | ||
b: { | ||
services: ['http://ifconfig.co/x-real-ip', 'http://ifconfig.me/ip'], | ||
timeout: 500, | ||
getIP: 'parallel' | ||
}, | ||
c: { | ||
timeout: 500 | ||
}, | ||
// An empty object is valid config | ||
d: {} | ||
}; | ||
utils.validateConfig(validCfg2).valid.should.be.true; | ||
var validCfg3 = { | ||
timeout: 500 | ||
}; | ||
utils.validateConfig(validCfg3).valid.should.be.true; | ||
expect(utils.validateConfig(config.a).valid).to.equal(true); | ||
expect(utils.validateConfig(config.b).valid).to.equal(true); | ||
expect(utils.validateConfig(config.c).valid).to.equal(true); | ||
expect(utils.validateConfig(config.d).valid).to.equal(true); | ||
}); | ||
var validCfg4 = {}; | ||
utils.validateConfig(validCfg4).valid.should.be.true; | ||
it('sould reject invalid config', function () { | ||
var invalidCfg1 = { | ||
replace: 'batman', | ||
services: [], | ||
timeout: 'robin', | ||
getIP: 'freeze' | ||
var config = { | ||
a: { | ||
replace: 'batman', | ||
services: [], | ||
timeout: 'robin', | ||
getIP: 'freeze' | ||
}, | ||
b: { | ||
replace: true | ||
}, | ||
c: { | ||
services: ['I am THE Batman'] | ||
} | ||
}; | ||
utils.validateConfig(invalidCfg1).errors.length.should.equal(4); | ||
expect(utils.validateConfig(config.a).errors.length).equal(4); | ||
expect(utils.validateConfig(config.b).errors.length).equal(1); | ||
expect(utils.validateConfig(config.c).errors.length).equal(1); | ||
var invalidCfg2 = { | ||
replace: true | ||
}; | ||
utils.validateConfig(invalidCfg2).errors.length.should.equal(1); | ||
var invalidCfg3 = { | ||
services: ['I am THE Batman'] | ||
}; | ||
utils.validateConfig(invalidCfg3).errors.length.should.equal(1); | ||
}); | ||
it('should merge a valid configuration with default configuration', function () { | ||
var defConf = { | ||
replace: false, | ||
services: ['http://ifconfig.co/x-real-ip','http://ifconfig.me/ip'], | ||
timeout: 500, | ||
getIP: 'sequential' | ||
}; | ||
var ext1 = {}; | ||
var ext2 = { | ||
services: ['http://ifconfig.co/x-real-ip','http://ifconfig.me/ip'], | ||
timeout: 1000, | ||
getIP: 'parallel' | ||
var config = { | ||
default: { | ||
replace: false, | ||
services: ['http://ifconfig.co/x-real-ip', 'http://ifconfig.me/ip'], | ||
timeout: 500, | ||
getIP: 'sequential' | ||
}, | ||
a: {}, | ||
b: { | ||
services: ['http://ifconfig.co/x-real-ip', 'http://ifconfig.me/ip'], | ||
timeout: 1000, | ||
getIP: 'parallel' | ||
}, | ||
c: { | ||
replace: true, | ||
services: ['http://ifconfig.co/x-real-ip'] | ||
} | ||
}; | ||
var ext3 = { | ||
replace: true, | ||
services: ['http://ifconfig.co/x-real-ip'] | ||
}; | ||
var merged = utils.mergeConfig(config.a, config.default); | ||
expect(merged).to.have.property('timeout', 500); | ||
expect(merged).to.have.property('services').with.lengthOf(2); | ||
expect(merged).to.have.property('getIP', 'sequential'); | ||
var merged = utils.mergeConfig(ext1, defConf); | ||
merged.should.have.property('timeout', 500); | ||
merged.should.have.property('services').with.lengthOf(2); | ||
merged.should.have.property('getIP', 'sequential'); | ||
merged = utils.mergeConfig(config.b, config.default); | ||
expect(merged).to.have.property('timeout', 1000); | ||
expect(merged).to.have.property('services').with.lengthOf(4); | ||
expect(merged).to.have.property('getIP', 'parallel'); | ||
merged = utils.mergeConfig(ext2, defConf); | ||
merged.should.have.property('timeout', 1000); | ||
merged.should.have.property('services').with.lengthOf(4); | ||
merged.should.have.property('getIP', 'parallel'); | ||
merged = utils.mergeConfig(config.c, config.default ); | ||
expect(merged).to.have.property('timeout', 500); | ||
expect(merged).to.have.property('services').with.lengthOf(1); | ||
merged = utils.mergeConfig(ext3, defConf); | ||
merged.should.have.property('timeout', 500); | ||
merged.should.have.property('services').with.lengthOf(1); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21468
13
477