request-content-length.any.js (1147B)
1 async_test(test => { 2 const client = new XMLHttpRequest(); 3 const data = "This is 22 bytes long."; 4 let happened = false; 5 client.upload.onprogress = test.step_func(e => { 6 assert_true(e.lengthComputable); 7 assert_equals(e.total, data.length); 8 happened = true; 9 }); 10 client.onload = test.step_func_done(() => { 11 assert_true(happened); 12 assert_true(client.responseText.includes(`Content-Length: ${data.length}`)); 13 }); 14 client.onerror = test.unreached_func(); 15 client.open("POST", "resources/echo-headers.py"); 16 client.send(data); 17 }, "Uploads need to set the Content-Length header"); 18 19 async_test(test => { 20 const client = new XMLHttpRequest(); 21 const data = "blah"; 22 const url = URL.createObjectURL(new Blob([data])); 23 client.open("GET", url); 24 client.send(); 25 client.onload = test.step_func_done(e => { 26 assert_true(e.lengthComputable); 27 assert_equals(e.total, data.length); 28 assert_equals(e.loaded, data.length); 29 assert_equals(client.responseText, data); 30 assert_equals(client.getResponseHeader("Content-Length"), String(data.length)); 31 }); 32 }, "Fetched blob: URLs set the Content-Length header");