Bug 1508310 - Implement Report-to header support - part 10 - delivering tests, r=smaug
authorAndrea Marchesini <amarchesini@mozilla.com>
Sat, 01 Dec 2018 21:26:10 +0100
changeset 508364 1d09c38b173c82c968b74eea3d2fc24e1e139639
parent 508363 c0e1002895a6f975ea6ac879b841b25123fd3522
child 508365 ec7421212c2218eb208eb08457fb660a81a6572d
push id1905
push userffxbld-merge
push dateMon, 21 Jan 2019 12:33:13 +0000
treeherdermozilla-release@c2fca1944d8c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1508310
milestone65.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1508310 - Implement Report-to header support - part 10 - delivering tests, r=smaug
dom/reporting/tests/iframe_delivering.html
dom/reporting/tests/mochitest.ini
dom/reporting/tests/test_delivering.html
new file mode 100644
--- /dev/null
+++ b/dom/reporting/tests/iframe_delivering.html
@@ -0,0 +1,73 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <title>Test for delivering reports</title>
+</head>
+<body>
+
+<script type="application/javascript">
+
+function ok(a, msg) {
+  parent.postMessage({type: "test", check: !!a, msg }, "*");
+}
+
+function is(a, b, msg) {
+  ok(a === b, msg);
+}
+
+function finish() {
+  parent.postMessage({type: "finish" }, "*");
+}
+
+function checkReport() {
+  return new Promise(resolve => {
+    let id = setInterval(_ => {
+      fetch("delivering.sjs?task=check")
+      .then(r => r.text())
+      .then(text => {
+        if (text) {
+          resolve(JSON.parse(text));
+          clearInterval(id);
+        }
+      });
+    }, 1000);
+  });
+}
+
+// Let's register a group + endpoint
+fetch("delivering.sjs?task=header")
+.then(r => r.text())
+.then(text => {
+  is(text, "OK", "Report-to header sent");
+})
+
+// Call a deprecating operation.
+.then(_ => {
+  let testingInterface = new TestingDeprecatedInterface();
+  ok(true, "Created a deprecated interface");
+})
+
+// Check if the report has been received.
+.then(_ => {
+  return checkReport();
+})
+.then(report => {
+  is(report.contentType, "application/reports+json", "Correct mime-type");
+  is(report.origin, "https://example.org", "Origin correctly set");
+  ok(!!report.body, "We have a report.body");
+  ok(report.body.age > 0, "Age is correctly set");
+  is(report.body.user_agent, navigator.userAgent, "User-agent matches");
+  ok(report.body.type, "deprecation", "Type is fine.");
+  ok(!!report.body.body, "We have the real report.body");
+  is(report.body.body.id, "DeprecatedTestingInterface", "Correct report.body.id");
+  is(report.body.body.message, "TestingDeprecatedInterface is a testing-only interface and this is its testing deprecation message.", "We have a report.body.message");
+  is(report.body.body.sourceFile, "https://example.org/tests/dom/reporting/tests/iframe_delivering.html", "report.body.sourceFile");
+  is(report.body.body.lineNumber, 46, "report.body.lineNumber");
+  is(report.body.body.columnNumber, 25, "report.body.columnNumber");
+})
+
+.then(finish);
+
+</script>
+</body>
+</html>
--- a/dom/reporting/tests/mochitest.ini
+++ b/dom/reporting/tests/mochitest.ini
@@ -1,7 +1,10 @@
 [DEFAULT]
 prefs =
   dom.reporting.enabled=true
+  dom.reporting.header.enabled=true
   dom.reporting.testing.enabled=true
 
 [test_deprecated.html]
 [test_memoryPressure.html]
+[test_delivering.html]
+support-files = delivering.sjs iframe_delivering.html
new file mode 100644
--- /dev/null
+++ b/dom/reporting/tests/test_delivering.html
@@ -0,0 +1,44 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <title>Test for delivering reports</title>
+  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+</head>
+<body>
+
+<script type="application/javascript">
+
+// Setting prefs.
+SpecialPowers.pushPrefEnv({ set: [
+  ["dom_reporting_delivering_timeout", 1],
+  ["dom_reporting_delivering_maxFailures", 2],
+]})
+
+// Tests run in iframes because the origin must be secure for report-to header.
+.then(_ => {
+  window.addEventListener("message", e => {
+    if (e.data.type == "finish") {
+      SimpleTest.finish();
+      return;
+    }
+
+    if (e.data.type == "test") {
+      ok(e.data.check, e.data.msg);
+      return;
+    }
+
+    ok(false, "Invalid message");
+  });
+
+  let ifr = document.createElement("iframe");
+  ifr.src = "https://example.org/tests/dom/reporting/tests/iframe_delivering.html";
+
+  document.body.appendChild(ifr);
+});
+
+SimpleTest.waitForExplicitFinish();
+
+</script>
+</body>
+</html>