tor-browser

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

xhr2_worker.js (2430B)


      1 /**
      2 * Any copyright is dedicated to the Public Domain.
      3 * http://creativecommons.org/publicdomain/zero/1.0/
      4 */
      5 /* eslint-env worker */
      6 onmessage = function (event) {
      7  const url = event.data;
      8 
      9  var xhr = new XMLHttpRequest();
     10  xhr.open("GET", url, false);
     11  xhr.send();
     12 
     13  const refText = xhr.responseText;
     14 
     15  function getResponse(type) {
     16    xhr = new XMLHttpRequest();
     17    xhr.open("GET", url, false);
     18    if (type !== undefined) {
     19      xhr.responseType = type;
     20    }
     21    xhr.send();
     22    return xhr.response;
     23  }
     24 
     25  if (getResponse() != refText) {
     26    throw new Error("unset responseType failed");
     27  }
     28 
     29  if (getResponse("") != refText) {
     30    throw new Error("'' responseType failed");
     31  }
     32 
     33  if (getResponse("text") != refText) {
     34    throw new Error("'text' responseType failed");
     35  }
     36 
     37  var array = new Uint8Array(getResponse("arraybuffer"));
     38  if (String.fromCharCode.apply(String, array) != refText) {
     39    throw new Error("'arraybuffer' responseType failed");
     40  }
     41 
     42  var blob = getResponse("blob");
     43  if (new FileReaderSync().readAsText(blob) != refText) {
     44    throw new Error("'blob' responseType failed");
     45  }
     46 
     47  // Make sure that we get invalid state exceptions when getting the wrong
     48  // property.
     49 
     50  function testResponseTextException(type) {
     51    xhr = new XMLHttpRequest();
     52    xhr.open("GET", url, false);
     53    xhr.responseType = type;
     54    xhr.send();
     55 
     56    var exception;
     57 
     58    try {
     59      xhr.responseText;
     60    } catch (e) {
     61      exception = e;
     62    }
     63 
     64    if (!exception) {
     65      throw new Error(
     66        "Failed to throw when getting responseText on '" + type + "' type"
     67      );
     68    }
     69 
     70    if (exception.name != "InvalidStateError") {
     71      throw new Error(
     72        "Unexpected error when getting responseText on '" + type + "' type"
     73      );
     74    }
     75 
     76    if (exception.code != DOMException.INVALID_STATE_ERR) {
     77      throw new Error(
     78        "Unexpected error code when getting responseText on '" + type + "' type"
     79      );
     80    }
     81  }
     82 
     83  testResponseTextException("arraybuffer");
     84  testResponseTextException("blob");
     85 
     86  // Make sure "document" works, but returns text.
     87  xhr = new XMLHttpRequest();
     88 
     89  if (xhr.responseType != "") {
     90    throw new Error("Default value for responseType is wrong!");
     91  }
     92 
     93  xhr.open("GET", url, false);
     94  xhr.responseType = "document";
     95  xhr.send();
     96 
     97  if (xhr.responseText != refText) {
     98    throw new Error("'document' type not working correctly");
     99  }
    100 
    101  postMessage("done");
    102 };