common_temporaryFileBlob.js (3629B)
1 var data = new Array(256).join("1234567890ABCDEF"); 2 3 function test_fetch_basic() { 4 info("Simple fetch test"); 5 6 fetch("/tests/dom/xhr/tests/temporaryFileBlob.sjs", { 7 method: "POST", 8 body: data, 9 }) 10 .then(response => { 11 return response.blob(); 12 }) 13 .then(blob => { 14 ok(blob instanceof Blob, "We have a blob!"); 15 is(blob.size, data.length, "Data length matches"); 16 if ("SpecialPowers" in self) { 17 is( 18 SpecialPowers.wrap(blob).blobImplType, 19 "StreamBlobImpl[TemporaryFileBlobImpl]", 20 "We have a blob stored into a stream file" 21 ); 22 } 23 24 var fr = new FileReader(); 25 fr.readAsText(blob); 26 fr.onload = function () { 27 is(fr.result, data, "Data content matches"); 28 next(); 29 }; 30 }); 31 } 32 33 function test_fetch_worker() { 34 generic_worker_test("fetch in workers", "fetch"); 35 } 36 37 function test_xhr_basic() { 38 info("Simple XHR test"); 39 40 let xhr = new XMLHttpRequest(); 41 xhr.responseType = "blob"; 42 xhr.open("POST", "/tests/dom/xhr/tests/temporaryFileBlob.sjs"); 43 xhr.send(data); 44 45 xhr.onreadystatechange = function () { 46 if (xhr.readyState == 4) { 47 let blob = xhr.response; 48 49 ok(blob instanceof Blob, "We have a blob!"); 50 is(blob.size, data.length, "Data length matches"); 51 if ("SpecialPowers" in self) { 52 is( 53 SpecialPowers.wrap(blob).blobImplType, 54 "StreamBlobImpl[TemporaryFileBlobImpl]", 55 "We have a blob stored into a stream file" 56 ); 57 } 58 59 var fr = new FileReader(); 60 fr.readAsText(blob); 61 fr.onload = function () { 62 is(fr.result, data, "Data content matches"); 63 next(); 64 }; 65 } 66 }; 67 } 68 69 function test_xhr_worker() { 70 generic_worker_test("XHR in workers", "xhr"); 71 } 72 73 function test_response_basic() { 74 info("Response"); 75 76 let r = new Response(data); 77 r.blob().then(blob => { 78 ok(blob instanceof Blob, "We have a blob!"); 79 is(blob.size, data.length, "Data length matches"); 80 if ("SpecialPowers" in self) { 81 is( 82 SpecialPowers.wrap(blob).blobImplType, 83 "StreamBlobImpl[TemporaryFileBlobImpl]", 84 "We have a blob stored into a stream file" 85 ); 86 } 87 88 var fr = new FileReader(); 89 fr.readAsText(blob); 90 fr.onload = function () { 91 is(fr.result, data, "Data content matches"); 92 next(); 93 }; 94 }); 95 } 96 97 function test_response_worker() { 98 generic_worker_test("Response in workers", "response"); 99 } 100 101 function test_request_basic() { 102 info("Request"); 103 104 let r = new Request("https://example.com", { body: data, method: "POST" }); 105 r.blob().then(blob => { 106 ok(blob instanceof Blob, "We have a blob!"); 107 is(blob.size, data.length, "Data length matches"); 108 if ("SpecialPowers" in self) { 109 is( 110 SpecialPowers.wrap(blob).blobImplType, 111 "StreamBlobImpl[TemporaryFileBlobImpl]", 112 "We have a blob stored into a stream file" 113 ); 114 } 115 116 var fr = new FileReader(); 117 fr.readAsText(blob); 118 fr.onload = function () { 119 is(fr.result, data, "Data content matches"); 120 next(); 121 }; 122 }); 123 } 124 125 function test_request_worker() { 126 generic_worker_test("Request in workers", "request"); 127 } 128 129 function generic_worker_test(title, what) { 130 info(title); 131 132 var w = new Worker("worker_temporaryFileBlob.js"); 133 w.onmessage = function (e) { 134 if (e.data.type == "info") { 135 info(e.data.msg); 136 } else if (e.data.type == "check") { 137 ok(e.data.what, e.data.msg); 138 } else if (e.data.type == "finish") { 139 next(); 140 } else { 141 ok(false, "Something wrong happened"); 142 } 143 }; 144 145 w.postMessage(what); 146 }