mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-17 15:40:22 +00:00
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
37 lines
1.4 KiB
JavaScript
37 lines
1.4 KiB
JavaScript
'use strict';
|
|
|
|
describe('urlUtils', function() {
|
|
describe('parse', function() {
|
|
it('should normalize a relative url', function () {
|
|
expect(urlResolve("foo").href).toMatch(/^https?:\/\/[^/]+\/foo$/);
|
|
});
|
|
|
|
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.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($document) {
|
|
function expectIsSameOrigin(url, expectedValue) {
|
|
expect(urlIsSameOrigin(url)).toBe(expectedValue);
|
|
expect(urlIsSameOrigin(urlResolve(url, true))).toBe(expectedValue);
|
|
}
|
|
expectIsSameOrigin('path', true);
|
|
var origin = urlResolve($document[0].location.href, true);
|
|
expectIsSameOrigin('//' + origin.host + '/path', true);
|
|
// Different domain.
|
|
expectIsSameOrigin('http://example.com/path', false);
|
|
// Auto fill protocol.
|
|
expectIsSameOrigin('//example.com/path', false);
|
|
// Should not match when the ports are different.
|
|
// This assumes that the test is *not* running on port 22 (very unlikely).
|
|
expectIsSameOrigin('//' + origin.hostname + ':22/path', false);
|
|
}));
|
|
});
|
|
});
|