Bug 1210581 - Test controlled worker loads (XHR, fetch, importScripts). r=ehsan, a=test-only
authorKyle Huey <khuey@kylehuey.com>
Thu, 01 Oct 2015 16:44:17 -0700
changeset 296229 4d1a7f8aff899d3e5288b5df1504dbe414ca3dd7
parent 296228 6b451428f5502814808f848fc60252d773e39d84
child 296230 744b9efcd17d1d6589a2c46dcc3dcf83ec42fef3
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)
reviewersehsan, test-only
bugs1210581
milestone43.0a2
Bug 1210581 - Test controlled worker loads (XHR, fetch, importScripts). r=ehsan, a=test-only
testing/web-platform/mozilla/tests/service-workers/service-worker/resources/load_worker.js
testing/web-platform/mozilla/tests/service-workers/service-worker/resources/worker-interception-iframe.https.html
testing/web-platform/mozilla/tests/service-workers/service-worker/resources/worker-load-interceptor.js
testing/web-platform/mozilla/tests/service-workers/service-worker/worker-interception.https.html
new file mode 100644
--- /dev/null
+++ b/testing/web-platform/mozilla/tests/service-workers/service-worker/resources/load_worker.js
@@ -0,0 +1,29 @@
+self.onmessage = function (evt) {
+    if (evt.data == "xhr") {
+	var xhr = new XMLHttpRequest();
+	xhr.open("GET", "synthesized-response.txt", true);
+	xhr.responseType = "text";
+	xhr.send();
+	xhr.onload = function (evt) {
+	    postMessage(xhr.responseText);
+	};
+	xhr.onerror = function() {
+	    postMessage("XHR failed!");
+	};
+    } else if (evt.data == "fetch") {
+	fetch("synthesized-response.txt")
+	  .then(function(response) {
+	      return response.text();
+            })
+	  .then(function(data) {
+	      postMessage(data);
+            })
+	  .catch(function(error) {
+	      postMessage("Fetch failed!");
+	    });
+    } else if (evt.data == "importScripts") {
+	importScripts("synthesized-response.js");
+    } else {
+	throw "Unexpected message! " + evt.data;
+    }
+};
new file mode 100644
--- /dev/null
+++ b/testing/web-platform/mozilla/tests/service-workers/service-worker/resources/worker-interception-iframe.https.html
@@ -0,0 +1,40 @@
+<script src="/resources/testharness.js"></script>
+<script src="../resources/get-host-info.sub.js"></script>
+<script src="test-helpers.sub.js?pipe=sub"></script>
+<script>
+var host_info = get_host_info();
+
+function boilerplate_test(msg) {
+    return new Promise(function(resolve, reject) {
+          var worker = new Worker("load_worker.js");
+          worker.onmessage = function(e) { resolve(e.data) };
+          worker.onerror = function(e) { reject(e) };
+          worker.postMessage(msg);
+        })
+      .then(function(data) {
+          assert_equals(data, "This load was successfully intercepted.");
+        });
+}
+
+function xhr_test() {
+    return boilerplate_test("xhr");
+}
+
+function fetch_test() {
+    return boilerplate_test("fetch");
+}
+
+function importScripts_test() {
+    return boilerplate_test("importScripts");
+}
+
+window.addEventListener('message', function(evt) {
+    var port = evt.ports[0];
+    xhr_test()
+      .then(fetch_test)
+      .then(importScripts_test)
+      .then(function() { port.postMessage({results: 'finish'}); })
+      .catch(function(e) { port.postMessage({results: 'failure:' + e}); });
+  });
+
+</script>
new file mode 100644
--- /dev/null
+++ b/testing/web-platform/mozilla/tests/service-workers/service-worker/resources/worker-load-interceptor.js
@@ -0,0 +1,14 @@
+importScripts('get-host-info.sub.js');
+
+var response_text = "This load was successfully intercepted.";
+var response_script = "postMessage(\"This load was successfully intercepted.\");";
+
+self.onfetch = function(event) {
+    var url = event.request.url;
+    if (url.indexOf("synthesized-response.txt") != -1) {
+	event.respondWith(new Response(response_text));
+    } else if (url.indexOf("synthesized-response.js") != -1) {
+	event.respondWith(new Response(response_script));
+    }
+};
+
new file mode 100644
--- /dev/null
+++ b/testing/web-platform/mozilla/tests/service-workers/service-worker/worker-interception.https.html
@@ -0,0 +1,148 @@
+<!DOCTYPE html>
+<title>Service Worker: intercepting Worker script loads</title>
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<script src="resources/test-helpers.sub.js"></script>
+<body>
+<script>
+promise_test(function(t) {
+    var worker_url = 'resources/dummy-synthesized-worker.js';
+    var service_worker = 'resources/dummy-worker-interceptor.js';
+    var scope = worker_url;
+
+    return service_worker_unregister_and_register(t, service_worker, scope)
+      .then(function(r) {
+          return wait_for_state(t, r.installing, 'activated');
+        })
+      .then(function() {
+          return new Promise(function(resolve, reject) {
+              var w = new Worker(worker_url);
+              w.onmessage = function(e) {
+                resolve(e.data);
+              }
+
+              w.onerror = function(e) {
+                reject(e.message);
+              }
+            });
+        })
+      .then(function(data) {
+          assert_equals(data, 'worker loading intercepted by service worker');
+          service_worker_unregister_and_done(t, scope);
+        });
+  }, 'Verify worker script from uncontrolled document is intercepted by Service Worker');
+
+promise_test(function(t) {
+    var worker_url = 'resources/dummy-same-origin-worker.js';
+    var service_worker = 'resources/dummy-worker-interceptor.js';
+    var scope = worker_url;
+
+    return service_worker_unregister_and_register(t, service_worker, scope)
+      .then(function(r) {
+          return wait_for_state(t, r.installing, 'activated');
+        })
+      .then(function() {
+          return new Promise(function(resolve, reject) {
+              var w = new Worker(worker_url);
+              w.onmessage = function(e) {
+                resolve(e.data);
+              }
+
+              w.onerror = function(e) {
+                reject(e.message);
+              }
+            });
+        })
+      .then(function(data) {
+          assert_equals(data, 'dummy-worker-script loaded');
+          service_worker_unregister_and_done(t, scope);
+        });
+  }, 'Verify worker script intercepted by same-origin response succeeds');
+
+promise_test(function(t) {
+    var worker_url = 'resources/dummy-cors-worker.js';
+    var service_worker = 'resources/dummy-worker-interceptor.js';
+    var scope = worker_url;
+
+    return service_worker_unregister_and_register(t, service_worker, scope)
+      .then(function(r) {
+          return wait_for_state(t, r.installing, 'activated');
+        })
+      .then(function() {
+          return new Promise(function(resolve, reject) {
+              var w = new Worker(worker_url);
+              w.onmessage = function(e) {
+                resolve(e.data);
+              }
+
+              w.onerror = function(e) {
+                reject(e.message);
+              }
+            });
+        })
+      .then(function(data) {
+          assert_equals(data, 'dummy-worker-script loaded');
+          service_worker_unregister_and_done(t, scope);
+        });
+  }, 'Verify worker script intercepted by cors response succeeds');
+
+promise_test(function(t) {
+    var worker_url = 'resources/dummy-no-cors-worker.js';
+    var service_worker = 'resources/dummy-worker-interceptor.js';
+    var scope = worker_url;
+
+    return service_worker_unregister_and_register(t, service_worker, scope)
+      .then(function(r) {
+          return wait_for_state(t, r.installing, 'activated');
+        })
+      .then(function() {
+          return new Promise(function(resolve, reject) {
+              var w = new Worker(worker_url);
+              w.onmessage = function(e) {
+                resolve(e.data);
+              }
+
+              w.onerror = function(e) {
+                reject(e);
+                return true;
+              }
+            });
+        })
+      .then(function(data) {
+          assert_unreached('intercepted no-cors worker load should fail');
+          service_worker_unregister_and_done(t, scope);
+        })
+      .catch(function(e) {
+          assert_true(true, 'intercepted no-cors worker load should fail');
+          service_worker_unregister_and_done(t, scope);
+       });
+  }, 'Verify worker script intercepted by no-cors cross-origin response fails');
+
+promise_test(function(t) {
+    var subdoc_url = 'resources/worker-interception-iframe.https.html?bypass';
+    var service_worker = 'resources/worker-load-interceptor.js';
+    var scope = 'resources/';
+
+    return service_worker_unregister_and_register(t, service_worker, scope)
+      .then(function(r) {
+          return wait_for_state(t, r.installing, 'activated');
+        })
+      .then(function() { return with_iframe(subdoc_url); })
+      .then(function(frame) {
+          return new Promise(function(resolve, reject) {
+              var channel = new MessageChannel();
+              channel.port1.onmessage = function(e) {
+                resolve(e.data);
+              }
+
+              frame.contentWindow.postMessage("GO", "*", [channel.port2]);
+            });
+        })
+      .then(function(data) {
+          assert_equals(data.results, 'finish');
+          service_worker_unregister_and_done(t, scope);
+        });
+  }, 'Verify worker loads from controlled document are intercepted by Service Worker');
+
+</script>
+</body>