Skip to content

Commit 7221912

Browse files
mfalkenjgraham
authored andcommitted
Bug 1555614 [wpt PR 17096] - service worker: WPT: update Service-Worker-Allowed test to match spec., a=testonly
Automatic update from web-platform-tests service worker: WPT: update Service-Worker-Allowed test to match spec. The spec changed recently at w3c/ServiceWorker#1307. Also refactor the tests to eliminate duplicate code and properly cleanup. Bug: 968436 Change-Id: I56d649769a64d064dc7342961dd6c4763df7e037 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1637019 Reviewed-by: Ben Kelly <[email protected]> Commit-Queue: Matt Falkenhagen <[email protected]> Cr-Commit-Position: refs/heads/master@{#664886} -- wp5At-commits: 46d44f68be1d4347372cc1ff71861994916a1114 wpt-pr: 17096
1 parent dd23ca4 commit 7221912

File tree

1 file changed

+60
-76
lines changed

1 file changed

+60
-76
lines changed

testing/web-platform/tests/service-workers/service-worker/Service-Worker-Allowed-header.https.html

+60-76
Original file line numberDiff line numberDiff line change
@@ -17,88 +17,72 @@
1717
return `${url}?pipe=header(Service-Worker-Allowed,${allowed_path})`;
1818
}
1919

20-
promise_test(async t => {
21-
const script = build_script_url('/allowed-path');
22-
const scope = '/allowed-path';
23-
const registration = await service_worker_unregister_and_register(
24-
t, script, scope);
25-
assert_true(registration instanceof ServiceWorkerRegistration, 'registered');
26-
assert_equals(registration.scope, normalizeURL(scope));
27-
return registration.unregister();
28-
}, 'Registering within Service-Worker-Allowed path');
20+
// register_test is a promise_test that registers a service worker.
21+
function register_test(script, scope, description) {
22+
promise_test(async t => {
23+
t.add_cleanup(() => {
24+
return service_worker_unregister(t, scope);
25+
});
2926

30-
promise_test(async t => {
31-
const script = build_script_url(new URL('/allowed-path', document.location));
32-
const scope = '/allowed-path';
33-
const registration = await service_worker_unregister_and_register(
34-
t, script, scope);
35-
assert_true(registration instanceof ServiceWorkerRegistration, 'registered');
36-
assert_equals(registration.scope, normalizeURL(scope));
37-
return registration.unregister();
38-
}, 'Registering within Service-Worker-Allowed path (absolute URL)');
27+
const registration = await service_worker_unregister_and_register(
28+
t, script, scope);
29+
assert_true(registration instanceof ServiceWorkerRegistration, 'registered');
30+
assert_equals(registration.scope, normalizeURL(scope));
31+
}, description);
32+
}
33+
34+
// register_fail_test is like register_test but expects a SecurityError.
35+
function register_fail_test(script, scope, description) {
36+
promise_test(async t => {
37+
t.add_cleanup(() => {
38+
return service_worker_unregister(t, scope);
39+
});
40+
41+
await service_worker_unregister(t, scope);
42+
await promise_rejects(t,
43+
'SecurityError',
44+
navigator.serviceWorker.register(script, {scope}));
45+
}, description);
46+
}
47+
48+
register_test(
49+
build_script_url('/allowed-path'),
50+
'/allowed-path',
51+
'Registering within Service-Worker-Allowed path');
3952

40-
promise_test(async t => {
41-
const script = build_script_url('../allowed-path-with-parent');
42-
const scope = 'allowed-path-with-parent';
43-
const registration = await service_worker_unregister_and_register(
44-
t, script, scope);
45-
assert_true(registration instanceof ServiceWorkerRegistration, 'registered');
46-
assert_equals(registration.scope, normalizeURL(scope));
47-
return registration.unregister();
48-
}, 'Registering within Service-Worker-Allowed path with parent reference');
53+
register_test(
54+
build_script_url(new URL('/allowed-path', document.location)),
55+
'/allowed-path',
56+
'Registering within Service-Worker-Allowed path (absolute URL)');
4957

50-
promise_test(async t => {
51-
const script = build_script_url('../allowed-path');
52-
const scope = '/disallowed-path';
53-
await service_worker_unregister(t, scope);
54-
return promise_rejects(t,
55-
'SecurityError',
56-
navigator.serviceWorker.register(script, {scope: scope}),
57-
'register should fail');
58-
}, 'Registering outside Service-Worker-Allowed path');
58+
register_test(
59+
build_script_url('../allowed-path-with-parent'),
60+
'allowed-path-with-parent',
61+
'Registering within Service-Worker-Allowed path with parent reference');
5962

60-
promise_test(async t => {
61-
const script = build_script_url('../allowed-path-with-parent');
62-
const scope = '/allowed-path-with-parent';
63-
await service_worker_unregister(t, scope);
64-
return promise_rejects(t,
65-
'SecurityError',
66-
navigator.serviceWorker.register(script, {scope: scope}),
67-
'register should fail');
68-
}, 'Registering outside Service-Worker-Allowed path with parent reference');
63+
register_fail_test(
64+
build_script_url('../allowed-path'),
65+
'/disallowed-path',
66+
'Registering outside Service-Worker-Allowed path'),
6967

70-
promise_test(async t => {
71-
const script = build_script_url(
72-
host_info.HTTPS_REMOTE_ORIGIN + '/');
73-
const scope = 'resources/this-scope-is-normally-allowed'
74-
const registration = await service_worker_unregister_and_register(
75-
t, script, scope);
76-
assert_true(registration instanceof ServiceWorkerRegistration, 'registered');
77-
assert_equals(registration.scope, normalizeURL(scope));
78-
return registration.unregister();
79-
}, 'Service-Worker-Allowed is cross-origin to script, registering on a normally allowed scope');
68+
register_fail_test(
69+
build_script_url('../allowed-path-with-parent'),
70+
'/allowed-path-with-parent',
71+
'Registering outside Service-Worker-Allowed path with parent reference');
8072

81-
promise_test(async t => {
82-
const script = build_script_url(
83-
host_info.HTTPS_REMOTE_ORIGIN + '/');
84-
const scope = '/this-scope-is-normally-disallowed'
85-
const registration = await service_worker_unregister_and_register(
86-
t, script, scope);
87-
assert_true(registration instanceof ServiceWorkerRegistration, 'registered');
88-
assert_equals(registration.scope, normalizeURL(scope));
89-
return registration.unregister();
90-
}, 'Service-Worker-Allowed is cross-origin to script, registering on a normally disallowed scope');
73+
register_fail_test(
74+
build_script_url(host_info.HTTPS_REMOTE_ORIGIN + '/'),
75+
'resources/this-scope-is-normally-allowed',
76+
'Service-Worker-Allowed is cross-origin to script, registering on a normally allowed scope');
9177

92-
promise_test(async t => {
93-
const script = build_script_url(
94-
host_info.HTTPS_REMOTE_ORIGIN + '/cross-origin/',
95-
host_info.HTTPS_REMOTE_ORIGIN);
96-
const scope = '/cross-origin/';
97-
await service_worker_unregister(t, scope);
98-
return promise_rejects(t,
99-
'SecurityError',
100-
navigator.serviceWorker.register(script, {scope: scope}),
101-
'register should fail');
102-
}, 'Service-Worker-Allowed is cross-origin to page, same-origin to script');
78+
register_fail_test(
79+
build_script_url(host_info.HTTPS_REMOTE_ORIGIN + '/'),
80+
'/this-scope-is-normally-disallowed',
81+
'Service-Worker-Allowed is cross-origin to script, registering on a normally disallowed scope');
10382

83+
register_fail_test(
84+
build_script_url(host_info.HTTPS_REMOTE_ORIGIN + '/cross-origin/',
85+
host_info.HTTPS_REMOTE_ORIGIN),
86+
'/cross-origin/',
87+
'Service-Worker-Allowed is cross-origin to page, same-origin to script');
10488
</script>

0 commit comments

Comments
 (0)