0

Add WPTs for race-network-and-fetch-handler option

Adds tests for `race-network-and-fetch-handler`, which is the service
worker static routing API's router source option.

This tests check the case when the network request wins and the fetch
handler wins the race respectively, both for the main resource and
subresource.

Bug: 1519727
Change-Id: I19fe304c294f64d99d08c76be113a41d726b8d52
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5256919
Commit-Queue: Shunya Shishido <sisidovski@chromium.org>
Reviewed-by: Yoshisato Yanagisawa <yyanagisawa@chromium.org>
Reviewed-by: Kouhei Ueno <kouhei@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1255046}
This commit is contained in:
sisidovski
2024-02-01 15:13:25 +00:00
committed by Chromium LUCI CQ
parent da59ffc581
commit 8017da1289
4 changed files with 132 additions and 1 deletions
third_party/blink/web_tests/external/wpt/service-workers/service-worker/tentative/static-router

@ -1,4 +1,7 @@
import os
import time
def main(request, response):
if 'server_slow' in request.url_parts.query:
time.sleep(0.2)
return 200, [(b'Content-Type', b'text/plain')], u'Network with %s request' % request.method

@ -64,7 +64,13 @@ const routerRules = {
source: 'network'
},
{condition: {urlPattern: '/**/direct.html'}, source: 'network'}
]
],
'condition-urlpattern-string-source-race-network-and-fetch-handler': [
{
condition: {urlPattern: '/**/direct.py'},
source: 'race-network-and-fetch-handler'
},
],
};
export {routerRules};

@ -0,0 +1,57 @@
'use strict';
import {routerRules} from './router-rules.js';
import {
recordRequest,
recordError,
getRecords,
resetRecords } from './static-router-sw.sub.js';
import './imported-sw.js';
self.addEventListener('install', async e => {
e.waitUntil(caches.open('v1').then(
cache => {cache.put('cache.txt', new Response('From cache'))}));
const params = new URLSearchParams(location.search);
const key = params.get('key');
try {
await e.addRoutes(routerRules[key]);
} catch (e) {
recordError(e);
}
self.skipWaiting();
});
self.addEventListener('activate', e => {
e.waitUntil(clients.claim());
});
self.addEventListener('fetch', function(event) {
recordRequest(event.request);
const url = new URL(event.request.url);
// Force slow response
if (url.searchParams.has('sw_slow')) {
const start = Date.now();
while (true) {
if (Date.now() - start > 200) {
break;
}
}
}
const nonce = url.searchParams.get('nonce');
event.respondWith(new Response(nonce));
});
self.addEventListener('message', function(event) {
if (event.data.reset) {
resetRecords();
}
if (event.data.port) {
const {requests, errors} = getRecords();
event.data.port.postMessage({requests, errors});
}
});

@ -0,0 +1,65 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>
Static Router: routers are evaluated with the request desitnation condition.
</title>
<script src="/common/get-host-info.sub.js"></script>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/service-workers/service-worker/resources/test-helpers.sub.js">
</script>
<script src="resources/static-router-helpers.sub.js">
</script>
<body>
<script>
const ROUTER_KEY = 'condition-urlpattern-string-source-race-network-and-fetch-handler';
const SW_SRC = 'resources/static-router-race-network-and-fetch-handler-sw.js';
const FRAME_SRC = 'resources/direct.py';
promise_test(async t => {
const rnd = randomString();
const worker = await registerAndActivate(t, ROUTER_KEY, SW_SRC);
const iframe = await createIframe(t, `${FRAME_SRC}?nonce=${rnd}&server_slow`);
// Expect the response from the fetch handler.
assert_equals(iframe.contentWindow.document.body.innerText, rnd);
const {requests} = await get_info_from_worker(worker);
assert_equals(requests.length, 1);
}, 'Main resource load matched the rule with race-network-and-fetch-handler source, and the fetch handler response is faster than the server response');
promise_test(async t => {
const rnd = randomString();
const worker = await registerAndActivate(t, ROUTER_KEY, SW_SRC);
const iframe = await createIframe(t, `${FRAME_SRC}?nonce=${rnd}&sw_slow`);
// Expect the response from the netowrk request.
assert_equals(iframe.contentWindow.document.body.innerText, "Network with GET request");
// Ensure the fetch handler is also executed.
const {requests} = await get_info_from_worker(worker);
assert_equals(requests.length, 1);
}, 'Main resource load matched the rule with race-network-and-fetch-handler source, and the server reseponse is faster than the fetch handler');
promise_test(async t => {
const rnd = randomString();
const worker = await registerAndActivate(t, ROUTER_KEY, SW_SRC);
const iframe = await createIframe(t, FRAME_SRC);
// Expect the response from the fetch handler.
const response = await iframe.contentWindow.fetch(`?nonce=${rnd}&server_slow`);
assert_equals(response.status, 200);
assert_equals(await response.text(), rnd);
const {requests} = await get_info_from_worker(worker);
assert_equals(requests.length, 2);
}, 'Subresource load matched the rule with race-network-and-fetch-handler source, and the fetch handler response is faster than the server response');
promise_test(async t => {
const rnd = randomString();
const worker = await registerAndActivate(t, ROUTER_KEY, SW_SRC);
const iframe = await createIframe(t, FRAME_SRC);
// Expect the response from the network request.
const response = await iframe.contentWindow.fetch(`?nonce=${rnd}&sw_slow`);
assert_equals(response.status, 200);
assert_equals(await response.text(), "Network with GET request");
// Ensure the fetch handler is also executed.
const {requests} = await get_info_from_worker(worker);
assert_equals(requests.length, 2);
}, 'Subresource load matched the rule with race-network-and-fetch-handler source, and the server reseponse is faster than the fetch handler');
</script>
</body>