summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/mochitest/test_ext_webrequest_upload.html
blob: 998ab980037828ae1c1af1ebeaff12fdfcaedb82 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<!DOCTYPE HTML>

<html>
<head>
<meta charset="utf-8">
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
  <script type="text/javascript" src="head.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>

<form method="post"
  action="file_WebRequest_page3.html?trigger=form"
  target="_blank"
  enctype="multipart/form-data"
  >
<input type="text" name="&quot;special&quot; chˆrs" value="spÛcial">
<input type="file" name="testFile">
<input type="file" name="emptyFile">
<input type="text" name="textInput1" value="value1">
</form>

<form method="post"
  action="file_WebRequest_page3.html?trigger=form"
  target="_blank"
  enctype="multipart/form-data"
  >
<input type="text" name="textInput2" value="value2">
<input type="file" name="testFile">
<input type="file" name="emptyFile">
</form>

</form>
<form method="post"
  action="file_WebRequest_page3.html?trigger=form"
  target="_blank"
  >
<input type="text" name="textInput" value="value1">
<input type="text" name="textInput" value="value2">
</form>
<script>
"use strict";

let files, testFile, blob, file, uploads;
add_task(function* test_setup() {
  files = yield new Promise(resolve => {
    SpecialPowers.createFiles([{name: "testFile.pdf", data: "Not really a PDF file :)", "type": "application/x-pdf"}], (result) => {
      resolve(result);
    });
  });
  testFile = files[0];
  blob = {
    name: "blobAsFile",
    content: new Blob(["A blob sent as a file"], {type: "text/csv"}),
    fileName: "blobAsFile.csv",
  };
  file = {
    name: "testFile",
    fileName: testFile.name,
  };
  uploads = {
    [blob.name]: blob,
    [file.name]: file,
  };
});

function background() {
  const FILTERS = {urls: ["<all_urls>"]};

  let requestBodySupported = true;

  function onUpload(details) {
    let url = new URL(details.url);
    let upload = url.searchParams.get("upload");
    if (!upload || !requestBodySupported) {
      return;
    }
    let requestBody = details.requestBody;
    browser.test.log(`onBeforeRequest upload: ${details.url} ${JSON.stringify(details.requestBody)}`);
    browser.test.assertTrue(!!requestBody, `Intercepted upload ${details.url} #${details.requestId} ${upload} have a requestBody`);
    if (!requestBody) {
      return;
    }
    let byteLength = parseInt(upload, 10);
    if (byteLength) {
      browser.test.assertTrue(!!requestBody.raw, `Binary upload ${details.url} #${details.requestId} ${upload} have a raw attribute`);
      browser.test.assertEq(byteLength, requestBody.raw && requestBody.raw.map(r => r.bytes && r.bytes.byteLength || 0).reduce((a, b) => a + b), `Binary upload size matches`);
      return;
    }
    if ("raw" in requestBody) {
      browser.test.assertEq(upload, JSON.stringify(requestBody.raw).replace(/(\bfile: ")[^"]+/, "$1<file>"), `Upload ${details.url} #${details.requestId} matches raw data`);
    } else {
      browser.test.assertEq(upload, JSON.stringify(requestBody.formData), `Upload ${details.url} #${details.requestId} matches form data.`);
    }
  }

  browser.webRequest.onCompleted.addListener(
    details => {
      browser.test.log(`onCompleted ${details.requestId} ${details.url}`);

      browser.test.sendMessage("done");
    },
    FILTERS);

  let onBeforeRequest = details => {
    browser.test.log(`${name} ${details.requestId} ${details.url}`);

    onUpload(details);
  };

  try {
    browser.webRequest.onBeforeRequest.addListener(
      onBeforeRequest, FILTERS, ["requestBody"]);
  } catch (e) {
    browser.test.assertTrue(/\brequestBody\b/.test(e.message),
                            "Request body is unsupported");

    // requestBody is disabled in release builds
    if (!/\brequestBody\b/.test(e.message)) {
      throw e;
    }

    browser.webRequest.onBeforeRequest.addListener(
      onBeforeRequest, FILTERS);
  }
}

add_task(function* test_xhr_forms() {
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: [
        "webRequest",
        "webRequestBlocking",
        "<all_urls>",
      ],
    },
    background,
  });

  yield extension.startup();

  for (let form of document.forms) {
    if (file.name in form.elements) {
      SpecialPowers.wrap(form.elements[file.name]).mozSetFileArray(files);
    }
    let action = new URL(form.action);
    let formData = new FormData(form);
    let webRequestFD = {};

    let updateActionURL = () => {
      for (let name of formData.keys()) {
        webRequestFD[name] = name in uploads ? [uploads[name].fileName] : formData.getAll(name);
      }
      action.searchParams.set("upload", JSON.stringify(webRequestFD));
      action.searchParams.set("enctype", form.enctype);
    };

    updateActionURL();

    form.action = action;
    form.submit();
    yield extension.awaitMessage("done");

    if (form.enctype !== "multipart/form-data") {
      continue;
    }

    let post = (data) => {
      let xhr = new XMLHttpRequest();
      action.searchParams.set("xhr", "1");
      xhr.open("POST", action.href);
      xhr.send(data);
      action.searchParams.delete("xhr");
      return extension.awaitMessage("done");
    };

    formData.append(blob.name, blob.content, blob.fileName);
    formData.append("formDataField", "some value");
    updateActionURL();
    yield post(formData);

    action.searchParams.set("upload", JSON.stringify([{file: "<file>"}]));
    yield post(testFile);

    action.searchParams.set("upload", `${blob.content.size} bytes`);
    yield post(blob.content);

    let byteLength = 16;
    action.searchParams.set("upload", `${byteLength} bytes`);
    yield post(new ArrayBuffer(byteLength));
  }

  yield extension.unload();
});
</script>
</body>
</html>