OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <!-- This test is prefixed with `chromium.` because it asserts internal |
| 3 implementation details using non-standard API that is only available via |
| 4 Chromium's content_shell. It should be maintained only in so far as those |
| 5 implementation details need to be held stable; assertions that can be |
| 6 expressed using standard interfaces should be added to the equivalent version |
| 7 of this test in the Web Platform Tests project. --> |
| 8 <title>Service Worker: Redirected response URL list (uses Chromium-internal API)
</title> |
| 9 <script src="../resources/testharness.js"></script> |
| 10 <script src="../resources/testharnessreport.js"></script> |
| 11 <script src="../resources/get-host-info.js?pipe=sub"></script> |
| 12 <script src="resources/test-helpers.js"></script> |
| 13 <script> |
| 14 // Tests redirect behavior. It calls fetch_method(url, fetch_option) and tests |
| 15 // the resulting response against the expected values. It also adds the |
| 16 // response to |cache| and checks the cached response matches the expected |
| 17 // values. |
| 18 // |
| 19 // |options|: a dictionary of parameters for the test |
| 20 // |options.url|: the URL to fetch |
| 21 // |options.fetch_option|: the options passed to |fetch_method| |
| 22 // |options.fetch_method|: the method used to fetch. Useful for testing an |
| 23 // iframe's fetch() vs. this page's fetch(). |
| 24 // |options.cache|: A Cache to add the response to |
| 25 // |options.expected_url_list|: an array of string values describing the |
| 26 // internal URL list; this information is not |
| 27 // available via a standard API |
| 28 function redirected_test(options) { |
| 29 return options.fetch_method.call(null, options.url, options.fetch_option).then
(response => { |
| 30 var cloned_response = response.clone(); |
| 31 assert_array_equals( |
| 32 self.internals.getInternalResponseURLList(response), |
| 33 options.expected_url_list, |
| 34 'The URL list of response must match. URL: ' + options.url); |
| 35 assert_array_equals( |
| 36 self.internals.getInternalResponseURLList(cloned_response), |
| 37 options.expected_url_list, |
| 38 'The URL list of cloned response must match. URL: ' + options.url); |
| 39 return options.cache.put(options.url, response); |
| 40 }) |
| 41 .then(_ => options.cache.match(options.url)) |
| 42 .then(response => { |
| 43 assert_array_equals( |
| 44 self.internals.getInternalResponseURLList(response), |
| 45 options.expected_url_list, |
| 46 'The URL list of response in CacheStorage must match. URL: ' + |
| 47 options.url); |
| 48 }); |
| 49 } |
| 50 |
| 51 var host_info = get_host_info(); |
| 52 var REDIRECT_URL = host_info['HTTP_ORIGIN'] + |
| 53 '/serviceworker/resources/redirect.php?Redirect='; |
| 54 var TARGET_URL = host_info['HTTP_ORIGIN'] + |
| 55 '/serviceworker/resources/simple.txt'; |
| 56 var REDIRECT_TO_TARGET_URL = REDIRECT_URL + encodeURIComponent(TARGET_URL); |
| 57 var frame; |
| 58 var cache; |
| 59 var setup; |
| 60 |
| 61 promise_test(t => { |
| 62 var SCOPE = 'resources/blank.html?redirected-response'; |
| 63 var SCRIPT = 'resources/fetch-rewrite-worker.js'; |
| 64 var CACHE_NAME = 'serviceworker/redirected-response'; |
| 65 setup = new Promise(function(resolve) { |
| 66 assert_true(!!self.internals, 'Chromium "internals" are exposed.'); |
| 67 resolve(); |
| 68 }) |
| 69 .then(() => service_worker_unregister_and_register(t, SCRIPT, SCOPE)) |
| 70 .then(registration => { |
| 71 promise_test(function() { |
| 72 return registration.unregister(); |
| 73 }, 'restore global state (service worker registration)'); |
| 74 |
| 75 return wait_for_state(t, registration.installing, 'activated'); |
| 76 }) |
| 77 .then(_ => self.caches.open(CACHE_NAME)) |
| 78 .then(c => { |
| 79 cache = c; |
| 80 |
| 81 promise_test(function() { |
| 82 return self.caches.delete(CACHE_NAME); |
| 83 }, 'restore global state (caches)'); |
| 84 |
| 85 return with_iframe(SCOPE); |
| 86 }) |
| 87 .then(f => { |
| 88 frame = f; |
| 89 |
| 90 add_completion_callback(function() { |
| 91 f.remove(); |
| 92 }); |
| 93 }); |
| 94 return setup; |
| 95 }, 'initialize global state (service worker registration and caches)'); |
| 96 |
| 97 // =============================================================== |
| 98 // Tests for requests that are out-of-scope of the service worker. |
| 99 // =============================================================== |
| 100 promise_test(t => setup |
| 101 .then(() => redirected_test({url: TARGET_URL, |
| 102 fetch_option: {}, |
| 103 fetch_method: self.fetch, |
| 104 cache: cache, |
| 105 expected_url_list: [TARGET_URL]})), |
| 106 'mode: "follow", non-intercepted request, no server redirect'); |
| 107 |
| 108 promise_test(t => setup |
| 109 .then(() => redirected_test({url: REDIRECT_TO_TARGET_URL, |
| 110 fetch_option: {}, |
| 111 fetch_method: self.fetch, |
| 112 cache: cache, |
| 113 expected_url_list: [REDIRECT_TO_TARGET_URL, TARGE
T_URL]})), |
| 114 'mode: "follow", non-intercepted request'); |
| 115 |
| 116 promise_test(t => setup |
| 117 .then(() => redirected_test({url: REDIRECT_TO_TARGET_URL + '&manual', |
| 118 fetch_option: {redirect: 'manual'}, |
| 119 fetch_method: self.fetch, |
| 120 cache: cache, |
| 121 expected_url_list: [REDIRECT_TO_TARGET_URL + '&ma
nual']})), |
| 122 'mode: "manual", non-intercepted request'); |
| 123 |
| 124 promise_test(t => setup |
| 125 .then(() => redirected_test({url: './?url=' + encodeURIComponent(TARGET_URL), |
| 126 fetch_option: {}, |
| 127 fetch_method: frame.contentWindow.fetch, |
| 128 cache: cache, |
| 129 expected_url_list: [TARGET_URL]})), |
| 130 'mode: "follow", no mode change, no server redirect'); |
| 131 |
| 132 // ======================================================= |
| 133 // Tests for requests that are in-scope of the service worker. The service |
| 134 // worker returns a redirected response. |
| 135 // ======================================================= |
| 136 promise_test(t => setup |
| 137 .then(() => redirected_test({url: './?url=' + encodeURIComponent(REDIRECT_TO_T
ARGET_URL) + |
| 138 '&original-redirect-mode=follow', |
| 139 fetch_option: {redirect: 'follow'}, |
| 140 fetch_method: frame.contentWindow.fetch, |
| 141 cache: cache, |
| 142 expected_url_list: [REDIRECT_TO_TARGET_URL, TARGE
T_URL]})), |
| 143 'mode: "follow", no mode change'); |
| 144 |
| 145 promise_test(t => setup |
| 146 .then(() => redirected_test({url: './?url=' + encodeURIComponent(REDIRECT_TO_T
ARGET_URL) + |
| 147 '&original-redirect-mode=manual&redirect-
mode=manual', |
| 148 fetch_option: {redirect: 'manual'}, |
| 149 fetch_method: frame.contentWindow.fetch, |
| 150 cache: cache, |
| 151 expected_url_list: [REDIRECT_TO_TARGET_URL]})), |
| 152 'mode: "manual", no mode change'); |
| 153 </script> |
OLD | NEW |