|
13 | 13 | const SCOPE = '../resources/basic.html'; |
14 | 14 | const BODY_METHODS = ['arrayBuffer', 'blob', 'formData', 'json', 'text']; |
15 | 15 |
|
16 | | - async function setupRegistration(t, scope) { |
17 | | - const reg = await navigator.serviceWorker.register('../resources/sw-intercept.js', { scope }); |
| 16 | + const error1 = new Error('error1'); |
| 17 | + error1.name = 'error1'; |
| 18 | + |
| 19 | + async function setupRegistration(t, scope, service_worker) { |
| 20 | + const reg = await navigator.serviceWorker.register(service_worker, { scope }); |
18 | 21 | await wait_for_state(t, reg.installing, 'activated'); |
19 | 22 | add_completion_callback(_ => reg.unregister()); |
20 | 23 | return reg; |
|
23 | 26 | promise_test(async t => { |
24 | 27 | const suffix = "?q=aborted-not-intercepted"; |
25 | 28 | const scope = SCOPE + suffix; |
26 | | - await setupRegistration(t, scope); |
| 29 | + await setupRegistration(t, scope, '../resources/sw-intercept.js'); |
27 | 30 | const iframe = await with_iframe(scope); |
28 | 31 | add_completion_callback(_ => iframe.remove()); |
29 | 32 | const w = iframe.contentWindow; |
|
56 | 59 | for (const bodyMethod of BODY_METHODS) { |
57 | 60 | promise_test(async t => { |
58 | 61 | const scope = SCOPE + "?q=aborted-" + bodyMethod + "-rejects"; |
59 | | - await setupRegistration(t, scope); |
| 62 | + await setupRegistration(t, scope, '../resources/sw-intercept.js'); |
60 | 63 | const iframe = await with_iframe(scope); |
61 | 64 | add_completion_callback(_ => iframe.remove()); |
62 | 65 | const w = iframe.contentWindow; |
|
84 | 87 |
|
85 | 88 | promise_test(async t => { |
86 | 89 | const scope = SCOPE + "?q=aborted-stream-errors"; |
87 | | - await setupRegistration(t, scope); |
| 90 | + await setupRegistration(t, scope, '../resources/sw-intercept.js'); |
88 | 91 | const iframe = await with_iframe(scope); |
89 | 92 | add_completion_callback(_ => iframe.remove()); |
90 | 93 | const w = iframe.contentWindow; |
|
100 | 103 | await promise_rejects_dom(t, "AbortError", w.DOMException, reader.read()); |
101 | 104 | await promise_rejects_dom(t, "AbortError", w.DOMException, reader.closed); |
102 | 105 | }, "Stream errors once aborted."); |
| 106 | + |
| 107 | + promise_test(async t => { |
| 108 | + const scope = SCOPE + "?q=aborted-with-abort-reason"; |
| 109 | + await setupRegistration(t, scope, '../resources/sw-intercept.js'); |
| 110 | + const iframe = await with_iframe(scope); |
| 111 | + add_completion_callback(_ => iframe.remove()); |
| 112 | + const w = iframe.contentWindow; |
| 113 | + |
| 114 | + const controller = new w.AbortController(); |
| 115 | + const signal = controller.signal; |
| 116 | + |
| 117 | + const fetchPromise = await w.fetch('data.json', { signal }); |
| 118 | + |
| 119 | + controller.abort(error1); |
| 120 | + |
| 121 | + await promise_rejects_exactly(t, error1, fetchPromise); |
| 122 | + }, "fetch() rejects with abort reason"); |
| 123 | + |
| 124 | + promise_test(async t => { |
| 125 | + const scope = SCOPE + "?q=service-worker-observes-abort-reason"; |
| 126 | + await setupRegistration(t, scope, '../resources/sw-intercept-abort.js'); |
| 127 | + const iframe = await with_iframe(scope); |
| 128 | + add_completion_callback(_ => iframe.remove()); |
| 129 | + const w = iframe.contentWindow; |
| 130 | + |
| 131 | + const controller = new w.AbortController(); |
| 132 | + const signal = controller.signal; |
| 133 | + |
| 134 | + const fetchPromise = w.fetch('data.json', { signal }); |
| 135 | + |
| 136 | + await new Promise(resolve => { |
| 137 | + w.navigator.serviceWorker.addEventListener('message', t.step_func(event => { |
| 138 | + assert_equals(event.data, "fetch event has arrived"); |
| 139 | + resolve(); |
| 140 | + }), {once: true}); |
| 141 | + }); |
| 142 | + |
| 143 | + controller.abort(error1); |
| 144 | + |
| 145 | + await new Promise(resolve => { |
| 146 | + w.navigator.serviceWorker.addEventListener('message', t.step_func(event => { |
| 147 | + assert_equals(event.data.message, error1.message); |
| 148 | + resolve(); |
| 149 | + }), {once: true}); |
| 150 | + }); |
| 151 | + |
| 152 | + await promise_rejects_exactly(t, error1, fetchPromise); |
| 153 | + }, "Service Worker can observe the fetch abort and associated abort reason"); |
| 154 | + |
| 155 | + promise_test(async t => { |
| 156 | + let incrementing_error = new Error('error1'); |
| 157 | + incrementing_error.name = 'error1'; |
| 158 | + |
| 159 | + const scope = SCOPE + "?q=serialization-on-abort"; |
| 160 | + await setupRegistration(t, scope, '../resources/sw-intercept-abort.js'); |
| 161 | + const iframe = await with_iframe(scope); |
| 162 | + add_completion_callback(_ => iframe.remove()); |
| 163 | + const w = iframe.contentWindow; |
| 164 | + |
| 165 | + const controller = new w.AbortController(); |
| 166 | + const signal = controller.signal; |
| 167 | + |
| 168 | + const fetchPromise = w.fetch('data.json', { signal }); |
| 169 | + |
| 170 | + await new Promise(resolve => { |
| 171 | + w.navigator.serviceWorker.addEventListener('message', t.step_func(event => { |
| 172 | + assert_equals(event.data, "fetch event has arrived"); |
| 173 | + resolve(); |
| 174 | + }), {once: true}); |
| 175 | + }); |
| 176 | + |
| 177 | + controller.abort(incrementing_error); |
| 178 | + |
| 179 | + const original_error_name = incrementing_error.name; |
| 180 | + |
| 181 | + incrementing_error.name = 'error2'; |
| 182 | + |
| 183 | + await new Promise(resolve => { |
| 184 | + w.navigator.serviceWorker.addEventListener('message', t.step_func(event => { |
| 185 | + assert_equals(event.data.name, original_error_name); |
| 186 | + resolve(); |
| 187 | + }), {once: true}); |
| 188 | + }); |
| 189 | + |
| 190 | + await promise_rejects_exactly(t, incrementing_error, fetchPromise); |
| 191 | + }, "Abort reason serialization happens on abort"); |
103 | 192 | </script> |
104 | 193 | </body> |
105 | 194 | </html> |
0 commit comments