• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// META: script=resources/fetch-tests.js
2
3function xhr_should_succeed(test, url) {
4  return new Promise((resolve, reject) => {
5    const xhr = new XMLHttpRequest();
6    xhr.open('GET', url);
7    xhr.onload = test.step_func(() => {
8      assert_equals(xhr.status, 200);
9      assert_equals(xhr.statusText, 'OK');
10      resolve(xhr.response);
11    });
12    xhr.onerror = () => reject('Got unexpected error event');
13    xhr.send();
14  });
15}
16
17function xhr_should_fail(test, url, method = 'GET') {
18  const xhr = new XMLHttpRequest();
19  xhr.open(method, url);
20  const result1 = new Promise((resolve, reject) => {
21    xhr.onload = () => reject('Got unexpected load event');
22    xhr.onerror = resolve;
23  });
24  const result2 = new Promise(resolve => {
25    xhr.onreadystatechange = test.step_func(() => {
26      if (xhr.readyState !== xhr.DONE) return;
27      assert_equals(xhr.status, 0);
28      resolve();
29    });
30  });
31  xhr.send();
32  return Promise.all([result1, result2]);
33}
34
35fetch_tests('XHR', xhr_should_succeed, xhr_should_fail);
36
37async_test(t => {
38  const blob_contents = 'test blob contents';
39  const blob_type = 'image/png';
40  const blob = new Blob([blob_contents], {type: blob_type});
41  const url = URL.createObjectURL(blob);
42  const xhr = new XMLHttpRequest();
43  xhr.open('GET', url);
44  xhr.onloadend = t.step_func_done(() => {
45    assert_equals(xhr.getResponseHeader('Content-Type'), blob_type);
46  });
47  xhr.send();
48}, 'XHR should return Content-Type from Blob');
49
50async_test(t => {
51  const blob_contents = 'test blob contents';
52  const blob = new Blob([blob_contents]);
53  const url = URL.createObjectURL(blob);
54  const xhr = new XMLHttpRequest();
55  xhr.open('GET', url);
56
57  // Revoke the object URL.  XHR should take a reference to the blob as soon as
58  // it receives it in open(), so the request succeeds even though we revoke the
59  // URL before calling send().
60  URL.revokeObjectURL(url);
61
62  xhr.onload = t.step_func_done(() => {
63    assert_equals(xhr.response, blob_contents);
64  });
65  xhr.onerror = t.unreached_func('Got unexpected error event');
66
67  xhr.send();
68}, 'Revoke blob URL after open(), will fetch');
69