fetch-event-respond-with-partial-stream.https.html (2362B)
1 <!DOCTYPE html> 2 <meta charset="utf-8"> 3 <title>respondWith streams data to an intercepted fetch()</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> 8 'use strict'; 9 10 const WORKER = 11 'resources/fetch-event-respond-with-partial-stream-worker.js'; 12 const SCOPE = 13 'resources/fetch-event-respond-with-partial-stream-iframe.html'; 14 15 promise_test(async t => { 16 let reg = await service_worker_unregister_and_register(t, WORKER, SCOPE) 17 add_completion_callback(() => reg.unregister()); 18 19 await wait_for_state(t, reg.installing, 'activated'); 20 21 let frame = await with_iframe(SCOPE); 22 t.add_cleanup(_ => frame.remove()); 23 24 let response = await frame.contentWindow.fetch('partial-stream.txt'); 25 26 let reader = response.body.getReader(); 27 28 let encoder = new TextEncoder(); 29 let decoder = new TextDecoder(); 30 31 let expected = 'partial-stream-content'; 32 let encodedExpected = encoder.encode(expected); 33 let received = ''; 34 let encodedReceivedLength = 0; 35 36 // Accumulate response data from the service worker. We do this as a loop 37 // to allow the browser the flexibility of rebuffering if it chooses. We 38 // do expect to get the partial data within the test timeout period, though. 39 // The spec is a bit vague at the moment about this, but it seems reasonable 40 // that the browser should not stall the response stream when the service 41 // worker has only written a partial result, but not closed the stream. 42 while (encodedReceivedLength < encodedExpected.length) { 43 let chunk = await reader.read(); 44 assert_false(chunk.done, 'partial body stream should not be closed yet'); 45 46 encodedReceivedLength += chunk.value.length; 47 received += decoder.decode(chunk.value); 48 } 49 50 // Note, the spec may allow some re-buffering between the service worker 51 // and the outer intercepted fetch. We could relax this exact chunk value 52 // match if necessary. The goal, though, is to ensure the outer fetch is 53 // not completely blocked until the service worker body is closed. 54 assert_equals(received, expected, 55 'should receive partial content through service worker interception'); 56 57 reg.active.postMessage('done'); 58 59 await reader.closed; 60 61 }, 'respondWith() streams data to an intercepted fetch()'); 62 </script>