Bug 1080987 - navigator.sendBeacon() needs to sent origin header - test. r=sicking, a=sledru
authorChristoph Kerschbaumer <mozilla@christophkerschbaumer.com>
Wed, 19 Nov 2014 16:03:30 -0800
changeset 242580 425e86d0d740ad31785a4da79083095d643cca14
parent 242579 ac5d2e7a585530a3d4d76029f62d49df719a0708
child 242581 6cfb21ae8405568b1c824b4f97c6e2fee4042555
push id4311
push userraliiev@mozilla.com
push dateMon, 12 Jan 2015 19:37:41 +0000
treeherdermozilla-beta@150c9fed433b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssicking, sledru
bugs1080987
milestone36.0a2
Bug 1080987 - navigator.sendBeacon() needs to sent origin header - test. r=sicking, a=sledru
dom/tests/mochitest/beacon/beacon-originheader-handler.sjs
dom/tests/mochitest/beacon/mochitest.ini
dom/tests/mochitest/beacon/test_beaconOriginHeader.html
new file mode 100644
--- /dev/null
+++ b/dom/tests/mochitest/beacon/beacon-originheader-handler.sjs
@@ -0,0 +1,41 @@
+/*
+ * TestSever customized specifically for the needs of:
+ * Bug 1080987 - navigator.sendBeacon() needs to sent origin header
+ */
+
+function handleRequest(request, response)
+{
+  response.setHeader("Cache-Control", "no-cache", false);
+  response.setHeader("Content-Type", "text/plain", false);
+
+  // case XHR-REQUEST: the xhr-request tries to query the
+  // stored header from the beacon request.
+  if (request.queryString == "queryheader") {
+    var header = getState("originHeader");
+    // if the beacon already stored the header - return.
+    if (header) {
+      response.write(header);
+      setState("originHeader", "");
+      return;
+    }
+    // otherwise wait for the beacon request
+    response.processAsync();
+    setObjectState("xhr-response", response);
+    return;
+  }
+
+  // case BEACON-REQUEST: get the beacon header and
+  // store the header on the server.
+  var header = request.getHeader("origin");
+  setState("originHeader", header);
+
+  // if there is an xhr-request waiting, return the header now.
+  getObjectState("xhr-response", function(xhrResponse) {
+    if (!xhrResponse) {
+      return;
+    }
+    setState("originHeader", "");
+    xhrResponse.write(header);
+    xhrResponse.finish();
+  });
+}
--- a/dom/tests/mochitest/beacon/mochitest.ini
+++ b/dom/tests/mochitest/beacon/mochitest.ini
@@ -1,9 +1,11 @@
 [DEFAULT]
 skip-if = buildapp == 'b2g' || e10s
 support-files = beacon-frame.html
                 beacon-handler.sjs
+                beacon-originheader-handler.sjs
 
 [test_beacon.html]
 [test_beaconFrame.html]
 [test_beaconPreflight.html]
 [test_beaconContentPolicy.html]
+[test_beaconOriginHeader.html]
new file mode 100644
--- /dev/null
+++ b/dom/tests/mochitest/beacon/test_beaconOriginHeader.html
@@ -0,0 +1,64 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <title>Bug 1080987 - navigator.sendBeacon() needs to sent origin header</title>
+  <!-- Including SimpleTest.js so we can use waitForExplicitFinish !-->
+  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+  <p id="display"></p>
+  <div id="content" style="visibility: hidden">
+    <iframe style="width:100%;" id="testframe"></iframe>
+  </div>
+
+<script class="testbody" type="text/javascript">
+
+SimpleTest.waitForExplicitFinish();
+
+const BEACON_URL = "http://example.com/tests/dom/tests/mochitest/beacon/beacon-originheader-handler.sjs";
+const ORIGIN_HEADER = "http://mochi.test:8888";
+
+/* Description of the test:
+ *   We call sendBeacon() cross origin and make sure that the
+ *   origin header is actually set in the request.
+ *
+ * Since sendBeacon() does not expect any response, we are storing the
+ * header on the server (*.sjs) and use an XMLHttpRequest to actually
+ * retrieve the header back from the server. We assert that the header
+ * is indeed correct. Since sendBeacon() and also the XMLHttpRequest()
+ * are performed in an asynchronous fashion, there is no guarantee that
+ * the sendBeacon() is actually executed before the XMLHttpRequest().
+ * Hence the xhr-response might be processed asynchronously.
+ */
+
+SpecialPowers.pushPrefEnv({'set': [["beacon.enabled", true]]}, runTest);
+
+function queryHeaderFromServer() {
+  var xhr = new XMLHttpRequest();
+  xhr.open("GET", "beacon-originheader-handler.sjs?queryheader", true);
+  xhr.onload = function() {
+    is(xhr.responseText, ORIGIN_HEADER, "SendBeacon sends right origin header");
+    SimpleTest.finish();
+  };
+  xhr.onerror = function() {
+    ok(false, "xhr request returned error");
+    SimpleTest.finish();
+  };
+  xhr.send();
+}
+
+function runTest() {
+  // generate data and send beacon
+  var formData = new FormData();
+  formData.append('name', 'value');
+  navigator.sendBeacon(BEACON_URL, formData);
+
+  // start quering the result from the server
+  queryHeaderFromServer();
+}
+
+</script>
+</pre>
+</body>
+</html>