Bug 1147367 - Add a test for checking the Request.context attribute for FetchEvents generated for fetch(); r=nsm

This commit is contained in:
Ehsan Akhgari 2015-03-25 09:14:14 -04:00
parent d0edee4796
commit 030ee5c6f9
6 changed files with 133 additions and 0 deletions

View File

@ -0,0 +1,15 @@
self.addEventListener("fetch", function(event) {
if (event.request.url.indexOf("index.html") >= 0 ||
event.request.url.indexOf("register.html") >= 0 ||
event.request.url.indexOf("unregister.html") >= 0) {
// Handle pass-through requests
event.respondWith(fetch(event.request));
} else if (event.request.url.indexOf("fetch.txt") >= 0) {
var body = event.request.context == "fetch" ?
"so fetch" : "so unfetch";
event.respondWith(new Response(body));
} else {
// Fail any request that we don't know about.
event.respondWith(Promise.reject());
}
});

View File

@ -0,0 +1,27 @@
<!DOCTYPE html>
<script>
function ok(v, msg) {
window.parent.postMessage({status: "ok", result: !!v, message: msg}, "*");
}
function is(a, b, msg) {
ok(a === b, msg + ", expected '" + b + "', got '" + a + "'");
}
function finish() {
window.parent.postMessage({status: "done"}, "*");
}
function testFetch() {
return fetch("fetch.txt").then(function(r) {
return r.text();
}).then(function(body) {
is(body, "so fetch", "A fetch() Request should have the 'fetch' context");
});
}
testFetch()
.then(function() {
finish();
});
</script>

View File

@ -0,0 +1,26 @@
<!DOCTYPE html>
<script>
function ok(v, msg) {
window.parent.postMessage({status: "ok", result: !!v, message: msg}, "*");
}
var isDone = false;
function done(reg) {
if (!isDone) {
ok(reg.waiting || reg.active, "Either active or waiting worker should be available.");
window.parent.postMessage({status: "registrationdone"}, "*");
isDone = true;
}
}
navigator.serviceWorker.register("context_test.js", {scope: "."})
.then(function(registration) {
if (registration.installing) {
registration.installing.onstatechange = function(e) {
done(registration);
};
} else {
done(registration);
}
});
</script>

View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<script>
navigator.serviceWorker.getRegistration(".").then(function(registration) {
registration.unregister().then(function(success) {
if (success) {
window.parent.postMessage({status: "unregistrationdone"}, "*");
}
});
});
</script>

View File

@ -26,6 +26,10 @@ support-files =
fetch/fetch_worker_script.js
fetch/fetch_tests.js
fetch/deliver-gzip.sjs
fetch/context/index.html
fetch/context/register.html
fetch/context/unregister.html
fetch/context/context_test.js
fetch/https/index.html
fetch/https/register.html
fetch/https/unregister.html
@ -74,3 +78,4 @@ support-files =
[test_serviceworker_not_sharedworker.html]
[test_match_all_client_id.html]
[test_sandbox_intercept.html]
[test_request_context.html]

View File

@ -0,0 +1,50 @@
<!--
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
-->
<!DOCTYPE HTML>
<html>
<head>
<title>Bug 1121157 - Test that Request objects passed to FetchEvent have the correct context</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">
<iframe></iframe>
</div>
<pre id="test"></pre>
<script class="testbody" type="text/javascript">
var iframe;
function runTest() {
iframe = document.querySelector("iframe");
iframe.src = "/tests/dom/workers/test/serviceworkers/fetch/context/register.html";
window.onmessage = function(e) {
if (e.data.status == "ok") {
ok(e.data.result, e.data.message);
} else if (e.data.status == "registrationdone") {
iframe.src = "/tests/dom/workers/test/serviceworkers/fetch/context/index.html";
} else if (e.data.status == "done") {
iframe.src = "/tests/dom/workers/test/serviceworkers/fetch/context/unregister.html";
} else if (e.data.status == "unregistrationdone") {
window.onmessage = null;
ok(true, "Test finished successfully");
SimpleTest.finish();
}
};
}
SimpleTest.waitForExplicitFinish();
onload = function() {
SpecialPowers.pushPrefEnv({"set": [
["dom.serviceWorkers.exemptFromPerDomainMax", true],
["dom.serviceWorkers.enabled", true],
["dom.serviceWorkers.testing.enabled", true],
]}, runTest);
};
</script>
</pre>
</body>
</html>