tor-browser

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

utils.js (3264B)


      1 function header(name, value) {
      2  return `header(${name},${value})`;
      3 }
      4 
      5 function contentType(type) {
      6  return header("Content-Type", type);
      7 }
      8 
      9 function contentTypeOptions(type) {
     10  return header("X-Content-Type-Options", type);
     11 }
     12 
     13 function testFetchNoCors(_t, path, { headers }) {
     14  return fetch(path, {
     15    ...(headers ? { headers } : {}),
     16    mode: "no-cors",
     17  });
     18 }
     19 
     20 function testElementInitiator(t, path, name) {
     21  let element = document.createElement(name);
     22  element.src = path;
     23  t.add_cleanup(() => element.remove());
     24  return new Promise((resolve, reject) => {
     25    element.onerror = e => reject(new TypeError());
     26    element.onload = resolve;
     27 
     28    document.body.appendChild(element);
     29  });
     30 }
     31 
     32 function testImageInitiator(t, path) {
     33  return testElementInitiator(t, path, "img");
     34 }
     35 
     36 function testAudioInitiator(t, path) {
     37  return testElementInitiator(t, path, "audio");
     38 }
     39 
     40 function testVideoInitiator(t, path) {
     41  return testElementInitiator(t, path, "video");
     42 }
     43 
     44 function testScriptInitiator(t, path) {
     45  return testElementInitiator(t, path, "script");
     46 }
     47 
     48 function runTest(t, test, file, options, ...pipe) {
     49  const path = `${file}${pipe.length ? `?pipe=${pipe.join("|")}` : ""}`;
     50  return test(t, path, options)
     51 }
     52 
     53 function testRunAll(file, testCallback, adapter, options) {
     54  let testcase = function (test, message, skip) {
     55    return {test, message, skip};
     56  };
     57 
     58  const name = "...";
     59  [ testcase(testFetchNoCors, `fetch(${name}, {mode: "no-cors"})`, false || options.skip.includes("fetch")),
     60    testcase(testImageInitiator, `<img src=${name}>`,  options.onlyFetch || options.skip.includes("image")),
     61    testcase(testAudioInitiator, `<audio src=${name}>`, options.onlyFetch || options.skip.includes("audio")),
     62    testcase(testVideoInitiator, `<video src=${name}>`, options.onlyFetch || options.skip.includes("video")),
     63    testcase(testScriptInitiator, `<script src=${name}>`, options.onlyFetch || options.skip.includes("script")),
     64  ].filter(({skip}) => !skip)
     65  .forEach(({test, message}) => {
     66    testCallback((t, ...args) => adapter(t, runTest(t, test, file, options, ...args), message), header => `${header}: ${message}`);
     67  });
     68 }
     69 
     70 function expected_block(file, testCallback, options = {}) {
     71  let defaultOptions = {
     72    onlyFetch: !self.GLOBAL.isWindow(),
     73    skip: []
     74  };
     75  testRunAll(file, testCallback, (t, promise, message) => promise_rejects_js(t, TypeError, promise, message), { ...defaultOptions, ...options });
     76 }
     77 
     78 function expected_allow(file, testCallback, options = {}) {
     79  let defaultOptions = {
     80    onlyFetch: !self.GLOBAL.isWindow(),
     81    skip: [],
     82    headers: null
     83  };
     84  testRunAll(file, testCallback, (_t, promise, _message) => promise, { ...defaultOptions, ...options });
     85 }
     86 
     87 function expected_allow_fetch(file, testCallback, options = {}) {
     88  let defaultOptions = {
     89    skip: [],
     90    headers: null,
     91  };
     92  testRunAll(file, testCallback, (_t, promise, _message) => promise, { ...defaultOptions, ...options, onlyFetch: true });
     93 }
     94 
     95 function expected_block_fetch(file, testCallback, options = {}) {
     96  let defaultOptions = {
     97    skip: [],
     98    headers: null,
     99  };
    100  testRunAll(file, testCallback, (t, promise, message) => promise_rejects_js(t, TypeError, promise, message), { ...defaultOptions, ...options, onlyFetch: true });
    101 }