fune/netwerk/test/unit/test_bug528292.js
Kris Maglione 918ed6c474 Bug 1431533: Part 5a - Auto-rewrite code to use ChromeUtils import methods. r=florian
This was done using the following script:
37e3803c7a/processors/chromeutils-import.jsm

MozReview-Commit-ID: 1Nc3XDu0wGl

--HG--
extra : source : 12fc4dee861c812fd2bd032c63ef17af61800c70
extra : intermediate-source : 34c999fa006bffe8705cf50c54708aa21a962e62
extra : histedit_source : b2be2c5e5d226e6c347312456a6ae339c1e634b0
2018-01-29 15:20:18 -08:00

90 lines
2.9 KiB
JavaScript

ChromeUtils.import("resource://testing-common/httpd.js");
ChromeUtils.import("resource://gre/modules/NetUtil.jsm");
const sentCookieVal = "foo=bar";
const responseBody = "response body";
XPCOMUtils.defineLazyGetter(this, "baseURL", function() {
return "http://localhost:" + httpServer.identity.primaryPort;
});
const preRedirectPath = "/528292/pre-redirect";
XPCOMUtils.defineLazyGetter(this, "preRedirectURL", function() {
return baseURL + preRedirectPath;
});
const postRedirectPath = "/528292/post-redirect";
XPCOMUtils.defineLazyGetter(this, "postRedirectURL", function() {
return baseURL + postRedirectPath;
});
var httpServer = null;
var receivedCookieVal = null;
function preRedirectHandler(metadata, response)
{
response.setStatusLine(metadata.httpVersion, 302, "Found");
response.setHeader("Location", postRedirectURL, false);
return;
}
function postRedirectHandler(metadata, response)
{
receivedCookieVal = metadata.getHeader("Cookie");
response.setHeader("Content-Type", "text/plain");
response.bodyOutputStream.write(responseBody, responseBody.length);
}
function inChildProcess() {
return Cc["@mozilla.org/xre/app-info;1"]
.getService(Ci.nsIXULRuntime)
.processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
}
function run_test()
{
// Start the HTTP server.
httpServer = new HttpServer();
httpServer.registerPathHandler(preRedirectPath, preRedirectHandler);
httpServer.registerPathHandler(postRedirectPath, postRedirectHandler);
httpServer.start(-1);
if (!inChildProcess()) {
// Disable third-party cookies in general.
Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch).
setIntPref("network.cookie.cookieBehavior", 1);
}
var ioService = Cc["@mozilla.org/network/io-service;1"].
getService(Ci.nsIIOService);
// Set up a channel with forceAllowThirdPartyCookie set to true. We'll use
// the channel both to set a cookie (since nsICookieService::setCookieString
// requires such a channel in order to successfully set a cookie) and then
// to load the pre-redirect URI.
var chan = NetUtil.newChannel({
uri: preRedirectURL,
loadUsingSystemPrincipal: true
}).QueryInterface(Ci.nsIHttpChannel)
.QueryInterface(Ci.nsIHttpChannelInternal);
chan.forceAllowThirdPartyCookie = true;
// Set a cookie on one of the URIs. It doesn't matter which one, since
// they're both from the same host, which is enough for the cookie service
// to send the cookie with both requests.
var postRedirectURI = ioService.newURI(postRedirectURL);
Cc["@mozilla.org/cookieService;1"].getService(Ci.nsICookieService).
setCookieString(postRedirectURI, null, sentCookieVal, chan);
// Load the pre-redirect URI.
chan.asyncOpen2(new ChannelListener(finish_test, null));
do_test_pending();
}
function finish_test(event)
{
Assert.equal(receivedCookieVal, sentCookieVal);
httpServer.stop(do_test_finished);
}