| <!DOCTYPE html> |
| <script src="../resources/testharness.js"></script> |
| <script src="../resources/testharnessreport.js"></script> |
| <script src="resources/test-helpers.js"></script> |
| <body> |
| <script> |
| var worker = 'resources/fetch-event-test-worker.js'; |
| |
| async_test(function(t) { |
| const scope = 'resources/simple.html?headers'; |
| service_worker_unregister_and_register(t, worker, scope) |
| .then(function(reg) { |
| return wait_for_state(t, reg.installing, 'activated'); |
| }) |
| .then(function() { return with_iframe(scope); }) |
| .then(function(frame) { |
| // We have this test to prevent unexpected exposure of headers to a |
| // ServiceWorker. Feel free to rebaseline this expectation if it |
| // looks good. |
| const headers = JSON.parse(frame.contentDocument.body.textContent); |
| const header_names = []; |
| for (const [name, value] of headers) { |
| header_names.push(name); |
| } |
| header_names.sort(); |
| assert_array_equals( |
| header_names, |
| ["accept", "upgrade-insecure-requests", "user-agent"], |
| 'event.request has the expected headers.'); |
| |
| frame.remove(); |
| return service_worker_unregister_and_done(t, scope); |
| }) |
| .catch(unreached_rejection(t)); |
| }, 'Service Worker headers in the request of a fetch event'); |
| |
| async_test(function(t) { |
| var scope = 'resources/simple.html?string'; |
| service_worker_unregister_and_register(t, worker, scope) |
| .then(function(reg) { |
| return wait_for_state(t, reg.installing, 'activated'); |
| }) |
| .then(function() { return with_iframe(scope); }) |
| .then(function(frame) { |
| assert_equals( |
| frame.contentDocument.body.textContent, |
| 'Test string', |
| 'Service Worker should respond to fetch with a test string'); |
| assert_equals( |
| frame.contentDocument.contentType, |
| 'text/plain', |
| 'The content type of the response created with a string should be text/plain'); |
| assert_equals( |
| frame.contentDocument.characterSet, |
| 'UTF-8', |
| 'The character set of the response created with a string should be UTF-8'); |
| frame.remove(); |
| return service_worker_unregister_and_done(t, scope); |
| }) |
| .catch(unreached_rejection(t)); |
| }, 'Service Worker responds to fetch event with string'); |
| |
| async_test(function(t) { |
| var scope = 'resources/simple.html?blob'; |
| service_worker_unregister_and_register(t, worker, scope) |
| .then(function(reg) { |
| return wait_for_state(t, reg.installing, 'activated'); |
| }) |
| .then(function() { return with_iframe(scope); }) |
| .then(function(frame) { |
| assert_equals( |
| frame.contentDocument.body.textContent, |
| 'Test blob', |
| 'Service Worker should respond to fetch with a test string'); |
| frame.remove(); |
| return service_worker_unregister_and_done(t, scope); |
| }) |
| .catch(unreached_rejection(t)); |
| }, 'Service Worker responds to fetch event with blob body'); |
| |
| async_test(function(t) { |
| var scope = 'resources/simple.html?referrer'; |
| service_worker_unregister_and_register(t, worker, scope) |
| .then(function(reg) { |
| return wait_for_state(t, reg.installing, 'activated'); |
| }) |
| .then(function() { return with_iframe(scope); }) |
| .then(function(frame) { |
| assert_equals( |
| frame.contentDocument.body.textContent, |
| 'Referrer: ' + document.location.href, |
| 'Service Worker should respond to fetch with the referrer URL'); |
| frame.remove(); |
| return service_worker_unregister_and_done(t, scope); |
| }) |
| .catch(unreached_rejection(t)); |
| }, 'Service Worker responds to fetch event with the referrer URL'); |
| |
| async_test(function(t) { |
| var scope = 'resources/simple.html?clientId'; |
| var frame; |
| var client_id1, client_id2; |
| service_worker_unregister_and_register(t, worker, scope) |
| .then(function(reg) { |
| return wait_for_state(t, reg.installing, 'activated'); |
| }) |
| .then(function() { return with_iframe(scope); }) |
| .then(function(f) { |
| frame = f; |
| assert_equals( |
| frame.contentDocument.body.textContent.substr(0, 19), |
| 'Client ID Not Found', |
| 'Service Worker should respond to navigation fetch with no ' + |
| 'client id'); |
| return frame.contentWindow.fetch('resources/other.html?clientId'); |
| }) |
| .then(function(response) { return response.text(); }) |
| .then(function(response_text) { |
| client_id1 = response_text.substr(17, 36); |
| assert_equals( |
| response_text.substr(0, 15), |
| 'Client ID Found', |
| 'Service Worker should respond to fetch with a client id'); |
| return frame.contentWindow.fetch('resources/other.html?clientId'); |
| }) |
| .then(function(response) { return response.text(); }) |
| .then(function(response_text) { |
| client_id2 = response_text.substr(17, 36); |
| assert_equals( |
| client_id1, |
| client_id2, |
| 'Service Worker should respond to another fetch from the same ' + |
| 'client with the same client id'); |
| frame.remove(); |
| return service_worker_unregister_and_done(t, scope); |
| }) |
| .catch(unreached_rejection(t)); |
| }, 'Service Worker responds to fetch event with a client id'); |
| |
| async_test(function(t) { |
| var scope = 'resources/simple.html?ignore'; |
| service_worker_unregister_and_register(t, worker, scope) |
| .then(function(reg) { |
| return wait_for_state(t, reg.installing, 'activated'); |
| }) |
| .then(function() { return with_iframe(scope); }) |
| .then(function(frame) { |
| assert_equals(frame.contentDocument.body.textContent, |
| 'Here\'s a simple html file.\n', |
| 'Response should come from fallback to native fetch'); |
| frame.remove(); |
| return service_worker_unregister_and_done(t, scope); |
| }) |
| .catch(unreached_rejection(t)); |
| }, 'Service Worker does not respond to fetch event'); |
| |
| async_test(function(t) { |
| var scope = 'resources/simple.html?null'; |
| service_worker_unregister_and_register(t, worker, scope) |
| .then(function(reg) { |
| return wait_for_state(t, reg.installing, 'activated'); |
| }) |
| .then(function() { return with_iframe(scope); }) |
| .then(function(frame) { |
| assert_equals(frame.contentDocument.body.textContent, |
| '', |
| 'Response should be the empty string'); |
| frame.remove(); |
| return service_worker_unregister_and_done(t, scope); |
| }) |
| .catch(unreached_rejection(t)); |
| }, 'Service Worker responds to fetch event with null response body'); |
| |
| async_test(function(t) { |
| var scope = 'resources/simple.html?fetch'; |
| service_worker_unregister_and_register(t, worker, scope) |
| .then(function(reg) { |
| return wait_for_state(t, reg.installing, 'activated'); |
| }) |
| .then(function() { return with_iframe(scope); }) |
| .then(function(frame) { |
| assert_equals(frame.contentDocument.body.textContent, |
| 'Here\'s an other html file.\n', |
| 'Response should come from fetched other file'); |
| frame.remove(); |
| return service_worker_unregister_and_done(t, scope); |
| }) |
| .catch(unreached_rejection(t)); |
| }, 'Service Worker fetches other file in fetch event'); |
| |
| async_test(function(t) { |
| var scope = 'resources/simple.html?form-post'; |
| var frame_name = 'xhr-post-frame'; |
| service_worker_unregister_and_register(t, worker, scope) |
| .then(function(reg) { |
| return wait_for_state(t, reg.installing, 'activated'); |
| }) |
| .then(function(sw) { |
| return new Promise(function(resolve) { |
| var frame = document.createElement('iframe'); |
| frame.name = frame_name; |
| document.body.appendChild(frame); |
| var form = document.createElement('form'); |
| form.target = frame_name; |
| form.action = scope; |
| form.method = 'post'; |
| var input1 = document.createElement('input'); |
| input1.type = 'text'; |
| input1.value = 'testValue1'; |
| input1.name = 'testName1' |
| form.appendChild(input1); |
| var input2 = document.createElement('input'); |
| input2.type = 'text'; |
| input2.value = 'testValue2'; |
| input2.name = 'testName2' |
| form.appendChild(input2); |
| document.body.appendChild(form); |
| frame.onload = function() { |
| document.body.removeChild(form); |
| resolve(frame); |
| }; |
| form.submit(); |
| }); |
| }) |
| .then(function(frame) { |
| assert_equals(frame.contentDocument.body.textContent, |
| 'POST:application/x-www-form-urlencoded:' + |
| 'testName1=testValue1&testName2=testValue2'); |
| frame.remove(); |
| return service_worker_unregister_and_done(t, scope); |
| }) |
| .catch(unreached_rejection(t)); |
| }, 'Service Worker responds to fetch event with POST form'); |
| |
| async_test(function(t) { |
| var scope = 'resources/simple.html?multiple-respond-with'; |
| service_worker_unregister_and_register(t, worker, scope) |
| .then(function(reg) { |
| return wait_for_state(t, reg.installing, 'activated'); |
| }) |
| .then(function() { return with_iframe(scope); }) |
| .then(function(frame) { |
| assert_equals( |
| frame.contentDocument.body.textContent, |
| '(0)(1)[InvalidStateError](2)[InvalidStateError]', |
| 'Multiple calls of respondWith must throw InvalidStateErrors.'); |
| frame.remove(); |
| return service_worker_unregister_and_done(t, scope); |
| }) |
| .catch(unreached_rejection(t)); |
| }, 'Multiple calls of respondWith must throw InvalidStateErrors'); |
| |
| async_test(function(t) { |
| var scope = 'resources/simple.html?used-check'; |
| var first_frame; |
| service_worker_unregister_and_register(t, worker, scope) |
| .then(function(reg) { |
| return wait_for_state(t, reg.installing, 'activated'); |
| }) |
| .then(function() { return with_iframe(scope); }) |
| .then(function(frame) { |
| assert_equals(frame.contentDocument.body.textContent, |
| 'Here\'s an other html file.\n', |
| 'Response should come from fetched other file'); |
| first_frame = frame; |
| return with_iframe(scope); |
| }) |
| .then(function(frame) { |
| // When we access to the scope in the second time, the content of the |
| // response is generated inside the ServiceWorker. The body contains |
| // the value of bodyUsed of the first response which is already |
| // consumed by FetchEvent.respondWith method. |
| assert_equals( |
| frame.contentDocument.body.textContent, |
| 'bodyUsed: true', |
| 'event.respondWith must set the used flag.'); |
| first_frame.remove(); |
| frame.remove(); |
| return service_worker_unregister_and_done(t, scope); |
| }) |
| .catch(unreached_rejection(t)); |
| }, 'Service Worker event.respondWith must set the used flag'); |
| |
| </script> |
| </body> |