diff --git a/demo/app.js b/demo/app.js index a6a6067..da88c39 100644 --- a/demo/app.js +++ b/demo/app.js @@ -31,10 +31,6 @@ angular.module('acme', ['environment']). } } }); - - // run the environment check, so the comprobation is made - // before controllers and services are built - envServiceProvider.check(); }). controller('Pages', ['$scope', 'envService', function($scope, envService) { $scope.environment = envService.get(); // store the current environment diff --git a/dist/angular-environment.js b/dist/angular-environment.js index 170dc34..f2032e8 100644 --- a/dist/angular-environment.js +++ b/dist/angular-environment.js @@ -24,6 +24,9 @@ angular.module('environment', []). return new RegExp(local.pregQuote(string).replace(/\\\*/g, '.*').replace(/\\\?/g, '.'), 'g'); }; + local.hasChecked = false; // true iff the check() function has been called at least once + local.host = undefined; // the host URL of the current page + this.environment = 'development'; // default this.data = {}; // user defined environments data @@ -98,7 +101,7 @@ angular.module('environment', []). */ this.check = function() { var self = this, - location = window.location.host, + location = local.host ? local.host : window.location.host, matches = [], keepGoing = true; @@ -123,9 +126,16 @@ angular.module('environment', []). self.environment = v.environment; } }); + + local.hasChecked = true; }; - this.$get = function() { + this.$get = function($location) { + if (!local.hasChecked) { + local.host = $location.host(); + this.check(); + } return this; }; + this.$get.$inject = ['$location']; }); diff --git a/dist/angular-environment.min.js b/dist/angular-environment.min.js index 7778b81..ffe32af 100644 --- a/dist/angular-environment.min.js +++ b/dist/angular-environment.min.js @@ -1,5 +1,6 @@ -angular.module('environment',[]).provider('envService',function(){'use strict';var local={};local.pregQuote=function(string,delimiter){return(string+'').replace(new RegExp('[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\'+(delimiter||'')+'-]','g'),'\\$&');};local.stringToRegex=function(string){return new RegExp(local.pregQuote(string).replace(/\\\*/g,'.*').replace(/\\\?/g,'.'),'g');};this.environment='development';this.data={};this.config=function(config){this.data=config;};this.set=function(environment){this.environment=environment;};this.get=function(){return this.environment;};this.read=function(variable){if(typeof variable==='undefined'||variable===''||variable==='all'){return this.data.vars[this.get()];} +angular.module('environment',[]).provider('envService',function(){'use strict';var local={};local.pregQuote=function(string,delimiter){return(string+'').replace(new RegExp('[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\'+(delimiter||'')+'-]','g'),'\\$&');};local.stringToRegex=function(string){return new RegExp(local.pregQuote(string).replace(/\\\*/g,'.*').replace(/\\\?/g,'.'),'g');};local.hasChecked=false;local.host=undefined;this.environment='development';this.data={};this.config=function(config){this.data=config;};this.set=function(environment){this.environment=environment;};this.get=function(){return this.environment;};this.read=function(variable){if(typeof variable==='undefined'||variable===''||variable==='all'){return this.data.vars[this.get()];} else if(typeof this.data.vars[this.get()][variable]==='undefined'){return this.data.vars.defaults[variable];} -return this.data.vars[this.get()][variable];};this.is=function(environment){return(environment===this.environment);};this.check=function(){var self=this,location=window.location.host,matches=[],keepGoing=true;angular.forEach(this.data.domains,function(v,k){angular.forEach(v,function(v){if(location.match(local.stringToRegex(v))){matches.push({environment:k,domain:v});}});});angular.forEach(matches,function(v,k){if(keepGoing){if(location===v.domain){keepGoing=false;} -void 0;self.environment=v.environment;}});};this.$get=function(){return this;};}); \ No newline at end of file +return this.data.vars[this.get()][variable];};this.is=function(environment){return(environment===this.environment);};this.check=function(){var self=this,location=local.host?local.host:window.location.host,matches=[],keepGoing=true;angular.forEach(this.data.domains,function(v,k){angular.forEach(v,function(v){if(location.match(local.stringToRegex(v))){matches.push({environment:k,domain:v});}});});angular.forEach(matches,function(v,k){if(keepGoing){if(location===v.domain){keepGoing=false;} +void 0;self.environment=v.environment;}});local.hasChecked=true;};this.$get=function($location){if(!local.hasChecked){local.host=$location.host();this.check();} +return this;};this.$get.$inject=['$location'];}); \ No newline at end of file diff --git a/src/angular-environment.js b/src/angular-environment.js index ba4379c..6036b04 100644 --- a/src/angular-environment.js +++ b/src/angular-environment.js @@ -24,6 +24,9 @@ angular.module('environment', []). return new RegExp(local.pregQuote(string).replace(/\\\*/g, '.*').replace(/\\\?/g, '.'), 'g'); }; + local.hasChecked = false; // true iff the check() function has been called at least once + local.host = undefined; // the host URL of the current page + this.environment = 'development'; // default this.data = {}; // user defined environments data @@ -98,7 +101,7 @@ angular.module('environment', []). */ this.check = function() { var self = this, - location = window.location.host, + location = local.host ? local.host : window.location.host, matches = [], keepGoing = true; @@ -123,9 +126,16 @@ angular.module('environment', []). self.environment = v.environment; } }); + + local.hasChecked = true; }; - this.$get = function() { + this.$get = function($location) { + if (!local.hasChecked) { + local.host = $location.host(); + this.check(); + } return this; }; + this.$get.$inject = ['$location']; }); diff --git a/test/angular-environment.js b/test/angular-environment.js index 666745f..4640af3 100644 --- a/test/angular-environment.js +++ b/test/angular-environment.js @@ -4,6 +4,7 @@ describe('Test environment provider', function () { var theEnvServiceProvider; var envService; + var $locationMock; beforeEach(function () { // Initialise the provider by injecting it into a fake module's @@ -15,21 +16,39 @@ describe('Test environment provider', function () { // Initialise the test.app injector. module('environment', 'test.app.environment'); inject(function () {}); - envService = theEnvServiceProvider.$get(); + }); + + describe('with development environment by default', function () { + beforeEach(function () { + $locationMock = { + host: function () { + return 'localhost'; + } + }; + spyOn($locationMock, 'host').and.callThrough(); - envService.config({ - vars: { - development: { - backend: 'https://backend-dev/' + theEnvServiceProvider.config({ + domains: { + development: ['localhost'], + production: ['app.*.com'] }, - production: { - backend: 'https://backend/' + vars: { + development: { + backend: 'https://backend-dev/' + }, + production: { + backend: 'https://backend/' + } } - } + }); + + envService = theEnvServiceProvider.$get($locationMock); + }); + + it('should call $locationMock.host to obtain host URL', function () { + expect($locationMock.host).toHaveBeenCalledTimes(1); }); - }); - describe('with envService provider', function () { it('tests the envService provider can be created', function () { expect(theEnvServiceProvider).toBeDefined(); }); @@ -53,4 +72,56 @@ describe('Test environment provider', function () { expect(envService.read('backend')).toBe('https://backend/'); }); }); + + describe('tests matching domains', function () { + var i = 0, testCases = [ + [true, 'app.web.com', 'app.*.com'], + [true, 'app.a.com', 'app.*.com'], + [true, 'app.web.com', 'app.web.com'], + [true, 'app.web.com', '*.com'], + [true, 'a.com', '*.com'], + [true, 'app.web.com', 'app.*'], + [true, 'app.a', 'app.*'], + [false, 'a.w.com', 'app.web.com'] + ]; + + for (i = 0; i < testCases.length; i++) { + (function (expectedMatch, actualHost, domainPattern) { + it('should ' + (expectedMatch ? '' : 'not') + ' identify production environment' + + ' when host=' + actualHost + ' and domain pattern=' + domainPattern, + function () { + $locationMock = { + host: function () { + return actualHost; + } + }; + spyOn($locationMock, 'host').and.callThrough(); + + theEnvServiceProvider.config({ + domains: { + development: ['localhost'], + production: [domainPattern] + }, + vars: { + development: { + backend: 'https://backend-dev/' + }, + production: { + backend: 'https://backend/' + } + }, + defaults: { + development: {}, + production: {} + } + }); + + envService = theEnvServiceProvider.$get($locationMock); + + expect(envService.is('production')).toBe(expectedMatch); + expect($locationMock.host).toHaveBeenCalledTimes(1); + }); + })(testCases[i][0], testCases[i][1], testCases[i][2]); + } + }); });