summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/fetch/test_fetch_basic_http.js
blob: 781af2ecde06c6db7996e6d8e42204ec78a50909 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
var path = "/tests/dom/xhr/tests/";

var passFiles = [
  ["file_XHR_pass1.xml", "GET", 200, "OK", "text/xml"],
  ["file_XHR_pass2.txt", "GET", 200, "OK", "text/plain"],
  ["file_XHR_pass3.txt", "GET", 200, "OK", "text/plain"],
];

function testURL() {
  var promises = [];
  passFiles.forEach(function (entry) {
    var p = fetch(path + entry[0]).then(function (res) {
      ok(
        res.type !== "error",
        "Response should not be an error for " + entry[0]
      );
      is(res.status, entry[2], "Status should match expected for " + entry[0]);
      is(
        res.statusText,
        entry[3],
        "Status text should match expected for " + entry[0]
      );
      if (entry[0] != "file_XHR_pass3.txt") {
        ok(
          res.url.endsWith(path + entry[0]),
          "Response url should match request for simple fetch for " + entry[0]
        );
      } else {
        ok(
          res.url.endsWith(path + "file_XHR_pass2.txt"),
          "Response url should match request for simple fetch for " + entry[0]
        );
      }
      is(
        res.headers.get("content-type"),
        entry[4],
        "Response should have content-type for " + entry[0]
      );
    });
    promises.push(p);
  });

  return Promise.all(promises);
}

var failFiles = [["ftp://localhost" + path + "file_XHR_pass1.xml", "GET"]];

function testURLFail() {
  var promises = [];
  failFiles.forEach(function (entry) {
    var p = fetch(entry[0]).then(
      function (res) {
        ok(false, "Response should be an error for " + entry[0]);
      },
      function (e) {
        ok(
          e instanceof TypeError,
          "Response should be an error for " + entry[0]
        );
      }
    );
    promises.push(p);
  });

  return Promise.all(promises);
}

function testRequestGET() {
  var promises = [];
  passFiles.forEach(function (entry) {
    var req = new Request(path + entry[0], { method: entry[1] });
    var p = fetch(req).then(function (res) {
      ok(
        res.type !== "error",
        "Response should not be an error for " + entry[0]
      );
      is(res.status, entry[2], "Status should match expected for " + entry[0]);
      is(
        res.statusText,
        entry[3],
        "Status text should match expected for " + entry[0]
      );
      if (entry[0] != "file_XHR_pass3.txt") {
        ok(
          res.url.endsWith(path + entry[0]),
          "Response url should match request for simple fetch for " + entry[0]
        );
      } else {
        ok(
          res.url.endsWith(path + "file_XHR_pass2.txt"),
          "Response url should match request for simple fetch for " + entry[0]
        );
      }
      is(
        res.headers.get("content-type"),
        entry[4],
        "Response should have content-type for " + entry[0]
      );
    });
    promises.push(p);
  });

  return Promise.all(promises);
}

function arraybuffer_equals_to(ab, s) {
  is(ab.byteLength, s.length, "arraybuffer byteLength should match");

  var u8v = new Uint8Array(ab);
  is(
    String.fromCharCode.apply(String, u8v),
    s,
    "arraybuffer bytes should match"
  );
}

function testResponses() {
  var fetches = [
    fetch(path + "file_XHR_pass2.txt").then(res => {
      is(res.status, 200, "status should match");
      return res
        .text()
        .then(v => is(v, "hello pass\n", "response should match"));
    }),

    fetch(path + "file_XHR_binary1.bin").then(res => {
      is(res.status, 200, "status should match");
      return res
        .arrayBuffer()
        .then(v =>
          arraybuffer_equals_to(
            v,
            "\xaa\xee\0\x03\xff\xff\xff\xff\xbb\xbb\xbb\xbb"
          )
        );
    }),

    new Promise((resolve, reject) => {
      var jsonBody = JSON.stringify({ title: "aBook", author: "john" });
      var req = new Request(path + "responseIdentical.sjs", {
        method: "POST",
        body: jsonBody,
      });
      var p = fetch(req).then(res => {
        is(res.status, 200, "status should match");
        return res.json().then(v => {
          is(JSON.stringify(v), jsonBody, "json response should match");
        });
      });
      resolve(p);
    }),

    new Promise((resolve, reject) => {
      var req = new Request(path + "responseIdentical.sjs", {
        method: "POST",
        body: "{",
      });
      var p = fetch(req).then(res => {
        is(res.status, 200, "wrong status");
        return res.json().then(
          v => ok(false, "expected json parse failure"),
          e => ok(true, "expected json parse failure")
        );
      });
      resolve(p);
    }),
  ];

  return Promise.all(fetches);
}

function testBlob() {
  return fetch(path + "/file_XHR_binary2.bin").then(r => {
    is(r.status, 200, "status should match");
    return r.blob().then(b => {
      is(b.size, 65536, "blob should have size 65536");
      return readAsArrayBuffer(b).then(function (ab) {
        var u8 = new Uint8Array(ab);
        for (var i = 0; i < 65536; i++) {
          if (u8[i] !== (i & 255)) {
            break;
          }
        }
        is(i, 65536, "wrong value at offset " + i);
      });
    });
  });
}

// This test is a copy of dom/html/test/formData_test.js testSend() modified to
// use the fetch API. Please change this if you change that.
function testFormDataSend() {
  var file,
    blob = new Blob(["hey"], { type: "text/plain" });

  var fd = new FormData();
  fd.append("string", "hey");
  fd.append("empty", blob);
  fd.append("explicit", blob, "explicit-file-name");
  fd.append("explicit-empty", blob, "");
  file = new File([blob], "testname", { type: "text/plain" });
  fd.append("file-name", file);
  file = new File([blob], "", { type: "text/plain" });
  fd.append("empty-file-name", file);
  file = new File([blob], "testname", { type: "text/plain" });
  fd.append("file-name-overwrite", file, "overwrite");

  var req = new Request("/tests/dom/html/test/form_submit_server.sjs", {
    method: "POST",
    body: fd,
  });

  return fetch(req).then(r => {
    is(r.status, 200, "status should match");
    return r.json().then(response => {
      for (var entry of response) {
        if (
          entry.headers["Content-Disposition"] != 'form-data; name="string"'
        ) {
          is(entry.headers["Content-Type"], "text/plain");
        }

        is(entry.body, "hey");
      }

      is(
        response[1].headers["Content-Disposition"],
        'form-data; name="empty"; filename="blob"'
      );

      is(
        response[2].headers["Content-Disposition"],
        'form-data; name="explicit"; filename="explicit-file-name"'
      );

      is(
        response[3].headers["Content-Disposition"],
        'form-data; name="explicit-empty"; filename=""'
      );

      is(
        response[4].headers["Content-Disposition"],
        'form-data; name="file-name"; filename="testname"'
      );

      is(
        response[5].headers["Content-Disposition"],
        'form-data; name="empty-file-name"; filename=""'
      );

      is(
        response[6].headers["Content-Disposition"],
        'form-data; name="file-name-overwrite"; filename="overwrite"'
      );
    });
  });
}

function runTest() {
  return Promise.resolve()
    .then(testURL)
    .then(testURLFail)
    .then(testRequestGET)
    .then(testResponses)
    .then(testBlob)
    .then(testFormDataSend);
  // Put more promise based tests here.
}