summaryrefslogtreecommitdiffstats
path: root/dom/serviceworkers/test/eventsource
diff options
context:
space:
mode:
Diffstat (limited to 'dom/serviceworkers/test/eventsource')
-rw-r--r--dom/serviceworkers/test/eventsource/eventsource.resource22
-rw-r--r--dom/serviceworkers/test/eventsource/eventsource.resource^headers^3
-rw-r--r--dom/serviceworkers/test/eventsource/eventsource_cors_response.html75
-rw-r--r--dom/serviceworkers/test/eventsource/eventsource_cors_response_intercept_worker.js30
-rw-r--r--dom/serviceworkers/test/eventsource/eventsource_mixed_content_cors_response.html75
-rw-r--r--dom/serviceworkers/test/eventsource/eventsource_mixed_content_cors_response_intercept_worker.js29
-rw-r--r--dom/serviceworkers/test/eventsource/eventsource_opaque_response.html75
-rw-r--r--dom/serviceworkers/test/eventsource/eventsource_opaque_response_intercept_worker.js30
-rw-r--r--dom/serviceworkers/test/eventsource/eventsource_register_worker.html27
-rw-r--r--dom/serviceworkers/test/eventsource/eventsource_synthetic_response.html75
-rw-r--r--dom/serviceworkers/test/eventsource/eventsource_synthetic_response_intercept_worker.js27
-rw-r--r--dom/serviceworkers/test/eventsource/eventsource_worker_helper.js17
12 files changed, 485 insertions, 0 deletions
diff --git a/dom/serviceworkers/test/eventsource/eventsource.resource b/dom/serviceworkers/test/eventsource/eventsource.resource
new file mode 100644
index 0000000000..eb62cbd4c5
--- /dev/null
+++ b/dom/serviceworkers/test/eventsource/eventsource.resource
@@ -0,0 +1,22 @@
+:this file must be enconded in utf8
+:and its Content-Type must be equal to text/event-stream
+
+retry:500
+data: 2
+unknow: unknow
+
+event: other_event_name
+retry:500
+data: 2
+unknow: unknow
+
+event: click
+retry:500
+
+event: blur
+retry:500
+
+event:keypress
+retry:500
+
+
diff --git a/dom/serviceworkers/test/eventsource/eventsource.resource^headers^ b/dom/serviceworkers/test/eventsource/eventsource.resource^headers^
new file mode 100644
index 0000000000..5b88be7c32
--- /dev/null
+++ b/dom/serviceworkers/test/eventsource/eventsource.resource^headers^
@@ -0,0 +1,3 @@
+Content-Type: text/event-stream
+Cache-Control: no-cache, must-revalidate
+Access-Control-Allow-Origin: *
diff --git a/dom/serviceworkers/test/eventsource/eventsource_cors_response.html b/dom/serviceworkers/test/eventsource/eventsource_cors_response.html
new file mode 100644
index 0000000000..115a0f5c65
--- /dev/null
+++ b/dom/serviceworkers/test/eventsource/eventsource_cors_response.html
@@ -0,0 +1,75 @@
+<!--
+ Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/
+-->
+<!DOCTYPE HTML>
+<html>
+<head>
+ <title>Bug 1182103 - Test EventSource scenarios with fetch interception</title>
+ <script type="text/javascript">
+
+ var prefix = "http://mochi.test:8888/tests/dom/serviceworkers/test/eventsource/";
+
+ function ok(aCondition, aMessage) {
+ parent.postMessage({status: "callback", data: "ok", condition: aCondition, message: aMessage}, "*");
+ }
+
+ function doUnregister() {
+ navigator.serviceWorker.getRegistration().then(swr => {
+ swr.unregister().then(function(result) {
+ ok(result, "Unregister should return true.");
+ parent.postMessage({status: "callback", data: "done"}, "*");
+ }, function(e) {
+ ok(false, "Unregistering the SW failed with " + e);
+ });
+ });
+ }
+
+ function doEventSource() {
+ var source = new EventSource(prefix + "eventsource.resource");
+ source.onmessage = function(e) {
+ source.onmessage = null;
+ source.close();
+ ok(true, "EventSource should work with cors responses");
+ doUnregister();
+ };
+ source.onerror = function(error) {
+ source.onerror = null;
+ source.close();
+ ok(false, "Something went wrong");
+ };
+ }
+
+ function onLoad() {
+ if (!parent) {
+ dump("eventsource/eventsource_cors_response.html shouldn't be launched directly!");
+ }
+
+ window.addEventListener("message", function onMessage(e) {
+ if (e.data.status === "callback") {
+ switch(e.data.data) {
+ case "eventsource":
+ doEventSource();
+ window.removeEventListener("message", onMessage);
+ break;
+ default:
+ ok(false, "Something went wrong")
+ break
+ }
+ }
+ });
+
+ navigator.serviceWorker.ready.then(function() {
+ parent.postMessage({status: "callback", data: "ready"}, "*");
+ });
+
+ navigator.serviceWorker.addEventListener("message", function(event) {
+ parent.postMessage(event.data, "*");
+ });
+ }
+
+ </script>
+</head>
+<body onload="onLoad()">
+</body>
+</html>
diff --git a/dom/serviceworkers/test/eventsource/eventsource_cors_response_intercept_worker.js b/dom/serviceworkers/test/eventsource/eventsource_cors_response_intercept_worker.js
new file mode 100644
index 0000000000..c2e5d416e7
--- /dev/null
+++ b/dom/serviceworkers/test/eventsource/eventsource_cors_response_intercept_worker.js
@@ -0,0 +1,30 @@
+// Cross origin request
+var prefix = "http://example.com/tests/dom/serviceworkers/test/eventsource/";
+
+self.importScripts("eventsource_worker_helper.js");
+
+self.addEventListener("fetch", function (event) {
+ var request = event.request;
+ var url = new URL(request.url);
+
+ if (
+ url.pathname !==
+ "/tests/dom/serviceworkers/test/eventsource/eventsource.resource"
+ ) {
+ return;
+ }
+
+ ok(request.mode === "cors", "EventSource should make a CORS request");
+ ok(
+ request.cache === "no-store",
+ "EventSource should make a no-store request"
+ );
+ var fetchRequest = new Request(prefix + "eventsource.resource", {
+ mode: "cors",
+ });
+ event.respondWith(
+ fetch(fetchRequest).then(fetchResponse => {
+ return fetchResponse;
+ })
+ );
+});
diff --git a/dom/serviceworkers/test/eventsource/eventsource_mixed_content_cors_response.html b/dom/serviceworkers/test/eventsource/eventsource_mixed_content_cors_response.html
new file mode 100644
index 0000000000..970cae517f
--- /dev/null
+++ b/dom/serviceworkers/test/eventsource/eventsource_mixed_content_cors_response.html
@@ -0,0 +1,75 @@
+<!--
+ Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/
+-->
+<!DOCTYPE HTML>
+<html>
+<head>
+ <title>Bug 1182103 - Test EventSource scenarios with fetch interception</title>
+ <script type="text/javascript">
+
+ var prefix = "https://example.com/tests/dom/serviceworkers/test/eventsource/";
+
+ function ok(aCondition, aMessage) {
+ parent.postMessage({status: "callback", data: "ok", condition: aCondition, message: aMessage}, "*");
+ }
+
+ function doUnregister() {
+ navigator.serviceWorker.getRegistration().then(swr => {
+ swr.unregister().then(function(result) {
+ ok(result, "Unregister should return true.");
+ parent.postMessage({status: "callback", data: "done"}, "*");
+ }, function(e) {
+ ok(false, "Unregistering the SW failed with " + e);
+ });
+ });
+ }
+
+ function doEventSource() {
+ var source = new EventSource(prefix + "eventsource.resource");
+ source.onmessage = function(e) {
+ source.onmessage = null;
+ source.close();
+ ok(false, "Something went wrong");
+ };
+ source.onerror = function(error) {
+ source.onerror = null;
+ source.close();
+ ok(true, "EventSource should not work with mixed content cors responses");
+ doUnregister();
+ };
+ }
+
+ function onLoad() {
+ if (!parent) {
+ dump("eventsource/eventsource_cors_response.html shouldn't be launched directly!");
+ }
+
+ window.addEventListener("message", function onMessage(e) {
+ if (e.data.status === "callback") {
+ switch(e.data.data) {
+ case "eventsource":
+ doEventSource();
+ window.removeEventListener("message", onMessage);
+ break;
+ default:
+ ok(false, "Something went wrong")
+ break
+ }
+ }
+ });
+
+ navigator.serviceWorker.ready.then(function() {
+ parent.postMessage({status: "callback", data: "ready"}, "*");
+ });
+
+ navigator.serviceWorker.addEventListener("message", function(event) {
+ parent.postMessage(event.data, "*");
+ });
+ }
+
+ </script>
+</head>
+<body onload="onLoad()">
+</body>
+</html>
diff --git a/dom/serviceworkers/test/eventsource/eventsource_mixed_content_cors_response_intercept_worker.js b/dom/serviceworkers/test/eventsource/eventsource_mixed_content_cors_response_intercept_worker.js
new file mode 100644
index 0000000000..9cb8d2d61f
--- /dev/null
+++ b/dom/serviceworkers/test/eventsource/eventsource_mixed_content_cors_response_intercept_worker.js
@@ -0,0 +1,29 @@
+var prefix = "http://example.com/tests/dom/serviceworkers/test/eventsource/";
+
+self.importScripts("eventsource_worker_helper.js");
+
+self.addEventListener("fetch", function (event) {
+ var request = event.request;
+ var url = new URL(request.url);
+
+ if (
+ url.pathname !==
+ "/tests/dom/serviceworkers/test/eventsource/eventsource.resource"
+ ) {
+ return;
+ }
+
+ ok(request.mode === "cors", "EventSource should make a CORS request");
+ ok(
+ request.cache === "no-store",
+ "EventSource should make a no-store request"
+ );
+ var fetchRequest = new Request(prefix + "eventsource.resource", {
+ mode: "cors",
+ });
+ event.respondWith(
+ fetch(fetchRequest).then(fetchResponse => {
+ return fetchResponse;
+ })
+ );
+});
diff --git a/dom/serviceworkers/test/eventsource/eventsource_opaque_response.html b/dom/serviceworkers/test/eventsource/eventsource_opaque_response.html
new file mode 100644
index 0000000000..bce12259cc
--- /dev/null
+++ b/dom/serviceworkers/test/eventsource/eventsource_opaque_response.html
@@ -0,0 +1,75 @@
+<!--
+ Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/
+-->
+<!DOCTYPE HTML>
+<html>
+<head>
+ <title>Bug 1182103 - Test EventSource scenarios with fetch interception</title>
+ <script type="text/javascript">
+
+ var prefix = "http://mochi.test:8888/tests/dom/serviceworkers/test/eventsource/";
+
+ function ok(aCondition, aMessage) {
+ parent.postMessage({status: "callback", data: "ok", condition: aCondition, message: aMessage}, "*");
+ }
+
+ function doUnregister() {
+ navigator.serviceWorker.getRegistration().then(swr => {
+ swr.unregister().then(function(result) {
+ ok(result, "Unregister should return true.");
+ parent.postMessage({status: "callback", data: "done"}, "*");
+ }, function(e) {
+ ok(false, "Unregistering the SW failed with " + e);
+ });
+ });
+ }
+
+ function doEventSource() {
+ var source = new EventSource(prefix + "eventsource.resource");
+ source.onmessage = function(e) {
+ source.onmessage = null;
+ source.close();
+ ok(false, "Something went wrong");
+ };
+ source.onerror = function(error) {
+ source.onerror = null;
+ source.close();
+ ok(true, "EventSource should not work with opaque responses");
+ doUnregister();
+ };
+ }
+
+ function onLoad() {
+ if (!parent) {
+ dump("eventsource/eventsource_opaque_response.html shouldn't be launched directly!");
+ }
+
+ window.addEventListener("message", function onMessage(e) {
+ if (e.data.status === "callback") {
+ switch(e.data.data) {
+ case "eventsource":
+ doEventSource();
+ window.removeEventListener("message", onMessage);
+ break;
+ default:
+ ok(false, "Something went wrong")
+ break
+ }
+ }
+ });
+
+ navigator.serviceWorker.ready.then(function() {
+ parent.postMessage({status: "callback", data: "ready"}, "*");
+ });
+
+ navigator.serviceWorker.addEventListener("message", function(event) {
+ parent.postMessage(event.data, "*");
+ });
+ }
+
+ </script>
+</head>
+<body onload="onLoad()">
+</body>
+</html>
diff --git a/dom/serviceworkers/test/eventsource/eventsource_opaque_response_intercept_worker.js b/dom/serviceworkers/test/eventsource/eventsource_opaque_response_intercept_worker.js
new file mode 100644
index 0000000000..5c8c75a161
--- /dev/null
+++ b/dom/serviceworkers/test/eventsource/eventsource_opaque_response_intercept_worker.js
@@ -0,0 +1,30 @@
+// Cross origin request
+var prefix = "http://example.com/tests/dom/serviceworkers/test/eventsource/";
+
+self.importScripts("eventsource_worker_helper.js");
+
+self.addEventListener("fetch", function (event) {
+ var request = event.request;
+ var url = new URL(request.url);
+
+ if (
+ url.pathname !==
+ "/tests/dom/serviceworkers/test/eventsource/eventsource.resource"
+ ) {
+ return;
+ }
+
+ ok(request.mode === "cors", "EventSource should make a CORS request");
+ ok(
+ request.cache === "no-store",
+ "EventSource should make a no-store request"
+ );
+ var fetchRequest = new Request(prefix + "eventsource.resource", {
+ mode: "no-cors",
+ });
+ event.respondWith(
+ fetch(fetchRequest).then(fetchResponse => {
+ return fetchResponse;
+ })
+ );
+});
diff --git a/dom/serviceworkers/test/eventsource/eventsource_register_worker.html b/dom/serviceworkers/test/eventsource/eventsource_register_worker.html
new file mode 100644
index 0000000000..59e8e92ab6
--- /dev/null
+++ b/dom/serviceworkers/test/eventsource/eventsource_register_worker.html
@@ -0,0 +1,27 @@
+<!--
+ Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/
+-->
+<!DOCTYPE HTML>
+<html>
+<head>
+ <title>Bug 1182103 - Test EventSource scenarios with fetch interception</title>
+ <script type="text/javascript">
+
+ function getURLParam (aTarget, aValue) {
+ return decodeURI(aTarget.search.replace(new RegExp("^(?:.*[&\\?]" + encodeURI(aValue).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1"));
+ }
+
+ function onLoad() {
+ navigator.serviceWorker.ready.then(function() {
+ parent.postMessage({status: "callback", data: "done"}, "*");
+ });
+
+ navigator.serviceWorker.register(getURLParam(document.location, "script"), {scope: "."});
+ }
+
+ </script>
+</head>
+<body onload="onLoad()">
+</body>
+</html>
diff --git a/dom/serviceworkers/test/eventsource/eventsource_synthetic_response.html b/dom/serviceworkers/test/eventsource/eventsource_synthetic_response.html
new file mode 100644
index 0000000000..7f6228c91e
--- /dev/null
+++ b/dom/serviceworkers/test/eventsource/eventsource_synthetic_response.html
@@ -0,0 +1,75 @@
+<!--
+ Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/
+-->
+<!DOCTYPE HTML>
+<html>
+<head>
+ <title>Bug 1182103 - Test EventSource scenarios with fetch interception</title>
+ <script type="text/javascript">
+
+ var prefix = "http://mochi.test:8888/tests/dom/serviceworkers/test/eventsource/";
+
+ function ok(aCondition, aMessage) {
+ parent.postMessage({status: "callback", data: "ok", condition: aCondition, message: aMessage}, "*");
+ }
+
+ function doUnregister() {
+ navigator.serviceWorker.getRegistration().then(swr => {
+ swr.unregister().then(function(result) {
+ ok(result, "Unregister should return true.");
+ parent.postMessage({status: "callback", data: "done"}, "*");
+ }, function(e) {
+ ok(false, "Unregistering the SW failed with " + e);
+ });
+ });
+ }
+
+ function doEventSource() {
+ var source = new EventSource(prefix + "eventsource.resource");
+ source.onmessage = function(e) {
+ source.onmessage = null;
+ source.close();
+ ok(true, "EventSource should work with synthetic responses");
+ doUnregister();
+ };
+ source.onerror = function(error) {
+ source.onmessage = null;
+ source.close();
+ ok(false, "Something went wrong");
+ };
+ }
+
+ function onLoad() {
+ if (!parent) {
+ dump("eventsource/eventsource_synthetic_response.html shouldn't be launched directly!");
+ }
+
+ window.addEventListener("message", function onMessage(e) {
+ if (e.data.status === "callback") {
+ switch(e.data.data) {
+ case "eventsource":
+ doEventSource();
+ window.removeEventListener("message", onMessage);
+ break;
+ default:
+ ok(false, "Something went wrong")
+ break
+ }
+ }
+ });
+
+ navigator.serviceWorker.ready.then(function() {
+ parent.postMessage({status: "callback", data: "ready"}, "*");
+ });
+
+ navigator.serviceWorker.addEventListener("message", function(event) {
+ parent.postMessage(event.data, "*");
+ });
+ }
+
+ </script>
+</head>
+<body onload="onLoad()">
+</body>
+</html>
diff --git a/dom/serviceworkers/test/eventsource/eventsource_synthetic_response_intercept_worker.js b/dom/serviceworkers/test/eventsource/eventsource_synthetic_response_intercept_worker.js
new file mode 100644
index 0000000000..72780e2979
--- /dev/null
+++ b/dom/serviceworkers/test/eventsource/eventsource_synthetic_response_intercept_worker.js
@@ -0,0 +1,27 @@
+self.importScripts("eventsource_worker_helper.js");
+
+self.addEventListener("fetch", function (event) {
+ var request = event.request;
+ var url = new URL(request.url);
+
+ if (
+ url.pathname !==
+ "/tests/dom/serviceworkers/test/eventsource/eventsource.resource"
+ ) {
+ return;
+ }
+
+ ok(request.mode === "cors", "EventSource should make a CORS request");
+ var headerList = {
+ "Content-Type": "text/event-stream",
+ "Cache-Control": "no-cache, must-revalidate",
+ };
+ var headers = new Headers(headerList);
+ var init = {
+ headers,
+ mode: "cors",
+ };
+ var body = "data: data0\r\r";
+ var response = new Response(body, init);
+ event.respondWith(response);
+});
diff --git a/dom/serviceworkers/test/eventsource/eventsource_worker_helper.js b/dom/serviceworkers/test/eventsource/eventsource_worker_helper.js
new file mode 100644
index 0000000000..676d2a4bbe
--- /dev/null
+++ b/dom/serviceworkers/test/eventsource/eventsource_worker_helper.js
@@ -0,0 +1,17 @@
+function ok(aCondition, aMessage) {
+ return new Promise(function (resolve, reject) {
+ self.clients.matchAll().then(function (res) {
+ if (!res.length) {
+ reject();
+ return;
+ }
+ res[0].postMessage({
+ status: "callback",
+ data: "ok",
+ condition: aCondition,
+ message: aMessage,
+ });
+ resolve();
+ });
+ });
+}