blob: 058064636d43ee08ca2d8cc2c463fcf6481f9d95 [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
Thomas Wisniewski3465be42016-10-06 18:00:0139 * 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. Steena1742ac2014-06-23 17:23:2942
43 * If lengthComputable is false:
44 * event.total should be 0
Thomas Wisniewski3465be42016-10-06 18:00:0145 * event.loaded must only ever increase in progress events
46 (and may never repeat its value).
Hallvord R. M. Steena1742ac2014-06-23 17:23:2947 * 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. Steen517f87f2014-05-10 11:15:1050 */
Thomas Wisniewski3465be42016-10-06 18:00:0151 var lastTotal;
52 var lastLoaded = -1;
Hallvord R. M. Steen517f87f2014-05-10 11:15:1053 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 Wisniewski3465be42016-10-06 18:00:0165 if(lastTotal === undefined){
66 lastTotal = e.total;
Hallvord R. M. Steen517f87f2014-05-10 11:15:1067 }
Thomas Wisniewski3465be42016-10-06 18:00:0168 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. Steen517f87f2014-05-10 11:15:1076 }), false)
Hallvord R. M. Steen224c2ae2014-07-21 17:28:3977 // 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:3978 client.open("GET", "resources/image.gif?pipe=gzip|trickle(45000:d1:r2)", true)
Hallvord R. M. Steen517f87f2014-05-10 11:15:1079 client.send()
Hallvord R. M. Steena1742ac2014-06-23 17:23:2980 })
Hallvord R. M. Steen517f87f2014-05-10 11:15:1081 </script>
82 </body>
83</html>