diff options
| author | Jeff Cross | 2013-07-25 10:53:43 -0700 |
|---|---|---|
| committer | Igor Minar | 2013-10-10 11:41:07 -0700 |
| commit | b019a48bb1176f3c9ce29b628a321b6ffe873393 (patch) | |
| tree | e1b06b53d3806019b9386acd532153ea14ff7c26 /test | |
| parent | 74ef7f14a49662ebf389a515b86e6dd68e746811 (diff) | |
| download | angular.js-b019a48bb1176f3c9ce29b628a321b6ffe873393.tar.bz2 | |
refactor(location): $location now uses urlUtils, not RegEx
The location service, and other portions of the application,
were relying on a complicated regular expression to get parts of a URL.
But there is already a private urlUtils provider,
which relies on HTMLAnchorElement to provide this information,
and is suitable for most cases.
In order to make urlUtils more accessible in the absence of DI,
its methods were converted to standalone functions available globally.
The urlUtils.resolve method was renamed urlResolve,
and was refactored to only take 1 argument, url,
and not the 2nd "parse" boolean.
The method now always returns a parsed url.
All places in code which previously wanted a string instead of a parsed
url can now get the value from the href property of the returned object.
Tests were also added to ensure IPv6 addresses were handled correctly.
Closes #3533
Closes #2950
Closes #3249
Diffstat (limited to 'test')
| -rw-r--r-- | test/ng/httpBackendSpec.js | 16 | ||||
| -rw-r--r-- | test/ng/locationSpec.js | 65 | ||||
| -rw-r--r-- | test/ng/urlUtilsSpec.js | 24 |
3 files changed, 15 insertions, 90 deletions
diff --git a/test/ng/httpBackendSpec.js b/test/ng/httpBackendSpec.js index faec5737..f39e83ea 100644 --- a/test/ng/httpBackendSpec.js +++ b/test/ng/httpBackendSpec.js @@ -363,7 +363,7 @@ describe('$httpBackend', function() { it('should convert 0 to 200 if content', function() { - $backend = createHttpBackend($browser, MockXhr, null, null, null, 'http'); + $backend = createHttpBackend($browser, MockXhr); $backend('GET', 'file:///whatever/index.html', null, callback); respond(0, 'SOME CONTENT'); @@ -373,19 +373,8 @@ describe('$httpBackend', function() { }); - it('should convert 0 to 200 if content - relative url', function() { - $backend = createHttpBackend($browser, MockXhr, null, null, null, 'file'); - - $backend('GET', '/whatever/index.html', null, callback); - respond(0, 'SOME CONTENT'); - - expect(callback).toHaveBeenCalled(); - expect(callback.mostRecentCall.args[0]).toBe(200); - }); - - it('should convert 0 to 404 if no content', function() { - $backend = createHttpBackend($browser, MockXhr, null, null, null, 'http'); + $backend = createHttpBackend($browser, MockXhr); $backend('GET', 'file:///whatever/index.html', null, callback); respond(0, ''); @@ -404,7 +393,6 @@ describe('$httpBackend', function() { expect(callback).toHaveBeenCalled(); expect(callback.mostRecentCall.args[0]).toBe(404); }); - }); }); diff --git a/test/ng/locationSpec.js b/test/ng/locationSpec.js index a60f1609..30f2f81a 100644 --- a/test/ng/locationSpec.js +++ b/test/ng/locationSpec.js @@ -695,69 +695,6 @@ describe('$location', function() { }); }); - - describe('SERVER_MATCH', function() { - - it('should parse basic url', function() { - var match = SERVER_MATCH.exec('http://www.angularjs.org/path?search#hash?x=x'); - - expect(match[1]).toBe('http'); - expect(match[3]).toBe('www.angularjs.org'); - }); - - - it('should parse file://', function() { - var match = SERVER_MATCH.exec('file:///Users/Shared/misko/work/angular.js/scenario/widgets.html'); - - expect(match[1]).toBe('file'); - expect(match[3]).toBe(''); - expect(match[5]).toBeFalsy(); - }); - - - it('should parse url with "-" in host', function() { - var match = SERVER_MATCH.exec('http://a-b1.c-d.09/path'); - - expect(match[1]).toBe('http'); - expect(match[3]).toBe('a-b1.c-d.09'); - expect(match[5]).toBeFalsy(); - }); - - - it('should parse host without "/" at the end', function() { - var match = SERVER_MATCH.exec('http://host.org'); - expect(match[3]).toBe('host.org'); - - match = SERVER_MATCH.exec('http://host.org#'); - expect(match[3]).toBe('host.org'); - - match = SERVER_MATCH.exec('http://host.org?'); - expect(match[3]).toBe('host.org'); - }); - - - it('should parse chrome extension urls', function() { - var match = SERVER_MATCH.exec('chrome-extension://jjcldkdmokihdaomalanmlohibnoplog/index.html?foo#bar'); - - expect(match[1]).toBe('chrome-extension'); - expect(match[3]).toBe('jjcldkdmokihdaomalanmlohibnoplog'); - }); - - it('should parse FFOS app:// urls', function() { - var match = SERVER_MATCH.exec('app://{d0419af1-8b42-41c5-96f4-ef4179e52315}/path'); - - expect(match[1]).toBe('app'); - expect(match[3]).toBe('{d0419af1-8b42-41c5-96f4-ef4179e52315}'); - expect(match[5]).toBeFalsy(); - expect(match[6]).toBe('/path'); - expect(match[8]).toBeFalsy(); - - match = SERVER_MATCH.exec('app://}foo{') - expect(match).toBe(null); - }); - }); - - describe('PATH_MATCH', function() { it('should parse just path', function() { @@ -1327,7 +1264,7 @@ describe('$location', function() { ); - it('should listen on click events on href and prevent browser default in hashbang mode', function() { + it('should listen on click events on href and prevent browser default in hashbang mode', function() { module(function() { return function($rootElement, $compile, $rootScope) { $rootElement.html('<a href="http://server/#/somePath">link</a>'); diff --git a/test/ng/urlUtilsSpec.js b/test/ng/urlUtilsSpec.js index 3c9bf847..18675ed8 100644 --- a/test/ng/urlUtilsSpec.js +++ b/test/ng/urlUtilsSpec.js @@ -1,29 +1,29 @@ 'use strict'; -describe('$$urlUtils', function() { +describe('urlUtils', function() { describe('parse', function() { - it('should normalize a relative url', inject(function($$urlUtils) { - expect($$urlUtils.resolve("foo")).toMatch(/^https?:\/\/[^/]+\/foo$/); - })); + it('should normalize a relative url', function () { + expect(urlResolve("foo").href).toMatch(/^https?:\/\/[^/]+\/foo$/); + }); - it('should parse relative URL into component pieces', inject(function($$urlUtils) { - var parsed = $$urlUtils.resolve("foo", true); + it('should parse relative URL into component pieces', function () { + var parsed = urlResolve("foo"); expect(parsed.href).toMatch(/https?:\/\//); - expect(parsed.protocol).toMatch(/^https?:/); + expect(parsed.protocol).toMatch(/^https?/); expect(parsed.host).not.toBe(""); expect(parsed.hostname).not.toBe(""); expect(parsed.pathname).not.toBe(""); - })); + }); }); describe('isSameOrigin', function() { - it('should support various combinations of urls - both string and parsed', inject(function($$urlUtils, $document) { + it('should support various combinations of urls - both string and parsed', inject(function($document) { function expectIsSameOrigin(url, expectedValue) { - expect($$urlUtils.isSameOrigin(url)).toBe(expectedValue); - expect($$urlUtils.isSameOrigin($$urlUtils.resolve(url, true))).toBe(expectedValue); + expect(urlIsSameOrigin(url)).toBe(expectedValue); + expect(urlIsSameOrigin(urlResolve(url, true))).toBe(expectedValue); } expectIsSameOrigin('path', true); - var origin = $$urlUtils.resolve($document[0].location.href, true); + var origin = urlResolve($document[0].location.href, true); expectIsSameOrigin('//' + origin.host + '/path', true); // Different domain. expectIsSameOrigin('http://example.com/path', false); |
