static-router-mutiple-conditions.https.html (3620B)
1 <!DOCTYPE html> 2 <meta charset="utf-8"> 3 <title> 4 Static Router: routers are evaluated with the request method condition. 5 </title> 6 <script src="/common/get-host-info.sub.js"></script> 7 <script src="/resources/testharness.js"></script> 8 <script src="/resources/testharnessreport.js"></script> 9 <script src="/service-workers/service-worker/resources/test-helpers.sub.js"> 10 </script> 11 <script src="resources/static-router-helpers.sub.js"> 12 </script> 13 <body> 14 <script> 15 const ROUTER_KEY = 'multiple-conditions-network'; 16 const ROUTER_KEY_WITH_DESTINATION = 17 'multiple-conditions-with-destination-network'; 18 const HTML_FILE = 'resources/simple.html'; 19 const REQUEST_SRC = 'resources/direct.py'; 20 21 const is_matched = async (worker) => { 22 const {requests} = await get_info_from_worker(worker); 23 return requests.length == 0; 24 } 25 26 const appendCSS = async (iwin, src) => { 27 const promise = new Promise(resolve => { 28 const link = iwin.document.createElement('link'); 29 link.rel = 'stylesheet'; 30 link.href = src; 31 iwin.document.head.appendChild(link); 32 link.onload = () => { 33 resolve(link); 34 }; 35 }); 36 37 return promise; 38 }; 39 40 const appendScript = async (iwin, src) => { 41 const promise = new Promise(resolve => { 42 const script = iwin.document.createElement('script'); 43 script.src = src; 44 iwin.document.body.appendChild(script); 45 script.onload = () => { 46 resolve(script); 47 }; 48 }); 49 50 return promise; 51 }; 52 53 iframeTest(HTML_FILE, ROUTER_KEY, async (t, iwin, worker) => { 54 // Reset the fetch count created by the setup process. 55 await reset_info_in_worker(worker); 56 const {requests} = await get_info_from_worker(worker); 57 assert_equals(requests.length, 0); 58 59 // Expected condtion: 60 // - urlPattern: { search: 'test' } 61 // - mode: 'cors' 62 // - method: POST 63 64 // Expect match. 65 let response = await iwin.fetch(`../${REQUEST_SRC}?test`, {mode: 'cors', method: 'post'}); 66 assert_equals(response.status, 200); 67 assert_true(await is_matched(worker)); 68 await reset_info_in_worker(worker); 69 70 // mode: 'no-cors' won't match. 71 response = await iwin.fetch(`../${REQUEST_SRC}?test`, {mode: 'no-cors', method: 'post'}); 72 assert_false(await is_matched(worker)); 73 await reset_info_in_worker(worker); 74 75 // method: GET won't match. 76 response = await iwin.fetch(`../${REQUEST_SRC}?test`, {mode: 'cors', method: 'get'}); 77 assert_false(await is_matched(worker)); 78 await reset_info_in_worker(worker); 79 80 // No seqarch query won't match. 81 response = await iwin.fetch(`../${REQUEST_SRC}`, {mode: 'cors', method: 'post'}); 82 assert_false(await is_matched(worker)); 83 await reset_info_in_worker(worker); 84 }, 'Multiple conditions work with `and` operation'); 85 86 iframeTest(HTML_FILE, ROUTER_KEY_WITH_DESTINATION, async (t, iwin, worker) => { 87 // Reset the fetch count created by the setup process. 88 await reset_info_in_worker(worker); 89 const {requests} = await get_info_from_worker(worker); 90 assert_equals(requests.length, 0); 91 92 // Expected condtion: 93 // - urlPattern: { search: 'test' } 94 // - destination: style 95 96 // Expect match. 97 await appendCSS(iwin, `../${REQUEST_SRC}?test`); 98 assert_true(await is_matched(worker)); 99 await reset_info_in_worker(worker); 100 101 // Other request destination won't match. 102 await appendScript(iwin, `../${REQUEST_SRC}?test`); 103 assert_false(await is_matched(worker)); 104 await reset_info_in_worker(worker); 105 106 // No seqarch query won't match. 107 await appendCSS(iwin, `../${REQUEST_SRC}`); 108 assert_false(await is_matched(worker)); 109 await reset_info_in_worker(worker); 110 }, 'Multiple conditions including requestDestination work with `and` operation'); 111 </script> 112 </body>