devip.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. "use strict";
  2. var devIp = require("../lib/dev-ip");
  3. var respNone = require("./fixtures/resp-none");
  4. var respSingle = require("./fixtures/resp-single");
  5. var respMultiple = require("./fixtures/resp-multiple");
  6. var sinon = require("sinon");
  7. var assert = require("chai").assert;
  8. var os = require("os");
  9. // From the resp files
  10. var match1 = "10.104.103.181";
  11. var match2 = "10.104.100.12";
  12. var regex = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
  13. describe("Getting the IP with a single result", function () {
  14. var osStub;
  15. var result;
  16. before(function () {
  17. osStub = sinon.stub(os, "networkInterfaces").returns(respSingle);
  18. });
  19. beforeEach(function () {
  20. result = devIp(null);
  21. });
  22. after(function () {
  23. osStub.restore();
  24. });
  25. it("should return the IP when a single match found", function () {
  26. var expected = match1;
  27. assert.deepEqual(result, [expected]);
  28. });
  29. it("should return a string matching the regex", function () {
  30. var actual = regex.exec(result);
  31. assert.isNotNull(actual);
  32. });
  33. });
  34. describe("Getting the IP with Multiple results", function () {
  35. var osStub;
  36. var result;
  37. before(function () {
  38. osStub = sinon.stub(os, "networkInterfaces").returns(respMultiple);
  39. });
  40. beforeEach(function () {
  41. result = devIp(null);
  42. });
  43. after(function () {
  44. osStub.restore();
  45. });
  46. it("should return an array of results", function () {
  47. assert.equal(result[0], match1);
  48. assert.equal(result[1], match2);
  49. });
  50. it("should return a string matching the regex", function () {
  51. var actual = regex.exec(result[0]);
  52. assert.isNotNull(actual);
  53. actual = regex.exec(result[1]);
  54. assert.isNotNull(actual);
  55. });
  56. });
  57. describe("Getting the IP with no results", function () {
  58. var osStub;
  59. var result;
  60. before(function () {
  61. osStub = sinon.stub(os, "networkInterfaces").returns(respNone);
  62. });
  63. after(function () {
  64. osStub.restore();
  65. });
  66. it("should return empty array", function () {
  67. var actual = devIp();
  68. assert.isArray(actual);
  69. assert.equal(actual.length, 0);
  70. });
  71. });