| Hallvord R. M. Steen | 517f87f | 2014-05-10 11:15:10 | [diff] [blame] | 1 | <!doctype html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <title>XMLHttpRequest: progress events and GZIP encoding</title> |
| Hallvord R. M. Steen | 574a963 | 2014-06-23 21:59:31 | [diff] [blame] | 5 | <meta name="timeout" content="long"> |
| Hallvord R. M. Steen | 517f87f | 2014-05-10 11:15:10 | [diff] [blame] | 6 | <script src="/resources/testharness.js"></script> |
| 7 | <script src="/resources/testharnessreport.js"></script> |
| Ms2ger | 91fef14 | 2014-11-01 09:24:37 | [diff] [blame^] | 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')]" /> |
| Hallvord R. M. Steen | 517f87f | 2014-05-10 11:15:10 | [diff] [blame] | 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 | /* |
| Hallvord R. M. Steen | a1742ac | 2014-06-23 17:23:29 | [diff] [blame] | 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 |
| Hallvord R. M. Steen | 517f87f | 2014-05-10 11:15:10 | [diff] [blame] | 34 | |
| 35 | Implications of this are tested here as follows: |
| Hallvord R. M. Steen | a1742ac | 2014-06-23 17:23:29 | [diff] [blame] | 36 | |
| 37 | * If lengthComputable is true: |
| 38 | * Event.total must match Content-length header |
| 39 | * event.loaded should be a smaller number while resource is loading |
| 40 | and match Content-length when loading is finished |
| 41 | * Setting event.loaded to equal event.total for each progress event if the |
| 42 | resource is not fully downloaded would be cheating |
| 43 | |
| 44 | * If lengthComputable is false: |
| 45 | * event.total should be 0 |
| 46 | * event.loaded should be the length of the decompressed content, i.e. |
| 47 | bigger than Content-length header value when finished loading |
| 48 | |
| Hallvord R. M. Steen | 517f87f | 2014-05-10 11:15:10 | [diff] [blame] | 49 | */ |
| 50 | client.addEventListener('loadend', test.step_func(function(e){ |
| 51 | var len = parseInt(client.getResponseHeader('content-length'), 10) |
| 52 | if(e.lengthComputable){ |
| 53 | assert_equals(e.total, len, 'event.total is content-length') |
| 54 | assert_equals(e.loaded, len, 'event.loaded should be content-length at loadend') |
| 55 | }else{ |
| 56 | assert_equals(e.total, 0, 'if implementation can\'t compute event.total for gzipped content it is 0') |
| 57 | assert_true(e.loaded >= len, 'event.loaded should be set even if total is not computable') |
| 58 | } |
| 59 | test.done(); |
| 60 | }), false) |
| 61 | client.addEventListener('progress', test.step_func(function(e){ |
| Hallvord R. M. Steen | 224c2ae | 2014-07-21 17:28:39 | [diff] [blame] | 62 | if(e.lengthComputable && e.total && e.loaded && e.target.readyState < 4){ |
| Hallvord R. M. Steen | a1742ac | 2014-06-23 17:23:29 | [diff] [blame] | 63 | assert_not_equals(e.total, e.loaded, 'total should not equal loaded while download/decode is incomplete') |
| Hallvord R. M. Steen | 224c2ae | 2014-07-21 17:28:39 | [diff] [blame] | 64 | // We should only do this assertation once |
| 65 | // it's theoretically possible that all the data would get in |
| 66 | // and a progress event fire before the readyState switches from 3 to 4 - |
| 67 | // in this case we might report bogus and random failures. Better to remove the event listener again.. |
| 68 | client.removeEventListener('progress', arguments.callee, false); |
| Hallvord R. M. Steen | 517f87f | 2014-05-10 11:15:10 | [diff] [blame] | 69 | } |
| 70 | }), false) |
| Hallvord R. M. Steen | 224c2ae | 2014-07-21 17:28:39 | [diff] [blame] | 71 | // image.gif is 165375 bytes compressed. Sending 45000 bytes at a time with 1 second delay will load it in 4 seconds |
| Hallvord R. M. Steen | d00fd18 | 2014-07-21 16:57:39 | [diff] [blame] | 72 | client.open("GET", "resources/image.gif?pipe=gzip|trickle(45000:d1:r2)", true) |
| Hallvord R. M. Steen | 517f87f | 2014-05-10 11:15:10 | [diff] [blame] | 73 | client.send() |
| Hallvord R. M. Steen | a1742ac | 2014-06-23 17:23:29 | [diff] [blame] | 74 | }) |
| Hallvord R. M. Steen | 517f87f | 2014-05-10 11:15:10 | [diff] [blame] | 75 | </script> |
| 76 | </body> |
| 77 | </html> |