tor-browser

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

test_fetch_basic_http.js (7630B)


      1 var path = "/tests/dom/xhr/tests/";
      2 
      3 var is_http2 = location.href.includes("http2");
      4 
      5 var passFiles = [
      6  ["file_XHR_pass1.xml", "GET", 200, "OK", "text/xml"],
      7  ["file_XHR_pass2.txt", "GET", 200, "OK", "text/plain"],
      8  ["file_XHR_pass3.txt", "GET", 200, "OK", "text/plain"],
      9 ];
     10 
     11 function testURL() {
     12  var promises = [];
     13  passFiles.forEach(function (entry) {
     14    var p = fetch(path + entry[0]).then(function (res) {
     15      ok(
     16        res.type !== "error",
     17        "Response should not be an error for " + entry[0]
     18      );
     19      is(res.status, entry[2], "Status should match expected for " + entry[0]);
     20      is(
     21        res.statusText,
     22        is_http2 ? "" : entry[3],
     23        "Status text should match expected for " +
     24          entry[0] +
     25          " " +
     26          is_http2 +
     27          " " +
     28          location.href
     29      );
     30      if (entry[0] != "file_XHR_pass3.txt") {
     31        ok(
     32          res.url.endsWith(path + entry[0]),
     33          "Response url should match request for simple fetch for " + entry[0]
     34        );
     35      } else {
     36        ok(
     37          res.url.endsWith(path + "file_XHR_pass2.txt"),
     38          "Response url should match request for simple fetch for " + entry[0]
     39        );
     40      }
     41      is(
     42        res.headers.get("content-type"),
     43        entry[4],
     44        "Response should have content-type for " + entry[0]
     45      );
     46    });
     47    promises.push(p);
     48  });
     49 
     50  return Promise.all(promises);
     51 }
     52 
     53 var failFiles = [["ftp://localhost" + path + "file_XHR_pass1.xml", "GET"]];
     54 
     55 function testURLFail() {
     56  var promises = [];
     57  failFiles.forEach(function (entry) {
     58    var p = fetch(entry[0]).then(
     59      function (res) {
     60        ok(false, "Response should be an error for " + entry[0]);
     61      },
     62      function (e) {
     63        ok(
     64          e instanceof TypeError,
     65          "Response should be an error for " + entry[0]
     66        );
     67      }
     68    );
     69    promises.push(p);
     70  });
     71 
     72  return Promise.all(promises);
     73 }
     74 
     75 function testRequestGET() {
     76  var promises = [];
     77  passFiles.forEach(function (entry) {
     78    var req = new Request(path + entry[0], { method: entry[1] });
     79    var p = fetch(req).then(function (res) {
     80      ok(
     81        res.type !== "error",
     82        "Response should not be an error for " + entry[0]
     83      );
     84      is(res.status, entry[2], "Status should match expected for " + entry[0]);
     85      is(
     86        res.statusText,
     87        is_http2 ? "" : entry[3],
     88        "Status text should match expected for " +
     89          entry[0] +
     90          " " +
     91          is_http2 +
     92          " " +
     93          location.href
     94      );
     95      if (entry[0] != "file_XHR_pass3.txt") {
     96        ok(
     97          res.url.endsWith(path + entry[0]),
     98          "Response url should match request for simple fetch for " + entry[0]
     99        );
    100      } else {
    101        ok(
    102          res.url.endsWith(path + "file_XHR_pass2.txt"),
    103          "Response url should match request for simple fetch for " + entry[0]
    104        );
    105      }
    106      is(
    107        res.headers.get("content-type"),
    108        entry[4],
    109        "Response should have content-type for " + entry[0]
    110      );
    111    });
    112    promises.push(p);
    113  });
    114 
    115  return Promise.all(promises);
    116 }
    117 
    118 function arraybuffer_equals_to(ab, s) {
    119  is(ab.byteLength, s.length, "arraybuffer byteLength should match");
    120 
    121  var u8v = new Uint8Array(ab);
    122  is(
    123    String.fromCharCode.apply(String, u8v),
    124    s,
    125    "arraybuffer bytes should match"
    126  );
    127 }
    128 
    129 function testResponses() {
    130  var fetches = [
    131    fetch(path + "file_XHR_pass2.txt").then(res => {
    132      is(res.status, 200, "status should match");
    133      return res
    134        .text()
    135        .then(v => is(v, "hello pass\n", "response should match"));
    136    }),
    137 
    138    fetch(path + "file_XHR_binary1.bin").then(res => {
    139      is(res.status, 200, "status should match");
    140      return res
    141        .arrayBuffer()
    142        .then(v =>
    143          arraybuffer_equals_to(
    144            v,
    145            "\xaa\xee\0\x03\xff\xff\xff\xff\xbb\xbb\xbb\xbb"
    146          )
    147        );
    148    }),
    149 
    150    new Promise((resolve, reject) => {
    151      var jsonBody = JSON.stringify({ title: "aBook", author: "john" });
    152      var req = new Request(path + "responseIdentical.sjs", {
    153        method: "POST",
    154        body: jsonBody,
    155      });
    156      var p = fetch(req).then(res => {
    157        is(res.status, 200, "status should match");
    158        return res.json().then(v => {
    159          is(JSON.stringify(v), jsonBody, "json response should match");
    160        });
    161      });
    162      resolve(p);
    163    }),
    164 
    165    new Promise((resolve, reject) => {
    166      var req = new Request(path + "responseIdentical.sjs", {
    167        method: "POST",
    168        body: "{",
    169      });
    170      var p = fetch(req).then(res => {
    171        is(res.status, 200, "wrong status");
    172        return res.json().then(
    173          v => ok(false, "expected json parse failure"),
    174          e => ok(true, "expected json parse failure")
    175        );
    176      });
    177      resolve(p);
    178    }),
    179  ];
    180 
    181  return Promise.all(fetches);
    182 }
    183 
    184 function testBlob() {
    185  return fetch(path + "/file_XHR_binary2.bin").then(r => {
    186    is(r.status, 200, "status should match");
    187    return r.blob().then(b => {
    188      is(b.size, 65536, "blob should have size 65536");
    189      return readAsArrayBuffer(b).then(function (ab) {
    190        var u8 = new Uint8Array(ab);
    191        for (var i = 0; i < 65536; i++) {
    192          if (u8[i] !== (i & 255)) {
    193            break;
    194          }
    195        }
    196        is(i, 65536, "wrong value at offset " + i);
    197      });
    198    });
    199  });
    200 }
    201 
    202 // This test is a copy of dom/html/test/formData_test.js testSend() modified to
    203 // use the fetch API. Please change this if you change that.
    204 function testFormDataSend() {
    205  var file,
    206    blob = new Blob(["hey"], { type: "text/plain" });
    207 
    208  var fd = new FormData();
    209  fd.append("string", "hey");
    210  fd.append("empty", blob);
    211  fd.append("explicit", blob, "explicit-file-name");
    212  fd.append("explicit-empty", blob, "");
    213  file = new File([blob], "testname", { type: "text/plain" });
    214  fd.append("file-name", file);
    215  file = new File([blob], "", { type: "text/plain" });
    216  fd.append("empty-file-name", file);
    217  file = new File([blob], "testname", { type: "text/plain" });
    218  fd.append("file-name-overwrite", file, "overwrite");
    219 
    220  var req = new Request("/tests/dom/html/test/form_submit_server.sjs", {
    221    method: "POST",
    222    body: fd,
    223  });
    224 
    225  return fetch(req).then(r => {
    226    is(r.status, 200, "status should match");
    227    return r.json().then(response => {
    228      for (var entry of response) {
    229        if (
    230          entry.headers["Content-Disposition"] != 'form-data; name="string"'
    231        ) {
    232          is(entry.headers["Content-Type"], "text/plain");
    233        }
    234 
    235        is(entry.body, "hey");
    236      }
    237 
    238      is(
    239        response[1].headers["Content-Disposition"],
    240        'form-data; name="empty"; filename="blob"'
    241      );
    242 
    243      is(
    244        response[2].headers["Content-Disposition"],
    245        'form-data; name="explicit"; filename="explicit-file-name"'
    246      );
    247 
    248      is(
    249        response[3].headers["Content-Disposition"],
    250        'form-data; name="explicit-empty"; filename=""'
    251      );
    252 
    253      is(
    254        response[4].headers["Content-Disposition"],
    255        'form-data; name="file-name"; filename="testname"'
    256      );
    257 
    258      is(
    259        response[5].headers["Content-Disposition"],
    260        'form-data; name="empty-file-name"; filename=""'
    261      );
    262 
    263      is(
    264        response[6].headers["Content-Disposition"],
    265        'form-data; name="file-name-overwrite"; filename="overwrite"'
    266      );
    267    });
    268  });
    269 }
    270 
    271 function runTest() {
    272  return Promise.resolve()
    273    .then(testURL)
    274    .then(testURLFail)
    275    .then(testRequestGET)
    276    .then(testResponses)
    277    .then(testBlob)
    278    .then(testFormDataSend);
    279  // Put more promise based tests here.
    280 }