big-gzip-body.https.any.js (2463B)
1 // META: global=window,worker 2 3 const EXPECTED_SIZE = 27000000; 4 const EXPECTED_SHA256 = [ 5 74, 100, 37, 243, 147, 61, 116, 60, 241, 221, 126, 6 18, 24, 71, 204, 28, 50, 62, 201, 130, 152, 225, 7 217, 183, 10, 201, 143, 214, 102, 155, 212, 248, 8 ]; 9 10 promise_test(async () => { 11 const response = await fetch('resources/big.text.gz'); 12 assert_true(response.ok); 13 const arrayBuffer = await response.arrayBuffer(); 14 assert_equals(arrayBuffer.byteLength, EXPECTED_SIZE, 15 'uncompressed size should match'); 16 const sha256 = await crypto.subtle.digest('SHA-256', arrayBuffer); 17 assert_array_equals(new Uint8Array(sha256), EXPECTED_SHA256, 18 'digest should match'); 19 }, 'large gzip data should be decompressed successfully'); 20 21 promise_test(async () => { 22 const response = await fetch('resources/big.text.gz'); 23 assert_true(response.ok); 24 const reader = response.body.getReader({mode: 'byob'}); 25 let offset = 0; 26 // Pre-allocate space for the output. The response body will be read 27 // chunk-by-chunk into this array. 28 let ab = new ArrayBuffer(EXPECTED_SIZE); 29 while (offset < EXPECTED_SIZE) { 30 // To stress the data pipe, we want to use a different size read each 31 // time. Unfortunately, JavaScript doesn't have a seeded random number 32 // generator, so this creates the possibility of making this test flaky if 33 // it doesn't work for some edge cases. 34 let size = Math.floor(Math.random() * 65535 + 1); 35 if (size + offset > EXPECTED_SIZE) { 36 size = EXPECTED_SIZE - offset; 37 } 38 const u8 = new Uint8Array(ab, offset, size); 39 const { value, done } = await reader.read(u8); 40 ab = value.buffer; 41 // Check that we got our original array back. 42 assert_equals(ab.byteLength, EXPECTED_SIZE, 43 'backing array should be the same size'); 44 assert_equals(offset, value.byteOffset, 'offset should match'); 45 assert_less_than_equal(value.byteLength, size, 46 'we should not have got more than we asked for'); 47 offset = value.byteOffset + value.byteLength; 48 if (done) break; 49 } 50 assert_equals(offset, EXPECTED_SIZE, 51 'we should have read the whole thing'); 52 const sha256 = await crypto.subtle.digest('SHA-256', new Uint8Array(ab)); 53 assert_array_equals(new Uint8Array(sha256), EXPECTED_SHA256, 54 'digest should match'); 55 }, 'large gzip data should be decompressed successfully with byte stream');