|
17 | 17 | return `${url}?pipe=header(Service-Worker-Allowed,${allowed_path})`;
|
18 | 18 | }
|
19 | 19 |
|
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 | + }); |
29 | 26 |
|
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'); |
39 | 52 |
|
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)'); |
49 | 57 |
|
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'); |
59 | 62 |
|
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'), |
69 | 67 |
|
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'); |
80 | 72 |
|
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'); |
91 | 77 |
|
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'); |
103 | 82 |
|
| 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'); |
104 | 88 | </script>
|
0 commit comments