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