static-router-race-network-and-fetch-handler.https.html (6135B)
1 <!DOCTYPE html> 2 <meta charset="utf-8"> 3 <title> 4 Static Router: routers are evaluated with the request desitnation 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 = 'condition-urlpattern-string-source-race-network-and-fetch-handler'; 16 const SW_SRC = 'resources/static-router-race-network-and-fetch-handler-sw.js'; 17 const FRAME_SRC = 'resources/direct.py'; 18 19 promise_test(async t => { 20 const rnd = randomString(); 21 const worker = await registerAndActivate(t, ROUTER_KEY, SW_SRC); 22 const iframe = await createIframe(t, `${FRAME_SRC}?nonce=${rnd}&server_slow`); 23 // Expect the response from the fetch handler. 24 assert_equals(iframe.contentWindow.document.body.innerText, rnd); 25 const {requests, errors} = await get_info_from_worker(worker); 26 assert_equals(requests.length, 1); 27 assert_equals(errors.length, 0); 28 }, 'Main resource load matched the rule with race-network-and-fetch-handler source, and the fetch handler response is faster than the server response'); 29 30 promise_test(async t => { 31 const rnd = randomString(); 32 const worker = await registerAndActivate(t, ROUTER_KEY, SW_SRC); 33 const iframe = await createIframe(t, `${FRAME_SRC}?nonce=${rnd}&sw_slow`); 34 // Expect the response from the netowrk request. 35 assert_equals(iframe.contentWindow.document.body.innerText, "Network with GET request"); 36 // Ensure the fetch handler is also executed. 37 const {requests, errors} = await get_info_from_worker(worker); 38 assert_equals(requests.length, 1); 39 assert_equals(errors.length, 0); 40 }, 'Main resource load matched the rule with race-network-and-fetch-handler source, and the server reseponse is faster than the fetch handler'); 41 42 promise_test(async t => { 43 const rnd = randomString(); 44 const worker = await registerAndActivate(t, ROUTER_KEY, SW_SRC); 45 const iframe = await createIframe(t, `${FRAME_SRC}?nonce=${rnd}&server_slow&call_fetch`); 46 // Expect the response from the fetch handler. 47 assert_equals(iframe.contentWindow.document.body.innerText, rnd); 48 const {requests, errors} = await get_info_from_worker(worker); 49 assert_equals(requests.length, 1); 50 assert_equals(errors.length, 0); 51 }, 'Main resource load matched the rule with race-network-and-fetch-handler source, and the fetch handler response is faster than the server response while fetch() is triggered inside the fetch handler'); 52 53 promise_test(async t => { 54 const rnd = randomString(); 55 const worker = await registerAndActivate(t, ROUTER_KEY, SW_SRC); 56 const iframe = await createIframe(t, `${FRAME_SRC}?nonce=${rnd}&sw_slow&call_fetch`); 57 // Expect the response from the netowrk request. 58 assert_equals(iframe.contentWindow.document.body.innerText, "Network with GET request"); 59 // Ensure the fetch handler is also executed. 60 const {requests, errors} = await get_info_from_worker(worker); 61 assert_equals(requests.length, 1); 62 assert_equals(errors.length, 0); 63 }, 'Main resource load matched the rule with race-network-and-fetch-handler source, and the server reseponse is faster than the fetch handler while fetch() is triggered inside the fetch handler'); 64 65 promise_test(async t => { 66 const rnd = randomString(); 67 const worker = await registerAndActivate(t, ROUTER_KEY, SW_SRC); 68 const iframe = await createIframe(t, FRAME_SRC); 69 // Expect the response from the fetch handler. 70 const response = await iframe.contentWindow.fetch(`?nonce=${rnd}&server_slow`); 71 assert_equals(response.status, 200); 72 assert_equals(await response.text(), rnd); 73 const {requests} = await get_info_from_worker(worker); 74 assert_equals(requests.length, 2); 75 }, 'Subresource load matched the rule with race-network-and-fetch-handler source, and the fetch handler response is faster than the server response'); 76 77 promise_test(async t => { 78 const rnd = randomString(); 79 const worker = await registerAndActivate(t, ROUTER_KEY, SW_SRC); 80 const iframe = await createIframe(t, FRAME_SRC); 81 // Expect the response from the network request. 82 const response = await iframe.contentWindow.fetch(`?nonce=${rnd}&sw_slow`); 83 assert_equals(response.status, 200); 84 assert_equals(await response.text(), "Network with GET request"); 85 // Ensure the fetch handler is also executed. 86 const {requests} = await get_info_from_worker(worker); 87 assert_equals(requests.length, 2); 88 }, 'Subresource load matched the rule with race-network-and-fetch-handler source, and the server reseponse is faster than the fetch handler'); 89 90 promise_test(async t => { 91 const rnd = randomString(); 92 const worker = await registerAndActivate(t, ROUTER_KEY, SW_SRC); 93 const iframe = await createIframe(t, FRAME_SRC); 94 // Expect the response from the network request. 95 const response = await iframe.contentWindow.fetch(`?nonce=${rnd}&sw_slow&server_no_content`); 96 assert_equals(response.status, 204); 97 // Ensure the fetch handler is also executed. 98 const {requests} = await get_info_from_worker(worker); 99 assert_equals(requests.length, 2); 100 }, 'Subresource load matched the rule with race-network-and-fetch-handler source, and the server reseponse with 204 response is faster than the fetch handler'); 101 102 103 promise_test(async t => { 104 const rnd = randomString(); 105 const worker = await registerAndActivate(t, ROUTER_KEY, SW_SRC); 106 const iframe = await createIframe(t, FRAME_SRC); 107 const response = await iframe.contentWindow.fetch(`?nonce=${rnd}&sw_slow&server_not_found`); 108 // Expect the response from the network request was faster, but the result was 404. 109 // So, the fetch handler result will be used instead. 110 assert_equals(response.status, 200); 111 assert_equals(await response.text(), rnd); 112 const {requests} = await get_info_from_worker(worker); 113 assert_equals(requests.length, 2); 114 }, 'Subresource load matched the rule with race-network-and-fetch-handler source, and the server reseponse is faster than the fetch handler, but not found'); 115 </script> 116 </body>