Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabian Hoffmann committed May 3, 2015
1 parent 8512b16 commit f0efe52
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
.idea
.grunt
27 changes: 27 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module.exports = function (grunt) {

grunt.initConfig({
jshint: {
files: [
"Gruntfile.js",
"index.js",
"test/**/*.js"
]
},
mochaTest: {
files: [ 'test/**/*.js' ]
},
mochaTestConfig: {
options: {
reporter: 'spec',
ui: 'tdd'
}
}
});

grunt.loadNpmTasks("grunt-contrib-jshint");
grunt.loadNpmTasks('grunt-mocha-test');

grunt.registerTask("default", ["jshint", "mochaTest"]);

};
19 changes: 19 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var os = require('os');

var currentAddress = function() {
var networkInterfaces = os.networkInterfaces( );
for (var networkInterface in networkInterfaces) {
var iface = networkInterfaces[networkInterface];

for (var i = 0; i < iface.length; i++) {
var name = iface[i];
if (name.address !== '127.0.0.1' && name.family === 'IPv4' && !name.internal) {
return "http://" + name.address;
}
}
}
};

module.exports = {
currentAddress: currentAddress
};
23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "puresec-microservice-js",
"version": "0.1.0",
"description": "microservice utilities for the puresec eco system",
"main": "index.js",
"repository": {
"type": "git",
"url": "git+https://fhopeman@github.com/fhopeman/puresec-microservice-js.git"
},
"author": "Fabian Hoffmann",
"bugs": {
"url": "https://github.com/fhopeman/puresec-microservice-js/issues"
},
"homepage": "https://github.com/fhopeman/puresec-microservice-js#readme",
"devDependencies": {
"grunt": "~0.4.5",
"grunt-cli": "^0.1.13",
"grunt-contrib-jshint": "~0.11.1",
"grunt-mocha-test": "^0.12.7",
"mocha": "^2.2.4",
"sinon": "^1.14.1"
}
}
43 changes: 43 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var testee = require('../index');

var assert = require("assert");
var sinon = require("sinon");
var os = require("os");

describe("microservice utils", function(){

afterEach(function () {
os.networkInterfaces.restore();
});

it("should return current external IPv4 address", function(){
sinon.stub(os, 'networkInterfaces').returns(
{lo: [
{address: '127.0.0.1', family: 'IPv4', internal: true },
{address: '::1', family: 'IPv6', internal: true }
],
wlan0: [
{address: '192.168.0.7', family: 'IPv4', internal: false },
{address: 'someFancyAddress0', family: 'IPv6', internal: false },
{address: 'someFancyAddress2', family: 'IPv6', internal: false }
]}
);

assert.equal(testee.currentAddress(), "http://192.168.0.7");
});

it("shouldn't return network address if no external IPv4 is available", function(){
sinon.stub(os, 'networkInterfaces').returns(
{lo: [
{address: '127.0.0.1', family: 'IPv4', internal: true },
{address: '::1', family: 'IPv6', internal: true }
],
wlan0: [{address: 'someFancyAddress0', family: 'IPv6', internal: false },
{address: 'someFancyAddress2', family: 'IPv6', internal: false }
]}
);

assert.equal(testee.currentAddress(), undefined);
});

});

0 comments on commit f0efe52

Please sign in to comment.