| <!DOCTYPE html> |
| <title>Service Worker: the fallback behavior of FetchEvent</title> |
| <script src="../resources/testharness.js"></script> |
| <script src="../resources/testharnessreport.js"></script> |
| <script src="../resources/get-host-info.js?pipe=sub"></script> |
| <script src="resources/test-helpers.js"></script> |
| <script> |
| |
| function assert_resolves(promise, description) { |
| return new Promise(function(resolve, reject) { |
| promise |
| .then( |
| function() { resolve(); }, |
| function() { reject(description); }); |
| }); |
| } |
| |
| function assert_rejects(promise, description) { |
| return new Promise(function(resolve, reject) { |
| promise |
| .then( |
| function() { reject(description); }, |
| function() { resolve(); }); |
| }); |
| } |
| |
| function get_fetched_urls(worker) { |
| return new Promise(function(resolve) { |
| var channel = new MessageChannel(); |
| channel.port1.onmessage = function(msg) { resolve(msg); }; |
| worker.postMessage({port: channel.port2}, [channel.port2]); |
| }); |
| } |
| |
| function check_urls(worker, expected_requests, description) { |
| return get_fetched_urls(worker) |
| .then(function(msg) { |
| var requests = msg.data.requests; |
| assert_object_equals(requests, expected_requests, description); |
| }); |
| } |
| |
| async_test(function(t) { |
| var SCOPE = 'resources/fetch-request-fallback-iframe.html'; |
| var SCRIPT = 'resources/fetch-request-fallback-worker.js'; |
| var host_info = get_host_info(); |
| var BASE_URL = host_info['HTTP_ORIGIN'] + |
| '/serviceworker/resources/fetch-access-control.php?'; |
| var BASE_PNG_URL = BASE_URL + 'PNGIMAGE&'; |
| var OTHER_BASE_URL = host_info['HTTP_REMOTE_ORIGIN'] + |
| '/serviceworker/resources/fetch-access-control.php?'; |
| var OTHER_BASE_PNG_URL = OTHER_BASE_URL + 'PNGIMAGE&'; |
| var REDIRECT_URL = host_info['HTTP_ORIGIN'] + |
| '/serviceworker/resources/redirect.php?Redirect='; |
| var frame; |
| var worker; |
| service_worker_unregister_and_register(t, SCRIPT, SCOPE) |
| .then(function(registration) { |
| worker = registration.installing; |
| return wait_for_state(t, worker, 'activated'); |
| }) |
| .then(function() { return with_iframe(SCOPE); }) |
| .then(function(f) { |
| frame = f; |
| return check_urls( |
| worker, |
| [{ |
| url: host_info['HTTP_ORIGIN'] + '/serviceworker/' + SCOPE, |
| mode: 'navigate' |
| }], |
| 'The SW must intercept the request for a main resourc.'); |
| }) |
| .then(function() { |
| return assert_resolves( |
| frame.contentWindow.xhr(BASE_URL), |
| 'SW fallbacked same origin XHR should succeed.'); |
| }) |
| .then(function() { |
| return check_urls( |
| worker, |
| [{ url: BASE_URL, mode: 'cors' }], |
| 'The SW must intercept the request of same origin XHR.'); |
| }) |
| .then(function() { |
| return assert_rejects( |
| frame.contentWindow.xhr(OTHER_BASE_URL), |
| 'SW fallbacked CORS-unsupported other origin XHR should fail.'); |
| }) |
| .then(function() { |
| return check_urls( |
| worker, |
| [{ url: OTHER_BASE_URL, mode: 'cors' }], |
| 'The SW must intercept the request of CORS-unsupported other ' + |
| 'origin XHR.'); |
| }) |
| .then(function() { |
| return assert_resolves( |
| frame.contentWindow.xhr(OTHER_BASE_URL + 'ACAOrigin=*'), |
| 'SW fallbacked CORS-supported other origin XHR should succeed.'); |
| }) |
| .then(function() { |
| return check_urls( |
| worker, |
| [{ url: OTHER_BASE_URL + 'ACAOrigin=*', mode: 'cors' }], |
| 'The SW must intercept the request of CORS-supported other ' + |
| 'origin XHR.'); |
| }) |
| .then(function() { |
| return assert_resolves( |
| frame.contentWindow.xhr( |
| REDIRECT_URL + encodeURIComponent(BASE_URL)), |
| 'SW fallbacked redirected XHR should succeed.'); |
| }) |
| .then(function() { |
| return check_urls( |
| worker, |
| [{ |
| url: REDIRECT_URL + encodeURIComponent(BASE_URL), |
| mode: 'cors' |
| }], |
| 'The SW must intercept only the first request of redirected ' + |
| 'XHR.'); |
| }) |
| .then(function() { |
| return assert_rejects( |
| frame.contentWindow.xhr( |
| REDIRECT_URL + encodeURIComponent(OTHER_BASE_URL)), |
| 'SW fallbacked XHR which is redirected to CORS-unsupported ' + |
| 'other origin should fail.'); |
| }) |
| .then(function() { |
| return check_urls( |
| worker, |
| [{ |
| url: REDIRECT_URL + encodeURIComponent(OTHER_BASE_URL), |
| mode: 'cors' |
| }], |
| 'The SW must intercept only the first request for XHR which is' + |
| ' redirected to CORS-unsupported other origin.'); |
| }) |
| .then(function() { |
| return assert_resolves( |
| frame.contentWindow.xhr( |
| REDIRECT_URL + |
| encodeURIComponent(OTHER_BASE_URL + 'ACAOrigin=*')), |
| 'SW fallbacked XHR which is redirected to CORS-supported other ' + |
| 'origin should succeed.'); |
| }) |
| .then(function() { |
| return check_urls( |
| worker, |
| [{ |
| url: REDIRECT_URL + |
| encodeURIComponent(OTHER_BASE_URL + 'ACAOrigin=*'), |
| mode: 'cors' |
| }], |
| 'The SW must intercept only the first request for XHR which is ' + |
| 'redirected to CORS-supported other origin.'); |
| }) |
| .then(function() { |
| return assert_resolves( |
| frame.contentWindow.load_image(BASE_PNG_URL, ''), |
| 'SW fallbacked image request should succeed.'); |
| }) |
| .then(function() { |
| return check_urls( |
| worker, |
| [{ url: BASE_PNG_URL, mode: 'no-cors' }], |
| 'The SW must intercept the request for image.'); |
| }) |
| .then(function() { |
| return assert_resolves( |
| frame.contentWindow.load_image(OTHER_BASE_PNG_URL, ''), |
| 'SW fallbacked other origin image request should succeed.'); |
| }) |
| .then(function() { |
| return check_urls( |
| worker, |
| [{ url: OTHER_BASE_PNG_URL, mode: 'no-cors' }], |
| 'The SW must intercept the request for other origin image.') |
| }) |
| .then(function() { |
| return assert_rejects( |
| frame.contentWindow.load_image(OTHER_BASE_PNG_URL, 'anonymous'), |
| 'SW fallbacked CORS-unsupported other origin image request ' + |
| 'should fail.'); |
| }) |
| .then(function() { |
| return check_urls( |
| worker, |
| [{ url: OTHER_BASE_PNG_URL, mode: 'cors' }], |
| 'The SW must intercept the request for CORS-unsupported other ' + |
| 'origin image.') |
| }) |
| .then(function() { |
| return assert_resolves( |
| frame.contentWindow.load_image( |
| OTHER_BASE_PNG_URL + 'ACAOrigin=*', 'anonymous'), |
| 'SW fallbacked CORS-supported other origin image request should' + |
| ' succeed.'); |
| }) |
| .then(function() { |
| return check_urls( |
| worker, |
| [{ url: OTHER_BASE_PNG_URL + 'ACAOrigin=*', mode: 'cors' }], |
| 'The SW must intercept the request for CORS-supported other ' + |
| 'origin image.') |
| }) |
| .then(function() { |
| return assert_resolves( |
| frame.contentWindow.load_image( |
| REDIRECT_URL + encodeURIComponent(BASE_PNG_URL), ''), |
| 'SW fallbacked redirected image request should succeed.'); |
| }) |
| .then(function() { |
| return check_urls( |
| worker, |
| [{ |
| url: REDIRECT_URL + encodeURIComponent(BASE_PNG_URL), |
| mode: 'no-cors' |
| }], |
| 'The SW must intercept only the first request for redirected ' + |
| 'image resource.'); |
| }) |
| .then(function() { |
| return assert_resolves( |
| frame.contentWindow.load_image( |
| REDIRECT_URL + encodeURIComponent(OTHER_BASE_PNG_URL), ''), |
| 'SW fallbacked image request which is redirected to other ' + |
| 'origin should succeed.'); |
| }) |
| .then(function() { |
| return check_urls( |
| worker, |
| [{ |
| url: REDIRECT_URL + encodeURIComponent(OTHER_BASE_PNG_URL), |
| mode: 'no-cors' |
| }], |
| 'The SW must intercept only the first request for image ' + |
| 'resource which is redirected to other origin.'); |
| }) |
| .then(function() { |
| return assert_rejects( |
| frame.contentWindow.load_image( |
| REDIRECT_URL + encodeURIComponent(OTHER_BASE_PNG_URL), |
| 'anonymous'), |
| 'SW fallbacked image request which is redirected to ' + |
| 'CORS-unsupported other origin should fail.'); |
| }) |
| .then(function() { |
| return check_urls( |
| worker, |
| [{ |
| url: REDIRECT_URL + encodeURIComponent(OTHER_BASE_PNG_URL), |
| mode: 'cors' |
| }], |
| 'The SW must intercept only the first request for image ' + |
| 'resource which is redirected to CORS-unsupported other origin.'); |
| }) |
| .then(function() { |
| return assert_resolves( |
| frame.contentWindow.load_image( |
| REDIRECT_URL + |
| encodeURIComponent(OTHER_BASE_PNG_URL + 'ACAOrigin=*'), |
| 'anonymous'), |
| 'SW fallbacked image request which is redirected to ' + |
| 'CORS-supported other origin should succeed.'); |
| }) |
| .then(function() { |
| return check_urls( |
| worker, |
| [{ |
| url: REDIRECT_URL + |
| encodeURIComponent(OTHER_BASE_PNG_URL + 'ACAOrigin=*'), |
| mode: 'cors' |
| }], |
| 'The SW must intercept only the first request for image ' + |
| 'resource which is redirected to CORS-supported other origin.'); |
| }) |
| .then(function() { |
| frame.remove(); |
| service_worker_unregister_and_done(t, SCOPE); |
| }) |
| .catch(unreached_rejection(t)); |
| }, 'Verify the fallback behavior of FetchEvent'); |
| </script> |