Bug 1495363 - Abort the previous request, if a form is submitted twice - tests. r=smaug, a=RyanVM
authorAndrea Marchesini <amarchesini@mozilla.com>
Wed, 16 Jan 2019 22:51:17 +0100
changeset 509533 f69157ba349ee9c70f1a05571541a8011872a126
parent 509532 1790d083bb5766db07a44f891bf786173d60da02
child 509534 01bd5daff7e0667df22541e13dc1c8b125124d66
push id1905
push userffxbld-merge
push dateMon, 21 Jan 2019 12:33:13 +0000
treeherdermozilla-release@c2fca1944d8c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug, RyanVM
bugs1495363
milestone65.0
Bug 1495363 - Abort the previous request, if a form is submitted twice - tests. r=smaug, a=RyanVM
dom/html/test/forms/file_bug1495363.html
dom/html/test/forms/file_bug1495363.sjs
dom/html/test/forms/mochitest.ini
dom/html/test/forms/test_bug1495363.html
new file mode 100644
--- /dev/null
+++ b/dom/html/test/forms/file_bug1495363.html
@@ -0,0 +1,48 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+</head>
+<body>
+
+<form id="form" method="POST" action="file_bug1495363.sjs" enctype="multipart/form-data">
+  <input type="text" name="post" id="post" />
+  <input type="file" name="file" id="file" />
+  <input id="btn" type="submit" />
+</form>
+
+<script type="application/javascript">
+
+let p = new Promise(resolve => {
+  let url = SimpleTest.getTestFileURL("../../../../dom/filesystem/tests/script_fileList.js");
+  let script = SpecialPowers.loadChromeScript(url);
+
+  function onOpened(message) {
+    SpecialPowers.wrap(document.getElementById("file")).mozSetFileArray([message.file]);
+    resolve();
+  }
+
+  script.addMessageListener("file.opened", onOpened);
+  script.sendAsyncMessage("file.open");
+});
+
+p.then(() => {
+  let form = document.getElementById("form");
+  form.onsubmit = function() {
+    setTimeout(() => {
+      document.getElementById("post").value = "TIMEOUT";
+      form.submit();
+      parent.timeoutExpired();
+    }, 0);
+    parent.formSubmitted();
+    return true;
+  }
+
+  document.getElementById("post").value = "CLICK";
+  document.getElementById("btn").click();
+});
+
+</script>
+</body>
+</html>
new file mode 100644
--- /dev/null
+++ b/dom/html/test/forms/file_bug1495363.sjs
@@ -0,0 +1,45 @@
+const CC = Components.Constructor;
+const BinaryInputStream = CC("@mozilla.org/binaryinputstream;1",
+                             "nsIBinaryInputStream",
+                             "setInputStream");
+
+function handleRequest(aRequest, aResponse) {
+  aResponse.setStatusLine(aRequest.httpVersion, 200);
+
+  // This returns number of requests received so far.
+  if (aRequest.queryString.includes("result")) {
+    let hints = getState("hints") || 0;
+    setState("hints", "0");
+
+    let submitter = getState("submitter");
+    setState("submitter", "");
+
+    aResponse.write(hints + "-" + submitter);
+    return;
+  }
+
+  // Here we count the number of requests and we store who was the last
+  // submitter.
+
+  let bodyStream = new BinaryInputStream(aRequest.bodyInputStream);
+  let requestBody = "";
+  while ((bodyAvail = bodyStream.available()) > 0) {
+    requestBody += bodyStream.readBytes(bodyAvail);
+  }
+
+  let lines = requestBody.split("\n");
+  let submitter = "";
+  for (let i = 0; i < lines.length; ++i) {
+    if (lines[i].trim() == 'Content-Disposition: form-data; name="post"') {
+      submitter = lines[i+2].trim();
+      break;
+    }
+  }
+
+  let hints = parseInt(getState("hints") || 0) + 1;
+  setState("hints", hints.toString());
+  setState("submitter", submitter);
+
+  aResponse.setHeader("Content-Type", "text/html", false);
+  aResponse.write("Hello World!");
+}
--- a/dom/html/test/forms/mochitest.ini
+++ b/dom/html/test/forms/mochitest.ini
@@ -108,8 +108,13 @@ skip-if = android_version == '18' || os 
 [test_stepup_stepdown.html]
 [test_textarea_attributes_reflection.html]
 [test_validation.html]
 [test_valueAsDate_pref.html]
 [test_valueasdate_attribute.html]
 [test_valueasnumber_attribute.html]
 [test_validation_not_in_doc.html]
 [test_reportValidation_preventDefault.html]
+[test_bug1495363.html]
+support-files =
+  file_bug1495363.html
+  file_bug1495363.sjs
+  !/dom/filesystem/tests/script_fileList.js
new file mode 100644
--- /dev/null
+++ b/dom/html/test/forms/test_bug1495363.html
@@ -0,0 +1,50 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <title>Test for Bug 1495363</title>
+  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+</head>
+<body>
+<script type="application/javascript">
+
+let readyToCheck = 0;
+window.timeoutExpired = () => {
+  ok(true, "Timeout expired");
+  readyToCheck++;
+  maybeCheckResults();
+}
+
+window.formSubmitted = () => {
+  ok(true, "Form submited!");
+  ifr.addEventListener("load", () => {
+    readyToCheck++;
+    maybeCheckResults();
+  }, {once: true});
+}
+
+SimpleTest.waitForExplicitFinish();
+
+var ifr = document.createElement('iframe');
+ifr.src = "file_bug1495363.html";
+document.body.appendChild(ifr);
+
+function maybeCheckResults() {
+  ok(readyToCheck <= 2, "So far so good");
+  if (readyToCheck < 2) {
+    return;
+  }
+
+  SimpleTest.executeSoon(() => {
+    fetch("file_bug1495363.sjs?result").then(r => r.text()).then(text => {
+      let parts = text.split("-");
+      is(parts[0], "1", "We have 1 request only");
+      is(parts[1], "TIMEOUT", "The request comes from the timer");
+      SimpleTest.finish();
+    });
+  });
+}
+
+</script>
+</body>
+</html>