Bug 1495363 - Abort the previous request, if a form is submitted twice - tests, r=smaug
authorAndrea Marchesini <amarchesini@mozilla.com>
Wed, 16 Jan 2019 22:51:17 +0100
changeset 514202 1d6b361c337ad7b5b4fa016030f2f2aa84a76f83
parent 514201 7973453599bb4562c4a13b9a91027ac9724640ff
child 514203 6f2303192174baaf9cab167db5f97411bad943bf
push id1953
push userffxbld-merge
push dateMon, 11 Mar 2019 12:10:20 +0000
treeherdermozilla-release@9c35dcbaa899 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1495363
milestone66.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1495363 - Abort the previous request, if a form is submitted twice - tests, r=smaug
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
@@ -111,8 +111,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>