static-router-subresource.https.html (10020B)
1 <!DOCTYPE html> 2 <meta charset="utf-8"> 3 <title>Static Router: simply skip fetch handler if pattern matches</title> 4 <script src="/resources/testharness.js"></script> 5 <script src="/resources/testharnessreport.js"></script> 6 <script src="resources/test-helpers.sub.js"></script> 7 <script src="/common/get-host-info.sub.js"></script> 8 <script src="resources/static-router-helpers.sub.js"></script> 9 <body> 10 <script> 11 const SCRIPT = 'resources/static-router-sw.js'; 12 const ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED = 13 'condition-urlpattern-constructed-source-network'; 14 const ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED_IGNORE_CASE = 15 'condition-urlpattern-constructed-ignore-case-source-network'; 16 const ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED_RESPECT_CASE = 17 'condition-urlpattern-constructed-respect-case-source-network'; 18 const ROUTER_RULE_KEY_URL_PATTERN_URLPATTERNCOMPATIBLE = 19 'condition-urlpattern-urlpatterncompatible-source-network'; 20 const ROUTER_RULE_KEY_URL_PATTERN_STRING = 21 'condition-urlpattern-string-source-network'; 22 const ROUTER_RULE_KEY_REQUEST = 'condition-request-source-network' 23 const ROUTER_RULE_KEY_URL_PATTERN_STRING_CACHE = 24 'condition-urlpattern-string-source-cache'; 25 const ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED_MATCH_ALL_CACHE = 26 'condition-urlpattern-constructed-match-all-source-cache'; 27 const ROUTER_RULE_KEY_URLPATTERN_CACHE_WITH_NAME = 28 'condition-urlpattern-string-source-cache-with-name'; 29 const ROUTER_RULE_KEY_OR = 'condition-or-source-network' 30 const ROUTER_RULE_KEY_NOT = 'condition-urlpattern-not-source-network'; 31 const SCOPE = 'resources/'; 32 const HTML_FILE = 'resources/simple.html'; 33 const TXT_FILE = 'resources/direct.txt'; 34 const CSV_FILE = 'resources/simple.csv'; 35 const NOT_FILE = 'resources/not.txt'; 36 // Warning: please prepare the corresponding `*.text.headers` files, otherwise 37 // iframeTest() fails to load the following files due to MIME mismatches. 38 const OR_TEST_FILES = [ 39 'resources/or-test/direct1.text', 40 'resources/or-test/direct2.text', 41 'resources/or-test/does-not-exist.text', 42 ]; 43 44 iframeTest(HTML_FILE, ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED, async (t, iwin) => { 45 const rnd = randomString(); 46 const response = await iwin.fetch('?nonce=' + rnd); 47 assert_equals(await response.text(), rnd); 48 }, 'Subresource load not matched with URLPattern condition'); 49 50 iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED, async (t, iwin) => { 51 const rnd = randomString(); 52 const response = await iwin.fetch('?nonce=' + rnd); 53 assert_equals(await response.text(), "Network\n"); 54 }, 'Subresource load matched with URLPattern condition'); 55 56 iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED, async (t, iwin, worker) => { 57 const rnd = randomString(); 58 // Confirm that the given URLPatternCompatible has a wildcard pattern for the 59 // hostname. Also, if |urlPattern| is a consutructed URLPattern object, 60 // baseURL won't be set while adding router rules, thus it matches the cross 61 // origin request as far as other components matches. So expecting the direct 62 // network request and the fetch handler doesn't capture the response. 63 // The response is going to be a opaque. 64 const origin = get_host_info().HTTPS_REMOTE_ORIGIN; 65 const response = await iwin.fetch( 66 `${origin}/${TXT_FILE}?nonce=${rnd}`, {mode: 'no-cors'}); 67 const {requests} = await get_info_from_worker(worker); 68 assert_equals(requests.length, 0); 69 assert_equals(response.type, 'opaque'); 70 }, 'Subresource cross origin load matched with URLPattern condition via constructed object'); 71 72 iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED_IGNORE_CASE, async (t, iwin) => { 73 const rnd = randomString(); 74 const response = await iwin.fetch('?nonce=' + rnd); 75 assert_equals(await response.text(), "Network\n"); 76 }, 'Subresource load matched with ignoreCase URLPattern condition'); 77 78 iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED_RESPECT_CASE, async (t, iwin) => { 79 const rnd = randomString(); 80 const response = await iwin.fetch('?nonce=' + rnd); 81 assert_equals(await response.text(), rnd); 82 }, 'Subresource load matched without ignoreCase URLPattern condition'); 83 84 iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_URLPATTERNCOMPATIBLE, async (t, iwin) => { 85 const rnd = randomString(); 86 const response = await iwin.fetch('?nonce=' + rnd); 87 assert_equals(await response.text(), "Network\n"); 88 }, 'Subresource load matched with URLPattern condition via URLPatternCompatible'); 89 90 iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_URLPATTERNCOMPATIBLE, async (t, iwin, worker) => { 91 // The SW script URL is added as a baseURL when |urlPattern| is passed via 92 // URLPatternCompatible, and there is not |baseURL| in it. Cross 93 // origin request will go through the fetch handler because |baseURL| info 94 // complements hostname with the hostname of the SW script. 95 const rnd = randomString(); 96 const origin = get_host_info().HTTPS_REMOTE_ORIGIN; 97 const response = await iwin.fetch(`${origin}/${TXT_FILE}?nonce=${rnd}`); 98 const {requests} = await get_info_from_worker(worker); 99 assert_equals(requests.length, 1); 100 assert_equals(await response.text(), rnd); 101 }, 'Subresource cross origin load not matched with URLPattern condition via URLPatternCompatible'); 102 103 iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_STRING, async (t, iwin) => { 104 const rnd = randomString(); 105 const response = await iwin.fetch('?nonce=' + rnd); 106 assert_equals(await response.text(), "Network\n"); 107 }, 'Subresource load matched with URLPattern condition via string'); 108 109 iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_STRING, async (t, iwin, worker) => { 110 // The SW script URL is added as a baseURL when |urlPattern| is passed via 111 // string, and there is not |baseURL| in it. Cross origin request will go 112 // through the fetch handler because |baseURL| info complements hostname with 113 // the hostname of the SW script. 114 const rnd = randomString(); 115 const origin = get_host_info().HTTPS_REMOTE_ORIGIN; 116 const response = await iwin.fetch(`${origin}/${TXT_FILE}?nonce=${rnd}`); 117 const {requests} = await get_info_from_worker(worker); 118 assert_equals(requests.length, 1); 119 assert_equals(await response.text(), rnd); 120 }, 'Subresource cross origin load not matched with URLPattern condition via string'); 121 122 iframeTest(CSV_FILE, ROUTER_RULE_KEY_REQUEST, async (t, iwin) => { 123 const rnd = randomString(); 124 const response = await iwin.fetch('?nonce=' + rnd, { mode: 'no-cors' }); 125 assert_equals(await response.text(), "matched,with,non-url,conditions\n"); 126 }, 'Subresource load matched with RequestMode condition'); 127 128 iframeTest(OR_TEST_FILES[0], ROUTER_RULE_KEY_OR, async (t, iwin) => { 129 const rnd = randomString(); 130 const response = await iwin.fetch('?nonce=' + rnd); 131 assert_equals(await response.text(), "Network\n"); 132 }, 'Subresource load matched with the nested `or` condition'); 133 134 iframeTest(OR_TEST_FILES[1], ROUTER_RULE_KEY_OR, async (t, iwin) => { 135 const rnd = randomString(); 136 const response = await iwin.fetch('?nonce=' + rnd); 137 assert_equals(await response.text(), "Network\n"); 138 }, 'Subresource load matched with the next `or` condition'); 139 140 iframeTest(OR_TEST_FILES[2], ROUTER_RULE_KEY_OR, async (t, iwin) => { 141 const rnd = randomString(); 142 const response = await iwin.fetch('?nonce=' + rnd); 143 assert_equals(await response.text(), rnd); 144 }, 'Subresource load not matched with `or` condition'); 145 146 iframeTest(HTML_FILE, ROUTER_RULE_KEY_URL_PATTERN_STRING_CACHE, async (t, iwin) => { 147 // No need to set `resources/` because the request is dispatched from iframe. 148 const CACHED_FILE = 'cache.txt'; 149 const response = await iwin.fetch(CACHED_FILE); 150 assert_equals(response.status, 200); 151 assert_equals(await response.text(), "From cache"); 152 153 // This doesn't match because the cache key is wrong. 154 const rnd = randomString(); 155 const response_with_param = await iwin.fetch(`${CACHED_FILE}?nonce=${rnd}`); 156 assert_equals(response_with_param.status, 404); 157 }, 'Subresource load matched with the cache source rule'); 158 159 iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED_MATCH_ALL_CACHE, async (t, iwin, worker) => { 160 // Send a request, which is not stored in the cache, but it exists over the network. 161 const rnd = randomString(); 162 let response = await iwin.fetch(`?nonce=${rnd}`); 163 assert_equals(await response.text(), "Network\n"); 164 assert_equals(response.status, 200); 165 166 // Send a request, which is not stored in the cache, and does not exist over the network. 167 const NON_EXISTING_FILE = 'not-found.txt'; 168 response = await iwin.fetch(`${NON_EXISTING_FILE}?nonce=${randomString()}`); 169 assert_equals(response.status, 404); 170 171 // Both requests are not handled by ServiceWorker. 172 const {requests} = await get_info_from_worker(worker); 173 assert_equals(requests.length, 0); 174 }, 'Subresource load did not match with the cache and fallback to the network'); 175 176 iframeTest(HTML_FILE, ROUTER_RULE_KEY_URLPATTERN_CACHE_WITH_NAME, async (t, iwin, worker) => { 177 // No need to set `resources/` because the request is dispatched from iframe. 178 const CACHED_FILE = 'cache.txt'; 179 const response = await iwin.fetch(CACHED_FILE); 180 assert_equals(response.status, 200); 181 assert_equals(await response.text(), "From cache"); 182 183 // This doesn't match because the cache key is wrong. 184 const rnd = randomString(); 185 const response_with_param = await iwin.fetch(`${CACHED_FILE}?nonce=${rnd}`); 186 assert_equals(response_with_param.status, 404); 187 }, 'Subresource load matched with the cache source, with specifying the cache name'); 188 189 iframeTest(TXT_FILE, ROUTER_RULE_KEY_NOT, async (t, iwin) => { 190 const rnd = randomString(); 191 const response = await iwin.fetch(`${NOT_FILE}?nonce=${rnd}`); 192 assert_equals(await response.text(), rnd); 193 }, 'Subresource load should not match with the not condition'); 194 195 iframeTest(TXT_FILE, ROUTER_RULE_KEY_NOT, async (t, iwin) => { 196 const rnd = randomString(); 197 const response = await iwin.fetch('?nonce=' + rnd); 198 assert_equals(await response.text(), "Network\n"); 199 }, 'Subresource load should match with a file other than not'); 200 201 </script> 202 </body>