OLD | NEW |
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <script src="../resources/testharness.js"></script> | 2 <script src="../resources/testharness.js"></script> |
3 <script src="../resources/testharnessreport.js"></script> | 3 <script src="../resources/testharnessreport.js"></script> |
| 4 <script src="../resources/get-host-info.js"></script> |
4 <script src="resources/test-helpers.js"></script> | 5 <script src="resources/test-helpers.js"></script> |
5 <body> | 6 <body> |
6 <script> | 7 <script> |
| 8 var host_info = get_host_info(); |
| 9 |
7 function worker_for_origins(origins) { | 10 function worker_for_origins(origins) { |
8 var worker = 'resources/foreign-fetch-worker.js?'; | 11 var worker = 'foreign-fetch-worker.js?'; |
9 return worker + encodeURIComponent(JSON.stringify(origins)); | 12 return worker + encodeURIComponent(JSON.stringify(origins)); |
10 } | 13 } |
11 | 14 |
| 15 function install_cross_origin_worker(t, worker, scope) { |
| 16 return with_iframe(host_info.HTTPS_REMOTE_ORIGIN + |
| 17 '/serviceworker/resources/install-worker-helper.html') |
| 18 .then(frame => new Promise((resolve, reject) => { |
| 19 var channel = new MessageChannel(); |
| 20 frame.contentWindow.postMessage({worker: worker, |
| 21 options: {scope: scope}, |
| 22 port: channel.port1}, |
| 23 '*', [channel.port1]); |
| 24 channel.port2.onmessage = reply => { |
| 25 if (reply.data == 'success') resolve(); |
| 26 else reject(reply.data); |
| 27 }; |
| 28 })); |
| 29 } |
| 30 |
| 31 function intercepted_url(scope) { |
| 32 return host_info.HTTPS_REMOTE_ORIGIN + '/serviceworker/resources/' + |
| 33 scope + '/intercept/foo'; |
| 34 } |
| 35 |
| 36 function non_intercepted_url(scope) { |
| 37 return host_info.HTTPS_REMOTE_ORIGIN + '/serviceworker/resources/' + |
| 38 scope + '/foo'; |
| 39 } |
| 40 |
12 promise_test(t => { | 41 promise_test(t => { |
13 var scope = 'resources/foreign-fetch/scope'; | 42 var scope = 'foreign-fetch/scope/wildcard'; |
14 return service_worker_unregister_and_register( | 43 return install_cross_origin_worker(t, worker_for_origins(['*']), scope) |
15 t, worker_for_origins(['*']), scope) | 44 .then(() => promise_rejects(t, new TypeError(), |
16 .then(r => { | 45 fetch(non_intercepted_url(scope)))) |
17 add_completion_callback(() => r.unregister()); | 46 .then(() => fetch(intercepted_url(scope))) |
18 return wait_for_state(t, r.installing, 'activated'); | |
19 }) | |
20 .then(() => fetch(scope + '/foo')) | |
21 .then(response => { | |
22 assert_equals(response.status, 404); | |
23 return fetch(scope + '/intercept/foo'); | |
24 }) | |
25 .then(response => response.text()) | 47 .then(response => response.text()) |
26 .then(response_text => { | 48 .then(response_text => { |
27 assert_equals(response_text, 'Foreign Fetch'); | 49 assert_equals(response_text, 'Foreign Fetch'); |
28 }); | 50 }); |
29 }, 'Service Worker intercepts fetches in scope with wildcard origin.'); | 51 }, 'Service Worker intercepts fetches in scope with wildcard origin.'); |
30 | 52 |
31 promise_test(t => { | 53 promise_test(t => { |
32 var scope = 'resources/foreign-fetch/scope/match-origin'; | 54 var scope = 'foreign-fetch/scope/match-origin'; |
33 return service_worker_unregister_and_register( | 55 return install_cross_origin_worker( |
34 t, worker_for_origins([location.origin]), scope) | 56 t, worker_for_origins([location.origin]), scope) |
35 .then(r => { | 57 .then(() => fetch(intercepted_url(scope))) |
36 add_completion_callback(() => r.unregister()); | |
37 return wait_for_state(t, r.installing, 'activated'); | |
38 }) | |
39 .then(() => fetch(scope + '/intercept/foo')) | |
40 .then(response => response.text()) | 58 .then(response => response.text()) |
41 .then(response_text => { | 59 .then(response_text => { |
42 assert_equals(response_text, 'Foreign Fetch'); | 60 assert_equals(response_text, 'Foreign Fetch'); |
43 }); | 61 }); |
44 }, 'Service Worker intercepts fetches in scope with explicit origin.'); | 62 }, 'Service Worker intercepts fetches in scope with explicit origin.'); |
45 | 63 |
46 promise_test(t => { | 64 promise_test(t => { |
47 var scope = 'resources/foreign-fetch/scope/nomatch-origin'; | 65 var scope = 'foreign-fetch/scope/nomatch-origin'; |
48 return service_worker_unregister_and_register( | 66 return install_cross_origin_worker( |
49 t, worker_for_origins(['https://example.com']), scope) | 67 t, worker_for_origins(['https://example.com']), scope) |
50 .then(r => { | 68 .then(() => promise_rejects(t, new TypeError(), |
51 add_completion_callback(() => r.unregister()); | 69 fetch(non_intercepted_url(scope)))); |
52 return wait_for_state(t, r.installing, 'activated'); | |
53 }) | |
54 .then(() => fetch(scope + '/intercept/foo')) | |
55 .then(response => { | |
56 assert_equals(response.status, 404); | |
57 }); | |
58 }, 'Service Worker doesn\'t intercept fetches with non matching origin.'); | 70 }, 'Service Worker doesn\'t intercept fetches with non matching origin.'); |
59 | 71 |
60 promise_test(t => { | 72 promise_test(t => { |
61 var scope = 'resources/foreign-fetch/scope/origin-list'; | 73 var scope = 'foreign-fetch/scope/origin-list'; |
62 return service_worker_unregister_and_register( | 74 return install_cross_origin_worker( |
63 t, worker_for_origins([location.origin, 'https://example.com']), scope) | 75 t, worker_for_origins([location.origin, 'https://example.com']), scope) |
64 .then(r => { | 76 .then(() => fetch(intercepted_url(scope))) |
65 add_completion_callback(() => r.unregister()); | |
66 return wait_for_state(t, r.installing, 'activated'); | |
67 }) | |
68 .then(() => fetch(scope + '/intercept/foo')) | |
69 .then(response => response.text()) | 77 .then(response => response.text()) |
70 .then(response_text => { | 78 .then(response_text => { |
71 assert_equals(response_text, 'Foreign Fetch'); | 79 assert_equals(response_text, 'Foreign Fetch'); |
72 }); | 80 }); |
73 }, 'Service Worker intercepts fetches in scope with explicit origin list.'); | 81 }, 'Service Worker intercepts fetches in scope with explicit origin list.'); |
74 | 82 |
75 </script> | 83 </script> |
76 </body> | 84 </body> |
OLD | NEW |