summaryrefslogtreecommitdiffstats
path: root/dom/html/test/browser_form_post_from_file_to_http.js
blob: e62912bdcd14c464a599be1ad9fbc25f000cd96d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */

const TEST_HTTP_POST =
  "http://example.org/browser/dom/html/test/form_submit_server.sjs";

// Test for bug 1351358.
async function runTest(doNewTab) {
  // Create file URI and test data file paths.
  let testFile = getChromeDir(getResolvedURI(gTestPath));
  testFile.append("dummy_page.html");
  const fileUriString = Services.io.newFileURI(testFile).spec;
  let filePaths = [];
  testFile.leafName = "form_data_file.txt";
  filePaths.push(testFile.path);
  testFile.leafName = "form_data_file.bin";
  filePaths.push(testFile.path);

  // Open file:// page tab in which to run the test.
  await BrowserTestUtils.withNewTab(
    fileUriString,
    async function (fileBrowser) {
      // Create a form to post to server that writes posted data into body as JSON.

      var promiseLoad;
      if (doNewTab) {
        promiseLoad = BrowserTestUtils.waitForNewTab(
          gBrowser,
          TEST_HTTP_POST,
          true,
          false
        );
      } else {
        promiseLoad = BrowserTestUtils.browserLoaded(
          fileBrowser,
          false,
          TEST_HTTP_POST
        );
      }

      /* eslint-disable no-shadow */
      await SpecialPowers.spawn(
        fileBrowser,
        [TEST_HTTP_POST, filePaths, doNewTab],
        (actionUri, filePaths, doNewTab) => {
          // eslint-disable-next-line mozilla/reject-importGlobalProperties
          Cu.importGlobalProperties(["File"]);

          let doc = content.document;
          let form = doc.body.appendChild(doc.createElement("form"));
          form.action = actionUri;
          form.method = "POST";
          form.enctype = "multipart/form-data";
          if (doNewTab) {
            form.target = "_blank";
          }

          let inputText = form.appendChild(doc.createElement("input"));
          inputText.type = "text";
          inputText.name = "text";
          inputText.value = "posted";

          let inputCheckboxOn = form.appendChild(doc.createElement("input"));
          inputCheckboxOn.type = "checkbox";
          inputCheckboxOn.name = "checked";
          inputCheckboxOn.checked = true;

          let inputCheckboxOff = form.appendChild(doc.createElement("input"));
          inputCheckboxOff.type = "checkbox";
          inputCheckboxOff.name = "unchecked";
          inputCheckboxOff.checked = false;

          let inputFile = form.appendChild(doc.createElement("input"));
          inputFile.type = "file";
          inputFile.name = "file";
          let fileList = [];
          let promises = [];
          for (let path of filePaths) {
            promises.push(
              File.createFromFileName(path).then(file => {
                fileList.push(file);
              })
            );
          }

          Promise.all(promises).then(() => {
            inputFile.mozSetFileArray(fileList);
            form.submit();
          });
        }
      );
      /* eslint-enable no-shadow */

      var href;
      var testBrowser;
      var newTab;
      if (doNewTab) {
        newTab = await promiseLoad;
        testBrowser = newTab.linkedBrowser;
        href = testBrowser.currentURI.spec;
      } else {
        testBrowser = fileBrowser;
        href = await promiseLoad;
      }
      is(
        href,
        TEST_HTTP_POST,
        "Check that the loaded page is the one to which we posted."
      );

      let binContentType;
      if (AppConstants.platform == "macosx") {
        binContentType = "application/macbinary";
      } else {
        binContentType = "application/octet-stream";
      }

      /* eslint-disable no-shadow */
      await SpecialPowers.spawn(
        testBrowser,
        [binContentType],
        binContentType => {
          let data = JSON.parse(content.document.body.textContent);
          is(
            data[0].headers["Content-Disposition"],
            'form-data; name="text"',
            "Check text input Content-Disposition"
          );
          is(data[0].body, "posted", "Check text input body");

          is(
            data[1].headers["Content-Disposition"],
            'form-data; name="checked"',
            "Check checkbox input Content-Disposition"
          );
          is(data[1].body, "on", "Check checkbox input body");

          // Note that unchecked checkbox details are not sent.

          is(
            data[2].headers["Content-Disposition"],
            'form-data; name="file"; filename="form_data_file.txt"',
            "Check text file input Content-Disposition"
          );
          is(
            data[2].headers["Content-Type"],
            "text/plain",
            "Check text file input Content-Type"
          );
          is(data[2].body, "1234\n", "Check text file input body");

          is(
            data[3].headers["Content-Disposition"],
            'form-data; name="file"; filename="form_data_file.bin"',
            "Check binary file input Content-Disposition"
          );
          is(
            data[3].headers["Content-Type"],
            binContentType,
            "Check binary file input Content-Type"
          );
          is(
            data[3].body,
            "\u0001\u0002\u0003\u0004\n",
            "Check binary file input body"
          );
        }
      );
      /* eslint-enable no-shadow */

      if (newTab) {
        BrowserTestUtils.removeTab(newTab);
      }
    }
  );
}

add_task(async function runWithDocumentChannel() {
  await runTest(false);
  await runTest(true);
});