Bug 1250266 - Always send a TTL in the Push mochitests. r=benbangert, a=test-only
authorKit Cambridge <kcambridge@mozilla.com>
Mon, 22 Feb 2016 11:54:54 -0800
changeset 317536 5ee04656852769fb8880f8ac2e7e59cfc6eaf862
parent 317535 21b3c24170a7ae554d536ad51bc0b70bce115d2b
child 317537 9f53aaa98134c63c13348465676541ecf20db19b
push id5780
push userryanvm@gmail.com
push dateSat, 19 Mar 2016 19:45:02 +0000
treeherdermozilla-beta@83043e8064e2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbenbangert, test-only
bugs1250266
milestone46.0
Bug 1250266 - Always send a TTL in the Push mochitests. r=benbangert, a=test-only MozReview-Commit-ID: 4hoBveTY2pE
dom/push/test/push-server.sjs
dom/push/test/test_data.html
dom/push/test/test_register.html
dom/push/test/test_serviceworker_lifetime.html
dom/push/test/webpush.js
--- a/dom/push/test/push-server.sjs
+++ b/dom/push/test/push-server.sjs
@@ -34,19 +34,26 @@ function handleRequest(request, response
   let bodyStream = Cc["@mozilla.org/binaryinputstream;1"].createInstance(Ci.nsIBinaryInputStream);
   bodyStream.setInputStream(request.bodyInputStream);
   let size = 0;
   let data = [];
   for (let available; available = bodyStream.available();) {
     data.push(bodyStream.readByteArray(available));
     size += available;
   }
-  xhr.send(concatUint8Arrays(data, size));
+
+  function reply(statusCode, statusText) {
+    response.setStatusLine(request.httpVersion, statusCode, statusText);
+    response.finish();
+  }
 
   xhr.onload = function(e) {
     debug("xhr : " + this.status);
-  }
+    reply(this.status, this.statusText);
+  };
   xhr.onerror = function(e) {
     debug("xhr error: " + e);
-  }
+    reply(500, "Internal Server Error");
+  };
 
-  response.setStatusLine(request.httpVersion, "200", "OK");
+  response.processAsync();
+  xhr.send(concatUint8Arrays(data, size));
 }
--- a/dom/push/test/test_data.html
+++ b/dom/push/test/test_data.html
@@ -102,17 +102,17 @@ http://creativecommons.org/licenses/publ
       new Uint8Array(data.auth),
       "Mismatched auth secret"
     );
   });
 
   function waitForMessage(pushSubscription, message) {
     return Promise.all([
       controlledFrame.waitOnWorkerMessage("finished"),
-      webpush(pushSubscription, message),
+      webpush(pushSubscription, message, 120),
     ]).then(([message]) => message);
   }
 
   add_task(function* sendPushMessageFromPage() {
     var typedArray = new Uint8Array([226, 130, 40, 240, 40, 140, 188]);
     var json = { hello: "world" };
 
     var message = yield waitForMessage(pushSubscription, "Text message from page");
@@ -162,16 +162,17 @@ http://creativecommons.org/licenses/publ
     // Send a blank message.
     var [message] = yield Promise.all([
       controlledFrame.waitOnWorkerMessage("finished"),
       fetch("http://mochi.test:8888/tests/dom/push/test/push-server.sjs", {
         method: "PUT",
         headers: {
           "X-Push-Method": "POST",
           "X-Push-Server": pushSubscription.endpoint,
+          "TTL": "120",
         },
       }),
     ]);
     ok(!message.data, "Should exclude data for blank messages");
   });
 
   add_task(function* unsubscribe() {
     controlledFrame.remove();
--- a/dom/push/test/test_register.html
+++ b/dom/push/test/test_register.html
@@ -66,16 +66,17 @@ http://creativecommons.org/licenses/publ
   add_task(function* waitForPushNotification() {
     yield Promise.all([
       controlledFrame.waitOnWorkerMessage("finished"),
       fetch("http://mochi.test:8888/tests/dom/push/test/push-server.sjs", {
         method: "PUT",
         headers: {
           "X-Push-Method": "POST",
           "X-Push-Server": pushSubscription.endpoint,
+          "TTL": "120",
         },
       }),
     ]);
   });
 
   add_task(function* unsubscribe() {
     controlledFrame.remove();
     yield pushSubscription.unsubscribe();
--- a/dom/push/test/test_serviceworker_lifetime.html
+++ b/dom/push/test/test_serviceworker_lifetime.html
@@ -70,19 +70,20 @@
     });
     return p;
   }
 
   function sendPushToPushServer(pushEndpoint) {
     // Work around CORS for now.
     var xhr = new XMLHttpRequest();
     xhr.open('GET', "http://mochi.test:8888/tests/dom/push/test/push-server.sjs", true);
-    xhr.setRequestHeader("X-Push-Method", "PUT");
+    xhr.setRequestHeader("X-Push-Method", "POST");
     xhr.setRequestHeader("X-Push-Server", pushEndpoint);
-    xhr.send("version=24601");
+    xhr.setRequestHeader("TTL", "120");
+    xhr.send(null);
   }
 
   function unregisterPushNotification(ctx) {
     return ctx.subscription.unsubscribe().then(function(result) {
       ok(result, "unsubscribe should succeed.");
       ctx.subscription = null;
       return ctx;
     });
--- a/dom/push/test/webpush.js
+++ b/dom/push/test/webpush.js
@@ -164,17 +164,17 @@
   /*
    * Request push for a message.  This returns a promise that resolves when the
    * push has been delivered to the push service.
    *
    * @param subscription A PushSubscription that contains endpoint and p256dh
    *                     parameters.
    * @param data         The message to send.
    */
-  function webpush(subscription, data) {
+  function webpush(subscription, data, ttl) {
     data = ensureView(data);
 
     var salt = g.crypto.getRandomValues(new Uint8Array(16));
     return webCrypto.generateKey(P256DH, false, ['deriveBits'])
       .then(localKey => {
         return Promise.all([
           encrypt(localKey.privateKey, subscription.getKey("p256dh"), salt, data),
           // 1337 p-256 specific haxx to get the raw value out of the spki value
@@ -184,23 +184,24 @@
         var options = {
           method: 'PUT',
           headers: {
             'X-Push-Server': subscription.endpoint,
             // Web Push requires POST requests.
             'X-Push-Method': 'POST',
             'Encryption-Key': 'keyid=p256dh;dh=' + base64url.encode(pubkey),
             Encryption: 'keyid=p256dh;salt=' + base64url.encode(salt),
-            'Content-Encoding': 'aesgcm128'
+            'Content-Encoding': 'aesgcm128',
+            'TTL': ttl,
           },
           body: payload,
         };
         return fetch('http://mochi.test:8888/tests/dom/push/test/push-server.sjs', options);
       }).then(response => {
-        if (response.status / 100 !== 2) {
+        if (Math.floor(response.status / 100) !== 2) {
           throw new Error('Unable to deliver message');
         }
         return response;
       });
   }
 
   g.webpush = webpush;
 }(this));