tor-browser

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

fetch-destination.https.html (17432B)


      1 <!DOCTYPE html>
      2 <title>Fetch destination tests</title>
      3 <script src="/resources/testharness.js"></script>
      4 <script src="/resources/testharnessreport.js"></script>
      5 <script src="/common/get-host-info.sub.js"></script>
      6 <script src="/common/media.js"></script>
      7 <script src="/service-workers/service-worker/resources/test-helpers.sub.js"></script>
      8 <script>
      9 let frame;
     10 
     11 // Set up the service worker and the frame.
     12 promise_test(t => {
     13    const kScope = 'resources/empty.https.html';
     14    const kScript = 'resources/fetch-destination-worker.js';
     15    return service_worker_unregister_and_register(t, kScript, kScope)
     16      .then(registration => {
     17          add_completion_callback(() => {
     18              registration.unregister();
     19            });
     20 
     21          return wait_for_state(t, registration.installing, 'activated');
     22        })
     23      .then(() => {
     24          return with_iframe(kScope);
     25        })
     26      .then(f => {
     27          frame = f;
     28          add_completion_callback(() => { f.remove(); });
     29        });
     30  }, 'Initialize global state');
     31 
     32 // Actual tests
     33 
     34 // Image destination
     35 ////////////////////
     36 
     37 // HTMLImageElement - image destination
     38 promise_test(async t => {
     39  await new Promise((resolve, reject) => {
     40      let node = frame.contentWindow.document.createElement("img");
     41      node.onload = resolve;
     42      node.onerror = reject;
     43      node.src = "dummy.png?dest=image";
     44  }).catch(() => {
     45      assert_unreached("Fetch errored.");
     46  });
     47 }, 'HTMLImageElement fetches with an "image" Request.destination');
     48 
     49 // HTMLImageElement with srcset attribute - image destination
     50 promise_test(async t => {
     51  await new Promise((resolve, reject) => {
     52      let node = frame.contentWindow.document.createElement("img");
     53      node.onload = resolve;
     54      node.onerror = reject;
     55      node.srcset = "dummy.png?t=srcset&dest=image";
     56  }).catch(() => {
     57      assert_unreached("Fetch errored.");
     58  });
     59 }, 'HTMLImageElement with srcset attribute fetches with an "image" Request.destination');
     60 
     61 // HTMLImageElement with srcset attribute - image destination
     62 promise_test(async t => {
     63  await new Promise((resolve, reject) => {
     64      let img = frame.contentWindow.document.createElement("img");
     65      let picture = frame.contentWindow.document.createElement("picture");
     66      let source = frame.contentWindow.document.createElement("source");
     67      picture.appendChild(source);
     68      picture.appendChild(img);
     69      img.onload = resolve;
     70      img.onerror = reject;
     71      source.srcset = "dummy.png?t=picture&dest=image";
     72  }).catch(() => {
     73      assert_unreached("Fetch errored.");
     74  });
     75 }, 'HTMLImageElement with a HTMLPictureElement parent attribute fetches with an "image" Request.destination');
     76 
     77 // SVGImageElement - image destination
     78 promise_test(async t => {
     79  await new Promise((resolve, reject) => {
     80      let svg = frame.contentWindow.document.createElementNS('http://www.w3.org/2000/svg','svg');
     81      svg.setAttributeNS('http://www.w3.org/2000/svg','xlink','http://www.w3.org/1999/xlink');
     82      let svgimg = frame.contentWindow.document.createElementNS('http://www.w3.org/2000/svg','image');
     83      svgimg.onload = resolve;
     84      svgimg.onerror = reject;
     85      svgimg.setAttributeNS('http://www.w3.org/1999/xlink','href','dummy.png?t=svg&dest=image');
     86      svg.appendChild(svgimg);
     87      frame.contentWindow.document.documentElement.appendChild(svg);
     88  }).catch(() => {
     89      assert_unreached("Fetch errored.");
     90  });
     91 }, 'SVGImageElement fetches with an "image" Request.destination');
     92 
     93 // Empty string destination
     94 ///////////////////////////
     95 
     96 // fetch() - empty string destination
     97 promise_test(async t => {
     98  let response = await frame.contentWindow.fetch("dummy?dest=");
     99  assert_true(response.ok);
    100 }, 'fetch() fetches with an empty string Request.destination');
    101 
    102 // XMLHttpRequest - empty string destination
    103 promise_test(async t => {
    104  let xhr;
    105  await new Promise((resolve, reject) => {
    106      xhr = new frame.contentWindow.XMLHttpRequest();
    107      xhr.onload = resolve;
    108      xhr.onerror = reject;
    109      xhr.open("GET", "dummy?t=xhr&dest=");
    110      xhr.send();
    111    }).catch(() => {
    112      assert_unreached("Fetch errored.");
    113    });
    114  assert_equals(xhr.status, 200);
    115 }, 'XMLHttpRequest() fetches with an empty string Request.destination');
    116 
    117 // EventSource - empty string destination
    118 promise_test(async t => {
    119  let xhr;
    120  await new Promise((resolve, reject) => {
    121      eventSource = new frame.contentWindow.EventSource("dummy.es?t=eventsource&dest=");
    122      eventSource.onopen = resolve;
    123      eventSource.onerror = reject;
    124    }).catch(() => {
    125      assert_unreached("Fetch errored.");
    126    });
    127 }, 'EventSource() fetches with an empty string Request.destination');
    128 
    129 // HTMLAudioElement - audio destination
    130 ///////////////////////////////////////
    131 promise_test(async t => {
    132  await new Promise((resolve, reject) => {
    133      let audioURL = getAudioURI("dummy_audio");
    134      let node = frame.contentWindow.document.createElement("audio");
    135      node.onloadeddata = resolve;
    136      node.onerror = reject;
    137      node.src = audioURL + "?dest=audio";
    138  }).catch(() => {
    139      assert_unreached("Fetch errored.");
    140  });
    141 }, 'HTMLAudioElement fetches with an "audio" Request.destination');
    142 
    143 // HTMLVideoElement - video destination
    144 ///////////////////////////////////////
    145 promise_test(async t => {
    146  await new Promise((resolve, reject) => {
    147      let videoURL = getVideoURI("dummy_video");
    148      let node = frame.contentWindow.document.createElement("video");
    149      node.onloadeddata = resolve;
    150      node.onerror = reject;
    151      node.src = videoURL + "?dest=video";
    152  }).catch(() => {
    153      assert_unreached("Fetch errored.");
    154  });
    155 }, 'HTMLVideoElement fetches with a "video" Request.destination');
    156 
    157 // script destinations
    158 //////////////////////
    159 
    160 // HTMLScriptElement - script destination
    161 promise_test(async t => {
    162  await new Promise((resolve, reject) => {
    163      let node = frame.contentWindow.document.createElement("script");
    164      node.onload = resolve;
    165      node.onerror = reject;
    166      node.src = "dummy?dest=script";
    167      frame.contentWindow.document.body.appendChild(node);
    168  }).catch(() => {
    169      assert_unreached("Fetch errored.");
    170  });
    171 }, 'HTMLScriptElement fetches with a "script" Request.destination');
    172 
    173 // audioworklet destination
    174 //////////////////////
    175 promise_test(async t => {
    176  let audioContext = new frame.contentWindow.AudioContext();
    177  await audioContext.audioWorklet.addModule("dummy?dest=audioworklet");
    178 }, 'AudioWorklet module fetches with a "audioworklet" Request.destination');
    179 
    180 // Style destination
    181 ////////////////////
    182 
    183 // HTMLLinkElement with rel=stylesheet - style destination
    184 promise_test(async t => {
    185  await new Promise((resolve, reject) => {
    186      let node = frame.contentWindow.document.createElement("link");
    187      node.rel = "stylesheet";
    188      node.onload = resolve;
    189      node.onerror = reject;
    190      node.href = "dummy?dest=style";
    191      frame.contentWindow.document.body.appendChild(node);
    192  }).catch(() => {
    193      assert_unreached("Fetch errored.");
    194  });
    195 }, 'HTMLLinkElement with rel=stylesheet fetches with a "style" Request.destination');
    196 
    197 // Import declaration with `type: "css"` - style destination
    198 promise_test(t => {
    199  return new Promise((resolve, reject) => {
    200      frame.contentWindow.onerror = reject;
    201 
    202      let node = frame.contentWindow.document.createElement("script");
    203      node.onload = resolve;
    204      node.onerror = reject;
    205      node.src = "import-declaration-type-css.js";
    206      node.type = "module";
    207      frame.contentWindow.document.body.appendChild(node);
    208  }).then(() => {
    209    frame.contentWindow.onerror = null;
    210  });
    211 }, 'Import declaration with `type: "css"` fetches with a "style" Request.destination');
    212 
    213 // JSON destination
    214 ///////////////////
    215 
    216 // Import declaration with `type: "json"` - json destination
    217 promise_test(t => {
    218  return new Promise((resolve, reject) => {
    219      frame.contentWindow.onerror = reject;
    220      let node = frame.contentWindow.document.createElement("script");
    221      node.onload = resolve;
    222      node.onerror = reject;
    223      node.src = "import-declaration-type-json.js";
    224      node.type = "module";
    225      frame.contentWindow.document.body.appendChild(node);
    226  }).then(() => {
    227    frame.contentWindow.onerror = null;
    228  });
    229 }, 'Import declaration with `type: "json"` fetches with a "json" Request.destination');
    230 
    231 // Preload tests
    232 ////////////////
    233 // HTMLLinkElement with rel=preload and as=fetch - empty string destination
    234 promise_test(async t => {
    235  await new Promise((resolve, reject) => {
    236      let node = frame.contentWindow.document.createElement("link");
    237      node.rel = "preload";
    238      node.as = "fetch";
    239      if (node.as != "fetch") {
    240        resolve();
    241      }
    242      node.onload = resolve;
    243      node.onerror = reject;
    244      node.href = "dummy?t=2&dest=";
    245      frame.contentWindow.document.body.appendChild(node);
    246  }).catch(() => {
    247      assert_unreached("Fetch errored.");
    248  });
    249 }, 'HTMLLinkElement with rel=preload and as=fetch fetches with an empty string Request.destination');
    250 
    251 // HTMLLinkElement with rel=preload and as=style - style destination
    252 promise_test(async t => {
    253  await new Promise((resolve, reject) => {
    254      let node = frame.contentWindow.document.createElement("link");
    255      node.rel = "preload";
    256      node.as = "style";
    257      if (node.as != "style") {
    258        resolve();
    259      }
    260      node.onload = resolve;
    261      node.onerror = reject;
    262      node.href = "dummy?t=2&dest=style";
    263      frame.contentWindow.document.body.appendChild(node);
    264  }).catch(() => {
    265      assert_unreached("Fetch errored.");
    266  });
    267 }, 'HTMLLinkElement with rel=preload and as=style fetches with a "style" Request.destination');
    268 
    269 // HTMLLinkElement with rel=preload and as=json - json destination
    270 promise_test(t => {
    271  return new Promise((resolve, reject) => {
    272      let node = frame.contentWindow.document.createElement("link");
    273      node.rel = "preload";
    274      node.as = "json";
    275      if (node.as != "json") {
    276        resolve();
    277      }
    278      node.onload = resolve;
    279      node.onerror = reject;
    280      node.href = "dummy.json?t=2&dest=json";
    281      frame.contentWindow.document.body.appendChild(node);
    282  });
    283 }, 'HTMLLinkElement with rel=preload and as=json fetches with a "json" Request.destination');
    284 
    285 // HTMLLinkElement with rel=preload and as=script - script destination
    286 promise_test(async t => {
    287  await new Promise((resolve, reject) => {
    288      let node = frame.contentWindow.document.createElement("link");
    289      node.rel = "preload";
    290      node.as = "script";
    291      if (node.as != "script") {
    292        resolve();
    293      }
    294      node.onload = resolve;
    295      node.onerror = reject;
    296      node.href = "dummy?t=2&dest=script";
    297      frame.contentWindow.document.body.appendChild(node);
    298  }).catch(() => {
    299      assert_unreached("Fetch errored.");
    300  });
    301 }, 'HTMLLinkElement with rel=preload and as=script fetches with a "script" Request.destination');
    302 
    303 // HTMLLinkElement with rel=preload and as=font - font destination
    304 promise_test(async t => {
    305  await new Promise((resolve, reject) => {
    306      let node = frame.contentWindow.document.createElement("link");
    307      node.rel = "preload";
    308      node.as = "font";
    309      if (node.as != "font") {
    310        resolve();
    311      }
    312      node.onload = resolve;
    313      node.onerror = reject;
    314      node.href = "dummy?t=2&dest=font";
    315      frame.contentWindow.document.body.appendChild(node);
    316  }).catch(() => {
    317      assert_unreached("Fetch errored.");
    318  });
    319 }, 'HTMLLinkElement with rel=preload and as=font fetches with a "font" Request.destination');
    320 
    321 // HTMLLinkElement with rel=preload and as=image - image destination
    322 promise_test(async t => {
    323  await new Promise((resolve, reject) => {
    324      let node = frame.contentWindow.document.createElement("link");
    325      node.rel = "preload";
    326      node.as = "image";
    327      if (node.as != "image") {
    328        resolve();
    329      }
    330      node.onload = resolve;
    331      node.onerror = reject;
    332      node.href = "dummy.png?t=2&dest=image";
    333      frame.contentWindow.document.body.appendChild(node);
    334  }).catch(() => {
    335      assert_unreached("Fetch errored.");
    336  });
    337 }, 'HTMLLinkElement with rel=preload and as=image fetches with a "image" Request.destination');
    338 
    339 // HTMLLinkElement with rel=preload and as=audio - audio destination
    340 promise_test(async t => {
    341  await new Promise((resolve, reject) => {
    342      let audioURL = getAudioURI("dummy_audio");
    343      let node = frame.contentWindow.document.createElement("link");
    344      node.rel = "preload";
    345      node.as = "audio";
    346      if (node.as != "audio") {
    347        resolve();
    348      }
    349      node.onload = resolve;
    350      node.onerror = reject;
    351      node.href = audioURL + "?dest=audio";
    352      frame.contentWindow.document.body.appendChild(node);
    353  }).catch(() => {
    354      assert_unreached("Fetch errored.");
    355  });
    356 }, 'HTMLLinkElement with rel=preload and as=audio fetches with a "audio" Request.destination');
    357 
    358 // HTMLLinkElement with rel=preload and as=video - video destination
    359 promise_test(async t => {
    360  await new Promise((resolve, reject) => {
    361      let videoURL = getVideoURI("dummy_video");
    362      let node = frame.contentWindow.document.createElement("link");
    363      node.rel = "preload";
    364      node.as = "video";
    365      if (node.as != "video") {
    366        resolve();
    367      }
    368      node.onload = resolve;
    369      node.onerror = reject;
    370      node.href = videoURL + "?dest=video";
    371      frame.contentWindow.document.body.appendChild(node);
    372  }).catch(() => {
    373      assert_unreached("Fetch errored.");
    374  });
    375 }, 'HTMLLinkElement with rel=preload and as=video fetches with a "video" Request.destination');
    376 
    377 // HTMLLinkElement with rel=preload and as=track - track destination
    378 promise_test(async t => {
    379  await new Promise((resolve, reject) => {
    380      let node = frame.contentWindow.document.createElement("link");
    381      node.rel = "preload";
    382      node.as = "track";
    383      if (node.as != "track") {
    384        resolve();
    385      }
    386      node.onload = resolve;
    387      node.onerror = reject;
    388      node.href = "dummy?dest=track";
    389      frame.contentWindow.document.body.appendChild(node);
    390  }).catch(() => {
    391      assert_unreached("Fetch errored.");
    392  });
    393 }, 'HTMLLinkElement with rel=preload and as=track fetches with a "track" Request.destination');
    394 
    395 // HTMLLinkElement with rel=preload and as=document - document destination
    396 promise_test(async t => {
    397  await new Promise((resolve, reject) => {
    398      let node = frame.contentWindow.document.createElement("link");
    399      node.rel = "preload";
    400      node.as = "document";
    401      if (node.as != "document") {
    402        resolve();
    403      }
    404      node.onload = resolve;
    405      node.onerror = reject;
    406      node.href = "dummy?dest=document";
    407      frame.contentWindow.document.body.appendChild(node);
    408  }).catch(() => {
    409      assert_unreached("Fetch errored.");
    410  });
    411 }, 'HTMLLinkElement with rel=preload and as=document fetches with a "document" Request.destination');
    412 
    413 // HTMLLinkElement with rel=preload and as=worker - worker destination
    414 promise_test(async t => {
    415  await new Promise((resolve, reject) => {
    416      let node = frame.contentWindow.document.createElement("link");
    417      node.rel = "preload";
    418      node.as = "worker";
    419      if (node.as != "worker") {
    420        resolve();
    421      }
    422      node.onload = resolve;
    423      node.onerror = reject;
    424      node.href = "dummy?dest=worker";
    425      frame.contentWindow.document.body.appendChild(node);
    426  }).catch(() => {
    427      assert_unreached("Fetch errored.");
    428  });
    429 }, 'HTMLLinkElement with rel=preload and as=worker fetches with a "worker" Request.destination');
    430 
    431 // HTMLLinkElement with rel=preload and as=sharedworker - sharedworker destination
    432 promise_test(async t => {
    433  await new Promise((resolve, reject) => {
    434      let node = frame.contentWindow.document.createElement("link");
    435      node.rel = "preload";
    436      node.as = "sharedworker";
    437      if (node.as != "sharedworker") {
    438        resolve();
    439      }
    440      node.onload = resolve;
    441      node.onerror = reject;
    442      node.href = "dummy?dest=sharedworker";
    443      frame.contentWindow.document.body.appendChild(node);
    444  }).catch(() => {
    445      assert_unreached("Fetch errored.");
    446  });
    447 }, 'HTMLLinkElement with rel=preload and as=sharedworker fetches with a "sharedworker" Request.destination');
    448 
    449 // HTMLLinkElement with rel=preload and as=xslt - xslt destination
    450 promise_test(async t => {
    451  await new Promise((resolve, reject) => {
    452      let node = frame.contentWindow.document.createElement("link");
    453      node.rel = "preload";
    454      node.as = "xslt";
    455      if (node.as != "xslt") {
    456        resolve();
    457      }
    458      node.onload = resolve;
    459      node.onerror = reject;
    460      node.href = "dummy?dest=xslt";
    461      frame.contentWindow.document.body.appendChild(node);
    462  }).catch(() => {
    463      assert_unreached("Fetch errored.");
    464  });
    465 }, 'HTMLLinkElement with rel=preload and as=xslt fetches with a "xslt" Request.destination');
    466 
    467 // HTMLLinkElement with rel=preload and as=manifest - manifest destination
    468 promise_test(async t => {
    469  await new Promise((resolve, reject) => {
    470      let node = frame.contentWindow.document.createElement("link");
    471      node.rel = "preload";
    472      node.as = "manifest";
    473      if (node.as != "manifest") {
    474        resolve();
    475      }
    476      node.onload = resolve;
    477      node.onerror = reject;
    478      node.href = "dummy?dest=manifest";
    479      frame.contentWindow.document.body.appendChild(node);
    480  }).catch(() => {
    481      assert_unreached("Fetch errored.");
    482  });
    483 }, 'HTMLLinkElement with rel=preload and as=manifest fetches with a "manifest" Request.destination');
    484 
    485 </script>