Skip to content

Commit

Permalink
Prerender: Externalize fetch-intercepted-by-service-worker web platfo…
Browse files Browse the repository at this point in the history
…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
hyowon-igalia authored and chromium-wpt-export-bot committed Sep 15, 2022
1 parent ab53b6a commit 5e09235
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 0 deletions.
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>
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>
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'));
}
}
});
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 speculation-rules/prerender/resources/prerendered-page.html
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>

0 comments on commit 5e09235

Please sign in to comment.