OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <title>Service Worker: Redirected response</title> |
| 3 <script src="/resources/testharness.js"></script> |
| 4 <script src="/resources/testharnessreport.js"></script> |
| 5 <script src="/common/get-host-info.sub.js"></script> |
| 6 <script src="resources/test-helpers.sub.js"></script> |
| 7 <script> |
| 8 // Tests redirect behavior. It calls fetch_method(url, fetch_option) and tests |
| 9 // the resulting response against the expected values. It also adds the |
| 10 // response to |cache| and checks the cached response matches the expected |
| 11 // values. |
| 12 // |
| 13 // |options|: a dictionary of parameters for the test |
| 14 // |options.url|: the URL to fetch |
| 15 // |options.fetch_option|: the options passed to |fetch_method| |
| 16 // |options.fetch_method|: the method used to fetch. Useful for testing an |
| 17 // iframe's fetch() vs. this page's fetch(). |
| 18 // |options.cache|: A Cache to add the response to |
| 19 // |options.expected_redirected|: The value of response.redirected |
| 20 function redirected_test(options) { |
| 21 return options.fetch_method.call(null, options.url, options.fetch_option).then
(response => { |
| 22 var cloned_response = response.clone(); |
| 23 assert_equals( |
| 24 response.redirected, options.expected_redirected, |
| 25 'The redirected flag of response must match. URL: ' + options.url); |
| 26 assert_equals( |
| 27 cloned_response.redirected, options.expected_redirected, |
| 28 'The redirected flag of cloned response must match. URL: ' + options
.url); |
| 29 return options.cache.put(options.url, response); |
| 30 }) |
| 31 .then(_ => options.cache.match(options.url)) |
| 32 .then(response => { |
| 33 assert_equals(response.redirected, options.expected_redirected, |
| 34 'The redirected flag of response in CacheStorage must match. URL: ' + |
| 35 options.url); |
| 36 }); |
| 37 } |
| 38 |
| 39 var host_info = get_host_info(); |
| 40 var REDIRECT_URL = host_info['HTTPS_ORIGIN'] + |
| 41 '/service-workers/service-worker/resources/redirect.py?Redire
ct='; |
| 42 var TARGET_URL = host_info['HTTPS_ORIGIN'] + |
| 43 '/service-workers/service-worker/resources/simple.txt'; |
| 44 var REDIRECT_TO_TARGET_URL = REDIRECT_URL + encodeURIComponent(TARGET_URL); |
| 45 var frame; |
| 46 var cache; |
| 47 var setup; |
| 48 |
| 49 promise_test(t => { |
| 50 var SCOPE = 'resources/blank.html?redirected-response'; |
| 51 var SCRIPT = 'resources/fetch-rewrite-worker.js'; |
| 52 var CACHE_NAME = 'service-workers/service-worker/redirected-response'; |
| 53 setup = service_worker_unregister_and_register(t, SCRIPT, SCOPE) |
| 54 .then(registration => { |
| 55 promise_test(function() { |
| 56 return registration.unregister(); |
| 57 }, 'restore global state (service worker registration)'); |
| 58 |
| 59 return wait_for_state(t, registration.installing, 'activated'); |
| 60 }) |
| 61 .then(_ => self.caches.open(CACHE_NAME)) |
| 62 .then(c => { |
| 63 cache = c; |
| 64 |
| 65 promise_test(function() { |
| 66 return self.caches.delete(CACHE_NAME); |
| 67 }, 'restore global state (caches)'); |
| 68 |
| 69 return with_iframe(SCOPE); |
| 70 }) |
| 71 .then(f => { |
| 72 frame = f; |
| 73 |
| 74 add_completion_callback(function() { |
| 75 f.remove(); |
| 76 }); |
| 77 }); |
| 78 return setup; |
| 79 }, 'initialize global state (service worker registration and caches)'); |
| 80 |
| 81 // =============================================================== |
| 82 // Tests for requests that are out-of-scope of the service worker. |
| 83 // =============================================================== |
| 84 promise_test(t => setup |
| 85 .then(() => redirected_test({url: TARGET_URL, |
| 86 fetch_option: {}, |
| 87 fetch_method: self.fetch, |
| 88 cache: cache, |
| 89 expected_redirected: false})), |
| 90 'mode: "follow", non-intercepted request, no server redirect'); |
| 91 |
| 92 promise_test(t => setup |
| 93 .then(() => redirected_test({url: REDIRECT_TO_TARGET_URL, |
| 94 fetch_option: {}, |
| 95 fetch_method: self.fetch, |
| 96 cache: cache, |
| 97 expected_redirected: true})), |
| 98 'mode: "follow", non-intercepted request'); |
| 99 |
| 100 promise_test(t => setup |
| 101 .then(() => redirected_test({url: REDIRECT_TO_TARGET_URL + '&manual', |
| 102 fetch_option: {redirect: 'manual'}, |
| 103 fetch_method: self.fetch, |
| 104 cache: cache, |
| 105 expected_redirected: false})), |
| 106 'mode: "manual", non-intercepted request'); |
| 107 |
| 108 promise_test(t => setup |
| 109 .then(() => promise_rejects( |
| 110 t, new TypeError(), |
| 111 self.fetch(REDIRECT_TO_TARGET_URL + '&error', |
| 112 {redirect:'error'}), |
| 113 'The redirect response from the server should be treated as'
+ |
| 114 ' an error when the redirect flag of request was \'error\'.')
), |
| 115 'mode: "error", non-intercepted request'); |
| 116 |
| 117 promise_test(t => setup |
| 118 .then(() => redirected_test({url:'./?url=' + encodeURIComponent(TARGET_URL), |
| 119 fetch_option: {}, |
| 120 fetch_method: frame.contentWindow.fetch, |
| 121 cache: cache, |
| 122 expected_redirected: false})), |
| 123 'mode: "follow", no mode change, no server redirect'); |
| 124 |
| 125 |
| 126 // ======================================================= |
| 127 // Tests for requests that are in-scope of the service worker. The service |
| 128 // worker returns a redirected response. |
| 129 // ======================================================= |
| 130 promise_test(t => setup |
| 131 .then(() => redirected_test({url: './?url=' + encodeURIComponent(REDIRECT_TO
_TARGET_URL) + |
| 132 '&original-redirect-mode=follow', |
| 133 fetch_option: {redirect: 'follow'}, |
| 134 fetch_method: frame.contentWindow.fetch, |
| 135 cache: cache, |
| 136 expected_redirected: true})), |
| 137 'mode: "follow", no mode change'); |
| 138 |
| 139 promise_test(t => setup |
| 140 .then(() => promise_rejects( |
| 141 t, new TypeError(), |
| 142 frame.contentWindow.fetch( |
| 143 './?url=' + encodeURIComponent(REDIRECT_TO_TARGET_URL) + |
| 144 '&original-redirect-mode=error&redirect-mode=follow', |
| 145 {redirect: 'error'}), |
| 146 'The redirected response from the service worker should be '
+ |
| 147 'treated as an error when the redirect flag of request was '
+ |
| 148 '\'error\'.')), |
| 149 'mode: "error", mode change: "follow"'); |
| 150 |
| 151 promise_test(t => setup |
| 152 .then(() => promise_rejects( |
| 153 t, new TypeError(), |
| 154 frame.contentWindow.fetch( |
| 155 './?url=' + encodeURIComponent(REDIRECT_TO_TARGET_URL) + |
| 156 '&original-redirect-mode=manual&redirect-mode=follow', |
| 157 {redirect: 'manual'}), |
| 158 'The redirected response from the service worker should be '
+ |
| 159 'treated as an error when the redirect flag of request was '
+ |
| 160 '\'manual\'.')), |
| 161 'mode: "manual", mode change: "follow"'); |
| 162 |
| 163 // ======================================================= |
| 164 // Tests for requests that are in-scope of the service worker. The service |
| 165 // worker returns an opaqueredirect response. |
| 166 // ======================================================= |
| 167 promise_test(t => setup |
| 168 .then(() => promise_rejects( |
| 169 t, new TypeError(), |
| 170 frame.contentWindow.fetch( |
| 171 './?url=' + encodeURIComponent(REDIRECT_TO_TARGET_URL) + |
| 172 '&original-redirect-mode=follow&redirect-mode=manual', |
| 173 {redirect: 'follow'}), |
| 174 'The opaqueredirect response from the service worker should
' + |
| 175 'be treated as an error when the redirect flag of request wa
s' + |
| 176 ' \'follow\'.')), |
| 177 'mode: "follow", mode change: "redirect"'); |
| 178 |
| 179 promise_test(t => setup |
| 180 .then(() => promise_rejects( |
| 181 t, new TypeError(), |
| 182 frame.contentWindow.fetch( |
| 183 './?url=' + encodeURIComponent(REDIRECT_TO_TARGET_URL) + |
| 184 '&original-redirect-mode=error&redirect-mode=manual', |
| 185 {redirect: 'error'}), |
| 186 'The opaqueredirect response from the service worker should
' + |
| 187 'be treated as an error when the redirect flag of request wa
s' + |
| 188 ' \'error\'.')), |
| 189 'mode: "error", mode change: "manual"'); |
| 190 |
| 191 promise_test(t => setup |
| 192 .then(() => redirected_test({url: './?url=' + encodeURIComponent(REDIRECT_TO
_TARGET_URL) + |
| 193 '&original-redirect-mode=manual&redirect
-mode=manual', |
| 194 fetch_option: {redirect: 'manual'}, |
| 195 fetch_method: frame.contentWindow.fetch, |
| 196 cache: cache, |
| 197 expected_redirected: false})), |
| 198 'mode: "manual", no mode change'); |
| 199 </script> |
OLD | NEW |