tor-browser

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

test_pick_canonical_url.js (2566B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 https://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const { pickCanonicalUrl } = ChromeUtils.importESModule(
      7  "moz-src:///browser/components/tabnotes/CanonicalURL.sys.mjs"
      8 );
      9 
     10 const LINK_REL_CANONICAL = "https://www.example.com/link_rel_canonical";
     11 const OPENGRAPH = "https://www.example.com/opengraph";
     12 const JSON_LD = "https://www.example.com/json-ld";
     13 const FALLBACK = "https://www.example.com/fallback";
     14 
     15 add_task(async function test_canonical_link_only() {
     16  Assert.equal(
     17    pickCanonicalUrl({ link: LINK_REL_CANONICAL, fallback: FALLBACK }),
     18    LINK_REL_CANONICAL,
     19    `should always pick link[rel="canonical"] if it was found`
     20  );
     21 });
     22 
     23 add_task(async function test_canonical_link_and_opengraph() {
     24  Assert.equal(
     25    pickCanonicalUrl({
     26      link: LINK_REL_CANONICAL,
     27      opengraph: OPENGRAPH,
     28      fallback: FALLBACK,
     29    }),
     30    LINK_REL_CANONICAL,
     31    `should always pick link[rel="canonical"] if it was found`
     32  );
     33 });
     34 
     35 add_task(async function test_canonical_link_and_json_ld() {
     36  Assert.equal(
     37    pickCanonicalUrl({
     38      link: LINK_REL_CANONICAL,
     39      jsonLd: JSON_LD,
     40      fallback: FALLBACK,
     41    }),
     42    LINK_REL_CANONICAL,
     43    `should always pick link[rel="canonical"] if it was found`
     44  );
     45 });
     46 
     47 add_task(async function test_canonical_link_and_opengraph_and_json_ld() {
     48  Assert.equal(
     49    pickCanonicalUrl({
     50      link: LINK_REL_CANONICAL,
     51      opengraph: OPENGRAPH,
     52      jsonLd: JSON_LD,
     53      fallback: FALLBACK,
     54    }),
     55    LINK_REL_CANONICAL,
     56    `should always pick link[rel="canonical"] if it was found`
     57  );
     58 });
     59 
     60 add_task(async function test_opengraph_only() {
     61  Assert.equal(
     62    pickCanonicalUrl({ opengraph: OPENGRAPH, fallback: FALLBACK }),
     63    OPENGRAPH,
     64    `should pick meta[property="og:url"] if canonical link not found`
     65  );
     66 });
     67 
     68 add_task(async function test_opengraph_and_json_ld() {
     69  Assert.equal(
     70    pickCanonicalUrl({
     71      opengraph: OPENGRAPH,
     72      jsonLd: JSON_LD,
     73      fallback: FALLBACK,
     74    }),
     75    OPENGRAPH,
     76    `should pick meta[property="og:url"] if canonical link not found`
     77  );
     78 });
     79 
     80 add_task(async function test_json_ld_only() {
     81  Assert.equal(
     82    pickCanonicalUrl({
     83      jsonLd: JSON_LD,
     84      fallback: FALLBACK,
     85    }),
     86    JSON_LD,
     87    "should pick JSON-LD data if neither canonical link nor og:url were found"
     88  );
     89 });
     90 
     91 add_task(async function test_fallback() {
     92  Assert.equal(
     93    pickCanonicalUrl({
     94      fallback: FALLBACK,
     95    }),
     96    FALLBACK,
     97    "should only use the fallback if nothing else was found"
     98  );
     99 });