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