forked from bluesmoon/boomerang
-
Notifications
You must be signed in to change notification settings - Fork 294
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Warn on window and document overrides (debug builds only)
- Loading branch information
Showing
8 changed files
with
279 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,4 @@ tests/page-templates/12-react/support/app.js | |
*.#* | ||
*#* | ||
*~ | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<%= header %> | ||
<%= boomerangSnippet %> | ||
|
||
<!-- | ||
this file will override the following methods: | ||
`EventTarget.prototype.addEventListener` | ||
`XMLHttpRequest.prototype.open` | ||
--> | ||
<script src="09-overrides.js" type="text/javascript"></script> | ||
|
||
<script> | ||
BOOMR_test.init({ | ||
testAfterOnBeacon: true | ||
}); | ||
</script> | ||
<%= footer %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/*eslint-env mocha*/ | ||
/*global BOOMR_test,assert*/ | ||
|
||
describe("e2e/00-basic/09-overrides", function() { | ||
var tf = BOOMR.plugins.TestFramework; | ||
var windowUnderTest = window; | ||
|
||
it("Should have sent a beacon", function() { | ||
assert.isTrue(tf.fired_onbeacon); | ||
}); | ||
|
||
// if window is already hijacked (phantomjs, i'm looking at you), punt | ||
if (!((typeof document.all) !== "function" && (typeof document.all) === "function")) { | ||
describe("clean window", function() { | ||
it("Should return an empty array", function() { | ||
var overrides = BOOMR.checkWindowOverrides(windowUnderTest); | ||
assert.isTrue(BOOMR.utils.isArray(overrides)); | ||
assert.lengthOf(overrides, 0); | ||
}); | ||
}); | ||
|
||
if (typeof Object.defineProperty === "function") { | ||
describe("window with overrides", function() { | ||
var _ = {}; | ||
var testMethods = [ | ||
"EventTarget.prototype.addEventListener", | ||
"XMLHttpRequest.prototype.open" | ||
]; | ||
before(function() { | ||
BOOMR.utils.forEach(testMethods, function(method) { | ||
_[method] = eval(method); // eslint-disable-line no-eval | ||
eval(method + " = function() {};"); // eslint-disable-line no-eval | ||
}); | ||
}); | ||
after(function() { | ||
BOOMR.utils.forEach(testMethods, function(method) { | ||
eval(method + " = _[method]"); // eslint-disable-line no-eval | ||
}); | ||
}); | ||
it("Should identify non-native methods found starting at `window`", function() { | ||
var overrides = BOOMR.checkWindowOverrides(windowUnderTest); | ||
assert.isTrue(BOOMR.utils.isArray(overrides)); | ||
assert.lengthOf(overrides, testMethods.length); | ||
assert.includeMembers(overrides, testMethods); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
// if document is already hijacked (phantomjs, i'm looking at you), punt | ||
if (!document.hasOwnProperty("readyState")) { | ||
describe("clean document", function() { | ||
it("Should return an empty array", function() { | ||
var overrides = BOOMR.checkDocumentOverrides(document); | ||
assert.isTrue(BOOMR.utils.isArray(overrides)); | ||
assert.lengthOf(overrides, 0); | ||
}); | ||
}); | ||
|
||
describe("document with overrides", function() { | ||
var _ = {}; | ||
before(function() { | ||
BOOMR.utils.forEach(["readyState", "domain", "hidden", "URL", "cookie"], function(prop) { | ||
_[prop] = document[prop]; | ||
Object.defineProperty(document, prop, { | ||
value: "foo" | ||
}); | ||
}); | ||
}); | ||
after(function() { | ||
BOOMR.utils.forEach(Object.keys(_), function(prop) { | ||
document[prop] = _[prop]; | ||
}); | ||
}); | ||
it("Should identify non-native properties on `document`", function() { | ||
var overrides = BOOMR.checkDocumentOverrides(document); | ||
assert.isTrue(BOOMR.utils.isArray(overrides)); | ||
assert.lengthOf(overrides, Object.keys(_).length); | ||
assert.includeMembers(overrides, Object.keys(_)); | ||
}); | ||
}); | ||
} | ||
|
||
}); |
2 changes: 1 addition & 1 deletion
2
tests/unit/02-array-filter.js → tests/unit/02-utils-arrayfilter.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/*eslint-env mocha*/ | ||
/*global chai*/ | ||
|
||
describe("BOOMR.utils.forEach()", function() { | ||
var assert = chai.assert; | ||
|
||
it("Should handle degenerate cases", function() { | ||
assert.doesNotThrow(function() { | ||
BOOMR.utils.forEach(); | ||
BOOMR.utils.forEach(null); | ||
BOOMR.utils.forEach(true); | ||
BOOMR.utils.forEach(false); | ||
BOOMR.utils.forEach(0); | ||
BOOMR.utils.forEach(1); | ||
BOOMR.utils.forEach(123); | ||
BOOMR.utils.forEach(""); | ||
BOOMR.utils.forEach({}); | ||
}); | ||
}); | ||
|
||
it("Should not call the callback when the array is empty", function(done) { | ||
BOOMR.utils.forEach([], function() { | ||
done(new Error("how dare you")); | ||
}); | ||
done(); | ||
}); | ||
|
||
it("Should have no return value", function() { | ||
var returnValue = BOOMR.utils.forEach([1, 2, 3], function() {}); | ||
assert.isUndefined(returnValue); | ||
}); | ||
|
||
it("Should iterate over an array", function() { | ||
var expected = [1, 2, 3]; | ||
var actual = []; | ||
BOOMR.utils.forEach(expected, function(i) { | ||
actual.push(i); | ||
}); | ||
assert.sameMembers(expected, actual); | ||
}); | ||
|
||
it("Should use the correct context on the callback", function(done) { | ||
function Obj(value) { | ||
this.value = value; | ||
} | ||
Obj.prototype.check = function() { | ||
assert.strictEqual(this.value, 123); | ||
done(); | ||
}; | ||
BOOMR.utils.forEach([0], Obj.prototype.check, new Obj(123)); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters