Skip to content

Commit 421e513

Browse files
sisidovskichromium-wpt-export-bot
authored andcommitted
Add baseURL to the URLPattern condition while routing rule registration
This behavior change was originally started in whatwg/urlpattern#182, and follows the spec change in whatwg/urlpattern#199. This CL changes the behavior of the router rule registration in the ServiceWorker Static Routing API, especially when the |urlPattern| condition receives URLPatternInit or USVString. Before this CL, the URLPatternInit input was accepted as it is, that means any unspecified fields are resulted in the wildcards (*). This behavior is inconsistent with the case when |urlPattern| accepts a string. When a string is passed, missing fields are complemented by baseURL, the SW script URL is internally treated as baseURL. After this CL, the URLPatternInit input also internally uses the SW script URL as a baseURL if it's not explicitly provided. This is achieved by the helper method `URLPattern::From()`, which was added in [1]. This change doesn't affect the case when the input is URLPattern, which means the input is the object constructed via `new URLPattern()`. [1] crrev.com/c/5053645 Bug: 1371756 Change-Id: I5cce80fde05cf18237c8b6412b00e017ff5aad5b
1 parent 0cb547a commit 421e513

File tree

3 files changed

+99
-42
lines changed

3 files changed

+99
-42
lines changed

service-workers/service-worker/tentative/static-router/resources/router-rules.js

+21-36
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,29 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44
const routerRules = {
5-
'condition-url-pattern-source-network': [
6-
{
7-
condition: {
8-
urlPattern: new URLPattern({
9-
pathname: '/**/direct.txt'
10-
})
11-
},
12-
source: 'network'
13-
},
14-
5+
'condition-urlpattern-constructed-source-network': [{
6+
condition: {urlPattern: new URLPattern({pathname: '/**/direct.txt'})},
7+
source: 'network'
8+
}],
9+
'condition-urlpattern-urlpatterninit-source-network': [
10+
{condition: {urlPattern: {pathname: '/**/direct.txt'}}, source: 'network'},
1511
],
16-
'condition-request-source-network': [
17-
{
18-
condition: {
19-
requestMode: 'no-cors'
20-
},
21-
source: 'network'
22-
}
23-
],
24-
'condition-or-source-network': [
25-
{
26-
condition: {
27-
or: [
28-
{
29-
or: [
30-
{
31-
urlPattern: '/**/or-test/direct1.*??*'
32-
}
33-
],
34-
},
35-
{
36-
urlPattern: '/**/or-test/direct2.*??*'
37-
}
38-
]
39-
},
40-
source: 'network'
41-
}
12+
'condition-urlpattern-string-source-network': [
13+
{condition: {urlPattern: '/**/direct.txt'}, source: 'network'},
4214
],
15+
'condition-request-source-network':
16+
[{condition: {requestMode: 'no-cors'}, source: 'network'}],
17+
'condition-or-source-network': [{
18+
condition: {
19+
or: [
20+
{
21+
or: [{urlPattern: '/**/or-test/direct1.*??*'}],
22+
},
23+
{urlPattern: '/**/or-test/direct2.*??*'}
24+
]
25+
},
26+
source: 'network'
27+
}],
4328
};
4429

4530
export {routerRules};

service-workers/service-worker/tentative/static-router/static-router-main-resource.https.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<body>
1111
<script>
1212
const SCRIPT = 'resources/static-router-sw.js';
13-
const ROUTER_RULE_KEY = 'condition-url-pattern-source-network'
13+
const ROUTER_RULE_KEY = 'condition-urlpattern-constructed-source-network';
1414
const SCOPE = 'resources/';
1515
const REGISTERED_ROUTE = 'resources/direct.txt';
1616
const NON_REGISTERED_ROUTE = 'resources/simple.html';

service-workers/service-worker/tentative/static-router/static-router-subresource.https.html

+77-5
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@
44
<script src="/resources/testharness.js"></script>
55
<script src="/resources/testharnessreport.js"></script>
66
<script src="resources/test-helpers.sub.js"></script>
7+
<script src="/common/get-host-info.sub.js"></script>
78
<body>
89
<script>
910
const SCRIPT = 'resources/static-router-sw.js';
10-
const ROUTER_RULE_KEY_URL_PATTERN = 'condition-url-pattern-source-network'
11+
const ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED =
12+
'condition-urlpattern-constructed-source-network';
13+
const ROUTER_RULE_KEY_URL_PATTERN_URLPATTERNINIT =
14+
'condition-urlpattern-urlpatterninit-source-network';
15+
const ROUTER_RULE_KEY_URL_PATTERN_STRING =
16+
'condition-urlpattern-string-source-network';
1117
const ROUTER_RULE_KEY_REQUEST = 'condition-request-source-network'
1218
const ROUTER_RULE_KEY_OR = 'condition-or-source-network'
1319
const SCOPE = 'resources/';
@@ -29,11 +35,12 @@
2935
const reg = await service_worker_unregister_and_register(
3036
t, swURL, SCOPE, {type: 'module'});
3137
add_completion_callback(() => reg.unregister());
32-
await wait_for_state(t, reg.installing, 'activated');
38+
const worker = reg.installing;
39+
await wait_for_state(t, worker, 'activated');
3340
const iframe = await with_iframe(url);
3441
const iwin = iframe.contentWindow;
3542
t.add_cleanup(() => iframe.remove());
36-
await callback(t, iwin);
43+
await callback(t, iwin, worker);
3744
}, name);
3845
}
3946

@@ -45,18 +52,83 @@
4552
return result;
4653
}
4754

48-
iframeTest(HTML_FILE, ROUTER_RULE_KEY_URL_PATTERN, async (t, iwin) => {
55+
function get_fetched_urls(worker) {
56+
return new Promise(function(resolve) {
57+
var channel = new MessageChannel();
58+
channel.port1.onmessage = function(msg) { resolve(msg); };
59+
worker.postMessage({port: channel.port2}, [channel.port2]);
60+
});
61+
}
62+
63+
iframeTest(HTML_FILE, ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED, async (t, iwin) => {
4964
const rnd = randomString();
5065
const response = await iwin.fetch('?nonce=' + rnd);
5166
assert_equals(await response.text(), rnd);
5267
}, 'Subresource load not matched with URLPattern condition');
5368

54-
iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN, async (t, iwin) => {
69+
iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED, async (t, iwin) => {
5570
const rnd = randomString();
5671
const response = await iwin.fetch('?nonce=' + rnd);
5772
assert_equals(await response.text(), "Network\n");
5873
}, 'Subresource load matched with URLPattern condition');
5974

75+
iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED, async (t, iwin, worker) => {
76+
const rnd = randomString();
77+
// Confirm that the given URLPatternInit has a wildcard pattern for the
78+
// hostname. Also, if |urlPattern| is a consutructed URLPattern object,
79+
// baseURL won't be set while adding router rules, thus it matches the cross
80+
// origin request as far as other components matches. So expecting the direct
81+
// network request and the fetch handler doesn't capture the response.
82+
// The response is going to be a opaque.
83+
const origin = get_host_info().HTTPS_REMOTE_ORIGIN;
84+
const response = await iwin.fetch(
85+
`${origin}/${TXT_FILE}?nonce=${rnd}`, {mode: 'no-cors'});
86+
const fetched_urls = await get_fetched_urls(worker);
87+
const {requests} = fetched_urls.data;
88+
assert_equals(requests.length, 0);
89+
assert_equals(response.type, 'opaque');
90+
}, 'Subresource cross origin load matched with URLPattern condition via constructed object');
91+
92+
iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_URLPATTERNINIT, async (t, iwin) => {
93+
const rnd = randomString();
94+
const response = await iwin.fetch('?nonce=' + rnd);
95+
assert_equals(await response.text(), "Network\n");
96+
}, 'Subresource load matched with URLPattern condition via URLPatternInit');
97+
98+
iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_URLPATTERNINIT, async (t, iwin, worker) => {
99+
// The SW script URL is added as a baseURL when |urlPattern| is passed via
100+
// URLPatternInit, and there is not |baseURL| in it. Cross origin request will
101+
// go through the fetch handler because |baseURL| info complements hostname
102+
// with the hostname of the SW script.
103+
const rnd = randomString();
104+
const origin = get_host_info().HTTPS_REMOTE_ORIGIN;
105+
const response = await iwin.fetch(`${origin}/${TXT_FILE}?nonce=${rnd}`);
106+
const fetched_urls = await get_fetched_urls(worker);
107+
const {requests} = fetched_urls.data;
108+
assert_equals(requests.length, 1);
109+
assert_equals(await response.text(), rnd);
110+
}, 'Subresource cross origin load not matched with URLPattern condition via URLPatternInit');
111+
112+
iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_STRING, async (t, iwin) => {
113+
const rnd = randomString();
114+
const response = await iwin.fetch('?nonce=' + rnd);
115+
assert_equals(await response.text(), "Network\n");
116+
}, 'Subresource load matched with URLPattern condition via string');
117+
118+
iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_STRING, async (t, iwin, worker) => {
119+
// The SW script URL is added as a baseURL when |urlPattern| is passed via
120+
// string, and there is not |baseURL| in it. Cross origin request will go
121+
// through the fetch handler because |baseURL| info complements hostname with
122+
// the hostname of the SW script.
123+
const rnd = randomString();
124+
const origin = get_host_info().HTTPS_REMOTE_ORIGIN;
125+
const response = await iwin.fetch(`${origin}/${TXT_FILE}?nonce=${rnd}`);
126+
const fetched_urls = await get_fetched_urls(worker);
127+
const {requests} = fetched_urls.data;
128+
assert_equals(requests.length, 1);
129+
assert_equals(await response.text(), rnd);
130+
}, 'Subresource cross origin load not matched with URLPattern condition via string');
131+
60132
iframeTest(CSV_FILE, ROUTER_RULE_KEY_REQUEST, async (t, iwin) => {
61133
const rnd = randomString();
62134
const response = await iwin.fetch('?nonce=' + rnd, { mode: 'no-cors' });

0 commit comments

Comments
 (0)