Skip to content

Commit

Permalink
AutoXHR: if we don't instrument xhr open then don't instrument send e…
Browse files Browse the repository at this point in the history
…ither
  • Loading branch information
querymetrics authored and nicjansma committed Apr 4, 2018
1 parent f66eb82 commit 58b0879
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 11 deletions.
15 changes: 13 additions & 2 deletions plugins/auto-xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -1100,7 +1100,7 @@
*/
BOOMR.proxy_XMLHttpRequest = function() {
var req, resource = { timing: {}, initiator: "xhr" }, orig_open, orig_send,
opened = false;
opened = false, excluded = false;

req = new BOOMR.orig_XMLHttpRequest();

Expand All @@ -1111,10 +1111,13 @@
a.href = url;

if (impl.excludeFilter(a)) {
// this xhr should be excluded from instrumentation
excluded = true;
BOOMR.debug("Exclude found for resource: " + a.href + " Skipping instrumentation!", "AutoXHR");
// skip instrumentation and call the original open method
// call the original open method
return orig_open.apply(req, arguments);
}
excluded = false;

// Default value of async is true
if (async === undefined) {
Expand Down Expand Up @@ -1310,6 +1313,9 @@
// so let's fire loadFinished now
resource.status = XHR_STATUS_OPEN_EXCEPTION;
loadFinished();

// rethrow the native method's exception
throw e;
}
};

Expand All @@ -1320,6 +1326,11 @@
* @returns {Object} The data normal XHR.send() would return
*/
req.send = function(data) {
if (excluded) {
// this xhr is excluded from instrumentation, call the original send method
return orig_send.apply(req, arguments);
}

req.resource.requestPayload = data;
BOOMR.fireEvent("xhr_send", req);
resource.timing.requestStart = BOOMR.now();
Expand Down
19 changes: 10 additions & 9 deletions tests/page-templates/05-angular/112-autoxhr-open-without-send.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe("e2e/05-angular/112-autoxhr-open-without-send", function() {
clearTimeout(window.timerid);
});

it("Should have sent three beacons", function() {
it("Should have sent four beacons", function() {
assert.equal(tf.beacons.length, 4);
});

Expand All @@ -19,20 +19,21 @@ describe("e2e/05-angular/112-autoxhr-open-without-send", function() {
});

it("Should have beacon 1 as a spa_hard", function() {
if (t.isMutationObserverSupported() || t.isResourceTimingSupported()) {
var b = tf.beacons[0];
assert.equal(b["http.initiator"], "spa_hard");
}
var b = tf.beacons[0];
assert.equal(b["http.initiator"], "spa_hard");
});

it("Should have beacon 2 as a spa", function() {
if (t.isMutationObserverSupported() || t.isResourceTimingSupported()) {
var b = tf.beacons[1];
assert.equal(b["http.initiator"], "spa");
}
var b = tf.beacons[1];
assert.equal(b["http.initiator"], "spa");
});

it("Should have beacon 3 as a spa", function() {
var b = tf.beacons[2];
assert.equal(b["http.initiator"], "spa");
});

it("Should have beacon 4 as an xhr", function() {
var b = tf.beacons[3];
assert.equal(b["http.initiator"], "xhr");
});
Expand Down
55 changes: 55 additions & 0 deletions tests/page-templates/05-angular/116-autoxhr-xhrexcludes.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<!--
Issue 662
Test that xhrs excluded from instrumentation don't cause SPA to wait indefinitely
-->
<%= header %>
<%= boomerangScript %>
<base href="/pages/05-angular/" />
<script src="../../vendor/angular/angular.js"></script>
<script src="../../vendor/angular-resource/angular-resource.js"></script>
<script src="../../vendor/angular-route/angular-route.js"></script>
<script>
window.angular_imgs = [3000];

window.angular_html5_mode = false;

// view a widget then come back so debugging (F5) is easier
window.angular_nav_routes = ["/widgets/1", "/"];

window.BOOMR = window.BOOMR || {};
window.BOOMR.xhr_excludes = {
"/pages/05-angular/support/img.jpg": true
};
</script>
<script src="support/app.js"></script>
<div ng-app="app">
<div ng-view>
</div>
</div>
<script src="116-autoxhr-xhrexcludes.js" type="text/javascript"></script>
<script>
BOOMR.subscribe("spa_init", function() {
// send an xhr during a spa navigation
setTimeout(function() {
// this xhr should match the exclude filter
var xhr = new XMLHttpRequest();
xhr.open("GET", "/pages/05-angular/support/img.jpg?rnd=" + Math.random());
xhr.send(null);
}, 100);
});
BOOMR_test.init({
testAfterOnBeacon: 3,
Angular: {
enabled: true
},
instrument_xhr: true,
autorun: false
});

// Timeout after 30s in case auto xhr is still waiting for a resource
window.timerid = setTimeout(function() {
BOOMR_test.runTests();
}, 30000);

</script>
<%= footer %>
36 changes: 36 additions & 0 deletions tests/page-templates/05-angular/116-autoxhr-xhrexcludes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*eslint-env mocha*/
/*global BOOMR,BOOMR_test,describe,it*/

describe("e2e/05-angular/116-autoxhr-xhrexcludes.js", function() {
var tf = BOOMR.plugins.TestFramework;
var t = BOOMR_test;

it("Should pass basic beacon validation", function(done) {
t.validateBeaconWasSent(done);
clearTimeout(window.timerid);
});

it("Should have sent three beacons", function() {
assert.equal(tf.beacons.length, 3);
});

it("Should have sent the first beacon as http.initiator = spa_hard", function() {
assert.equal(tf.beacons[0]["http.initiator"], "spa_hard");
});

it("Should have beacon 1 as a spa_hard", function() {
var b = tf.beacons[0];
assert.equal(b["http.initiator"], "spa_hard");
});

it("Should have beacon 2 as a spa", function() {
var b = tf.beacons[1];
assert.equal(b["http.initiator"], "spa");
});

it("Should have beacon 3 as a spa", function() {
var b = tf.beacons[2];
assert.equal(b["http.initiator"], "spa");
});
});

0 comments on commit 58b0879

Please sign in to comment.