Bug 1844673 [wpt PR 41130] - Update wpt metadata, a=testonly
[gecko.git] / netwerk / test / unit / test_bug528292.js
blob84449aff71b6dc15d9f798ecb4a64a62f391a40f
1 "use strict";
3 const { HttpServer } = ChromeUtils.importESModule(
4   "resource://testing-common/httpd.sys.mjs"
5 );
7 const sentCookieVal = "foo=bar";
8 const responseBody = "response body";
10 ChromeUtils.defineLazyGetter(this, "baseURL", function () {
11   return "http://localhost:" + httpServer.identity.primaryPort;
12 });
14 const preRedirectPath = "/528292/pre-redirect";
16 ChromeUtils.defineLazyGetter(this, "preRedirectURL", function () {
17   return baseURL + preRedirectPath;
18 });
20 const postRedirectPath = "/528292/post-redirect";
22 ChromeUtils.defineLazyGetter(this, "postRedirectURL", function () {
23   return baseURL + postRedirectPath;
24 });
26 var httpServer = null;
27 var receivedCookieVal = null;
29 function preRedirectHandler(metadata, response) {
30   response.setStatusLine(metadata.httpVersion, 302, "Found");
31   response.setHeader("Location", postRedirectURL, false);
34 function postRedirectHandler(metadata, response) {
35   receivedCookieVal = metadata.getHeader("Cookie");
36   response.setHeader("Content-Type", "text/plain");
37   response.bodyOutputStream.write(responseBody, responseBody.length);
40 function inChildProcess() {
41   return Services.appinfo.processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
44 add_task(async () => {
45   // Start the HTTP server.
46   httpServer = new HttpServer();
47   httpServer.registerPathHandler(preRedirectPath, preRedirectHandler);
48   httpServer.registerPathHandler(postRedirectPath, postRedirectHandler);
49   httpServer.start(-1);
51   if (!inChildProcess()) {
52     // Disable third-party cookies in general.
53     Services.prefs.setIntPref("network.cookie.cookieBehavior", 1);
54     Services.prefs.setBoolPref(
55       "network.cookieJarSettings.unblocked_for_testing",
56       true
57     );
58   }
60   // Set up a channel with forceAllowThirdPartyCookie set to true.  We'll use
61   // the channel both to set a cookie and then to load the pre-redirect URI.
62   var chan = NetUtil.newChannel({
63     uri: preRedirectURL,
64     loadUsingSystemPrincipal: true,
65   })
66     .QueryInterface(Ci.nsIHttpChannel)
67     .QueryInterface(Ci.nsIHttpChannelInternal);
68   chan.forceAllowThirdPartyCookie = true;
70   // Set a cookie on one of the URIs.  It doesn't matter which one, since
71   // they're both from the same host, which is enough for the cookie service
72   // to send the cookie with both requests.
73   var postRedirectURI = Services.io.newURI(postRedirectURL);
75   await CookieXPCShellUtils.setCookieToDocument(
76     postRedirectURI.spec,
77     sentCookieVal
78   );
80   // Load the pre-redirect URI.
81   await new Promise(resolve => {
82     chan.asyncOpen(new ChannelListener(resolve, null));
83   });
85   Assert.equal(receivedCookieVal, sentCookieVal);
86   httpServer.stop(do_test_finished);
87 });