Skip to content

Commit 0fa7353

Browse files
committed
Tests for whatwg/fetch#435
1 parent c1d40be commit 0fa7353

File tree

3 files changed

+167
-0
lines changed

3 files changed

+167
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<!DOCTYPE html>
2+
<script src="/resources/testharness.js"></script>
3+
<script src="/resources/testharnessreport.js"></script>
4+
<script src="resources/get-host-info.sub.js"></script>
5+
<script src="resources/test-helpers.sub.js"></script>
6+
<body>
7+
<script>
8+
const worker = 'resources/fetch-event-within-sw-worker.js';
9+
10+
function wait(ms) {
11+
return new Promise(r => setTimeout(r, ms));
12+
}
13+
14+
function reset() {
15+
for (const iframe of [...document.querySelectorAll('.test-iframe')]) {
16+
iframe.remove();
17+
}
18+
return navigator.serviceWorker.getRegistrations().then(registrations => {
19+
return Promise.all(registrations.map(r => r.unregister()));
20+
}).then(() => caches.keys()).then(cacheKeys => {
21+
return Promise.all(cacheKeys.map(c => caches.delete(c)));
22+
});
23+
}
24+
25+
function regReady(reg) {
26+
return new Promise((resolve, reject) => {
27+
if (reg.active) {
28+
resolve();
29+
return;
30+
}
31+
const nextWorker = reg.waiting || reg.installing;
32+
33+
nextWorker.addEventListener('statechange', () => {
34+
if (nextWorker.state == 'redundant') {
35+
reject(Error(`Service worker failed to install`));
36+
return;
37+
}
38+
if (nextWorker.state == 'activated') {
39+
resolve();
40+
}
41+
});
42+
});
43+
}
44+
45+
function closeAllNotifications() {
46+
47+
}
48+
49+
function registerSwAndOpenFrame() {
50+
return reset().then(() => navigator.serviceWorker.register(worker, {scope: 'resources/'}))
51+
.then(reg => regReady(reg))
52+
.then(() => with_iframe('resources/simple.html'));
53+
}
54+
55+
promise_test(() => {
56+
return registerSwAndOpenFrame().then(iframe => {
57+
return Promise.all([
58+
iframe.contentWindow.fetch('dummy.txt').then(r => r.text()),
59+
iframe.contentWindow.caches.open('test')
60+
.then(cache =>
61+
cache.add('dummy.txt').then(() => cache.match('dummy.txt'))
62+
).then(response => {
63+
if (!response) return 'cache match failed';
64+
return response.text();
65+
})
66+
])
67+
}).then(([fetchText, cacheText]) => {
68+
assert_equals(fetchText, 'intercepted', 'fetch intercepted');
69+
assert_equals(cacheText, 'intercepted', 'cache.add intercepted');
70+
});
71+
}, 'Service worker intercepts requests from window');
72+
73+
promise_test(() => {
74+
return registerSwAndOpenFrame().then(iframe => {
75+
return Promise.all([
76+
iframe.contentWindow.fetch('dummy.txt-inner-fetch').then(r => r.text()),
77+
iframe.contentWindow.fetch('dummy.txt-inner-cache').then(r => r.text())
78+
])
79+
}).then(([fetchText, cacheText]) => {
80+
assert_equals(fetchText, 'Hello world\n', 'fetch within SW not intercepted');
81+
assert_equals(cacheText, 'Hello world\n', 'cache.add within SW not intercepted');
82+
});
83+
}, `Service worker does not intercept fetch/cache requests within service worker`);
84+
85+
promise_test(t => {
86+
return registerSwAndOpenFrame().then(iframe => {
87+
if (Notification.permission != "granted") {
88+
Notification.requestPermission();
89+
t.set_status(this.NOTRUN, 'You must allow notifications for this origin before running this test.');
90+
throw Error('You must allow notifications for this origin before running this test.');
91+
}
92+
return Promise.race([
93+
new Promise(resolve => {
94+
const bc = new BroadcastChannel('icon-request');
95+
bc.onmessage = () => {
96+
bc.close();
97+
resolve();
98+
};
99+
100+
const notification = new iframe.contentWindow.Notification('test', {
101+
icon: 'notification-icon.png'
102+
});
103+
notification.close();
104+
}),
105+
wait(1000).then(() => { throw Error(`Did not capture icon request for notification created from page`); })
106+
]).then(() => Promise.race([
107+
new Promise(resolve => {
108+
const bc = new BroadcastChannel('icon-request');
109+
bc.onmessage = () => {
110+
bc.close();
111+
resolve();
112+
};
113+
114+
iframe.contentWindow.fetch('show-notification');
115+
}),
116+
wait(1000).then(() => { throw Error(`Did not capture icon request for notification created within SW`); })
117+
]));
118+
});
119+
}, `Notification requests intercepted both from window and SW`);
120+
121+
</script>
122+
</body>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
skipWaiting();
2+
3+
addEventListener('fetch', event => {
4+
const url = new URL(event.request.url);
5+
6+
if (url.origin == location.origin) {
7+
if (url.pathname.endsWith('/dummy.txt')) {
8+
event.respondWith(new Response('intercepted'));
9+
return;
10+
}
11+
12+
if (url.pathname.endsWith('/dummy.txt-inner-fetch')) {
13+
event.respondWith(fetch('dummy.txt'));
14+
return;
15+
}
16+
17+
if (url.pathname.endsWith('/dummy.txt-inner-cache')) {
18+
event.respondWith(
19+
caches.open('test-inner-cache').then(cache =>
20+
cache.add('dummy.txt').then(() => cache.match('dummy.txt'))
21+
)
22+
);
23+
return;
24+
}
25+
26+
if (url.pathname.endsWith('/show-notification')) {
27+
event.respondWith(
28+
registration.showNotification('test', {
29+
icon: 'notification-icon.png'
30+
}).then(() => registration.getNotifications()).then(notifications => {
31+
for (const n of notifications) n.close();
32+
return new Response('done');
33+
})
34+
);
35+
return;
36+
}
37+
38+
if (url.pathname.endsWith('/notification-icon.png')) {
39+
new BroadcastChannel('icon-request').postMessage('yay');
40+
event.respondWith(new Response('done'));
41+
return;
42+
}
43+
}
44+
});

service-workers/service-worker/resources/test-helpers.sub.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ function unreached_rejection(test, prefix) {
5252
function with_iframe(url) {
5353
return new Promise(function(resolve) {
5454
var frame = document.createElement('iframe');
55+
frame.className = 'test-iframe';
5556
frame.src = url;
5657
frame.onload = function() { resolve(frame); };
5758
document.body.appendChild(frame);

0 commit comments

Comments
 (0)