OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <title>Service Worker: intercepting Worker script loads</title> |
| 3 <script src="/resources/testharness.js"></script> |
| 4 <script src="/resources/testharnessreport.js"></script> |
| 5 <script src="resources/test-helpers.sub.js"></script> |
| 6 <body> |
| 7 <script> |
| 8 promise_test(function(t) { |
| 9 var worker_url = 'resources/dummy-synthesized-worker.js'; |
| 10 var service_worker = 'resources/dummy-worker-interceptor.js'; |
| 11 var scope = worker_url; |
| 12 |
| 13 return service_worker_unregister_and_register(t, service_worker, scope) |
| 14 .then(function(r) { |
| 15 return wait_for_state(t, r.installing, 'activated'); |
| 16 }) |
| 17 .then(function() { |
| 18 return new Promise(function(resolve, reject) { |
| 19 var w = new Worker(worker_url); |
| 20 w.onmessage = function(e) { |
| 21 resolve(e.data); |
| 22 } |
| 23 |
| 24 w.onerror = function(e) { |
| 25 reject(e.message); |
| 26 } |
| 27 }); |
| 28 }) |
| 29 .then(function(data) { |
| 30 assert_equals(data, 'worker loading intercepted by service worker'); |
| 31 service_worker_unregister_and_done(t, scope); |
| 32 }); |
| 33 }, 'Verify worker script from uncontrolled document is intercepted by Service
Worker'); |
| 34 |
| 35 promise_test(function(t) { |
| 36 var worker_url = 'resources/dummy-same-origin-worker.js'; |
| 37 var service_worker = 'resources/dummy-worker-interceptor.js'; |
| 38 var scope = worker_url; |
| 39 |
| 40 return service_worker_unregister_and_register(t, service_worker, scope) |
| 41 .then(function(r) { |
| 42 return wait_for_state(t, r.installing, 'activated'); |
| 43 }) |
| 44 .then(function() { |
| 45 return new Promise(function(resolve, reject) { |
| 46 var w = new Worker(worker_url); |
| 47 w.onmessage = function(e) { |
| 48 resolve(e.data); |
| 49 } |
| 50 |
| 51 w.onerror = function(e) { |
| 52 reject(e.message); |
| 53 } |
| 54 }); |
| 55 }) |
| 56 .then(function(data) { |
| 57 assert_equals(data, 'dummy-worker-script loaded'); |
| 58 service_worker_unregister_and_done(t, scope); |
| 59 }); |
| 60 }, 'Verify worker script intercepted by same-origin response succeeds'); |
| 61 |
| 62 promise_test(function(t) { |
| 63 var worker_url = 'resources/dummy-cors-worker.js'; |
| 64 var service_worker = 'resources/dummy-worker-interceptor.js'; |
| 65 var scope = worker_url; |
| 66 |
| 67 return service_worker_unregister_and_register(t, service_worker, scope) |
| 68 .then(function(r) { |
| 69 return wait_for_state(t, r.installing, 'activated'); |
| 70 }) |
| 71 .then(function() { |
| 72 return new Promise(function(resolve, reject) { |
| 73 var w = new Worker(worker_url); |
| 74 w.onmessage = function(e) { |
| 75 resolve(e.data); |
| 76 } |
| 77 |
| 78 w.onerror = function(e) { |
| 79 reject(e.message); |
| 80 } |
| 81 }); |
| 82 }) |
| 83 .then(function(data) { |
| 84 assert_equals(data, 'dummy-worker-script loaded'); |
| 85 service_worker_unregister_and_done(t, scope); |
| 86 }); |
| 87 }, 'Verify worker script intercepted by cors response succeeds'); |
| 88 |
| 89 promise_test(function(t) { |
| 90 var worker_url = 'resources/dummy-no-cors-worker.js'; |
| 91 var service_worker = 'resources/dummy-worker-interceptor.js'; |
| 92 var scope = worker_url; |
| 93 |
| 94 return service_worker_unregister_and_register(t, service_worker, scope) |
| 95 .then(function(r) { |
| 96 return wait_for_state(t, r.installing, 'activated'); |
| 97 }) |
| 98 .then(function() { |
| 99 return new Promise(function(resolve, reject) { |
| 100 var w = new Worker(worker_url); |
| 101 w.onmessage = function(e) { |
| 102 resolve(e.data); |
| 103 } |
| 104 |
| 105 w.onerror = function(e) { |
| 106 reject(e); |
| 107 return true; |
| 108 } |
| 109 }); |
| 110 }) |
| 111 .then(function(data) { |
| 112 assert_unreached('intercepted no-cors worker load should fail'); |
| 113 service_worker_unregister_and_done(t, scope); |
| 114 }) |
| 115 .catch(function(e) { |
| 116 assert_true(true, 'intercepted no-cors worker load should fail'); |
| 117 service_worker_unregister_and_done(t, scope); |
| 118 }); |
| 119 }, 'Verify worker script intercepted by no-cors cross-origin response fails'); |
| 120 |
| 121 promise_test(function(t) { |
| 122 var subdoc_url = 'resources/worker-interception-iframe.https.html?bypass'; |
| 123 var service_worker = 'resources/worker-load-interceptor.js'; |
| 124 var scope = 'resources/'; |
| 125 |
| 126 window.addEventListener('message', t.step_func(on_message), false); |
| 127 function on_message(e) { |
| 128 assert_equals(e.data.results, "This load was successfully intercepted."); |
| 129 t.done(); |
| 130 } |
| 131 |
| 132 return service_worker_unregister_and_register(t, service_worker, scope) |
| 133 .then(function(r) { |
| 134 return wait_for_state(t, r.installing, 'activated'); |
| 135 }) |
| 136 .then(function() { return with_iframe(subdoc_url); }) |
| 137 .then(function(frame) { |
| 138 return new Promise(function(resolve, reject) { |
| 139 var channel = new MessageChannel(); |
| 140 channel.port1.onmessage = function(e) { |
| 141 frame.remove(); |
| 142 resolve(e.data); |
| 143 } |
| 144 |
| 145 frame.contentWindow.postMessage("GO", "*", [channel.port2]); |
| 146 }); |
| 147 }) |
| 148 .then(function(data) { |
| 149 assert_equals(data.results, 'finish'); |
| 150 service_worker_unregister_and_done(t, scope); |
| 151 }); |
| 152 }, 'Verify worker loads from controlled document are intercepted by Service Wo
rker'); |
| 153 |
| 154 </script> |
| 155 </body> |
OLD | NEW |