| <!DOCTYPE html> |
| <script src="../resources/testharness.js"></script> |
| <script src="../resources/testharnessreport.js"></script> |
| <script src="../resources/get-host-info.js"></script> |
| <script src="resources/test-helpers.js"></script> |
| <script src="resources/foreign-fetch-helpers.js"></script> |
| <body> |
| <script> |
| var host_info = get_host_info(); |
| |
| function worker_for_origins(origins) { |
| var worker = 'foreign-fetch-worker.js?'; |
| var params = {origins: origins, relscopes: ['/intercept']}; |
| return worker + encodeURIComponent(JSON.stringify(params)); |
| } |
| |
| function worker_for_scopes(relative_scopes) { |
| var worker = 'foreign-fetch-worker.js?'; |
| var params = {relscopes: relative_scopes}; |
| return worker + encodeURIComponent(JSON.stringify(params)); |
| } |
| |
| function intercepted_url(scope) { |
| return host_info.HTTPS_REMOTE_ORIGIN + '/serviceworker/resources/' + |
| scope + '/intercept/foo?basic'; |
| } |
| |
| function non_intercepted_url(scope) { |
| return host_info.HTTPS_REMOTE_ORIGIN + '/serviceworker/resources/' + |
| scope + '/foo?basic'; |
| } |
| |
| promise_test(t => { |
| var scope = 'foreign-fetch/scope/wildcard'; |
| return install_cross_origin_worker(t, worker_for_origins(['*']), scope) |
| .then(() => promise_rejects(t, new TypeError(), |
| fetch(non_intercepted_url(scope)))) |
| .then(() => fetch(intercepted_url(scope))) |
| .then(response => response.text()) |
| .then(response_text => { |
| assert_equals(response_text, 'Foreign Fetch'); |
| }); |
| }, 'Service Worker intercepts fetches in scope with wildcard origin.'); |
| |
| promise_test(t => { |
| var scope = 'foreign-fetch/scope/match-origin'; |
| return install_cross_origin_worker( |
| t, worker_for_origins([location.origin]), scope) |
| .then(() => fetch(intercepted_url(scope))) |
| .then(response => response.text()) |
| .then(response_text => { |
| assert_equals(response_text, 'Foreign Fetch'); |
| }); |
| }, 'Service Worker intercepts fetches in scope with explicit origin.'); |
| |
| promise_test(t => { |
| var scope = 'foreign-fetch/scope/nomatch-origin'; |
| return install_cross_origin_worker( |
| t, worker_for_origins(['https://example.com']), scope) |
| .then(() => promise_rejects(t, new TypeError(), |
| fetch(non_intercepted_url(scope)))); |
| }, 'Service Worker doesn\'t intercept fetches with non matching origin.'); |
| |
| promise_test(t => { |
| var scope = 'foreign-fetch/scope/origin-list'; |
| return install_cross_origin_worker( |
| t, worker_for_origins([location.origin, 'https://example.com']), scope) |
| .then(() => fetch(intercepted_url(scope))) |
| .then(response => response.text()) |
| .then(response_text => { |
| assert_equals(response_text, 'Foreign Fetch'); |
| }); |
| }, 'Service Worker intercepts fetches in scope with explicit origin list.'); |
| |
| promise_test(t => { |
| var scope = 'resources/foreign-fetch/same-origin'; |
| return service_worker_unregister_and_register( |
| t, 'resources/' + worker_for_origins(['*']), scope) |
| .then(r => { |
| add_completion_callback(() => r.unregister()); |
| return wait_for_state(t, r.installing, 'activated'); |
| }) |
| .then(() => fetch(scope + '/intercept/foo?basic')) |
| .then(response => { |
| assert_equals(response.status, 404); |
| }); |
| }, 'Service Worker does not intercept same origin fetches.'); |
| |
| promise_test(t => { |
| var scope = 'reply-to-message.html?onmessage'; |
| var remote_url = |
| host_info.HTTPS_REMOTE_ORIGIN + '/serviceworker/resources/' + scope; |
| return install_cross_origin_worker(t, worker_for_scopes(['']), scope) |
| .then(() => with_iframe(remote_url)) |
| .then(frame => new Promise(resolve => { |
| var channel = new MessageChannel(); |
| frame.contentWindow.postMessage('ping', '*', [channel.port1]); |
| channel.port2.onmessage = reply => resolve(reply.data); |
| })) |
| .then(reply => { |
| assert_equals(reply, 'ping'); |
| }); |
| }, 'Service Worker does not intercept navigations.'); |
| |
| promise_test(t => { |
| var scope = 'simple.txt?fallback'; |
| var remote_url = |
| host_info.HTTPS_REMOTE_ORIGIN + '/serviceworker/resources/' + scope; |
| return install_cross_origin_worker(t, worker_for_scopes(['']), scope) |
| .then(() => fetch(remote_url)) |
| .then(response => {}); |
| }, 'Service Worker that fallback to network should not crash.'); |
| |
| promise_test(t => { |
| var ff_scope = 'foreign-fetch/scope/controlled?basic'; |
| var remote_url = |
| host_info.HTTPS_REMOTE_ORIGIN + '/serviceworker/resources/' + ff_scope; |
| var scope = 'resources/simple.html'; |
| var worker = 'resources/empty-worker.js'; |
| return install_cross_origin_worker(t, worker_for_scopes(['']), ff_scope) |
| .then(() => service_worker_unregister_and_register(t, worker, scope)) |
| .then(r => { |
| add_completion_callback(() => r.unregister()); |
| return wait_for_state(t, r.installing, 'activated'); |
| }) |
| .then(() => with_iframe(scope)) |
| .then(frame => frame.contentWindow.fetch(remote_url)) |
| .then(response => response.text()) |
| .then(response_text => { |
| assert_equals(response_text, 'Foreign Fetch'); |
| }); |
| }, 'Foreign fetch can intercept requests from SW controlled pages.'); |
| |
| promise_test(t => { |
| var scope = 'simple.txt?meta'; |
| var remote_url = |
| host_info.HTTPS_REMOTE_ORIGIN + '/serviceworker/resources/' + scope; |
| return install_cross_origin_worker(t, worker_for_scopes(['']), scope) |
| .then(() => fetch(remote_url, {mode: 'no-cors'})) |
| .then(response => response.json()) |
| .then(response_data => { |
| assert_equals(self.location.href, response_data.referrer); |
| assert_equals(self.location.origin, response_data.origin); |
| }) |
| .then(() => with_iframe('resources/blank.html')) |
| .then(frame => { |
| var meta = frame.contentDocument.createElement('meta'); |
| meta.setAttribute('name', 'referrer'); |
| meta.setAttribute('content', 'no-referrer'); |
| frame.contentDocument.head.appendChild(meta); |
| return frame.contentWindow.fetch(remote_url, {mode: 'no-cors'}); |
| }) |
| .then(response => response.json()) |
| .then(response_data => { |
| assert_equals('', response_data.referrer); |
| assert_equals('null', response_data.origin); |
| }); |
| }, 'Referrer and origin are set correctly in ForeignFetchEvent.'); |
| |
| function fetch_from_iframe(origin, url) { |
| return with_iframe(origin + |
| '/serviceworker/resources/foreign-fetch-helper-iframe.html') |
| .then(frame => new Promise((resolve) => { |
| var channel = new MessageChannel(); |
| frame.contentWindow.postMessage({url: url, |
| port: channel.port1}, |
| '*', [channel.port1]); |
| channel.port2.onmessage = reply => resolve(reply.data); |
| })); |
| } |
| |
| promise_test(t => { |
| var scope = 'simple.txt?basic_insecure'; |
| var remote_url = |
| host_info.AUTHENTICATED_ORIGIN + '/serviceworker/resources/' + scope; |
| return install_cross_origin_worker(t, worker_for_scopes(['']), scope, |
| host_info.AUTHENTICATED_ORIGIN) |
| .then(() => fetch_from_iframe(host_info.HTTPS_REMOTE_ORIGIN, remote_url)) |
| .then(response => assert_equals(response, 'Success: Foreign Fetch')) |
| .then(() => fetch_from_iframe(host_info.UNAUTHENTICATED_ORIGIN, |
| remote_url)) |
| .then(response => assert_equals(response, |
| 'Error: TypeError: Failed to fetch')); |
| }, 'Service Worker does not intercept fetches from an insecure context.'); |
| |
| </script> |
| </body> |