|
4 | 4 | <script src="/resources/testharness.js"></script>
|
5 | 5 | <script src="/resources/testharnessreport.js"></script>
|
6 | 6 | <script src="resources/test-helpers.sub.js"></script>
|
| 7 | +<script src="/common/get-host-info.sub.js"></script> |
7 | 8 | <body>
|
8 | 9 | <script>
|
9 | 10 | const SCRIPT = 'resources/static-router-sw.js';
|
10 |
| -const ROUTER_RULE_KEY_URL_PATTERN = 'condition-url-pattern-source-network' |
| 11 | +const ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED = |
| 12 | + 'condition-urlpattern-constructed-source-network'; |
| 13 | +const ROUTER_RULE_KEY_URL_PATTERN_URLPATTERNINIT = |
| 14 | + 'condition-urlpattern-urlpatterninit-source-network'; |
| 15 | +const ROUTER_RULE_KEY_URL_PATTERN_STRING = |
| 16 | + 'condition-urlpattern-string-source-network'; |
11 | 17 | const ROUTER_RULE_KEY_REQUEST = 'condition-request-source-network'
|
12 | 18 | const ROUTER_RULE_KEY_OR = 'condition-or-source-network'
|
13 | 19 | const SCOPE = 'resources/';
|
|
29 | 35 | const reg = await service_worker_unregister_and_register(
|
30 | 36 | t, swURL, SCOPE, {type: 'module'});
|
31 | 37 | add_completion_callback(() => reg.unregister());
|
32 |
| - await wait_for_state(t, reg.installing, 'activated'); |
| 38 | + const worker = reg.installing; |
| 39 | + await wait_for_state(t, worker, 'activated'); |
33 | 40 | const iframe = await with_iframe(url);
|
34 | 41 | const iwin = iframe.contentWindow;
|
35 | 42 | t.add_cleanup(() => iframe.remove());
|
36 |
| - await callback(t, iwin); |
| 43 | + await callback(t, iwin, worker); |
37 | 44 | }, name);
|
38 | 45 | }
|
39 | 46 |
|
|
45 | 52 | return result;
|
46 | 53 | }
|
47 | 54 |
|
48 |
| -iframeTest(HTML_FILE, ROUTER_RULE_KEY_URL_PATTERN, async (t, iwin) => { |
| 55 | +function get_fetched_urls(worker) { |
| 56 | + return new Promise(function(resolve) { |
| 57 | + var channel = new MessageChannel(); |
| 58 | + channel.port1.onmessage = function(msg) { resolve(msg); }; |
| 59 | + worker.postMessage({port: channel.port2}, [channel.port2]); |
| 60 | + }); |
| 61 | +} |
| 62 | + |
| 63 | +iframeTest(HTML_FILE, ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED, async (t, iwin) => { |
49 | 64 | const rnd = randomString();
|
50 | 65 | const response = await iwin.fetch('?nonce=' + rnd);
|
51 | 66 | assert_equals(await response.text(), rnd);
|
52 | 67 | }, 'Subresource load not matched with URLPattern condition');
|
53 | 68 |
|
54 |
| -iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN, async (t, iwin) => { |
| 69 | +iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED, async (t, iwin) => { |
55 | 70 | const rnd = randomString();
|
56 | 71 | const response = await iwin.fetch('?nonce=' + rnd);
|
57 | 72 | assert_equals(await response.text(), "Network\n");
|
58 | 73 | }, 'Subresource load matched with URLPattern condition');
|
59 | 74 |
|
| 75 | +iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED, async (t, iwin, worker) => { |
| 76 | + const rnd = randomString(); |
| 77 | + // Confirm that the given URLPatternInit has a wildcard pattern for the |
| 78 | + // hostname. Also, if |urlPattern| is a consutructed URLPattern object, |
| 79 | + // baseURL won't be set while adding router rules, thus it matches the cross |
| 80 | + // origin request as far as other components matches. So expecting the direct |
| 81 | + // network request and the fetch handler doesn't capture the response. |
| 82 | + // The response is going to be a opaque. |
| 83 | + const origin = get_host_info().HTTPS_REMOTE_ORIGIN; |
| 84 | + const response = await iwin.fetch( |
| 85 | + `${origin}/${TXT_FILE}?nonce=${rnd}`, {mode: 'no-cors'}); |
| 86 | + const fetched_urls = await get_fetched_urls(worker); |
| 87 | + const {requests} = fetched_urls.data; |
| 88 | + assert_equals(requests.length, 0); |
| 89 | + assert_equals(response.type, 'opaque'); |
| 90 | +}, 'Subresource cross origin load matched with URLPattern condition via constructed object'); |
| 91 | + |
| 92 | +iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_URLPATTERNINIT, async (t, iwin) => { |
| 93 | + const rnd = randomString(); |
| 94 | + const response = await iwin.fetch('?nonce=' + rnd); |
| 95 | + assert_equals(await response.text(), "Network\n"); |
| 96 | +}, 'Subresource load matched with URLPattern condition via URLPatternInit'); |
| 97 | + |
| 98 | +iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_URLPATTERNINIT, async (t, iwin, worker) => { |
| 99 | + // The SW script URL is added as a baseURL when |urlPattern| is passed via |
| 100 | + // URLPatternInit, and there is not |baseURL| in it. Cross origin request will |
| 101 | + // go through the fetch handler because |baseURL| info complements hostname |
| 102 | + // with the hostname of the SW script. |
| 103 | + const rnd = randomString(); |
| 104 | + const origin = get_host_info().HTTPS_REMOTE_ORIGIN; |
| 105 | + const response = await iwin.fetch(`${origin}/${TXT_FILE}?nonce=${rnd}`); |
| 106 | + const fetched_urls = await get_fetched_urls(worker); |
| 107 | + const {requests} = fetched_urls.data; |
| 108 | + assert_equals(requests.length, 1); |
| 109 | + assert_equals(await response.text(), rnd); |
| 110 | +}, 'Subresource cross origin load not matched with URLPattern condition via URLPatternInit'); |
| 111 | + |
| 112 | +iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_STRING, async (t, iwin) => { |
| 113 | + const rnd = randomString(); |
| 114 | + const response = await iwin.fetch('?nonce=' + rnd); |
| 115 | + assert_equals(await response.text(), "Network\n"); |
| 116 | +}, 'Subresource load matched with URLPattern condition via string'); |
| 117 | + |
| 118 | +iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_STRING, async (t, iwin, worker) => { |
| 119 | + // The SW script URL is added as a baseURL when |urlPattern| is passed via |
| 120 | + // string, and there is not |baseURL| in it. Cross origin request will go |
| 121 | + // through the fetch handler because |baseURL| info complements hostname with |
| 122 | + // the hostname of the SW script. |
| 123 | + const rnd = randomString(); |
| 124 | + const origin = get_host_info().HTTPS_REMOTE_ORIGIN; |
| 125 | + const response = await iwin.fetch(`${origin}/${TXT_FILE}?nonce=${rnd}`); |
| 126 | + const fetched_urls = await get_fetched_urls(worker); |
| 127 | + const {requests} = fetched_urls.data; |
| 128 | + assert_equals(requests.length, 1); |
| 129 | + assert_equals(await response.text(), rnd); |
| 130 | +}, 'Subresource cross origin load not matched with URLPattern condition via string'); |
| 131 | + |
60 | 132 | iframeTest(CSV_FILE, ROUTER_RULE_KEY_REQUEST, async (t, iwin) => {
|
61 | 133 | const rnd = randomString();
|
62 | 134 | const response = await iwin.fetch('?nonce=' + rnd, { mode: 'no-cors' });
|
|
0 commit comments