Branch data Line data Source code
1 [ + ]: 2 : import assert from 'node:assert/strict'; 2 : 2 : import { XMLHttpRequest } from '../../../lib/whatwg-xhr.js'; 3 : 2 : 4 [ + ]: 2 : export default (activeURL) => { 5 : 1 : 6 : 1 : const xhr = new XMLHttpRequest(); 7 : 1 : 8 : 1 : let lastTotal; 9 : 1 : let lastLoaded = -1; 10 : 1 : 11 [ + ]: 1 : xhr.addEventListener('loadend', (e) => { 12 : 1 : let len = parseInt(xhr.getResponseHeader('content-length'), 10); 13 : 1 : 14 : 1 : if(e.lengthComputable){ 15 : 1 : assert.strictEqual(e.total, len, 'event.total is content-length'); 16 : 1 : assert.strictEqual(e.loaded, len, 'event.loaded should be content-length at loadend'); 17 [ - ]: 1 : } 18 : 0 : else{ 19 : 0 : assert.strictEqual(e.total, 0, 'if implementation can\'t compute event.total for gzipped content it is 0'); 20 : 0 : assert(e.loaded >= len, 'event.loaded should be set even if total is not computable'); 21 : 0 : } 22 : 1 : 23 : 1 : xhr.onloadend = xhr.progress = null; 24 : 1 : }); 25 : 1 : 26 [ + ]: 1 : xhr.addEventListener('progress', (e) => { 27 [ + ]: 4 : if(lastTotal === undefined){ 28 : 1 : lastTotal = e.total; 29 : 1 : } 30 : 4 : 31 : 4 : if(e.lengthComputable && e.total && e.loaded){ 32 : 4 : assert.strictEqual(e.total, lastTotal, 'event.total should remain invariant'); 33 : 4 : assert(e.loaded <= lastTotal, 'event.loaded should not exceed content-length'); 34 [ - ]: 4 : } 35 : 0 : else{ 36 : 0 : assert.strictEqual(e.total, 0, 'event.total should be 0'); 37 : 0 : } 38 : 4 : 39 : 4 : assert(e.loaded > lastLoaded, 'event.loaded should only ever increase'); 40 : 4 : lastLoaded = e.loaded; 41 : 1 : }); 42 : 1 : 43 : 1 : xhr.open('GET', `${activeURL}/image.gif?pipe=gzip|trickle(45000:d1:r2)`); 44 : 1 : xhr.send(); 45 : 1 : } 46 : 2 : 47 : 2 : /* 48 : 2 : * progress-events-response-data-gzip.htm 49 : 2 : * 50 : 2 : 51 : 2 : <!doctype html> 52 : 2 : <html> 53 : 2 : <head> 54 : 2 : <title>XMLHttpRequest: progress events and GZIP encoding</title> 55 : 2 : <meta name="timeout" content="long"> 56 : 2 : <script src="/resources/testharness.js"></script> 57 : 2 : <script src="/resources/testharnessreport.js"></script> 58 : 2 : <link rel="help" href="https://xhr.spec.whatwg.org/#firing-events-using-the-progressevent-interface-for-http" data-tested-assertations="following::p[contains(text(),'content-encodings')]" /> 59 : 2 : <!-- TODO: find better spec reference when https://www.w3.org/Bugs/Public/show_bug.cgi?id=25587 is fixed --> 60 : 2 : </head> 61 : 2 : <body> 62 : 2 : <div id="log"></div> 63 : 2 : <script> 64 : 2 : var test = async_test() 65 : 2 : test.step(function() { 66 : 2 : var client = new XMLHttpRequest() 67 : 2 : *//* 68 : 2 : 69 : 2 : Two behaviours are considered acceptable, so there are two ways to 70 : 2 : pass this test 71 : 2 : 72 : 2 : a) Set data for the compressed resource: 73 : 2 : * event.total reflects the Content-length of the gzipp'ed resource 74 : 2 : * event.loaded how many gzipped bytes have arrived over the wire so far 75 : 2 : * lengthComputable is true 76 : 2 : 77 : 2 : or 78 : 2 : 79 : 2 : b) If the implementation does not provide progress details for the compressed 80 : 2 : resource, set 81 : 2 : * lengthComputable to false 82 : 2 : * event.total to 0 83 : 2 : * event.loaded to the number of bytes available so far after gzip decoding 84 : 2 : 85 : 2 : Implications of this are tested here as follows: 86 : 2 : 87 : 2 : * If lengthComputable is true: 88 : 2 : * Event.total must match Content-length header 89 : 2 : * event.loaded must only ever increase in progress events 90 : 2 : (and may never repeat its value). 91 : 2 : * event.loaded must never exceed the Content-length. 92 : 2 : 93 : 2 : * If lengthComputable is false: 94 : 2 : * event.total should be 0 95 : 2 : * event.loaded must only ever increase in progress events 96 : 2 : (and may never repeat its value). 97 : 2 : * event.loaded should be the length of the decompressed content, i.e. 98 : 2 : bigger than Content-length header value when finished loading 99 : 2 : 100 : 2 : *//* 101 : 2 : var lastTotal; 102 : 2 : var lastLoaded = -1; 103 : 2 : client.addEventListener('loadend', test.step_func(function(e){ 104 : 2 : var len = parseInt(client.getResponseHeader('content-length'), 10) 105 : 2 : if(e.lengthComputable){ 106 : 2 : assert_equals(e.total, len, 'event.total is content-length') 107 : 2 : assert_equals(e.loaded, len, 'event.loaded should be content-length at loadend') 108 : 2 : }else{ 109 : 2 : assert_equals(e.total, 0, 'if implementation can\'t compute event.total for gzipped content it is 0') 110 : 2 : assert_true(e.loaded >= len, 'event.loaded should be set even if total is not computable') 111 : 2 : } 112 : 2 : test.done(); 113 : 2 : }), false) 114 : 2 : client.addEventListener('progress', test.step_func(function(e){ 115 : 2 : if(lastTotal === undefined){ 116 : 2 : lastTotal = e.total; 117 : 2 : } 118 : 2 : if(e.lengthComputable && e.total && e.loaded){ 119 : 2 : assert_equals(e.total, lastTotal, 'event.total should remain invariant') 120 : 2 : assert_less_than_equal(e.loaded, lastTotal, 'event.loaded should not exceed content-length') 121 : 2 : }else{ 122 : 2 : assert_equals(e.total, 0, 'event.total should be 0') 123 : 2 : } 124 : 2 : assert_greater_than(e.loaded, lastLoaded, 'event.loaded should only ever increase') 125 : 2 : lastLoaded = e.loaded; 126 : 2 : }), false) 127 : 2 : // image.gif is 165375 bytes compressed. Sending 45000 bytes at a time with 1 second delay will load it in 4 seconds 128 : 2 : client.open("GET", "resources/image.gif?pipe=gzip|trickle(45000:d1:r2)", true) 129 : 2 : client.send() 130 : 2 : }) 131 : 2 : </script> 132 : 2 : </body> 133 : 2 : </html> 134 : 2 : 135 : 2 : * 136 : 2 : * progress-events-response-data-gzip.htm 137 : 2 : */