tor-browser

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

browser_form_post_from_file_to_http.js (5555B)


      1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
      2 /* vim: set ft=javascript ts=2 et sw=2 tw=80: */
      3 
      4 const TEST_HTTP_POST =
      5  "http://example.org/browser/dom/html/test/form_submit_server.sjs";
      6 
      7 // Test for bug 1351358.
      8 async function runTest(doNewTab) {
      9  // Create file URI and test data file paths.
     10  let testFile = getChromeDir(getResolvedURI(gTestPath));
     11  testFile.append("dummy_page.html");
     12  const fileUriString = Services.io.newFileURI(testFile).spec;
     13  let filePaths = [];
     14  testFile.leafName = "form_data_file.txt";
     15  filePaths.push(testFile.path);
     16  testFile.leafName = "form_data_file.bin";
     17  filePaths.push(testFile.path);
     18 
     19  // Open file:// page tab in which to run the test.
     20  await BrowserTestUtils.withNewTab(
     21    fileUriString,
     22    async function (fileBrowser) {
     23      // Create a form to post to server that writes posted data into body as JSON.
     24 
     25      var promiseLoad;
     26      if (doNewTab) {
     27        promiseLoad = BrowserTestUtils.waitForNewTab(
     28          gBrowser,
     29          TEST_HTTP_POST,
     30          true,
     31          false
     32        );
     33      } else {
     34        promiseLoad = BrowserTestUtils.browserLoaded(
     35          fileBrowser,
     36          false,
     37          TEST_HTTP_POST
     38        );
     39      }
     40 
     41      /* eslint-disable no-shadow */
     42      await SpecialPowers.spawn(
     43        fileBrowser,
     44        [TEST_HTTP_POST, filePaths, doNewTab],
     45        (actionUri, filePaths, doNewTab) => {
     46          // eslint-disable-next-line mozilla/reject-importGlobalProperties
     47          Cu.importGlobalProperties(["File"]);
     48 
     49          let doc = content.document;
     50          let form = doc.body.appendChild(doc.createElement("form"));
     51          form.action = actionUri;
     52          form.method = "POST";
     53          form.enctype = "multipart/form-data";
     54          if (doNewTab) {
     55            form.target = "_blank";
     56          }
     57 
     58          let inputText = form.appendChild(doc.createElement("input"));
     59          inputText.type = "text";
     60          inputText.name = "text";
     61          inputText.value = "posted";
     62 
     63          let inputCheckboxOn = form.appendChild(doc.createElement("input"));
     64          inputCheckboxOn.type = "checkbox";
     65          inputCheckboxOn.name = "checked";
     66          inputCheckboxOn.checked = true;
     67 
     68          let inputCheckboxOff = form.appendChild(doc.createElement("input"));
     69          inputCheckboxOff.type = "checkbox";
     70          inputCheckboxOff.name = "unchecked";
     71          inputCheckboxOff.checked = false;
     72 
     73          let inputFile = form.appendChild(doc.createElement("input"));
     74          inputFile.type = "file";
     75          inputFile.name = "file";
     76          let fileList = [];
     77          let promises = [];
     78          for (let path of filePaths) {
     79            promises.push(
     80              File.createFromFileName(path).then(file => {
     81                fileList.push(file);
     82              })
     83            );
     84          }
     85 
     86          Promise.all(promises).then(() => {
     87            inputFile.mozSetFileArray(fileList);
     88            form.submit();
     89          });
     90        }
     91      );
     92      /* eslint-enable no-shadow */
     93 
     94      var href;
     95      var testBrowser;
     96      var newTab;
     97      if (doNewTab) {
     98        newTab = await promiseLoad;
     99        testBrowser = newTab.linkedBrowser;
    100        href = testBrowser.currentURI.spec;
    101      } else {
    102        testBrowser = fileBrowser;
    103        href = await promiseLoad;
    104      }
    105      is(
    106        href,
    107        TEST_HTTP_POST,
    108        "Check that the loaded page is the one to which we posted."
    109      );
    110 
    111      let binContentType;
    112      if (AppConstants.platform == "macosx") {
    113        binContentType = "application/macbinary";
    114      } else {
    115        binContentType = "application/octet-stream";
    116      }
    117 
    118      /* eslint-disable no-shadow */
    119      await SpecialPowers.spawn(
    120        testBrowser,
    121        [binContentType],
    122        binContentType => {
    123          let data = JSON.parse(content.document.body.textContent);
    124          is(
    125            data[0].headers["Content-Disposition"],
    126            'form-data; name="text"',
    127            "Check text input Content-Disposition"
    128          );
    129          is(data[0].body, "posted", "Check text input body");
    130 
    131          is(
    132            data[1].headers["Content-Disposition"],
    133            'form-data; name="checked"',
    134            "Check checkbox input Content-Disposition"
    135          );
    136          is(data[1].body, "on", "Check checkbox input body");
    137 
    138          // Note that unchecked checkbox details are not sent.
    139 
    140          is(
    141            data[2].headers["Content-Disposition"],
    142            'form-data; name="file"; filename="form_data_file.txt"',
    143            "Check text file input Content-Disposition"
    144          );
    145          is(
    146            data[2].headers["Content-Type"],
    147            "text/plain",
    148            "Check text file input Content-Type"
    149          );
    150          is(data[2].body, "1234\n", "Check text file input body");
    151 
    152          is(
    153            data[3].headers["Content-Disposition"],
    154            'form-data; name="file"; filename="form_data_file.bin"',
    155            "Check binary file input Content-Disposition"
    156          );
    157          is(
    158            data[3].headers["Content-Type"],
    159            binContentType,
    160            "Check binary file input Content-Type"
    161          );
    162          is(
    163            data[3].body,
    164            "\u0001\u0002\u0003\u0004\n",
    165            "Check binary file input body"
    166          );
    167        }
    168      );
    169      /* eslint-enable no-shadow */
    170 
    171      if (newTab) {
    172        BrowserTestUtils.removeTab(newTab);
    173      }
    174    }
    175  );
    176 }
    177 
    178 add_task(async function runWithDocumentChannel() {
    179  await runTest(false);
    180  await runTest(true);
    181 });