summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/fetch/utils.js
diff options
context:
space:
mode:
Diffstat (limited to 'dom/tests/mochitest/fetch/utils.js')
-rw-r--r--dom/tests/mochitest/fetch/utils.js37
1 files changed, 37 insertions, 0 deletions
diff --git a/dom/tests/mochitest/fetch/utils.js b/dom/tests/mochitest/fetch/utils.js
new file mode 100644
index 000000000..5d294041e
--- /dev/null
+++ b/dom/tests/mochitest/fetch/utils.js
@@ -0,0 +1,37 @@
+// Utilities
+// =========
+
+// Helper that uses FileReader or FileReaderSync based on context and returns
+// a Promise that resolves with the text or rejects with error.
+function readAsText(blob) {
+ if (typeof FileReader !== "undefined") {
+ return new Promise(function(resolve, reject) {
+ var fs = new FileReader();
+ fs.onload = function() {
+ resolve(fs.result);
+ }
+ fs.onerror = reject;
+ fs.readAsText(blob);
+ });
+ } else {
+ var fs = new FileReaderSync();
+ return Promise.resolve(fs.readAsText(blob));
+ }
+}
+
+function readAsArrayBuffer(blob) {
+ if (typeof FileReader !== "undefined") {
+ return new Promise(function(resolve, reject) {
+ var fs = new FileReader();
+ fs.onload = function() {
+ resolve(fs.result);
+ }
+ fs.onerror = reject;
+ fs.readAsArrayBuffer(blob);
+ });
+ } else {
+ var fs = new FileReaderSync();
+ return Promise.resolve(fs.readAsArrayBuffer(blob));
+ }
+}
+