tor-browser

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

browser_ManifestFinder_browserHasManifestLink.js (2455B)


      1 "use strict";
      2 const { ManifestFinder } = ChromeUtils.importESModule(
      3  "resource://gre/modules/ManifestFinder.sys.mjs"
      4 );
      5 const defaultURL = new URL(
      6  "http://example.org/browser/dom/manifest/test/resource.sjs"
      7 );
      8 defaultURL.searchParams.set("Content-Type", "text/html; charset=utf-8");
      9 
     10 const tests = [
     11  {
     12    body: `
     13    <link rel="manifesto" href='${defaultURL}?body={"name":"fail"}'>
     14    <link rel="foo bar manifest bar test" href='${defaultURL}?body={"name":"value"}'>
     15    <link rel="manifest" href='${defaultURL}?body={"name":"fail"}'>
     16  `,
     17    run(result) {
     18      ok(result, "Document has a web manifest.");
     19    },
     20  },
     21  {
     22    body: `
     23    <link rel="amanifista" href='${defaultURL}?body={"name":"fail"}'>
     24    <link rel="foo bar manifesto bar test" href='${defaultURL}?body={"name":"pass-1"}'>
     25    <link rel="manifesto" href='${defaultURL}?body={"name":"fail"}'>`,
     26    run(result) {
     27      ok(!result, "Document does not have a web manifest.");
     28    },
     29  },
     30  {
     31    body: `
     32    <link rel="manifest" href="">
     33    <link rel="manifest" href='${defaultURL}?body={"name":"fail"}'>`,
     34    run(result) {
     35      ok(!result, "Manifest link is has empty href.");
     36    },
     37  },
     38  {
     39    body: `
     40    <link rel="manifest">
     41    <link rel="manifest" href='${defaultURL}?body={"name":"fail"}'>`,
     42    run(result) {
     43      ok(!result, "Manifest link is missing.");
     44    },
     45  },
     46 ];
     47 
     48 function makeTestURL({ body }) {
     49  const url = new URL(defaultURL);
     50  url.searchParams.set("body", encodeURIComponent(body));
     51  return url.href;
     52 }
     53 
     54 /**
     55 * Test basic API error conditions
     56 */
     57 add_task(async function () {
     58  const expected = "Invalid types should throw a TypeError.";
     59  for (let invalidValue of [undefined, null, 1, {}, "test"]) {
     60    try {
     61      await ManifestFinder.contentManifestLink(invalidValue);
     62      ok(false, expected);
     63    } catch (e) {
     64      is(e.name, "TypeError", expected);
     65    }
     66    try {
     67      await ManifestFinder.browserManifestLink(invalidValue);
     68      ok(false, expected);
     69    } catch (e) {
     70      is(e.name, "TypeError", expected);
     71    }
     72  }
     73 });
     74 
     75 add_task(async function () {
     76  const runningTests = tests
     77    .map(test => ({
     78      gBrowser,
     79      test,
     80      url: makeTestURL(test),
     81    }))
     82    .map(tabOptions =>
     83      BrowserTestUtils.withNewTab(tabOptions, async function (browser) {
     84        const result = await ManifestFinder.browserHasManifestLink(browser);
     85        tabOptions.test.run(result);
     86      })
     87    );
     88  await Promise.all(runningTests);
     89 });