Skip to content

Commit

Permalink
Add main resource tests for the static router and small test js fix
Browse files Browse the repository at this point in the history
This CL adds WPT to ensure if the matched main resource request skips
ServiceWorker fetch handler.

Also this test removed unnecessary "async" from the
BestEffortServiceWorker browser test js.

Bug: 1371756, 1420517
Change-Id: Iab72cd2835f000312c2c0cd41b78799261038356
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4707378
Reviewed-by: Minoru Chikamune <[email protected]>
Reviewed-by: Kouhei Ueno <[email protected]>
Commit-Queue: Shunya Shishido <[email protected]>
Reviewed-by: Yoshisato Yanagisawa <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1174243}
  • Loading branch information
sisidovski authored and chromium-wpt-export-bot committed Jul 24, 2023
1 parent c1a0c70 commit 13ef5f8
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!DOCTYPE html>
<title>Simple</title>
Here's a simple html file.
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
'use strict';

var requests = [];

self.addEventListener('install', e => {
e.registerRouter({
condition: {urlPattern: "*.txt"},
source: "network"
});
e.registerRouter([
{condition: {urlPattern: '*.txt'}, source: 'network'}, {
condition: {urlPattern: '*/simple-test-for-condition-main-resource.html'},
source: 'network'
}
]);
self.skipWaiting();
});

Expand All @@ -13,7 +17,13 @@ self.addEventListener('activate', e => {
});

self.addEventListener('fetch', function(event) {
requests.push({url: event.request.url, mode: event.request.mode});
const url = new URL(event.request.url);
const nonce = url.searchParams.get('nonce');
event.respondWith(new Response(nonce));
});

self.addEventListener('message', function(event) {
event.data.port.postMessage({requests: requests});
requests = [];
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>Static Router: simply skip fetch handler for main resource if pattern matches</title>
<script src="/common/get-host-info.sub.js"></script>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<body>
<script>
const SCRIPT = 'resources/static-router-sw.js';
const SCOPE = 'resources/';
const REGISTERED_ROUTE_HTML =
'resources/simple-test-for-condition-main-resource.html';
const NON_REGISTERED_ROUTE_HTML = 'resources/simple.html';
const host_info = get_host_info();
const path = new URL(".", window.location).pathname;

// Register a service worker, then create an iframe at url.
function iframeTest(url, callback, name) {
return promise_test(async t => {
const reg = await service_worker_unregister_and_register(t, SCRIPT, SCOPE);
add_completion_callback(() => reg.unregister());
const worker = reg.installing;
await wait_for_state(t, worker, 'activated');
const iframe = await with_iframe(url);
const iwin = iframe.contentWindow;
t.add_cleanup(() => iframe.remove());
await callback(t, iwin, worker);
}, name);
}

function get_fetched_urls(worker) {
return new Promise(function(resolve) {
var channel = new MessageChannel();
channel.port1.onmessage = function(msg) { resolve(msg); };
worker.postMessage({port: channel.port2}, [channel.port2]);
});
}

iframeTest(REGISTERED_ROUTE_HTML, async (t, iwin, worker) => {
const fetched_urls = await get_fetched_urls(worker);
const {requests} = fetched_urls.data;
assert_equals(requests.length, 0);
assert_equals(iwin.document.body.innerText, "Here's a simple html file.");
}, 'Main resource load matched with the condition');

iframeTest(NON_REGISTERED_ROUTE_HTML, async (t, iwin, worker) => {
const fetched_urls = await get_fetched_urls(worker);
const {requests} = fetched_urls.data;
assert_equals(requests.length, 1);
assert_equals(
requests[0].url,
`${host_info['HTTPS_ORIGIN']}${path}${NON_REGISTERED_ROUTE_HTML}`);
assert_equals(requests[0].mode, 'navigate');
}, 'Main resource load not matched with the condition');

</script>
</body>

0 comments on commit 13ef5f8

Please sign in to comment.