forked from web-platform-tests/wpt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prerender: Externalize fetch-intercepted-by-service-worker web platfo…
…rm test Bug: 1253158 Change-Id: I0ef6780b94be9acc0aa8d623a9d2cf9226f0a252 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3881591 Reviewed-by: Lingqi Chi <[email protected]> Commit-Queue: Hyowon Kim <[email protected]> Cr-Commit-Position: refs/heads/main@{#1047336}
- Loading branch information
1 parent
ab53b6a
commit 5e09235
Showing
5 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
speculation-rules/prerender/fetch-intercepted-by-service-worker.https.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<!DOCTYPE html> | ||
<title>Service worker intercepts a fetch request coming from a prerendered page</title> | ||
<meta name="timeout" content="long"> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<script src="/common/utils.js"></script> | ||
<script src="/service-workers/service-worker/resources/test-helpers.sub.js"></script> | ||
<script src="/speculation-rules/prerender/resources/utils.js"></script> | ||
<body> | ||
<script> | ||
const uid = token(); | ||
|
||
const PAGE_URL = `resources/fetch-intercepted-by-service-worker.html?uid=${uid}`; | ||
const WORKER_URL = 'resources/fetch-intercept-worker.js'; | ||
|
||
setup(() => assertSpeculationRulesIsSupported()); | ||
|
||
promise_test(async t => { | ||
const registration = | ||
await service_worker_unregister_and_register(t, WORKER_URL, PAGE_URL); | ||
t.add_cleanup(() => registration.unregister()); | ||
await wait_for_state(t, registration.installing, 'activated'); | ||
|
||
const bc = new PrerenderChannel('prerender-channel', uid); | ||
t.add_cleanup(_ => bc.close()); | ||
|
||
const gotMessage = new Promise(resolve => { | ||
bc.addEventListener('message', e => { | ||
resolve(e.data); | ||
}, { | ||
once: true | ||
}); | ||
}); | ||
|
||
startPrerendering(PAGE_URL); | ||
|
||
const result = await gotMessage; | ||
assert_equals( | ||
result, 'intercepted by service worker', | ||
"fetch() should go through a service worker's fetch event handler"); | ||
}, 'fetch() in a prerendering page should go through a service worker'); | ||
|
||
</script> | ||
</body> |
42 changes: 42 additions & 0 deletions
42
speculation-rules/prerender/navigation-intercepted-by-service-worker.https.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<!DOCTYPE html> | ||
<title>Service worker intercepts a navigation and starts prerendering</title> | ||
<meta name="timeout" content="long"> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<script src="/common/utils.js"></script> | ||
<script src="/service-workers/service-worker/resources/test-helpers.sub.js"></script> | ||
<script src="/speculation-rules/prerender/resources/utils.js"></script> | ||
<body> | ||
<script> | ||
const uid = token(); | ||
|
||
const PAGE_URL = `resources/non-existent-page.html?should-intercept&uid=${uid}`; | ||
const WORKER_URL = 'resources/fetch-intercept-worker.js'; | ||
|
||
setup(() => assertSpeculationRulesIsSupported()); | ||
|
||
promise_test(async t => { | ||
const registration = | ||
await service_worker_unregister_and_register(t, WORKER_URL, PAGE_URL); | ||
t.add_cleanup(() => registration.unregister()); | ||
await wait_for_state(t, registration.installing, 'activated'); | ||
|
||
const bc = new PrerenderChannel('prerender-channel', uid); | ||
t.add_cleanup(_ => bc.close()); | ||
|
||
const gotMessage = new Promise(resolve => { | ||
bc.addEventListener('message', e => { | ||
resolve(e.data); | ||
}, { | ||
once: true | ||
}); | ||
}); | ||
|
||
startPrerendering(PAGE_URL); | ||
|
||
const result = await gotMessage; | ||
assert_equals(result, 'prerender success'); | ||
}, 'navigation should be intercepted by a service worker'); | ||
|
||
</script> | ||
</body> |
9 changes: 9 additions & 0 deletions
9
speculation-rules/prerender/resources/fetch-intercept-worker.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
self.addEventListener('fetch', e => { | ||
if (e.request.url.includes('should-intercept')) { | ||
if (e.request.destination === 'document') { | ||
e.respondWith(fetch('./prerendered-page.html')); | ||
} else { | ||
e.respondWith(new Response('intercepted by service worker')); | ||
} | ||
} | ||
}); |
22 changes: 22 additions & 0 deletions
22
speculation-rules/prerender/resources/fetch-intercepted-by-service-worker.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<!DOCTYPE html> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<script src="/speculation-rules/prerender/resources/utils.js"></script> | ||
<script> | ||
|
||
const params = new URLSearchParams(location.search); | ||
const uid = params.get('uid'); | ||
|
||
async function startFetch() { | ||
assert_true(document.prerendering); | ||
|
||
const response = await fetch('cache.txt?should-intercept'); | ||
const body = await response.text(); | ||
|
||
const bc = new PrerenderChannel('prerender-channel', uid); | ||
bc.postMessage(body); | ||
bc.close(); | ||
} | ||
|
||
startFetch(); | ||
</script> |
17 changes: 17 additions & 0 deletions
17
speculation-rules/prerender/resources/prerendered-page.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<!DOCTYPE html> | ||
<title>Prerendered page</title> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<script src="/speculation-rules/prerender/resources/utils.js"></script> | ||
<body> | ||
<script> | ||
assert_true(document.prerendering); | ||
|
||
const params = new URLSearchParams(location.search); | ||
const uid = params.get('uid'); | ||
|
||
const bc = new PrerenderChannel('prerender-channel', uid); | ||
bc.postMessage('prerender success'); | ||
bc.close(); | ||
</script> | ||
</body> |