| 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 |
| Thomas Wisniewski | 3465be4 | 2016-10-06 18:00:01 | [diff] [blame] | 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. |
| Hallvord R. M. Steen | a1742ac | 2014-06-23 17:23:29 | [diff] [blame] | 42 | |
| 43 | * If lengthComputable is false: |
| 44 | * event.total should be 0 |
| Thomas Wisniewski | 3465be4 | 2016-10-06 18:00:01 | [diff] [blame] | 45 | * event.loaded must only ever increase in progress events |
| 46 | (and may never repeat its value). |
| Hallvord R. M. Steen | a1742ac | 2014-06-23 17:23:29 | [diff] [blame] | 47 | * event.loaded should be the length of the decompressed content, i.e. |
| 48 | bigger than Content-length header value when finished loading |
| 49 | |
| Hallvord R. M. Steen | 517f87f | 2014-05-10 11:15:10 | [diff] [blame] | 50 | */ |
| Thomas Wisniewski | 3465be4 | 2016-10-06 18:00:01 | [diff] [blame] | 51 | var lastTotal; |
| 52 | var lastLoaded = -1; |
| Hallvord R. M. Steen | 517f87f | 2014-05-10 11:15:10 | [diff] [blame] | 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){ |
| Thomas Wisniewski | 3465be4 | 2016-10-06 18:00:01 | [diff] [blame] | 65 | if(lastTotal === undefined){ |
| 66 | lastTotal = e.total; |
| Hallvord R. M. Steen | 517f87f | 2014-05-10 11:15:10 | [diff] [blame] | 67 | } |
| Thomas Wisniewski | 3465be4 | 2016-10-06 18:00:01 | [diff] [blame] | 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; |
| Hallvord R. M. Steen | 517f87f | 2014-05-10 11:15:10 | [diff] [blame] | 76 | }), false) |
| Hallvord R. M. Steen | 224c2ae | 2014-07-21 17:28:39 | [diff] [blame] | 77 | // 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] | 78 | 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] | 79 | client.send() |
| Hallvord R. M. Steen | a1742ac | 2014-06-23 17:23:29 | [diff] [blame] | 80 | }) |
| Hallvord R. M. Steen | 517f87f | 2014-05-10 11:15:10 | [diff] [blame] | 81 | </script> |
| 82 | </body> |
| 83 | </html> |