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