fetch-request-fallback.https.html (10101B)
1 <!DOCTYPE html> 2 <title>Service Worker: the fallback behavior of FetchEvent</title> 3 <script src="/resources/testharness.js"></script> 4 <script src="/resources/testharnessreport.js"></script> 5 <script src="/common/get-host-info.sub.js"></script> 6 <script src="resources/test-helpers.sub.js"></script> 7 <script> 8 function get_fetched_urls(worker) { 9 return new Promise(function(resolve) { 10 var channel = new MessageChannel(); 11 channel.port1.onmessage = function(msg) { resolve(msg); }; 12 worker.postMessage({port: channel.port2}, [channel.port2]); 13 }); 14 } 15 16 function check_urls(worker, expected_requests) { 17 return get_fetched_urls(worker) 18 .then(function(msg) { 19 var requests = msg.data.requests; 20 assert_object_equals(requests, expected_requests); 21 }); 22 } 23 24 var path = new URL(".", window.location).pathname; 25 var SCOPE = 'resources/fetch-request-fallback-iframe.html'; 26 var SCRIPT = 'resources/fetch-request-fallback-worker.js'; 27 var host_info = get_host_info(); 28 var BASE_URL = host_info['HTTPS_ORIGIN'] + 29 path + 'resources/fetch-access-control.py?'; 30 var BASE_PNG_URL = BASE_URL + 'PNGIMAGE&'; 31 var OTHER_BASE_URL = host_info['HTTPS_REMOTE_ORIGIN'] + 32 path + 'resources/fetch-access-control.py?'; 33 var OTHER_BASE_PNG_URL = OTHER_BASE_URL + 'PNGIMAGE&'; 34 var REDIRECT_URL = host_info['HTTPS_ORIGIN'] + 35 path + 'resources/redirect.py?Redirect='; 36 var register; 37 38 promise_test(function(t) { 39 var registration; 40 var worker; 41 42 register = service_worker_unregister_and_register(t, SCRIPT, SCOPE) 43 .then(function(r) { 44 registration = r; 45 worker = registration.installing; 46 return wait_for_state(t, worker, 'activated'); 47 }) 48 .then(function() { return with_iframe(SCOPE); }) 49 .then(function(frame) { 50 // This test should not be considered complete until after the service 51 // worker has been unregistered. Currently, `testharness.js` does not 52 // support asynchronous global "tear down" logic, so this must be 53 // expressed using a dedicated `promise_test`. Because the other 54 // sub-tests in this file are declared synchronously, this test will be 55 // the final test executed. 56 promise_test(function(t) { 57 t.add_cleanup(function() { 58 frame.remove(); 59 }); 60 return registration.unregister(); 61 }, 'restore global state'); 62 63 return {frame: frame, worker: worker}; 64 }); 65 66 return register; 67 }, 'initialize global state'); 68 69 function promise_frame_test(body, desc) { 70 promise_test(function(test) { 71 return register.then(function(result) { 72 return body(test, result.frame, result.worker); 73 }); 74 }, desc); 75 } 76 77 promise_frame_test(function(t, frame, worker) { 78 return check_urls( 79 worker, 80 [{ 81 url: host_info['HTTPS_ORIGIN'] + path + SCOPE, 82 mode: 'navigate' 83 }]); 84 }, 'The SW must intercept the request for a main resource.'); 85 86 promise_frame_test(function(t, frame, worker) { 87 return frame.contentWindow.xhr(BASE_URL) 88 .then(function() { 89 return check_urls( 90 worker, 91 [{ url: BASE_URL, mode: 'cors' }]); 92 }); 93 }, 'The SW must intercept the request of same origin XHR.'); 94 95 promise_frame_test(function(t, frame, worker) { 96 return promise_rejects_js( 97 t, 98 frame.contentWindow.Error, 99 frame.contentWindow.xhr(OTHER_BASE_URL), 100 'SW fallbacked CORS-unsupported other origin XHR should fail.') 101 .then(function() { 102 return check_urls( 103 worker, 104 [{ url: OTHER_BASE_URL, mode: 'cors' }]); 105 }); 106 }, 'The SW must intercept the request of CORS-unsupported other origin XHR.'); 107 108 promise_frame_test(function(t, frame, worker) { 109 return frame.contentWindow.xhr(OTHER_BASE_URL + 'ACAOrigin=*') 110 .then(function() { 111 return check_urls( 112 worker, 113 [{ url: OTHER_BASE_URL + 'ACAOrigin=*', mode: 'cors' }]); 114 }) 115 }, 'The SW must intercept the request of CORS-supported other origin XHR.'); 116 117 promise_frame_test(function(t, frame, worker) { 118 return frame.contentWindow.xhr( 119 REDIRECT_URL + encodeURIComponent(BASE_URL)) 120 .then(function() { 121 return check_urls( 122 worker, 123 [{ 124 url: REDIRECT_URL + encodeURIComponent(BASE_URL), 125 mode: 'cors' 126 }]); 127 }); 128 }, 'The SW must intercept only the first request of redirected XHR.'); 129 130 promise_frame_test(function(t, frame, worker) { 131 return promise_rejects_js( 132 t, 133 frame.contentWindow.Error, 134 frame.contentWindow.xhr( 135 REDIRECT_URL + encodeURIComponent(OTHER_BASE_URL)), 136 'SW fallbacked XHR which is redirected to CORS-unsupported ' + 137 'other origin should fail.') 138 .then(function() { 139 return check_urls( 140 worker, 141 [{ 142 url: REDIRECT_URL + encodeURIComponent(OTHER_BASE_URL), 143 mode: 'cors' 144 }]); 145 }); 146 }, 'The SW must intercept only the first request for XHR which is' + 147 ' redirected to CORS-unsupported other origin.'); 148 149 promise_frame_test(function(t, frame, worker) { 150 return frame.contentWindow.xhr( 151 REDIRECT_URL + 152 encodeURIComponent(OTHER_BASE_URL + 'ACAOrigin=*')) 153 .then(function() { 154 return check_urls( 155 worker, 156 [{ 157 url: REDIRECT_URL + 158 encodeURIComponent(OTHER_BASE_URL + 'ACAOrigin=*'), 159 mode: 'cors' 160 }]); 161 }); 162 }, 'The SW must intercept only the first request for XHR which is ' + 163 'redirected to CORS-supported other origin.'); 164 165 promise_frame_test(function(t, frame, worker) { 166 return frame.contentWindow.load_image(BASE_PNG_URL, '') 167 .then(function() { 168 return check_urls( 169 worker, 170 [{ url: BASE_PNG_URL, mode: 'no-cors' }]); 171 }); 172 }, 'The SW must intercept the request for image.'); 173 174 promise_frame_test(function(t, frame, worker) { 175 return frame.contentWindow.load_image(OTHER_BASE_PNG_URL, '') 176 .then(function() { 177 return check_urls( 178 worker, 179 [{ url: OTHER_BASE_PNG_URL, mode: 'no-cors' }]); 180 }); 181 }, 'The SW must intercept the request for other origin image.'); 182 183 promise_frame_test(function(t, frame, worker) { 184 return promise_rejects_js( 185 t, 186 frame.contentWindow.Error, 187 frame.contentWindow.load_image(OTHER_BASE_PNG_URL, 'anonymous'), 188 'SW fallbacked CORS-unsupported other origin image request ' + 189 'should fail.') 190 .then(function() { 191 return check_urls( 192 worker, 193 [{ url: OTHER_BASE_PNG_URL, mode: 'cors' }]); 194 }) 195 }, 'The SW must intercept the request for CORS-unsupported other ' + 196 'origin image.'); 197 198 promise_frame_test(function(t, frame, worker) { 199 return frame.contentWindow.load_image( 200 OTHER_BASE_PNG_URL + 'ACAOrigin=*', 'anonymous') 201 .then(function() { 202 return check_urls( 203 worker, 204 [{ url: OTHER_BASE_PNG_URL + 'ACAOrigin=*', mode: 'cors' }]); 205 }); 206 }, 'The SW must intercept the request for CORS-supported other ' + 207 'origin image.'); 208 209 promise_frame_test(function(t, frame, worker) { 210 return frame.contentWindow.load_image( 211 REDIRECT_URL + encodeURIComponent(BASE_PNG_URL), '') 212 .catch(function() { 213 assert_unreached( 214 'SW fallbacked redirected image request should succeed.'); 215 }) 216 .then(function() { 217 return check_urls( 218 worker, 219 [{ 220 url: REDIRECT_URL + encodeURIComponent(BASE_PNG_URL), 221 mode: 'no-cors' 222 }]); 223 }); 224 }, 'The SW must intercept only the first request for redirected ' + 225 'image resource.'); 226 227 promise_frame_test(function(t, frame, worker) { 228 return frame.contentWindow.load_image( 229 REDIRECT_URL + encodeURIComponent(OTHER_BASE_PNG_URL), '') 230 .catch(function() { 231 assert_unreached( 232 'SW fallbacked image request which is redirected to ' + 233 'other origin should succeed.'); 234 }) 235 .then(function() { 236 return check_urls( 237 worker, 238 [{ 239 url: REDIRECT_URL + encodeURIComponent(OTHER_BASE_PNG_URL), 240 mode: 'no-cors' 241 }]); 242 }) 243 }, 'The SW must intercept only the first request for image ' + 244 'resource which is redirected to other origin.'); 245 246 promise_frame_test(function(t, frame, worker) { 247 return promise_rejects_js( 248 t, 249 frame.contentWindow.Error, 250 frame.contentWindow.load_image( 251 REDIRECT_URL + encodeURIComponent(OTHER_BASE_PNG_URL), 252 'anonymous'), 253 'SW fallbacked image request which is redirected to ' + 254 'CORS-unsupported other origin should fail.') 255 .then(function() { 256 return check_urls( 257 worker, 258 [{ 259 url: REDIRECT_URL + encodeURIComponent(OTHER_BASE_PNG_URL), 260 mode: 'cors' 261 }]); 262 }); 263 }, 'The SW must intercept only the first request for image ' + 264 'resource which is redirected to CORS-unsupported other origin.'); 265 266 promise_frame_test(function(t, frame, worker) { 267 return frame.contentWindow.load_image( 268 REDIRECT_URL + 269 encodeURIComponent(OTHER_BASE_PNG_URL + 'ACAOrigin=*'), 270 'anonymous') 271 .then(function() { 272 return check_urls( 273 worker, 274 [{ 275 url: REDIRECT_URL + 276 encodeURIComponent(OTHER_BASE_PNG_URL + 'ACAOrigin=*'), 277 mode: 'cors' 278 }]); 279 }); 280 }, 'The SW must intercept only the first request for image ' + 281 'resource which is redirected to CORS-supported other origin.'); 282 </script>