Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit c357b1a

Browse files
test(Angular): refactor auto bootstrap tests
1 parent 603b66e commit c357b1a

File tree

1 file changed

+61
-66
lines changed

1 file changed

+61
-66
lines changed

test/AngularSpec.js

+61-66
Original file line numberDiff line numberDiff line change
@@ -1708,83 +1708,78 @@ describe('angular', function() {
17081708
dealoc(appElement);
17091709
});
17101710

1711-
it('should bootstrap from an extension into an extension document for same-origin documents only', function() {
1712-
// IE does not support `document.currentScript` (nor extensions with protocol), so skip test.
1713-
if (msie) return;
1714-
1715-
// Extension URLs are browser-specific, so we must choose a scheme that is supported by the browser to make
1716-
// sure that the URL is properly parsed.
1717-
var extensionScheme;
1718-
var userAgent = window.navigator.userAgent;
1719-
if (/Firefox\//.test(userAgent)) {
1720-
extensionScheme = 'moz-extension';
1721-
} else if (/Edge\//.test(userAgent)) {
1722-
extensionScheme = 'ms-browser-extension';
1723-
} else if (/Chrome\//.test(userAgent)) {
1724-
extensionScheme = 'chrome-extension';
1725-
} else if (/Safari\//.test(userAgent)) {
1726-
extensionScheme = 'safari-extension';
1727-
} else {
1728-
extensionScheme = 'browserext'; // Upcoming standard scheme.
1729-
}
1711+
// IE does not support `document.currentScript` (nor extensions with protocol), so skip tests.
1712+
if (!msie) {
1713+
describe('auto bootstrap restrictions', function() {
17301714

1731-
var src = extensionScheme + '://something';
1732-
// Fake a minimal document object (the actual document.currentScript is readonly).
1733-
var fakeDoc = {
1734-
currentScript: { getAttribute: function() { return src; } },
1735-
location: {protocol: extensionScheme + ':', origin: extensionScheme + '://something'},
1736-
createElement: document.createElement.bind(document)
1737-
};
1738-
expect(allowAutoBootstrap(fakeDoc)).toBe(true);
1715+
function createFakeDoc(attrs, protocol, currentScript) {
17391716

1740-
src = extensionScheme + '://something-else';
1741-
expect(allowAutoBootstrap(fakeDoc)).toBe(false);
1742-
});
1717+
protocol = protocol || 'http:';
1718+
var origin = protocol + '//something';
17431719

1744-
it('should bootstrap from a script with an empty or missing `src` attribute', function() {
1745-
// IE does not support `document.currentScript` (nor extensions with protocol), so skip test.
1746-
if (msie) return;
1720+
if (currentScript === undefined) {
1721+
currentScript = document.createElement('script');
1722+
Object.keys(attrs).forEach(function(key) { currentScript.setAttribute(key, attrs[key]); });
1723+
}
17471724

1748-
// Fake a minimal document object (the actual document.currentScript is readonly).
1749-
var src;
1750-
var fakeDoc = {
1751-
createElement: document.createElement.bind(document),
1752-
currentScript: {getAttribute: function() { return src; }},
1753-
location: {origin: 'some-value', protocol: 'http:'}
1754-
};
1725+
// Fake a minimal document object (the actual document.currentScript is readonly).
1726+
return {
1727+
currentScript: currentScript,
1728+
location: {protocol: protocol, origin: origin},
1729+
createElement: document.createElement.bind(document)
1730+
};
1731+
}
17551732

1756-
src = null;
1757-
expect(allowAutoBootstrap(fakeDoc)).toBe(true);
1733+
it('should bootstrap from an extension into an extension document for same-origin documents only', function() {
1734+
1735+
// Extension URLs are browser-specific, so we must choose a scheme that is supported by the browser to make
1736+
// sure that the URL is properly parsed.
1737+
var protocol;
1738+
var userAgent = window.navigator.userAgent;
1739+
if (/Firefox\//.test(userAgent)) {
1740+
protocol = 'moz-extension:';
1741+
} else if (/Edge\//.test(userAgent)) {
1742+
protocol = 'ms-browser-extension:';
1743+
} else if (/Chrome\//.test(userAgent)) {
1744+
protocol = 'chrome-extension:';
1745+
} else if (/Safari\//.test(userAgent)) {
1746+
protocol = 'safari-extension:';
1747+
} else {
1748+
protocol = 'browserext:'; // Upcoming standard scheme.
1749+
}
17581750

1759-
src = '';
1760-
expect(allowAutoBootstrap(fakeDoc)).toBe(true);
1761-
});
1751+
expect(allowAutoBootstrap(createFakeDoc({src: protocol + '//something'}, protocol))).toBe(true);
1752+
expect(allowAutoBootstrap(createFakeDoc({src: protocol + '//something-else'}, protocol))).toBe(false);
1753+
});
17621754

1763-
it('should not bootstrap from an extension into a non-extension document', function() {
1764-
// IE does not support `document.currentScript` (nor extensions with protocol), so skip test.
1765-
if (msie) return;
1755+
it('should bootstrap from a script with empty or no source (e.g. src, href or xlink:href attributes)', function() {
17661756

1767-
var src = 'resource://something';
1768-
// Fake a minimal document object (the actual document.currentScript is readonly).
1769-
var fakeDoc = {
1770-
currentScript: { getAttribute: function() { return src; } },
1771-
location: {protocol: 'http:'},
1772-
createElement: document.createElement.bind(document)
1773-
};
1774-
expect(allowAutoBootstrap(fakeDoc)).toBe(false);
1757+
expect(allowAutoBootstrap(createFakeDoc({src: null}))).toBe(true);
1758+
expect(allowAutoBootstrap(createFakeDoc({src: ''}))).toBe(true);
17751759

1776-
src = 'file://whatever';
1777-
expect(allowAutoBootstrap(fakeDoc)).toBe(true);
1778-
});
1760+
expect(allowAutoBootstrap(createFakeDoc({href: null}))).toBe(true);
1761+
expect(allowAutoBootstrap(createFakeDoc({href: ''}))).toBe(true);
17791762

1780-
it('should not bootstrap if bootstrapping is disabled', function() {
1781-
isAutoBootstrapAllowed = false;
1782-
angularInit(jqLite('<div ng-app></div>')[0], bootstrapSpy);
1783-
expect(bootstrapSpy).not.toHaveBeenCalled();
1784-
isAutoBootstrapAllowed = true;
1785-
});
1786-
});
1763+
expect(allowAutoBootstrap(createFakeDoc({'xlink:href': null}))).toBe(true);
1764+
expect(allowAutoBootstrap(createFakeDoc({'xlink:href': ''}))).toBe(true);
1765+
});
1766+
1767+
1768+
it('should not bootstrap from an extension into a non-extension document', function() {
17871769

1770+
expect(allowAutoBootstrap(createFakeDoc({src: 'resource://something'}))).toBe(false);
1771+
expect(allowAutoBootstrap(createFakeDoc({src: 'file://whatever'}))).toBe(true);
1772+
});
1773+
1774+
it('should not bootstrap if bootstrapping is disabled', function() {
1775+
isAutoBootstrapAllowed = false;
1776+
angularInit(jqLite('<div ng-app></div>')[0], bootstrapSpy);
1777+
expect(bootstrapSpy).not.toHaveBeenCalled();
1778+
isAutoBootstrapAllowed = true;
1779+
});
1780+
});
1781+
}
1782+
});
17881783

17891784
describe('AngularJS service', function() {
17901785
it('should override services', function() {

0 commit comments

Comments
 (0)