tor-browser

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

test_json_ld.js (2397B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 https://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const { findCandidates } = ChromeUtils.importESModule(
      7  "moz-src:///browser/components/tabnotes/CanonicalURL.sys.mjs"
      8 );
      9 
     10 /**
     11 * @param {string[]} scripts
     12 * @returns {Document}
     13 */
     14 function getDocument(scripts) {
     15  const scriptTags = scripts
     16    .map(content => `<script type="application/ld+json">${content}</script>`)
     17    .join("\n");
     18 
     19  const html = `
     20 <!DOCTYPE html>
     21 <html>
     22 <head>
     23  <meta charset="utf-8">
     24 </head>
     25 <body>
     26  ${scriptTags}
     27 </body>
     28 </html>
     29 `;
     30  return Document.parseHTMLUnsafe(html);
     31 }
     32 
     33 add_task(async function test_json_ld_missing() {
     34  const doc = getDocument([]);
     35 
     36  const candidates = findCandidates(doc);
     37 
     38  Assert.equal(
     39    candidates.jsonLd,
     40    undefined,
     41    `JSON-LD data should not be found`
     42  );
     43 });
     44 
     45 add_task(async function test_json_ld_basic() {
     46  const doc = getDocument([
     47    JSON.stringify({
     48      "@context": "https://schema.org/",
     49      "@type": "Thing",
     50      url: "https://www.example.com",
     51    }),
     52  ]);
     53 
     54  const candidates = findCandidates(doc);
     55 
     56  Assert.equal(
     57    candidates.jsonLd,
     58    "https://www.example.com",
     59    `JSON-LD data should be found`
     60  );
     61 });
     62 
     63 add_task(async function test_json_ld_selects_first() {
     64  const doc = getDocument([
     65    JSON.stringify({
     66      "@context": "https://schema.org/",
     67      "@type": "Thing",
     68      url: "https://www.example.com/1",
     69    }),
     70    JSON.stringify({
     71      "@context": "https://schema.org/",
     72      "@type": "CreativeWork",
     73      url: "https://www.example.com/2",
     74    }),
     75    JSON.stringify({
     76      "@context": "https://schema.org/",
     77      "@type": "WebPage",
     78      url: "https://www.example.com/3",
     79    }),
     80  ]);
     81 
     82  const candidates = findCandidates(doc);
     83 
     84  Assert.equal(
     85    candidates.jsonLd,
     86    "https://www.example.com/1",
     87    `the first JSON-LD data should be preferred`
     88  );
     89 });
     90 
     91 add_task(async function test_json_ld_robust_to_url_array() {
     92  const doc = getDocument([
     93    JSON.stringify({
     94      "@context": "https://schema.org/",
     95      "@type": "SiteMap",
     96      url: [
     97        "https://www.example.com/1",
     98        "https://www.example.com/2",
     99        "https://www.example.com/3",
    100      ],
    101    }),
    102  ]);
    103 
    104  const candidates = findCandidates(doc);
    105 
    106  Assert.equal(
    107    candidates.jsonLd,
    108    undefined,
    109    `when url is an array, the JSON-LD data should not be used`
    110  );
    111 });