Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add baseURL to the URLPattern condition while routing rule registration #43272

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,29 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
const routerRules = {
'condition-url-pattern-source-network': [
{
condition: {
urlPattern: new URLPattern({
pathname: '/**/direct.txt'
})
},
source: 'network'
},

'condition-urlpattern-constructed-source-network': [{
condition: {urlPattern: new URLPattern({pathname: '/**/direct.txt'})},
source: 'network'
}],
'condition-urlpattern-urlpatterninit-source-network': [
{condition: {urlPattern: {pathname: '/**/direct.txt'}}, source: 'network'},
],
'condition-request-source-network': [
{
condition: {
requestMode: 'no-cors'
},
source: 'network'
}
],
'condition-or-source-network': [
{
condition: {
or: [
{
or: [
{
urlPattern: '/**/or-test/direct1.*??*'
}
],
},
{
urlPattern: '/**/or-test/direct2.*??*'
}
]
},
source: 'network'
}
'condition-urlpattern-string-source-network': [
{condition: {urlPattern: '/**/direct.txt'}, source: 'network'},
],
'condition-request-source-network':
[{condition: {requestMode: 'no-cors'}, source: 'network'}],
'condition-or-source-network': [{
condition: {
or: [
{
or: [{urlPattern: '/**/or-test/direct1.*??*'}],
},
{urlPattern: '/**/or-test/direct2.*??*'}
]
},
source: 'network'
}],
};

export {routerRules};
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<body>
<script>
const SCRIPT = 'resources/static-router-sw.js';
const ROUTER_RULE_KEY = 'condition-url-pattern-source-network'
const ROUTER_RULE_KEY = 'condition-urlpattern-constructed-source-network';
const SCOPE = 'resources/';
const REGISTERED_ROUTE = 'resources/direct.txt';
const NON_REGISTERED_ROUTE = 'resources/simple.html';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<body>
<script>
const SCRIPT = 'resources/static-router-sw.js';
const ROUTER_RULE_KEY_URL_PATTERN = 'condition-url-pattern-source-network'
const ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED =
'condition-urlpattern-constructed-source-network';
const ROUTER_RULE_KEY_URL_PATTERN_URLPATTERNINIT =
'condition-urlpattern-urlpatterninit-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_OR = 'condition-or-source-network'
const SCOPE = 'resources/';
Expand All @@ -29,11 +35,12 @@
const reg = await service_worker_unregister_and_register(
t, swURL, SCOPE, {type: 'module'});
add_completion_callback(() => reg.unregister());
await wait_for_state(t, reg.installing, 'activated');
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);
await callback(t, iwin, worker);
}, name);
}

Expand All @@ -45,18 +52,83 @@
return result;
}

iframeTest(HTML_FILE, ROUTER_RULE_KEY_URL_PATTERN, async (t, iwin) => {
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(HTML_FILE, ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED, async (t, iwin) => {
const rnd = randomString();
const response = await iwin.fetch('?nonce=' + rnd);
assert_equals(await response.text(), rnd);
}, 'Subresource load not matched with URLPattern condition');

iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN, async (t, iwin) => {
iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED, 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');

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
// 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
// network request and the fetch handler doesn't capture the response.
// The response is going to be a opaque.
const origin = get_host_info().HTTPS_REMOTE_ORIGIN;
const response = await iwin.fetch(
`${origin}/${TXT_FILE}?nonce=${rnd}`, {mode: 'no-cors'});
const fetched_urls = await get_fetched_urls(worker);
const {requests} = fetched_urls.data;
assert_equals(requests.length, 0);
assert_equals(response.type, 'opaque');
}, 'Subresource cross origin load matched with URLPattern condition via constructed object');

iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_URLPATTERNINIT, 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');

iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_URLPATTERNINIT, 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.
const rnd = randomString();
const origin = get_host_info().HTTPS_REMOTE_ORIGIN;
const response = await iwin.fetch(`${origin}/${TXT_FILE}?nonce=${rnd}`);
const fetched_urls = await get_fetched_urls(worker);
const {requests} = fetched_urls.data;
assert_equals(requests.length, 1);
assert_equals(await response.text(), rnd);
}, 'Subresource cross origin load not matched with URLPattern condition via URLPatternInit');

iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_STRING, 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 string');

iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_STRING, async (t, iwin, worker) => {
// The SW script URL is added as a baseURL when |urlPattern| is passed via
// string, 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 fetched_urls = await get_fetched_urls(worker);
const {requests} = fetched_urls.data;
assert_equals(requests.length, 1);
assert_equals(await response.text(), rnd);
}, 'Subresource cross origin load not matched with URLPattern condition via string');

iframeTest(CSV_FILE, ROUTER_RULE_KEY_REQUEST, async (t, iwin) => {
const rnd = randomString();
const response = await iwin.fetch('?nonce=' + rnd, { mode: 'no-cors' });
Expand Down