tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

url-with-fetch.any.js (2419B)


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