Skip to content

Commit

Permalink
Fixes 00-basic/09-overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
nicjansma committed Apr 4, 2018
1 parent 94cfa61 commit 3201f02
Show file tree
Hide file tree
Showing 21 changed files with 185 additions and 96 deletions.
4 changes: 4 additions & 0 deletions boomerang.js
Original file line number Diff line number Diff line change
Expand Up @@ -2115,6 +2115,10 @@ BOOMR_check_doc_domain();
* @returns {Array} Array of overridden function names
*/
BOOMR.checkWindowOverrides = function(win) {
if (!Object.getOwnPropertyNames) {
return [];
}

var freshWindow, objects, overridden = [];
function setup() {
var iframe = d.createElement("iframe");
Expand Down
8 changes: 7 additions & 1 deletion tests/boomerang-test-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,11 @@
//
if (BOOMR.window && BOOMR.window.BOOMR_test_config) {
BOOMR.window.BOOMR_test.init(BOOMR.window.BOOMR_test_config);
delete BOOMR.window.BOOMR_test_config;

try {
delete BOOMR.window.BOOMR_test_config;
}
catch (e) {
// nop
}
}
17 changes: 14 additions & 3 deletions tests/page-template-snippets/header.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,24 @@
// Set RT Cookie to empty, preventing navigation related issues with session tests
document.cookie = "RT=\"\";domain=.<%= mainServer %>;path=/";
// Prevent Boomerang from setting a cookie on unload so as to prevent cookies to show up when we load the next E2E test
window.addEventListener("beforeunload", function (e) {
function preventCookie(e) {
if (BOOMR) {
BOOMR.disable();
}
e.preventDefault();
});
if (e) {
e.preventDefault();
}
}
if (window.addEventListener) {
window.addEventListener("beforeunload", preventCookie);
} else {
window.onbeforeunload = preventCookie;
}
// approx number of lines in the header
window.HEADER_LINES = 55;
</script>
<script src="../../vendor/mocha/mocha.js"></script>
Expand Down
184 changes: 126 additions & 58 deletions tests/page-templates/00-basic/09-overrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,80 +5,148 @@ describe("e2e/00-basic/09-overrides", function() {
var tf = BOOMR.plugins.TestFramework;
var windowUnderTest = window;

// overrides which are commonly seen in browsers or extensions
var KNOWN_OVERRIDES = [
"webkitRTCPeerConnection.prototype.setConfiguration", // Chrome
"RTCPeerConnection.prototype.setConfiguration", // Chrome
"WebAssembly.Module", // Edge
"WebAssembly.Instance" // Edge
];

function cleanOverridesList(overrides) {
var result = [];

for (var i = 0; i < overrides.length; i++) {
if (overrides[i].indexOf("content.") === 0) {
// seen in Firefox
continue;
}

if (KNOWN_OVERRIDES.indexOf(overrides[i]) === -1) {
result.push(overrides[i]);
}
}

return result;
}

it("Should have sent a beacon", function() {
assert.isTrue(tf.fired_onbeacon);
});

var hasGetOwnPropertyName = Object.getOwnPropertyNames;
var hasReadyState = document.hasOwnProperty && !document.hasOwnProperty("readyState");
var hasDocumentAll = !((typeof document.all) !== "function" && (typeof document.all) === "function");

// 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);
describe("clean window", function() {
it("Should return an empty array", function() {
if (!hasGetOwnPropertyName || !hasDocumentAll) {
return this.skip();
}

var overrides = cleanOverridesList(BOOMR.checkWindowOverrides(windowUnderTest));

assert.isTrue(BOOMR.utils.isArray(overrides));
assert.lengthOf(overrides, 0);
});
});

describe("window with overrides", function() {
var _ = {};
var testMethods = [
"EventTarget.prototype.addEventListener",
"XMLHttpRequest.prototype.open"
];

var validTestMethods = [];

before(function() {
if (!hasGetOwnPropertyName || !hasDocumentAll || !Object.defineProperty) {
return;
}

BOOMR.utils.forEach(testMethods, function(method) {
try {
_[method] = eval(method); // eslint-disable-line no-eval
eval(method + " = function() {};"); // eslint-disable-line no-eval
validTestMethods.push(method);
}
catch (e) {
// ignore
}
});
});

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);
});
after(function() {
if (!hasGetOwnPropertyName || !hasDocumentAll || !Object.defineProperty) {
return;
}

BOOMR.utils.forEach(validTestMethods, function(method) {
eval(method + " = _[method]"); // eslint-disable-line no-eval
});
}
}
});

it("Should identify non-native methods found starting at `window`", function() {
if (!hasGetOwnPropertyName || !hasDocumentAll || !Object.defineProperty) {
return this.skip();
}

var overrides = cleanOverridesList(BOOMR.checkWindowOverrides(windowUnderTest));
assert.isTrue(BOOMR.utils.isArray(overrides));
assert.lengthOf(overrides, validTestMethods.length);
assert.includeMembers(overrides, validTestMethods);
});
});

// 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("clean document", function() {
it("Should return an empty array", function() {
if (!hasGetOwnPropertyName || !hasReadyState) {
return this.skip();
}

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];
describe("document with overrides", function() {
var _ = {};
before(function() {
if (!hasGetOwnPropertyName || !hasReadyState) {
return;
}

BOOMR.utils.forEach(["readyState", "domain", "hidden", "URL", "cookie"], function(prop) {
_[prop] = document[prop];
Object.defineProperty(document, prop, {
value: "foo"
});
});
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(_));
});

after(function() {
if (!hasGetOwnPropertyName || !hasReadyState) {
return;
}

BOOMR.utils.forEach(Object.keys(_), function(prop) {
document[prop] = _[prop];
});
});
}

it("Should identify non-native properties on `document`", function() {
if (!hasGetOwnPropertyName || !hasReadyState) {
return this.skip();
}

var overrides = BOOMR.checkDocumentOverrides(document);
assert.isTrue(BOOMR.utils.isArray(overrides));
assert.lengthOf(overrides, Object.keys(_).length);
assert.includeMembers(overrides, Object.keys(_));
});
});
});
4 changes: 2 additions & 2 deletions tests/page-templates/14-errors/00-send.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ describe("e2e/14-errors/00-send", function() {
}
});

it("Should have lineNumber ~ 51", function() {
it("Should have lineNumber ~ " + (HEADER_LINES + 3), function() {
var b = tf.lastBeacon();
var err = BOOMR.plugins.Errors.decompressErrors(C.jsUrlDecompress(b.err))[0];

if (err.lineNumber) {
assert.closeTo(err.lineNumber, 51, 5);
assert.closeTo(err.lineNumber, HEADER_LINES + 3, 5);
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ describe("e2e/14-errors/02-send-after-onload-enabled", function() {
}
});

it("Should have lineNumber ~ 51", function() {
it("Should have lineNumber ~ " + (HEADER_LINES + 3), function() {
var b = tf.lastBeacon();
var err = BOOMR.plugins.Errors.decompressErrors(C.jsUrlDecompress(b.err))[0];

if (err.lineNumber) {
assert.closeTo(err.lineNumber, 51, 5);
assert.closeTo(err.lineNumber, HEADER_LINES + 3, 5);
}
});
});
4 changes: 2 additions & 2 deletions tests/page-templates/14-errors/04-dupe.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ describe("e2e/14-errors/04-dupe", function() {
}
});

it("Should have lineNumber ~ 51", function() {
it("Should have lineNumber ~ " + (HEADER_LINES + 3), function() {
var b = tf.lastBeacon();
var err = BOOMR.plugins.Errors.decompressErrors(C.jsUrlDecompress(b.err))[0];

if (err.lineNumber) {
assert.closeTo(err.lineNumber, 51, 5);
assert.closeTo(err.lineNumber, HEADER_LINES + 3, 5);
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ describe("e2e/14-errors/05-send-after-onload-multiple", function() {
}
});

it("Should have lineNumber ~ 52", function() {
it("Should have lineNumber ~ " + (HEADER_LINES + 4), function() {
var b = tf.lastBeacon();
var errs = BOOMR.plugins.Errors.decompressErrors(C.jsUrlDecompress(b.err));

for (var i = 0; i < errs.length; i++) {
var err = errs[i];

if (err.lineNumber) {
assert.closeTo(err.lineNumber, 52, 5);
assert.closeTo(err.lineNumber, HEADER_LINES + 4, 5);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ describe("e2e/14-errors/06-send-after-onload-dupe-of-load", function() {
}
});

it("Should have lineNumber ~ 51", function() {
it("Should have lineNumber ~ " + (HEADER_LINES + 3), function() {
var b = tf.lastBeacon();
var err = BOOMR.plugins.Errors.decompressErrors(C.jsUrlDecompress(b.err))[0];

if (err.lineNumber) {
assert.closeTo(err.lineNumber, 51, 5);
assert.closeTo(err.lineNumber, HEADER_LINES + 3, 5);
}
});
});
4 changes: 2 additions & 2 deletions tests/page-templates/14-errors/07-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ describe("e2e/14-errors/07-global", function() {
}
});

it("Should have lineNumber ~ 52", function() {
it("Should have lineNumber ~ " + (HEADER_LINES + 4), function() {
var b = tf.lastBeacon();
var err = BOOMR.plugins.Errors.decompressErrors(C.jsUrlDecompress(b.err))[0];

if (err.lineNumber) {
assert.closeTo(err.lineNumber, 52, 5);
assert.closeTo(err.lineNumber, HEADER_LINES + 4, 5);
}
});
});
4 changes: 2 additions & 2 deletions tests/page-templates/14-errors/09-console.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ describe("e2e/14-errors/09-console", function() {
}
});

it("Should have lineNumber ~ 59 for the first error", function() {
it("Should have lineNumber ~ " + (HEADER_LINES + 11) + " for the first error", function() {
var b = tf.lastBeacon();
var err = BOOMR.plugins.Errors.decompressErrors(C.jsUrlDecompress(b.err))[0];

if (err.lineNumber) {
assert.closeTo(err.lineNumber, 59, 5);
assert.closeTo(err.lineNumber, HEADER_LINES + 11, 5);
}
});

Expand Down
4 changes: 2 additions & 2 deletions tests/page-templates/14-errors/10-events-window.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ describe("e2e/14-errors/10-events-window", function() {
}
});

it("Should have lineNumber ~ 70", function() {
it("Should have lineNumber ~ " + (HEADER_LINES + 25), function() {
var b = tf.lastBeacon();
var err = BOOMR.plugins.Errors.decompressErrors(C.jsUrlDecompress(b.err))[0];

if (err.lineNumber) {
assert.closeTo(err.lineNumber, 70, 10);
assert.closeTo(err.lineNumber, HEADER_LINES + 25, 5);
}
});

Expand Down
4 changes: 2 additions & 2 deletions tests/page-templates/14-errors/11-events-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ describe("e2e/14-errors/11-events-element", function() {
}
});

it("Should have lineNumber ~ 66", function() {
it("Should have lineNumber ~ " + (HEADER_LINES + 18), function() {
var b = tf.lastBeacon();
var err = BOOMR.plugins.Errors.decompressErrors(C.jsUrlDecompress(b.err))[0];

if (err.lineNumber) {
assert.closeTo(err.lineNumber, 66, 5);
assert.closeTo(err.lineNumber, HEADER_LINES + 18, 5);
}
});
});
Loading

0 comments on commit 3201f02

Please sign in to comment.