tor-browser

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

test_blob_worker_xhr_post_multifile.html (3470B)


      1 <!--
      2  Any copyright is dedicated to the Public Domain.
      3  http://creativecommons.org/publicdomain/zero/1.0/
      4 -->
      5 <html>
      6 <head>
      7  <title>Indexed Database Property Test</title>
      8 
      9  <script src="/tests/SimpleTest/SimpleTest.js"></script>
     10  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
     11 
     12  <script type="text/javascript">
     13  /**
     14   * Create a composite/multi-file Blob on the worker, then post it as an XHR
     15   * payload and ensure that we don't hang/generate an assertion/etc. but
     16   * instead generate the expected 404.  This test is basically the same as
     17   * test_blob_worker_xhr_post.html except for the composite Blob.
     18   */
     19  function* testSteps()
     20  {
     21    const BLOB_DATA = ["fun ", "times ", "all ", "around!"];
     22    const BLOB_TYPE = "text/plain";
     23    const BLOB_SIZE = BLOB_DATA.join("").length;
     24 
     25    info("Setting up");
     26 
     27    let request = indexedDB.open(window.location.pathname, 1);
     28    request.onerror = errorHandler;
     29    request.onupgradeneeded = grabEventAndContinueHandler;
     30    request.onsuccess = unexpectedSuccessHandler;
     31    let event = yield undefined;
     32 
     33    let db = event.target.result;
     34    db.onerror = errorHandler;
     35 
     36    ok(db, "Created database");
     37 
     38    info("Creating objectStore");
     39 
     40    let objectStore = db.createObjectStore("foo", { autoIncrement: true });
     41 
     42    request.onupgradeneeded = unexpectedSuccessHandler;
     43    request.onsuccess = grabEventAndContinueHandler;
     44    event = yield undefined;
     45 
     46    ok(true, "Opened database");
     47 
     48    let blob = new Blob(BLOB_DATA, { type: BLOB_TYPE });
     49 
     50    info("Adding blob to database");
     51 
     52    objectStore = db.transaction("foo", "readwrite").objectStore("foo");
     53    objectStore.add(blob).onsuccess = grabEventAndContinueHandler;
     54    event = yield undefined;
     55 
     56    let blobKey = event.target.result;
     57    ok(blobKey, "Got a key for the blob");
     58 
     59    info("Getting blob from the database");
     60 
     61    objectStore = db.transaction("foo").objectStore("foo");
     62    objectStore.get(blobKey).onsuccess = grabEventAndContinueHandler;
     63    event = yield undefined;
     64 
     65    blob = event.target.result;
     66 
     67    ok(blob instanceof Blob, "Got a blob");
     68    is(blob.size, BLOB_SIZE, "Correct size");
     69    is(blob.type, BLOB_TYPE, "Correct type");
     70 
     71    function workerScript() {
     72      /* eslint-env worker */
     73      onmessage = function(event) {
     74        var blob = event.data;
     75        var compositeBlob = new Blob(["preceding string. ", blob],
     76                                     { type: "text/plain" });
     77        var xhr = new XMLHttpRequest();
     78        // We just want to make sure the error case doesn't fire; it's fine for
     79        // us to just want a 404.
     80        xhr.open("POST", "http://mochi.test:8888/does-not-exist", true);
     81        xhr.onload = function() {
     82          postMessage({ status: xhr.status });
     83        };
     84        xhr.onerror = function() {
     85          postMessage({ status: "error" });
     86        };
     87        xhr.send(compositeBlob);
     88      };
     89    }
     90 
     91    let workerScriptUrl =
     92      URL.createObjectURL(new Blob(["(", workerScript.toString(), ")()"]));
     93 
     94    let xhrWorker = new Worker(workerScriptUrl);
     95    xhrWorker.postMessage(blob);
     96    xhrWorker.onmessage = grabEventAndContinueHandler;
     97    event = yield undefined;
     98 
     99    is(event.data.status, 404, "XHR generated the expected 404");
    100    xhrWorker.terminate();
    101 
    102    URL.revokeObjectURL(workerScriptUrl);
    103 
    104    finishTest();
    105  }
    106  </script>
    107  <script type="text/javascript" src="helpers.js"></script>
    108 
    109 </head>
    110 
    111 <body onload="runTest();"></body>
    112 
    113 </html>