Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / netwerk / test / unit_ipc / test_channel_id.js
blob1031dba4e20901c13e8a812652e881a2f3843d62
1 const { HttpServer } = ChromeUtils.importESModule(
2   "resource://testing-common/httpd.sys.mjs"
3 );
5 /*
6  * Test that when doing HTTP requests, the nsIHttpChannel is detected in
7  * both parent and child and shares the same channelId across processes.
8  */
10 let httpserver;
11 let port;
13 function startHttpServer() {
14   httpserver = new HttpServer();
16   httpserver.registerPathHandler("/resource", (metadata, response) => {
17     response.setStatusLine(metadata.httpVersion, 200, "OK");
18     response.setHeader("Content-Type", "text/plain", false);
19     response.setHeader("Cache-Control", "no-cache", false);
20     response.bodyOutputStream.write("data", 4);
21   });
23   httpserver.registerPathHandler("/redirect", (metadata, response) => {
24     response.setStatusLine(metadata.httpVersion, 302, "Redirect");
25     response.setHeader("Location", "/resource", false);
26     response.setHeader("Cache-Control", "no-cache", false);
27   });
29   httpserver.start(-1);
30   port = httpserver.identity.primaryPort;
33 function stopHttpServer(next) {
34   httpserver.stop(next);
37 let expectedParentChannels = [];
38 let expectedChildMessages = [];
40 let maybeFinishWaitForParentChannels;
41 let parentChannelsDone = new Promise(resolve => {
42   maybeFinishWaitForParentChannels = () => {
43     if (!expectedParentChannels.length) {
44       dump("All expected parent channels were detected\n");
45       resolve();
46     }
47   };
48 });
50 function observer(subject) {
51   let channel = subject.QueryInterface(Ci.nsIHttpChannel);
53   let uri = channel.URI.spec;
54   let origUri = channel.originalURI.spec;
55   let id = channel.channelId;
56   dump(`Parent detected channel: ${uri} (orig=${origUri}): channelId=${id}\n`);
58   // did we expect a new channel?
59   let expected = expectedParentChannels.shift();
60   Assert.ok(!!expected);
62   // Start waiting for the messages about request/response from child
63   for (let event of expected) {
64     let message = `${event}:${id}`;
65     dump(`Expecting message from child: ${message}\n`);
67     let messagePromise = do_await_remote_message(message).then(() => {
68       dump(`Expected message from child arrived: ${message}\n`);
69     });
70     expectedChildMessages.push(messagePromise);
71   }
73   // If we don't expect any further parent channels, finish the parent wait
74   maybeFinishWaitForParentChannels();
77 function run_test() {
78   startHttpServer();
79   Services.obs.addObserver(observer, "http-on-modify-request");
80   run_test_in_child("child_channel_id.js", makeRequests);
83 function makeRequests() {
84   // First, a normal request without any redirect. Expect one channel detected
85   // in parent, used by both request and response.
86   expectedParentChannels.push(["request", "response"]);
87   sendCommand(`makeRequest("http://localhost:${port}/resource");`);
89   // Second request will be redirected. Expect two channels, one with the
90   // original request, then the redirected one which gets the final response.
91   expectedParentChannels.push(["request"], ["response"]);
92   sendCommand(`makeRequest("http://localhost:${port}/redirect");`);
94   waitForParentChannels();
97 function waitForParentChannels() {
98   parentChannelsDone.then(waitForChildMessages);
101 function waitForChildMessages() {
102   dump(`Waiting for ${expectedChildMessages.length} child messages\n`);
103   Promise.all(expectedChildMessages).then(finish);
106 function finish() {
107   Services.obs.removeObserver(observer, "http-on-modify-request");
108   sendCommand("finish();", () => stopHttpServer(do_test_finished));