• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// META: script=resources/fetch-tests.js
2
3function fetch_should_succeed(test, request) {
4  return fetch(request).then(response => response.text());
5}
6
7function fetch_should_fail(test, url, method = 'GET') {
8  return promise_rejects_js(test, TypeError, fetch(url, {method: method}));
9}
10
11fetch_tests('fetch', fetch_should_succeed, fetch_should_fail);
12
13promise_test(t => {
14  const blob_contents = 'test blob contents';
15  const blob_type = 'image/png';
16  const blob = new Blob([blob_contents], {type: blob_type});
17  const url = URL.createObjectURL(blob);
18
19  return fetch(url).then(response => {
20    assert_equals(response.headers.get('Content-Type'), blob_type);
21  });
22}, 'fetch should return Content-Type from Blob');
23
24promise_test(t => {
25  const blob_contents = 'test blob contents';
26  const blob = new Blob([blob_contents]);
27  const url = URL.createObjectURL(blob);
28  const request = new Request(url);
29
30  // Revoke the object URL.  Request should take a reference to the blob as
31  // soon as it receives it in open(), so the request succeeds even though we
32  // revoke the URL before calling fetch().
33  URL.revokeObjectURL(url);
34
35  return fetch_should_succeed(t, request).then(text => {
36    assert_equals(text, blob_contents);
37  });
38}, 'Revoke blob URL after creating Request, will fetch');
39
40promise_test(function(t) {
41  const blob_contents = 'test blob contents';
42  const blob = new Blob([blob_contents]);
43  const url = URL.createObjectURL(blob);
44
45  const result = fetch_should_succeed(t, url).then(text => {
46    assert_equals(text, blob_contents);
47  });
48
49  // Revoke the object URL. fetch should have already resolved the blob URL.
50  URL.revokeObjectURL(url);
51
52  return result;
53}, 'Revoke blob URL after calling fetch, fetch should succeed');
54