test_bug528292.js (2665B)
1 "use strict"; 2 3 const { HttpServer } = ChromeUtils.importESModule( 4 "resource://testing-common/httpd.sys.mjs" 5 ); 6 7 const sentCookieVal = "foo=bar"; 8 const responseBody = "response body"; 9 10 ChromeUtils.defineLazyGetter(this, "baseURL", function () { 11 return "http://localhost:" + httpServer.identity.primaryPort; 12 }); 13 14 const preRedirectPath = "/528292/pre-redirect"; 15 16 ChromeUtils.defineLazyGetter(this, "preRedirectURL", function () { 17 return baseURL + preRedirectPath; 18 }); 19 20 const postRedirectPath = "/528292/post-redirect"; 21 22 ChromeUtils.defineLazyGetter(this, "postRedirectURL", function () { 23 return baseURL + postRedirectPath; 24 }); 25 26 var httpServer = null; 27 var receivedCookieVal = null; 28 29 function preRedirectHandler(metadata, response) { 30 response.setStatusLine(metadata.httpVersion, 302, "Found"); 31 response.setHeader("Location", postRedirectURL, false); 32 } 33 34 function postRedirectHandler(metadata, response) { 35 receivedCookieVal = metadata.getHeader("Cookie"); 36 response.setHeader("Content-Type", "text/plain"); 37 response.bodyOutputStream.write(responseBody, responseBody.length); 38 } 39 40 function inChildProcess() { 41 return Services.appinfo.processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT; 42 } 43 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); 50 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 } 59 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; 69 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); 74 75 await CookieXPCShellUtils.setCookieToDocument( 76 postRedirectURI.spec, 77 sentCookieVal 78 ); 79 80 // Load the pre-redirect URI. 81 await new Promise(resolve => { 82 chan.asyncOpen(new ChannelListener(resolve, null)); 83 }); 84 85 Assert.equal(receivedCookieVal, sentCookieVal); 86 httpServer.stop(do_test_finished); 87 });