1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 const { HttpServer
} = ChromeUtils
.importESModule(
8 "resource://testing-common/httpd.sys.mjs"
13 function inChildProcess() {
14 return Services
.appinfo
.processType
!= Ci
.nsIXULRuntime
.PROCESS_TYPE_DEFAULT
;
16 function makeChan(path
) {
17 return NetUtil
.newChannel({
18 uri
: "http://localhost:" + httpserver
.identity
.primaryPort
+ "/" + path
,
19 loadUsingSystemPrincipal
: true,
20 }).QueryInterface(Ci
.nsIHttpChannel
);
23 function setup_chan(path
, isPrivate
, callback
) {
24 var chan
= makeChan(path
);
25 chan
.QueryInterface(Ci
.nsIPrivateBrowsingChannel
).setPrivate(isPrivate
);
26 chan
.asyncOpen(new ChannelListener(callback
));
29 function set_cookie(value
, callback
) {
30 return setup_chan("set?cookie=" + value
, false, callback
);
33 function set_private_cookie(value
, callback
) {
34 return setup_chan("set?cookie=" + value
, true, callback
);
37 function check_cookie_presence(value
, isPrivate
, expected
, callback
) {
39 "present?cookie=" + value
.replace("=", "|"),
42 req
.QueryInterface(Ci
.nsIHttpChannel
);
43 Assert
.equal(req
.responseStatus
, expected
? 200 : 404);
49 function presentHandler(metadata
, response
) {
51 var match
= /cookie=([^&]*)/.exec(metadata
.queryString
);
56 .includes(match
[1].replace("|", "="));
59 response
.setStatusLine("1.0", present
? 200 : 404, "");
62 function setHandler(metadata
, response
) {
63 response
.setStatusLine("1.0", 200, "Cookie set");
64 var match
= /cookie=([^&]*)/.exec(metadata
.queryString
);
66 response
.setHeader("Set-Cookie", match
[1]);
71 // Allow all cookies if the pref service is available in this process.
72 if (!inChildProcess()) {
73 Services
.prefs
.setIntPref("network.cookie.cookieBehavior", 0);
74 Services
.prefs
.setBoolPref(
75 "network.cookieJarSettings.unblocked_for_testing",
80 httpserver
= new HttpServer();
81 httpserver
.registerPathHandler("/set", setHandler
);
82 httpserver
.registerPathHandler("/present", presentHandler
);
87 function check_cookie(req
) {
88 req
.QueryInterface(Ci
.nsIHttpChannel
);
89 Assert
.equal(req
.responseStatus
, 200);
92 req
.getResponseHeader("Set-Cookie") != "",
93 "expected a Set-Cookie header"
96 do_throw("missing Set-Cookie header");
104 function runNextTest() {
105 executeSoon(tests
.shift());
108 tests
.push(function () {
109 set_cookie("C1=V1", check_cookie
);
111 tests
.push(function () {
112 set_private_cookie("C2=V2", check_cookie
);
114 tests
.push(function () {
115 // Check that the first cookie is present in a non-private request
116 check_cookie_presence("C1=V1", false, true, runNextTest
);
118 tests
.push(function () {
119 // Check that the second cookie is present in a private request
120 check_cookie_presence("C2=V2", true, true, runNextTest
);
122 tests
.push(function () {
123 // Check that the first cookie is not present in a private request
124 check_cookie_presence("C1=V1", true, false, runNextTest
);
126 tests
.push(function () {
127 // Check that the second cookie is not present in a non-private request
128 check_cookie_presence("C2=V2", false, false, runNextTest
);
131 // The following test only works in a non-e10s situation at the moment,
132 // since the notification needs to run in the parent process but there is
133 // no existing mechanism to make that happen.
134 if (!inChildProcess()) {
135 tests
.push(function () {
136 // Simulate all private browsing instances being closed
137 Services
.obs
.notifyObservers(null, "last-pb-context-exited");
138 // Check that all private cookies are now unavailable in new private requests
139 check_cookie_presence("C2=V2", true, false, runNextTest
);
143 tests
.push(function () {
144 httpserver
.stop(do_test_finished
);