summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/fetch/common_temporaryFileBlob.js
blob: 51aed67cf2f4f082d2b60fe3ca52ea04afa840af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
var data = new Array(256).join("1234567890ABCDEF");

function test_fetch_basic() {
  info("Simple fetch test");

  fetch("/tests/dom/xhr/tests/temporaryFileBlob.sjs", {
    method: "POST",
    body: data,
  })
    .then(response => {
      return response.blob();
    })
    .then(blob => {
      ok(blob instanceof Blob, "We have a blob!");
      is(blob.size, data.length, "Data length matches");
      if ("SpecialPowers" in self) {
        is(
          SpecialPowers.wrap(blob).blobImplType,
          "StreamBlobImpl[TemporaryFileBlobImpl]",
          "We have a blob stored into a stream file"
        );
      }

      var fr = new FileReader();
      fr.readAsText(blob);
      fr.onload = function () {
        is(fr.result, data, "Data content matches");
        next();
      };
    });
}

function test_fetch_worker() {
  generic_worker_test("fetch in workers", "fetch");
}

function test_xhr_basic() {
  info("Simple XHR test");

  let xhr = new XMLHttpRequest();
  xhr.responseType = "blob";
  xhr.open("POST", "/tests/dom/xhr/tests/temporaryFileBlob.sjs");
  xhr.send(data);

  xhr.onreadystatechange = function () {
    if (xhr.readyState == 4) {
      let blob = xhr.response;

      ok(blob instanceof Blob, "We have a blob!");
      is(blob.size, data.length, "Data length matches");
      if ("SpecialPowers" in self) {
        is(
          SpecialPowers.wrap(blob).blobImplType,
          "StreamBlobImpl[TemporaryFileBlobImpl]",
          "We have a blob stored into a stream file"
        );
      }

      var fr = new FileReader();
      fr.readAsText(blob);
      fr.onload = function () {
        is(fr.result, data, "Data content matches");
        next();
      };
    }
  };
}

function test_xhr_worker() {
  generic_worker_test("XHR in workers", "xhr");
}

function test_response_basic() {
  info("Response");

  let r = new Response(data);
  r.blob().then(blob => {
    ok(blob instanceof Blob, "We have a blob!");
    is(blob.size, data.length, "Data length matches");
    if ("SpecialPowers" in self) {
      is(
        SpecialPowers.wrap(blob).blobImplType,
        "StreamBlobImpl[TemporaryFileBlobImpl]",
        "We have a blob stored into a stream file"
      );
    }

    var fr = new FileReader();
    fr.readAsText(blob);
    fr.onload = function () {
      is(fr.result, data, "Data content matches");
      next();
    };
  });
}

function test_response_worker() {
  generic_worker_test("Response in workers", "response");
}

function test_request_basic() {
  info("Request");

  let r = new Request("https://example.com", { body: data, method: "POST" });
  r.blob().then(blob => {
    ok(blob instanceof Blob, "We have a blob!");
    is(blob.size, data.length, "Data length matches");
    if ("SpecialPowers" in self) {
      is(
        SpecialPowers.wrap(blob).blobImplType,
        "StreamBlobImpl[TemporaryFileBlobImpl]",
        "We have a blob stored into a stream file"
      );
    }

    var fr = new FileReader();
    fr.readAsText(blob);
    fr.onload = function () {
      is(fr.result, data, "Data content matches");
      next();
    };
  });
}

function test_request_worker() {
  generic_worker_test("Request in workers", "request");
}

function generic_worker_test(title, what) {
  info(title);

  var w = new Worker("worker_temporaryFileBlob.js");
  w.onmessage = function (e) {
    if (e.data.type == "info") {
      info(e.data.msg);
    } else if (e.data.type == "check") {
      ok(e.data.what, e.data.msg);
    } else if (e.data.type == "finish") {
      next();
    } else {
      ok(false, "Something wrong happened");
    }
  };

  w.postMessage(what);
}