Chromium Code Reviews| 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 // Tests without service workers. | |
|
falken
2017/05/19 01:58:43
I suggest the same comment tweaks as the other fil
mike3
2017/05/19 15:45:32
Done.
| |
| 82 promise_test(t => setup | |
| 83 .then(() => redirected_test({url: TARGET_URL, | |
| 84 fetch_option: {}, | |
| 85 fetch_method: self.fetch, | |
| 86 cache: cache, | |
| 87 expected_redirected: false})), | |
| 88 'mode: "follow", non-intercepted request, no server redirect'); | |
| 89 | |
| 90 promise_test(t => setup | |
| 91 .then(() => redirected_test({url: REDIRECT_TO_TARGET_URL, | |
| 92 fetch_option: {}, | |
| 93 fetch_method: self.fetch, | |
| 94 cache: cache, | |
| 95 expected_redirected: true})), | |
| 96 'mode: "follow", non-intercepted request'); | |
| 97 | |
| 98 promise_test(t => setup | |
| 99 .then(() => redirected_test({url: REDIRECT_TO_TARGET_URL + '&manual', | |
| 100 fetch_option: {redirect: 'manual'}, | |
| 101 fetch_method: self.fetch, | |
| 102 cache: cache, | |
| 103 expected_redirected: false})), | |
| 104 'mode: "manual", non-intercepted request'); | |
| 105 | |
| 106 promise_test(t => setup | |
| 107 .then(() => promise_rejects( | |
| 108 t, new TypeError(), | |
| 109 self.fetch(REDIRECT_TO_TARGET_URL + '&error', | |
| 110 {redirect:'error'}), | |
| 111 'The redirect response from the server should be treated as' + | |
| 112 ' an error when the redirect flag of request was \'error\'.') ), | |
| 113 'mode: "error", non-intercepted request'); | |
| 114 | |
| 115 promise_test(t => setup | |
| 116 .then(() => redirected_test({url:'./?url=' + encodeURIComponent(TARGET_URL), | |
| 117 fetch_option: {}, | |
| 118 fetch_method: frame.contentWindow.fetch, | |
| 119 cache: cache, | |
| 120 expected_redirected: false})), | |
| 121 'mode: "follow", no mode change, no server redirect'); | |
| 122 | |
| 123 | |
| 124 // The service worker returns a redirected response. | |
| 125 promise_test(t => setup | |
| 126 .then(() => redirected_test({url: './?url=' + encodeURIComponent(REDIRECT_TO _TARGET_URL) + | |
| 127 '&original-redirect-mode=follow', | |
| 128 fetch_option: {redirect: 'follow'}, | |
| 129 fetch_method: frame.contentWindow.fetch, | |
| 130 cache: cache, | |
| 131 expected_redirected: true})), | |
| 132 'mode: "follow", no mode change'); | |
| 133 | |
| 134 promise_test(t => setup | |
| 135 .then(() => promise_rejects( | |
| 136 t, new TypeError(), | |
| 137 frame.contentWindow.fetch( | |
| 138 './?url=' + encodeURIComponent(REDIRECT_TO_TARGET_URL) + | |
| 139 '&original-redirect-mode=error&redirect-mode=follow', | |
| 140 {redirect: 'error'}), | |
| 141 'The redirected response from the service worker should be ' + | |
| 142 'treated as an error when the redirect flag of request was ' + | |
| 143 '\'error\'.')), | |
| 144 'mode: "error", mode change: "follow"'); | |
| 145 | |
| 146 promise_test(t => setup | |
| 147 .then(() => promise_rejects( | |
| 148 t, new TypeError(), | |
| 149 frame.contentWindow.fetch( | |
| 150 './?url=' + encodeURIComponent(REDIRECT_TO_TARGET_URL) + | |
| 151 '&original-redirect-mode=manual&redirect-mode=follow', | |
| 152 {redirect: 'manual'}), | |
| 153 'The redirected response from the service worker should be ' + | |
| 154 'treated as an error when the redirect flag of request was ' + | |
| 155 '\'manual\'.')), | |
| 156 'mode: "manual", mode change: "follow"'); | |
| 157 | |
| 158 // The service worker returns an opaqueredirect response. | |
|
falken
2017/05/19 01:58:43
also try to format this so it's clear it refers to
mike3
2017/05/19 15:45:32
Done.
| |
| 159 promise_test(t => setup | |
| 160 .then(() => promise_rejects( | |
| 161 t, new TypeError(), | |
| 162 frame.contentWindow.fetch( | |
| 163 './?url=' + encodeURIComponent(REDIRECT_TO_TARGET_URL) + | |
| 164 '&original-redirect-mode=follow&redirect-mode=manual', | |
| 165 {redirect: 'follow'}), | |
| 166 'The opaqueredirect response from the service worker should ' + | |
| 167 'be treated as an error when the redirect flag of request wa s' + | |
| 168 ' \'follow\'.')), | |
| 169 'mode: "follow", mode change: "redirect"'); | |
| 170 | |
| 171 promise_test(t => setup | |
| 172 .then(() => promise_rejects( | |
| 173 t, new TypeError(), | |
| 174 frame.contentWindow.fetch( | |
| 175 './?url=' + encodeURIComponent(REDIRECT_TO_TARGET_URL) + | |
| 176 '&original-redirect-mode=error&redirect-mode=manual', | |
| 177 {redirect: 'error'}), | |
| 178 'The opaqueredirect response from the service worker should ' + | |
| 179 'be treated as an error when the redirect flag of request wa s' + | |
| 180 ' \'error\'.')), | |
| 181 'mode: "error", mode change: "manual"'); | |
| 182 | |
| 183 promise_test(t => setup | |
| 184 .then(() => redirected_test({url: './?url=' + encodeURIComponent(REDIRECT_TO _TARGET_URL) + | |
| 185 '&original-redirect-mode=manual&redirect -mode=manual', | |
| 186 fetch_option: {redirect: 'manual'}, | |
| 187 fetch_method: frame.contentWindow.fetch, | |
| 188 cache: cache, | |
| 189 expected_redirected: false})), | |
| 190 'mode: "manual", no mode change'); | |
| 191 </script> | |
| OLD | NEW |