Bug 1182113 - Test XSLT serviceworker load. r=bkelly
authorAlbert Crespell <alberto.crespellperez@telefonica.com>
Thu, 10 Sep 2015 12:42:02 +0200
changeset 295608 ec9dc8f952c507d16bc65b30ca768201aac860c5
parent 295607 9efbf9792f34fc2349f077cf58dff4c68baa8fbc
child 295609 2ec5cb2beea7dd3a0ddcea712a4370d639284633
push id5245
push userraliiev@mozilla.com
push dateThu, 29 Oct 2015 11:30:51 +0000
treeherdermozilla-beta@dac831dc1bd0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbkelly
bugs1182113
milestone43.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 1182113 - Test XSLT serviceworker load. r=bkelly
dom/workers/test/serviceworkers/mochitest.ini
dom/workers/test/serviceworkers/test_xslt.html
dom/workers/test/serviceworkers/xslt/test.xml
dom/workers/test/serviceworkers/xslt/xslt.sjs
dom/workers/test/serviceworkers/xslt_worker.js
--- a/dom/workers/test/serviceworkers/mochitest.ini
+++ b/dom/workers/test/serviceworkers/mochitest.ini
@@ -157,16 +157,18 @@ support-files =
   test_request_context.js
   fetch_event_client.js
   sw_clients/dummy.html
   fetch/plugin/worker.js
   fetch/plugin/plugins.html
   eventsource/*
   sw_clients/file_blob_upload_frame.html
   redirect_post.sjs
+  xslt_worker.js
+  xslt/*
 
 [test_app_protocol.html]
 skip-if = release_build
 [test_bug1151916.html]
 [test_claim.html]
 [test_claim_fetch.html]
 [test_claim_oninstall.html]
 [test_close.html]
@@ -245,12 +247,13 @@ skip-if = toolkit == "android" || toolki
 [test_strict_mode_warning.html]
 [test_third_party_iframes.html]
 [test_unregister.html]
 [test_workerUnregister.html]
 [test_workerUpdate.html]
 [test_workerupdatefoundevent.html]
 [test_opaque_intercept.html]
 [test_fetch_event_client_postmessage.html]
+[test_xslt.html]
 [test_escapedSlashes.html]
 [test_eventsource_intercept.html]
 [test_not_intercept_plugin.html]
 [test_file_blob_upload.html]
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/serviceworkers/test_xslt.html
@@ -0,0 +1,113 @@
+<!--
+  Any copyright is dedicated to the Public Domain.
+  http://creativecommons.org/publicdomain/zero/1.0/
+-->
+<!DOCTYPE HTML>
+<html>
+<head>
+  <title>Bug 1182113 - Test service worker XSLT interception</title>
+  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<p id="display"></p>
+<div id="content"></div>
+<pre id="test"></pre>
+<script class="testbody" type="text/javascript">
+  var registration;
+  var worker;
+
+  function start() {
+    return navigator.serviceWorker.register("xslt_worker.js",
+                                            { scope: "./" })
+      .then((swr) => registration = swr);
+  }
+
+  function unregister() {
+    return registration.unregister().then(function(result) {
+      ok(result, "Unregister should return true.");
+    }, function(e) {
+      dump("Unregistering the SW failed with " + e + "\n");
+    });
+  }
+
+  function getXmlString(xmlObject) {
+    serializer = new XMLSerializer();
+    return serializer.serializeToString(iframe.contentDocument);
+  }
+
+  function synthetic() {
+    content = document.getElementById("content");
+    ok(content, "parent exists.");
+
+    iframe = document.createElement("iframe");
+    content.appendChild(iframe);
+
+    iframe.setAttribute('src', "xslt/test.xml");
+
+    var p = new Promise(function(res, rej) {
+      iframe.onload = function(e) {
+        dump("Set request mode\n");
+        registration.active.postMessage("synthetic");
+        xmlString = getXmlString(iframe.contentDocument);
+        ok(!xmlString.includes("Error"), "Load synthetic cross origin XSLT should be allowed");
+        res();
+      };
+    });
+
+    return p;
+  }
+
+  function cors() {
+    var p = new Promise(function(res, rej) {
+      iframe.onload = function(e) {
+        xmlString = getXmlString(iframe.contentDocument);
+        ok(!xmlString.includes("Error"), "Load CORS cross origin XSLT should be allowed");
+        res();
+      };
+    });
+
+    registration.active.postMessage("cors");
+    iframe.setAttribute('src', "xslt/test.xml");
+
+    return p;
+  }
+
+  function opaque() {
+    var p = new Promise(function(res, rej) {
+      iframe.onload = function(e) {
+        xmlString = getXmlString(iframe.contentDocument);
+        ok(xmlString.includes("Error"), "Load opaque cross origin XSLT should not be allowed");
+        res();
+      };
+    });
+
+    registration.active.postMessage("opaque");
+    iframe.setAttribute('src', "xslt/test.xml");
+
+    return p;
+  }
+
+  function runTest() {
+     start()
+      .then(synthetic)
+      .then(opaque)
+      .then(cors)
+      .then(unregister)
+      .catch(function(e) {
+        ok(false, "Some test failed with error " + e);
+      }).then(SimpleTest.finish);
+  }
+
+  SimpleTest.waitForExplicitFinish();
+  SpecialPowers.pushPrefEnv({"set": [
+    ["dom.serviceWorkers.exemptFromPerDomainMax", true],
+    ["dom.serviceWorkers.interception.enabled", true],
+    ["dom.serviceWorkers.enabled", true],
+    ["dom.serviceWorkers.testing.enabled", true],
+  ]}, runTest);
+</script>
+</pre>
+</body>
+</html>
+
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/serviceworkers/xslt/test.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0"?>
+<?xml-stylesheet type="text/xsl" href="test.xsl"?>
+<result>
+  <Title>Example</Title>
+  <Error>Error</Error>
+</result>
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/serviceworkers/xslt/xslt.sjs
@@ -0,0 +1,12 @@
+function handleRequest(request, response) {
+  response.setHeader("Content-Type", "application/xslt+xml", false);
+  response.setHeader("Access-Control-Allow-Origin", "*");
+
+  var body = request.queryString;
+  if (!body) {
+    response.setStatusLine(null, 500, "Invalid querystring");
+    return;
+  }
+
+  response.write(unescape(body));
+}
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/serviceworkers/xslt_worker.js
@@ -0,0 +1,52 @@
+var testType = 'synthetic';
+
+var xslt = "<?xml version=\"1.0\"?> " +
+           "<xsl:stylesheet version=\"1.0\"" +
+           "   xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">" +
+           "  <xsl:template match=\"node()|@*\">" +
+           "    <xsl:copy>" +
+           "      <xsl:apply-templates select=\"node()|@*\"/>" +
+           "    </xsl:copy>" +
+           "  </xsl:template>" +
+           "  <xsl:template match=\"Error\"/>" +
+           "</xsl:stylesheet>";
+
+onfetch = function(event) {
+  if (event.request.url.includes('test.xsl')) {
+    if (testType == 'synthetic') {
+      if (event.request.mode != 'cors') {
+        event.respondWith(Response.error());
+        return;
+      }
+
+      event.respondWith(Promise.resolve(
+        new Response(xslt, { headers: {'Content-Type': 'application/xslt+xml'}})
+      ));
+    }
+    else if (testType == 'cors') {
+      if (event.request.mode != 'cors') {
+        event.respondWith(Response.error());
+        return;
+      }
+
+      var url = "http://example.com/tests/dom/workers/test/serviceworkers/xslt/xslt.sjs?" + escape(xslt);
+      event.respondWith(fetch(url, { mode: 'cors' }));
+    }
+    else if (testType == 'opaque') {
+      if (event.request.mode != 'cors') {
+        event.respondWith(Response.error());
+        return;
+      }
+
+      var url = "http://example.com/tests/dom/workers/test/serviceworkers/xslt/xslt.sjs?" + escape(xslt);
+      event.respondWith(fetch(url, { mode: 'no-cors' }));
+    }
+    else {
+      event.respondWith(Response.error());
+    }
+  }
+};
+
+onmessage = function(event) {
+  testType = event.data;
+};