Bug 1321528: Part 2 - Add tests for response header modification timing. r=mixedpuppy a=jcristau
authorKris Maglione <maglione.k@gmail.com>
Wed, 07 Dec 2016 11:04:52 -1000
changeset 353084 4cdf04be5d8eed2c994ff077a534f07400fbaa03
parent 353083 1c4863a4ff98cf10b14d7b00b33ed95dc384d967
child 353085 06fd54cb05583d9ae414d747b03d885e1a09b5b6
push id6795
push userjlund@mozilla.com
push dateMon, 23 Jan 2017 14:19:46 +0000
treeherdermozilla-esr52@76101b503191 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmixedpuppy, jcristau
bugs1321528
milestone52.0a2
Bug 1321528: Part 2 - Add tests for response header modification timing. r=mixedpuppy a=jcristau MozReview-Commit-ID: 46Qsss8YhbY
toolkit/components/extensions/test/mochitest/test_ext_webrequest_suspend.html
--- a/toolkit/components/extensions/test/mochitest/test_ext_webrequest_suspend.html
+++ b/toolkit/components/extensions/test/mochitest/test_ext_webrequest_suspend.html
@@ -129,11 +129,88 @@ add_task(function* test_error_resume() {
 
   yield extension.awaitMessage("got-before-send-headers");
   yield extension.awaitMessage("got-error-occurred");
 
   yield extension.unload();
   chromeScript.destroy();
 });
 
+
+// Test that response header modifications take effect before onStartRequest fires.
+add_task(function* test_set_responseHeaders() {
+  let extension = ExtensionTestUtils.loadExtension({
+    manifest: {
+      permissions: [
+        "webRequest",
+        "webRequestBlocking",
+        "http://example.com/",
+      ],
+    },
+
+    background() {
+      browser.webRequest.onHeadersReceived.addListener(
+        details => {
+          browser.test.log(`onHeadersReceived({url: ${details.url}})`);
+
+          details.responseHeaders.push({name: "foo", value: "bar"});
+
+          return {responseHeaders: details.responseHeaders};
+        },
+        {urls: ["http://example.com/?modify_headers"]},
+        ["blocking", "responseHeaders"]);
+    },
+  });
+
+  yield extension.startup();
+
+  yield new Promise(resolve => setTimeout(resolve, 0));
+
+  let chromeScript = SpecialPowers.loadChromeScript(() => {
+    const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
+
+    Cu.import("resource://gre/modules/NetUtil.jsm");
+    Cu.import("resource://gre/modules/Services.jsm");
+    Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+
+    let ssm = Services.scriptSecurityManager;
+
+    let channel = NetUtil.newChannel({
+      uri: "http://example.com/?modify_headers",
+      loadingPrincipal: ssm.createCodebasePrincipalFromOrigin("http://example.com"),
+      contentPolicyType: Ci.nsIContentPolicy.TYPE_XMLHTTPREQUEST,
+      securityFlags: Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
+    });
+
+    channel.asyncOpen2({
+      QueryInterface: XPCOMUtils.generateQI([Ci.nsIStreamListener]),
+
+      onStartRequest(request, context) {
+        request.QueryInterface(Ci.nsIHttpChannel);
+
+        try {
+          sendAsyncMessage("response-header-foo", request.getResponseHeader("foo"));
+        } catch (e) {
+          sendAsyncMessage("response-header-foo", null);
+        }
+        request.cancel(Cr.NS_BINDING_ABORTED);
+      },
+
+      onStopRequest() {
+      },
+
+      onDataAvailable() {
+        throw new Components.Exception("", Cr.NS_ERROR_FAILURE);
+      },
+    });
+  });
+
+  let headerValue = yield chromeScript.promiseOneMessage("response-header-foo");
+  is(headerValue, "bar", "Expected Foo header value");
+
+  yield extension.unload();
+  chromeScript.destroy();
+});
+
+
 </script>
 </body>
 </html>