tor-browser

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

test_fetch_basic.js (5130B)


      1 function testAboutURL() {
      2  var p1 = fetch("about:blank").then(
      3    function (res) {
      4      ok(false, "about:blank should fail");
      5    },
      6    function (e) {
      7      ok(e instanceof TypeError, "about:blank should fail");
      8    }
      9  );
     10 
     11  var p2 = fetch("about:config").then(
     12    function (res) {
     13      ok(false, "about:config should fail");
     14    },
     15    function (e) {
     16      ok(e instanceof TypeError, "about:config should fail");
     17    }
     18  );
     19 
     20  return Promise.all([p1, p2]);
     21 }
     22 
     23 function testDataURL() {
     24  return Promise.all(
     25    [
     26      [
     27        "data:text/plain;charset=UTF-8,Hello",
     28        "text/plain;charset=UTF-8",
     29        "Hello",
     30      ],
     31      [
     32        "data:text/plain;charset=utf-8;base64,SGVsbG8=",
     33        "text/plain;charset=utf-8",
     34        "Hello",
     35      ],
     36      [
     37        "data:text/xml,%3Cres%3Ehello%3C/res%3E%0A",
     38        "text/xml",
     39        "<res>hello</res>\n",
     40      ],
     41      ["data:text/plain,hello%20pass%0A", "text/plain", "hello pass\n"],
     42      ["data:,foo", "text/plain;charset=US-ASCII", "foo"],
     43      ["data:text/plain;base64,Zm9v", "text/plain", "foo"],
     44      ["data:text/plain,foo#bar", "text/plain", "foo"],
     45      ["data:text/plain,foo%23bar", "text/plain", "foo#bar"],
     46    ].map(test => {
     47      var uri = test[0],
     48        contentType = test[1],
     49        expectedBody = test[2];
     50      return fetch(uri).then(res => {
     51        ok(true, "Data URL fetch should resolve");
     52        if (res.type == "error") {
     53          ok(false, "Data URL fetch should not fail.");
     54          return Promise.reject();
     55        }
     56        ok(res instanceof Response, "Fetch should resolve to a Response");
     57        is(res.status, 200, "Data URL status should be 200");
     58        is(res.statusText, "OK", "Data URL statusText should be OK");
     59        ok(
     60          res.headers.has("content-type"),
     61          "Headers must have Content-Type header"
     62        );
     63        is(
     64          res.headers.get("content-type"),
     65          contentType,
     66          "Content-Type header should match specified value"
     67        );
     68        return res
     69          .text()
     70          .then(body => is(body, expectedBody, "Data URL Body should match"));
     71      });
     72    })
     73  );
     74 }
     75 
     76 function testSameOriginBlobURL() {
     77  var blob = new Blob(["english ", "sentence"], { type: "text/plain" });
     78  var url = URL.createObjectURL(blob);
     79  return fetch(url).then(function (res) {
     80    URL.revokeObjectURL(url);
     81    ok(true, "Blob URL fetch should resolve");
     82    if (res.type == "error") {
     83      ok(false, "Blob URL fetch should not fail.");
     84      return Promise.reject();
     85    }
     86    ok(res instanceof Response, "Fetch should resolve to a Response");
     87    is(res.status, 200, "Blob fetch status should be 200");
     88    is(res.statusText, "OK", "Blob fetch statusText should be OK");
     89    ok(
     90      res.headers.has("content-type"),
     91      "Headers must have Content-Type header"
     92    );
     93    is(
     94      res.headers.get("content-type"),
     95      blob.type,
     96      "Content-Type header should match specified value"
     97    );
     98    ok(
     99      res.headers.has("content-length"),
    100      "Headers must have Content-Length header"
    101    );
    102    is(
    103      parseInt(res.headers.get("content-length")),
    104      16,
    105      "Content-Length should match Blob's size"
    106    );
    107    return res.text().then(function (body) {
    108      is(body, "english sentence", "Blob fetch body should match");
    109    });
    110  });
    111 }
    112 
    113 function testNonGetBlobURL() {
    114  var blob = new Blob(["english ", "sentence"], { type: "text/plain" });
    115  var url = URL.createObjectURL(blob);
    116  return Promise.all(
    117    ["HEAD", "POST", "PUT", "DELETE"].map(method => {
    118      var req = new Request(url, { method });
    119      return fetch(req)
    120        .then(function (res) {
    121          ok(false, "Blob URL with non-GET request should not succeed");
    122        })
    123        .catch(function (e) {
    124          ok(
    125            e instanceof TypeError,
    126            "Blob URL with non-GET request should get a TypeError"
    127          );
    128        });
    129    })
    130  ).then(function () {
    131    URL.revokeObjectURL(url);
    132  });
    133 }
    134 
    135 function testMozErrors() {
    136  // mozErrors shouldn't be available to content and be ignored.
    137  return fetch("http://localhost:4/should/fail", { mozErrors: true })
    138    .then(res => {
    139      ok(false, "Request should not succeed");
    140    })
    141    .catch(err => {
    142      ok(err instanceof TypeError);
    143    });
    144 }
    145 
    146 function testRequestMozErrors() {
    147  // mozErrors shouldn't be available to content and be ignored.
    148  const r = new Request("http://localhost:4/should/fail", { mozErrors: true });
    149  return fetch(r)
    150    .then(res => {
    151      ok(false, "Request should not succeed");
    152    })
    153    .catch(err => {
    154      ok(err instanceof TypeError);
    155    });
    156 }
    157 
    158 function testViewSourceURL() {
    159  var p2 = fetch("view-source:/").then(
    160    function (res) {
    161      ok(false, "view-source: URL should fail");
    162    },
    163    function (e) {
    164      ok(e instanceof TypeError, "view-source: URL should fail");
    165    }
    166  );
    167 }
    168 
    169 function runTest() {
    170  return Promise.resolve()
    171    .then(testAboutURL)
    172    .then(testDataURL)
    173    .then(testSameOriginBlobURL)
    174    .then(testNonGetBlobURL)
    175    .then(testMozErrors)
    176    .then(testRequestMozErrors)
    177    .then(testViewSourceURL);
    178  // Put more promise based tests here.
    179 }