Bug 1424701 - Test for uploading files via ServiceWorker, r=bkelly
authorAndrea Marchesini <amarchesini@mozilla.com>
Wed, 10 Jan 2018 17:04:09 +0100
changeset 450367 479b32d510f5963b8b4c74baa8ba45d47aa0776c
parent 450366 5241bdbfa0a559c001a9de789c466c9506493490
child 450368 b62099b50fc7c8a625092a23be6d532fa45ce6a6
push id8527
push userCallek@gmail.com
push dateThu, 11 Jan 2018 21:05:50 +0000
treeherdermozilla-beta@95342d212a7a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbkelly
bugs1424701
milestone59.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 1424701 - Test for uploading files via ServiceWorker, r=bkelly
dom/workers/test/serviceworkers/mochitest.ini
dom/workers/test/serviceworkers/script_file_upload.js
dom/workers/test/serviceworkers/server_file_upload.sjs
dom/workers/test/serviceworkers/sw_file_upload.js
dom/workers/test/serviceworkers/test_file_upload.html
--- a/dom/workers/test/serviceworkers/mochitest.ini
+++ b/dom/workers/test/serviceworkers/mochitest.ini
@@ -341,8 +341,10 @@ tags = openwindow
 [test_workerUnregister.html]
 [test_workerUpdate.html]
 [test_workerupdatefoundevent.html]
 [test_xslt.html]
 [test_async_waituntil.html]
 [test_worker_reference_gc_timeout.html]
 [test_nofetch_handler.html]
 [test_bad_script_cache.html]
+[test_file_upload.html]
+support-files = script_file_upload.js sw_file_upload.js server_file_upload.sjs
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/serviceworkers/script_file_upload.js
@@ -0,0 +1,14 @@
+var { classes: Cc, interfaces: Ci, utils: Cu } = Components;
+Cu.importGlobalProperties(["File"]);
+
+addMessageListener("file.open", function (e) {
+  var testFile = Cc["@mozilla.org/file/directory_service;1"]
+                   .getService(Ci.nsIDirectoryService)
+                   .QueryInterface(Ci.nsIProperties)
+                   .get("ProfD", Ci.nsIFile);
+  testFile.append("prefs.js");
+
+  File.createFromNsIFile(testFile).then(function(file) {
+    sendAsyncMessage("file.opened", { file });
+  });
+});
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/serviceworkers/server_file_upload.sjs
@@ -0,0 +1,17 @@
+const CC = Components.Constructor;
+const BinaryInputStream = CC("@mozilla.org/binaryinputstream;1",
+                             "nsIBinaryInputStream",
+                             "setInputStream");
+const BinaryOutputStream = CC("@mozilla.org/binaryoutputstream;1",
+                              "nsIBinaryOutputStream",
+                              "setOutputStream");
+
+function handleRequest(request, response) {
+  var bodyStream = new BinaryInputStream(request.bodyInputStream);
+  var bodyBytes = [];
+  while ((bodyAvail = bodyStream.available()) > 0)
+    Array.prototype.push.apply(bodyBytes, bodyStream.readByteArray(bodyAvail));
+
+  var bos = new BinaryOutputStream(response.bodyOutputStream);
+  bos.writeByteArray(bodyBytes, bodyBytes.length);
+}
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/serviceworkers/sw_file_upload.js
@@ -0,0 +1,13 @@
+self.skipWaiting();
+
+addEventListener('fetch', event => {
+  event.respondWith(
+    fetch(event.request)
+  );
+});
+
+addEventListener('message', function(event) {
+  if (event.data === 'claim') {
+    event.waitUntil(clients.claim());
+  }
+});
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/serviceworkers/test_file_upload.html
@@ -0,0 +1,56 @@
+<!--
+  Any copyright is dedicated to the Public Domain.
+  http://creativecommons.org/publicdomain/zero/1.0/
+-->
+<!DOCTYPE HTML>
+<html>
+<head>
+  <title>Bug 1424701 - Test for service worker + file upload</title>
+  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <script type="text/javascript" src="utils.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<input id="input" type="file">
+<script class="testbody" type="text/javascript">
+
+async function onOpened(message) {
+  let input = document.getElementById("input");
+  SpecialPowers.wrap(input).mozSetFileArray([message.file]);
+  script.destroy();
+
+  let registration = await navigator.serviceWorker.register('sw_file_upload.js',
+                                                            {scope: "." });
+
+  let worker = registration.installing || registration.active;
+  await waitForState(worker, 'activated');
+  worker.postMessage('claim');
+  await waitForControlled(window);
+
+  let res = await fetch('server_file_upload.sjs', {
+    method: 'POST',
+    body: input.files[0],
+  });
+
+  let data = await res.clone().text();
+  ok(data.length > 0, "We have data!");
+
+  await registration.unregister();
+  SimpleTest.finish();
+}
+
+let url = SimpleTest.getTestFileURL("script_file_upload.js");
+let script = SpecialPowers.loadChromeScript(url);
+
+SimpleTest.waitForExplicitFinish();
+SpecialPowers.pushPrefEnv({"set": [
+  ["dom.serviceWorkers.exemptFromPerDomainMax", true],
+  ["dom.serviceWorkers.enabled", true],
+  ["dom.serviceWorkers.testing.enabled", true]
+]}).then(() => {
+  script.addMessageListener("file.opened", onOpened);
+  script.sendAsyncMessage("file.open");
+});
+</script>
+</body>
+</html>