4 <title> Verify devtools can utilize nsIChannel::LOAD_BYPASS_SERVICE_WORKER to bypass the service worker
</title>
5 <script src=
"/tests/SimpleTest/SimpleTest.js"></script>
6 <script src=
"error_reporting_helpers.js"></script>
7 <link rel=
"stylesheet" href=
"/tests/SimpleTest/test.css"/>
8 <meta http-equiv=
"Content-type" content=
"text/html;charset=UTF-8">
11 <div id=
"content" style=
"display: none"></div>
12 <script src=
"utils.js"></script>
13 <script type=
"text/javascript">
16 async
function testBypassSW () {
17 let Ci
= SpecialPowers
.Ci
;
19 // Bypass SW imitates the "Disable Cache" option in dev-tools.
20 // Note: if we put the setter/getter into dev-tools, we should take care of
21 // the implementation of enabling/disabling cache since it just overwrite the
22 // defaultLoadFlags of docShell.
23 function setBypassServiceWorker(aDocShell
, aBypass
) {
25 aDocShell
.defaultLoadFlags
|= Ci
.nsIChannel
.LOAD_BYPASS_SERVICE_WORKER
;
29 aDocShell
.defaultLoadFlags
&= ~Ci
.nsIChannel
.LOAD_BYPASS_SERVICE_WORKER
;
32 function getBypassServiceWorker(aDocShell
) {
33 return !!(aDocShell
.defaultLoadFlags
&
34 Ci
.nsIChannel
.LOAD_BYPASS_SERVICE_WORKER
);
37 async
function fetchFakeDocAndCheckIfIntercepted(aWindow
) {
38 const fakeDoc
= "fake.html";
40 // Note: The fetching document doesn't exist, so the expected status of the
41 // repsonse is 404 unless the request is hijacked.
42 let response
= await aWindow
.fetch(fakeDoc
);
43 if (response
.status
=== 404) {
45 } else if (!response
.ok
) {
46 throw(response
.statusText
);
49 let text
= await response
.text();
50 if (text
.includes("Hello")) {
55 throw("Unexpected error");
58 let docShell
= SpecialPowers
.wrap(window
).docShell
;
60 info("Test 1: Enable bypass service worker for the docShell");
62 setBypassServiceWorker(docShell
, true);
63 ok(getBypassServiceWorker(docShell
),
64 "The loadFlags in docShell does bypass the serviceWorker by default");
66 let intercepted
= await
fetchFakeDocAndCheckIfIntercepted(window
);
68 "The fetched document wasn't intercepted by the serviceWorker");
70 info("Test 2: Disable the bypass service worker for the docShell");
72 setBypassServiceWorker(docShell
, false);
73 ok(!getBypassServiceWorker(docShell
),
74 "The loadFlags in docShell doesn't bypass the serviceWorker by default");
76 intercepted
= await
fetchFakeDocAndCheckIfIntercepted(window
);
78 "The fetched document was intercepted by the serviceWorker");
81 // (This doesn't really need to be its own task, but it allows the actual test
82 // case to be self-contained.)
83 add_task(function setupPrefs() {
84 return SpecialPowers
.pushPrefEnv({"set": [
85 ["dom.serviceWorkers.enabled", true],
86 ["dom.serviceWorkers.testing.enabled", true],
90 add_task(async
function test_bypassServiceWorker() {
91 const swURL
= "fetch.js";
92 let registration
= await navigator
.serviceWorker
.register(swURL
);
93 await
waitForState(registration
.installing
, 'activated');
98 ok(false, "Reason:" + e
);
101 await registration
.unregister();