progress-events-response-data-gzip.htm (3712B)
1 <!doctype html> 2 <html> 3 <head> 4 <title>XMLHttpRequest: progress events and GZIP encoding</title> 5 <meta name="timeout" content="long"> 6 <script src="/resources/testharness.js"></script> 7 <script src="/resources/testharnessreport.js"></script> 8 <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')]" /> 9 <!-- TODO: find better spec reference when https://www.w3.org/Bugs/Public/show_bug.cgi?id=25587 is fixed --> 10 </head> 11 <body> 12 <div id="log"></div> 13 <script> 14 var test = async_test() 15 test.step(function() { 16 var client = new XMLHttpRequest() 17 /* 18 19 Two behaviours are considered acceptable, so there are two ways to 20 pass this test 21 22 a) Set data for the compressed resource: 23 * event.total reflects the Content-length of the gzipp'ed resource 24 * event.loaded how many gzipped bytes have arrived over the wire so far 25 * lengthComputable is true 26 27 or 28 29 b) If the implementation does not provide progress details for the compressed 30 resource, set 31 * lengthComputable to false 32 * event.total to 0 33 * event.loaded to the number of bytes available so far after gzip decoding 34 35 Implications of this are tested here as follows: 36 37 * If lengthComputable is true: 38 * Event.total must match Content-length header 39 * event.loaded must only ever increase in progress events 40 (and may never repeat its value). 41 * event.loaded must never exceed the Content-length. 42 43 * If lengthComputable is false: 44 * event.total should be 0 45 * event.loaded must only ever increase in progress events 46 (and may never repeat its value). 47 * event.loaded should be the length of the decompressed content, i.e. 48 bigger than Content-length header value when finished loading 49 50 */ 51 var lastTotal; 52 var lastLoaded = -1; 53 client.addEventListener('loadend', test.step_func(function(e){ 54 var len = parseInt(client.getResponseHeader('content-length'), 10) 55 if(e.lengthComputable){ 56 assert_equals(e.total, len, 'event.total is content-length') 57 assert_equals(e.loaded, len, 'event.loaded should be content-length at loadend') 58 }else{ 59 assert_equals(e.total, 0, 'if implementation can\'t compute event.total for gzipped content it is 0') 60 assert_true(e.loaded >= len, 'event.loaded should be set even if total is not computable') 61 } 62 test.done(); 63 }), false) 64 client.addEventListener('progress', test.step_func(function(e){ 65 if(lastTotal === undefined){ 66 lastTotal = e.total; 67 } 68 if(e.lengthComputable && e.total && e.loaded){ 69 assert_equals(e.total, lastTotal, 'event.total should remain invariant') 70 assert_less_than_equal(e.loaded, lastTotal, 'event.loaded should not exceed content-length') 71 }else{ 72 assert_equals(e.total, 0, 'event.total should be 0') 73 } 74 assert_greater_than(e.loaded, lastLoaded, 'event.loaded should only ever increase') 75 lastLoaded = e.loaded; 76 }), false) 77 // image.gif is 165375 bytes compressed. Sending 45000 bytes at a time with 1 second delay will load it in 4 seconds 78 client.open("GET", "resources/image.gif?pipe=gzip|trickle(45000:d1:r2)", true) 79 client.send() 80 }) 81 </script> 82 </body> 83 </html>