OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <title>Service Worker: FetchEvent for resources</title> |
| 3 <script src="../resources/testharness.js"></script> |
| 4 <script src="../resources/testharnessreport.js"></script> |
| 5 <script src="../resources/get-host-info.js?pipe=sub"></script> |
| 6 <script src="resources/test-helpers.js"></script> |
| 7 <script> |
| 8 |
| 9 function assert_resolves(promise, description) { |
| 10 return promise.catch(function() { throw description; }); |
| 11 } |
| 12 |
| 13 function assert_rejects(promise, description) { |
| 14 return promise.then( |
| 15 function() { throw description; }, |
| 16 function() {}); |
| 17 } |
| 18 |
| 19 function iframe_test(url) { |
| 20 return new Promise(function(resolve, reject) { |
| 21 var frame = document.createElement('iframe'); |
| 22 // We can't catch the network error on iframe. So we use the timer. |
| 23 var timer = setTimeout(function() { |
| 24 reject(); |
| 25 frame.remove(); |
| 26 }, 500); |
| 27 frame.src = url; |
| 28 frame.onload = function() { |
| 29 clearTimeout(timer); |
| 30 if (frame.contentDocument.body.textContent == 'Hello world\n') |
| 31 resolve(); |
| 32 else |
| 33 reject(); |
| 34 frame.remove(); |
| 35 }; |
| 36 document.body.appendChild(frame); |
| 37 }); |
| 38 } |
| 39 |
| 40 async_test(function(t) { |
| 41 var SCOPE = 'resources/fetch-request-redirect-iframe.html'; |
| 42 var SCRIPT = 'resources/fetch-rewrite-worker.js'; |
| 43 var host_info = get_host_info(); |
| 44 var REDIRECT_URL = host_info['HTTP_ORIGIN'] + |
| 45 '/serviceworker/resources/redirect.php?Redirect='; |
| 46 var IMAGE_URL = host_info['HTTP_ORIGIN'] + '/resources/square.png'; |
| 47 var AUDIO_URL = |
| 48 host_info['HTTP_ORIGIN'] + '/media/resources/load-video.php?' + |
| 49 'name=../../../../media/content/silence.oga&type=audio/ogg'; |
| 50 var XHR_URL = host_info['HTTP_ORIGIN'] + |
| 51 '/serviceworker/resources/simple.txt'; |
| 52 var HTML_URL = host_info['HTTP_ORIGIN'] + '/resources/dummy.html'; |
| 53 |
| 54 var REDIRECT_TO_IMAGE_URL = REDIRECT_URL + encodeURIComponent(IMAGE_URL); |
| 55 var REDIRECT_TO_AUDIO_URL = REDIRECT_URL + encodeURIComponent(AUDIO_URL); |
| 56 var REDIRECT_TO_XHR_URL = REDIRECT_URL + encodeURIComponent(XHR_URL); |
| 57 var REDIRECT_TO_HTML_URL = REDIRECT_URL + encodeURIComponent(HTML_URL); |
| 58 |
| 59 var worker; |
| 60 var frame; |
| 61 service_worker_unregister_and_register(t, SCRIPT, SCOPE) |
| 62 .then(function(registration) { |
| 63 worker = registration.installing; |
| 64 return wait_for_state(t, worker, 'activated'); |
| 65 }) |
| 66 .then(function() { return with_iframe(SCOPE); }) |
| 67 .then(function(f) { |
| 68 frame = f; |
| 69 return Promise.all([ |
| 70 // XMLHttpRequest tests. |
| 71 assert_resolves(frame.contentWindow.xhr(XHR_URL), |
| 72 'Normal XHR should succeed.'), |
| 73 assert_resolves(frame.contentWindow.xhr(REDIRECT_TO_XHR_URL), |
| 74 'Redirected XHR should succeed.'), |
| 75 assert_resolves( |
| 76 frame.contentWindow.xhr( |
| 77 './?url=' + encodeURIComponent(REDIRECT_TO_XHR_URL) + |
| 78 '&redirect-mode=follow'), |
| 79 'Redirected XHR with Request.redirect=follow should succeed.'), |
| 80 assert_rejects( |
| 81 frame.contentWindow.xhr( |
| 82 './?url=' + encodeURIComponent(REDIRECT_TO_XHR_URL) + |
| 83 '&redirect-mode=error'), |
| 84 'Redirected XHR with Request.redirect=error should fail.'), |
| 85 assert_rejects( |
| 86 frame.contentWindow.xhr( |
| 87 './?url=' + encodeURIComponent(REDIRECT_TO_XHR_URL) + |
| 88 '&redirect-mode=manual'), |
| 89 'Redirected XHR with Request.redirect=manual should fail.'), |
| 90 |
| 91 // Image loading tests. |
| 92 assert_resolves(frame.contentWindow.load_image(IMAGE_URL), |
| 93 'Normal image resource should be loaded.'), |
| 94 assert_resolves( |
| 95 frame.contentWindow.load_image(REDIRECT_TO_IMAGE_URL), |
| 96 'Redirected image resource should be loaded.'), |
| 97 assert_resolves( |
| 98 frame.contentWindow.load_image( |
| 99 './?url=' + encodeURIComponent(REDIRECT_TO_IMAGE_URL) + |
| 100 '&redirect-mode=follow'), |
| 101 'Loading redirected image with Request.redirect=follow should' + |
| 102 ' succeed.'), |
| 103 assert_rejects( |
| 104 frame.contentWindow.load_image( |
| 105 './?url=' + encodeURIComponent(REDIRECT_TO_IMAGE_URL) + |
| 106 '&redirect-mode=error'), |
| 107 'Loading redirected image with Request.redirect=error should ' + |
| 108 'fail.'), |
| 109 assert_rejects( |
| 110 frame.contentWindow.load_image( |
| 111 './?url=' + encodeURIComponent(REDIRECT_TO_IMAGE_URL) + |
| 112 '&redirect-mode=manual'), |
| 113 'Loading redirected image with Request.redirect=manual should' + |
| 114 ' fail.'), |
| 115 |
| 116 // Audio loading tests. |
| 117 assert_resolves(frame.contentWindow.load_audio(AUDIO_URL), |
| 118 'Normal audio resource should be loaded.'), |
| 119 assert_resolves( |
| 120 frame.contentWindow.load_audio(REDIRECT_TO_AUDIO_URL), |
| 121 'Redirected audio resource should be loaded.'), |
| 122 assert_resolves( |
| 123 frame.contentWindow.load_audio( |
| 124 './?url=' + encodeURIComponent(REDIRECT_TO_AUDIO_URL) + |
| 125 '&redirect-mode=follow'), |
| 126 'Loading redirected audio with Request.redirect=follow should' + |
| 127 ' succeed.'), |
| 128 assert_rejects( |
| 129 frame.contentWindow.load_audio( |
| 130 './?url=' + encodeURIComponent(REDIRECT_TO_AUDIO_URL) + |
| 131 '&redirect-mode=error'), |
| 132 'Loading redirected audio with Request.redirect=error should ' + |
| 133 'fail.'), |
| 134 assert_rejects( |
| 135 frame.contentWindow.load_audio( |
| 136 './?url=' + encodeURIComponent(REDIRECT_TO_AUDIO_URL) + |
| 137 '&redirect-mode=manual'), |
| 138 'Loading redirected audio with Request.redirect=manual should' + |
| 139 ' fail.'), |
| 140 |
| 141 // Iframe tests. |
| 142 assert_resolves(iframe_test(HTML_URL), |
| 143 'Normal iframe loading should succeed.'), |
| 144 assert_resolves( |
| 145 iframe_test(REDIRECT_TO_HTML_URL), |
| 146 'Normal redirected iframe loading should succeed.'), |
| 147 assert_resolves( |
| 148 iframe_test(SCOPE + '?url=' + |
| 149 encodeURIComponent(REDIRECT_TO_HTML_URL) + |
| 150 '&redirect-mode=follow'), |
| 151 'Redirected iframe loading with Request.redirect=follow should'+ |
| 152 ' succeed.'), |
| 153 assert_rejects( |
| 154 iframe_test(SCOPE + '?url=' + |
| 155 encodeURIComponent(REDIRECT_TO_HTML_URL) + |
| 156 '&redirect-mode=error'), |
| 157 'Redirected iframe loading with Request.redirect=error should '+ |
| 158 'fail.'), |
| 159 assert_resolves( |
| 160 iframe_test(SCOPE + '?url=' + |
| 161 encodeURIComponent(REDIRECT_TO_HTML_URL) + |
| 162 '&redirect-mode=manual'), |
| 163 'Redirected iframe loading with Request.redirect=manual should'+ |
| 164 ' succeed.'), |
| 165 ]); |
| 166 }) |
| 167 .then(function() { |
| 168 frame.remove(); |
| 169 service_worker_unregister_and_done(t, SCOPE); |
| 170 }) |
| 171 .catch(unreached_rejection(t)); |
| 172 }, 'Verify redirect mode of Fetch API and ServiceWorker FetchEvent.'); |
| 173 </script> |
OLD | NEW |