blob: dc166a2396a4f7dd88ecd6a91da1337fd81620ec [file] [log] [blame]
Hallvord R. M. Steen517f87f2014-05-10 11:15:101<!doctype html>
2<html>
3 <head>
4 <title>XMLHttpRequest: progress events and GZIP encoding</title>
Hallvord R. M. Steen574a9632014-06-23 21:59:315 <meta name="timeout" content="long">
Hallvord R. M. Steen517f87f2014-05-10 11:15:106 <script src="/resources/testharness.js"></script>
7 <script src="/resources/testharnessreport.js"></script>
Ms2ger91fef142014-11-01 09:24:378 <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. Steen517f87f2014-05-10 11:15:109 <!-- 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. Steena1742ac2014-06-23 17:23:2918
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. Steen517f87f2014-05-10 11:15:1034
35 Implications of this are tested here as follows:
Hallvord R. M. Steena1742ac2014-06-23 17:23:2936
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. Steen517f87f2014-05-10 11:15:1049 */
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. Steen224c2ae2014-07-21 17:28:3962 if(e.lengthComputable && e.total && e.loaded && e.target.readyState < 4){
Hallvord R. M. Steena1742ac2014-06-23 17:23:2963 assert_not_equals(e.total, e.loaded, 'total should not equal loaded while download/decode is incomplete')
Hallvord R. M. Steen224c2ae2014-07-21 17:28:3964 // 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. Steen517f87f2014-05-10 11:15:1069 }
70 }), false)
Hallvord R. M. Steen224c2ae2014-07-21 17:28:3971 // 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. Steend00fd182014-07-21 16:57:3972 client.open("GET", "resources/image.gif?pipe=gzip|trickle(45000:d1:r2)", true)
Hallvord R. M. Steen517f87f2014-05-10 11:15:1073 client.send()
Hallvord R. M. Steena1742ac2014-06-23 17:23:2974 })
Hallvord R. M. Steen517f87f2014-05-10 11:15:1075 </script>
76 </body>
77</html>