diff options
| -rw-r--r-- | src/services.js | 4 | ||||
| -rw-r--r-- | test/servicesSpec.js | 72 |
2 files changed, 53 insertions, 23 deletions
diff --git a/src/services.js b/src/services.js index a8004dd1..70d774a9 100644 --- a/src/services.js +++ b/src/services.js @@ -1,4 +1,4 @@ -var URL_MATCH = /^(file|ftp|http|https):\/\/(\w+:{0,1}\w*@)?([\w\.-]*)(:([0-9]+))?([^\?#]+)(\?([^#]*))?(#(.*))?$/, +var URL_MATCH = /^(file|ftp|http|https):\/\/(\w+:{0,1}\w*@)?([\w\.-]*)(:([0-9]+))?(\/[^\?#]*)?(\?([^#]*))?(#(.*))?$/, HASH_MATCH = /^([^\?]*)?(\?([^\?]*))?$/, DEFAULT_PORTS = {'http': 80, 'https': 443, 'ftp':21}, EAGER = 'eager', @@ -184,7 +184,7 @@ angularServiceInject("$location", function(browser) { loc.protocol = match[1]; loc.host = match[3] || ''; loc.port = match[5] || DEFAULT_PORTS[loc.protocol] || _null; - loc.path = match[6]; + loc.path = match[6] || ''; loc.search = parseKeyValue(match[8]); loc.hash = match[10] || ''; diff --git a/test/servicesSpec.js b/test/servicesSpec.js index cf45becb..1f30b969 100644 --- a/test/servicesSpec.js +++ b/test/servicesSpec.js @@ -128,19 +128,6 @@ describe("service", function(){ expect($browser.getUrl()).toEqual('http://www.angularjs.org/a/b'); }); - it('should parse file://', function(){ - scope.$location.update('file:///Users/Shared/misko/work/angular.js/scenario/widgets.html'); - expect(scope.$location.href).toEqual("file:///Users/Shared/misko/work/angular.js/scenario/widgets.html"); - expect(scope.$location.protocol).toEqual("file"); - expect(scope.$location.host).toEqual(""); - expect(scope.$location.port).toEqual(null); - expect(scope.$location.path).toEqual("/Users/Shared/misko/work/angular.js/scenario/widgets.html"); - expect(scope.$location.search).toEqual({}); - expect(scope.$location.hash).toEqual(''); - expect(scope.$location.hashPath).toEqual(''); - expect(scope.$location.hashSearch).toEqual({}); - }); - it('should update hashPath and hashSearch on hash update', function(){ scope.$location.update('http://server/#path?a=b'); scope.$eval(); @@ -178,14 +165,6 @@ describe("service", function(){ expect(scope.$location.hash).toEqual(''); }); - it("should parse url which contains - in host", function(){ - scope.$location.update('http://a-b1.c-d.09/path'); - expect(scope.$location.href).toEqual('http://a-b1.c-d.09/path'); - expect(scope.$location.protocol).toEqual('http'); - expect(scope.$location.host).toEqual('a-b1.c-d.09'); - expect(scope.$location.path).toEqual('/path'); - }); - it('should update hash before any processing', function(){ var scope = compile('<div>'); var log = ''; @@ -582,4 +561,55 @@ describe("service", function(){ }); }); + + + describe('URL_MATCH', function() { + + it('should parse basic url', function() { + var match = URL_MATCH.exec('http://www.angularjs.org/path?search#hash?x=x'); + + expect(match[1]).toEqual('http'); + expect(match[3]).toEqual('www.angularjs.org'); + expect(match[6]).toEqual('/path'); + expect(match[8]).toEqual('search'); + expect(match[10]).toEqual('hash?x=x'); + }); + + it('should parse file://', function(){ + var match = URL_MATCH.exec('file:///Users/Shared/misko/work/angular.js/scenario/widgets.html'); + + expect(match[1]).toEqual('file'); + expect(match[3]).toEqual(''); + expect(match[5]).toEqual(null); + expect(match[6]).toEqual('/Users/Shared/misko/work/angular.js/scenario/widgets.html'); + expect(match[8]).not.toBeDefined(); + }); + + it('should parse url with "-" in host', function(){ + var match = URL_MATCH.exec('http://a-b1.c-d.09/path'); + + expect(match[1]).toEqual('http'); + expect(match[3]).toEqual('a-b1.c-d.09'); + expect(match[5]).toEqual(null); + expect(match[6]).toEqual('/path'); + expect(match[8]).not.toBeDefined(); + }); + + it('should parse host without "/" at the end', function() { + var match = URL_MATCH.exec('http://host.org'); + expect(match[3]).toEqual('host.org'); + + match = URL_MATCH.exec('http://host.org#'); + expect(match[3]).toEqual('host.org'); + + match = URL_MATCH.exec('http://host.org?'); + expect(match[3]).toEqual('host.org'); + }); + + it('should match with just "/" path', function() { + var match = URL_MATCH.exec('http://server/#?book=moby'); + + expect(match[10]).toEqual('?book=moby'); + }); + }); }); |
