Skip to content

Commit

Permalink
Add test for main and subresource load from cache, with cacheName
Browse files Browse the repository at this point in the history
This is the test to ensure cacheName works correctly to get resource
from the cache.

This CL also changed `URLPatternInit` phrase to `URLPatternCompatible`,
which is the super set of URLPatternInit. And this change algins with
the latest spec.
https://w3c.github.io/ServiceWorker/#dom-routercondition-urlpattern

Bug: 41492704
Change-Id: I963ceeabac834f2f076a7b4717b11b6cc28722af
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5377560
Reviewed-by: Minoru Chikamune <chikamune@chromium.org>
Reviewed-by: Yoshisato Yanagisawa <yyanagisawa@chromium.org>
Reviewed-by: Kouhei Ueno <kouhei@chromium.org>
Commit-Queue: Shunya Shishido <sisidovski@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1274701}
  • Loading branch information
sisidovski authored and chromium-wpt-export-bot committed Mar 19, 2024
1 parent f1dc220 commit 6af6dbf
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const TEST_CACHE_NAME = 'v1';

const routerRules = {
'condition-urlpattern-constructed-source-network': [{
condition: {urlPattern: new URLPattern({pathname: '/**/direct.txt'})},
Expand All @@ -6,7 +8,7 @@ const routerRules = {
'condition-urlpattern-constructed-match-all-source-cache': [
{condition: {urlPattern: new URLPattern({})}, source: 'cache'},
],
'condition-urlpattern-urlpatterninit-source-network': [
'condition-urlpattern-urlpatterncompatible-source-network': [
{condition: {urlPattern: {pathname: '/**/direct.txt'}}, source: 'network'},
],
'condition-urlpattern-string-source-network': [
Expand All @@ -15,6 +17,9 @@ const routerRules = {
'condition-urlpattern-string-source-cache': [
{condition: {urlPattern: '/**/cache.txt'}, source: 'cache'},
],
'condition-urlpattern-string-source-cache-with-name': [
{condition: {urlPattern: '/**/cache.txt'}, source: {cacheName: TEST_CACHE_NAME}},
],
'condition-urlpattern-constructed-ignore-case-source-network': [{
condition: {
urlPattern:
Expand Down Expand Up @@ -95,4 +100,4 @@ const routerRules = {
],
};

export {routerRules};
export {routerRules, TEST_CACHE_NAME as cacheName};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

import {routerRules} from './router-rules.js';
import {routerRules, cacheName} from './router-rules.js';
import {
recordRequest,
recordError,
Expand All @@ -10,7 +10,7 @@ import {
import './imported-sw.js';

self.addEventListener('install', async e => {
e.waitUntil(caches.open('v1').then(
e.waitUntil(caches.open(cacheName).then(
cache => {cache.put('cache.txt', new Response('From cache'))}));

const params = new URLSearchParams(location.search);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
const ROUTER_RULE_KEY_URLPATTERN_CACHE =
'condition-urlpattern-string-source-cache';
const ROUTER_RULE_KEY_REQUEST_CACHE = 'condition-request-navigate-source-cache';
const ROUTER_RULE_KEY_URLPATTERN_CACHE_WITH_NAME =
'condition-urlpattern-string-source-cache-with-name';
const REGISTERED_ROUTE = 'resources/direct.txt';
const CACHED_ROUTE = 'resources/cache.txt';
const NON_REGISTERED_ROUTE = 'resources/simple.html';
Expand Down Expand Up @@ -65,5 +67,10 @@
assert_equals(iwin.document.body.innerText, "Here's a simple html file.");
}, 'Main resource fallback to the network when there is no cache entry');

iframeTest(CACHED_ROUTE, ROUTER_RULE_KEY_URLPATTERN_CACHE_WITH_NAME, async (t, iwin, worker) => {
const {requests} = await get_info_from_worker(worker);
assert_equals(requests.length, 0);
assert_equals(iwin.document.body.innerText, "From cache");
}, 'Main resource load matched with the cache source, with specifying the cache name');
</script>
</body>
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@
'condition-urlpattern-constructed-ignore-case-source-network';
const ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED_RESPECT_CASE =
'condition-urlpattern-constructed-respect-case-source-network';
const ROUTER_RULE_KEY_URL_PATTERN_URLPATTERNINIT =
'condition-urlpattern-urlpatterninit-source-network';
const ROUTER_RULE_KEY_URL_PATTERN_URLPATTERNCOMPATIBLE =
'condition-urlpattern-urlpatterncompatible-source-network';
const ROUTER_RULE_KEY_URL_PATTERN_STRING =
'condition-urlpattern-string-source-network';
const ROUTER_RULE_KEY_REQUEST = 'condition-request-source-network'
const ROUTER_RULE_KEY_URL_PATTERN_STRING_CACHE =
'condition-urlpattern-string-source-cache';
const ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED_MATCH_ALL_CACHE =
'condition-urlpattern-constructed-match-all-source-cache';
const ROUTER_RULE_KEY_URLPATTERN_CACHE_WITH_NAME =
'condition-urlpattern-string-source-cache-with-name';
const ROUTER_RULE_KEY_OR = 'condition-or-source-network'
const SCOPE = 'resources/';
const HTML_FILE = 'resources/simple.html';
Expand Down Expand Up @@ -51,7 +53,7 @@

iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED, async (t, iwin, worker) => {
const rnd = randomString();
// Confirm that the given URLPatternInit has a wildcard pattern for the
// Confirm that the given URLPatternCompatible has a wildcard pattern for the
// hostname. Also, if |urlPattern| is a consutructed URLPattern object,
// baseURL won't be set while adding router rules, thus it matches the cross
// origin request as far as other components matches. So expecting the direct
Expand All @@ -77,24 +79,24 @@
assert_equals(await response.text(), rnd);
}, 'Subresource load matched without ignoreCase URLPattern condition');

iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_URLPATTERNINIT, async (t, iwin) => {
iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_URLPATTERNCOMPATIBLE, async (t, iwin) => {
const rnd = randomString();
const response = await iwin.fetch('?nonce=' + rnd);
assert_equals(await response.text(), "Network\n");
}, 'Subresource load matched with URLPattern condition via URLPatternInit');
}, 'Subresource load matched with URLPattern condition via URLPatternCompatible');

iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_URLPATTERNINIT, async (t, iwin, worker) => {
iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_URLPATTERNCOMPATIBLE, async (t, iwin, worker) => {
// The SW script URL is added as a baseURL when |urlPattern| is passed via
// URLPatternInit, and there is not |baseURL| in it. Cross origin request will
// go through the fetch handler because |baseURL| info complements hostname
// with the hostname of the SW script.
// URLPatternCompatible, and there is not |baseURL| in it. Cross
// origin request will go through the fetch handler because |baseURL| info
// complements hostname with the hostname of the SW script.
const rnd = randomString();
const origin = get_host_info().HTTPS_REMOTE_ORIGIN;
const response = await iwin.fetch(`${origin}/${TXT_FILE}?nonce=${rnd}`);
const {requests} = await get_info_from_worker(worker);
assert_equals(requests.length, 1);
assert_equals(await response.text(), rnd);
}, 'Subresource cross origin load not matched with URLPattern condition via URLPatternInit');
}, 'Subresource cross origin load not matched with URLPattern condition via URLPatternCompatible');

iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_STRING, async (t, iwin) => {
const rnd = randomString();
Expand Down Expand Up @@ -168,5 +170,19 @@
const {requests} = await get_info_from_worker(worker);
assert_equals(requests.length, 0);
}, 'Subresource load did not match with the cache and fallback to the network');

iframeTest(HTML_FILE, ROUTER_RULE_KEY_URLPATTERN_CACHE_WITH_NAME, async (t, iwin, worker) => {
// No need to set `resources/` because the request is dispatched from iframe.
const CACHED_FILE = 'cache.txt';
const response = await iwin.fetch(CACHED_FILE);
assert_equals(response.status, 200);
assert_equals(await response.text(), "From cache");

// This doesn't match because the cache key is wrong.
const rnd = randomString();
const response_with_param = await iwin.fetch(`${CACHED_FILE}?nonce=${rnd}`);
assert_equals(response_with_param.status, 404);
}, 'Subresource load matched with the cache source, with specifying the cache name');

</script>
</body>

0 comments on commit 6af6dbf

Please sign in to comment.