OLD | NEW |
(Empty) | |
| 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="resources/test-helpers.js?pipe=sub"></script> |
| 6 <script> |
| 7 var expected_urls = []; |
| 8 |
| 9 function xhr_fail_test(frame, url) { |
| 10 expected_urls.push(url); |
| 11 return new Promise(function(resolve, reject) { |
| 12 frame.contentWindow.xhr(url) |
| 13 .then(function(){ |
| 14 reject(msg + ' should fail.'); |
| 15 }) |
| 16 .catch(function(){ |
| 17 resolve(); |
| 18 }); |
| 19 }); |
| 20 } |
| 21 |
| 22 function xhr_succeed_test(frame, url) { |
| 23 expected_urls.push(url); |
| 24 return new Promise(function(resolve, reject) { |
| 25 frame.contentWindow.xhr(url) |
| 26 .then(function(){ |
| 27 resolve(); |
| 28 }) |
| 29 .catch(function(){ |
| 30 reject(msg + ' should succeed.'); |
| 31 }); |
| 32 }); |
| 33 } |
| 34 |
| 35 async_test(function(t) { |
| 36 var SCOPE = 'resources/fetch-request-fallback-iframe.html'; |
| 37 var SCRIPT = 'resources/fetch-request-fallback-worker.js'; |
| 38 var host_info = get_host_info(); |
| 39 var BASE_URL = host_info['HTTP_ORIGIN'] + |
| 40 '/serviceworker/resources/fetch-access-control.php?'; |
| 41 var OTHER_BASE_URL = host_info['HTTP_REMOTE_ORIGIN'] + |
| 42 '/serviceworker/resources/fetch-access-control.php?'; |
| 43 var REDIRECT_URL = host_info['HTTP_ORIGIN'] + |
| 44 '/serviceworker/resources/redirect.php?Redirect='; |
| 45 var frame; |
| 46 var worker; |
| 47 service_worker_unregister_and_register(t, SCRIPT, SCOPE) |
| 48 .then(function(registration) { |
| 49 return wait_for_update(t, registration); |
| 50 }) |
| 51 .then(function(sw) { |
| 52 worker = sw; |
| 53 return wait_for_state(t, sw, 'activated'); |
| 54 }) |
| 55 .then(function() { return with_iframe(SCOPE); }) |
| 56 .then(function(f) { |
| 57 frame = f; |
| 58 return xhr_succeed_test(frame, BASE_URL); |
| 59 }) |
| 60 .then(function(f) { |
| 61 return xhr_fail_test(frame, OTHER_BASE_URL); |
| 62 }) |
| 63 .then(function(f) { |
| 64 return xhr_succeed_test(frame, OTHER_BASE_URL + 'ACAOrigin=*'); |
| 65 }) |
| 66 .then(function(f) { |
| 67 return xhr_succeed_test(frame, |
| 68 REDIRECT_URL + encodeURIComponent(BASE_URL)); |
| 69 }) |
| 70 .then(function() { |
| 71 return xhr_fail_test( |
| 72 frame, |
| 73 REDIRECT_URL + encodeURIComponent(OTHER_BASE_URL)); |
| 74 }) |
| 75 .then(function() { |
| 76 return xhr_succeed_test( |
| 77 frame, |
| 78 REDIRECT_URL + |
| 79 encodeURIComponent(OTHER_BASE_URL + 'ACAOrigin=*')); |
| 80 }) |
| 81 .then(function() { |
| 82 return new Promise(function(resolve) { |
| 83 var channel = new MessageChannel(); |
| 84 channel.port1.onmessage = t.step_func(function(msg) { |
| 85 unload_iframe(frame); |
| 86 resolve(msg); |
| 87 }); |
| 88 worker.postMessage({port: channel.port2}, [channel.port2]); |
| 89 }); |
| 90 }) |
| 91 .then(function(msg) { |
| 92 var requests = msg.data.requests; |
| 93 assert_equals(requests.length, expected_urls.length + 1, |
| 94 'The count of the requests which are passed to the ' + |
| 95 'ServiceWorker must be correct.'); |
| 96 assert_equals(requests[0].url, new URL(SCOPE, location).toString(), |
| 97 'The first request to the SW must be the request for ' + |
| 98 'the page.'); |
| 99 assert_equals(requests[0].mode, 'no-cors', |
| 100 'The mode of the first request to the SW must be ' + |
| 101 'no-cors.'); |
| 102 for (var i = 0; i < expected_urls.length; ++i) { |
| 103 assert_equals(requests[i + 1].url, expected_urls[i], |
| 104 'The URL of the request which was passed from XHR ' + |
| 105 'to the ServiceWorker must be correct.'); |
| 106 assert_equals(requests[i + 1].mode, 'cors', |
| 107 'The mode of the request which was passed from XHR ' + |
| 108 'to the ServiceWorker must be cors.'); |
| 109 } |
| 110 service_worker_unregister_and_done(t, SCOPE); |
| 111 }) |
| 112 .catch(unreached_rejection(t)); |
| 113 }, 'Verify the fallback behavior of FetchEvent'); |
| 114 </script> |
OLD | NEW |