netwerk/test/unit/test_bug528292.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 Cu.import("resource://testing-common/httpd.js");
michael@0 2
michael@0 3 const sentCookieVal = "foo=bar";
michael@0 4 const responseBody = "response body";
michael@0 5
michael@0 6 XPCOMUtils.defineLazyGetter(this, "baseURL", function() {
michael@0 7 return "http://localhost:" + httpServer.identity.primaryPort;
michael@0 8 });
michael@0 9
michael@0 10 const preRedirectPath = "/528292/pre-redirect";
michael@0 11
michael@0 12 XPCOMUtils.defineLazyGetter(this, "preRedirectURL", function() {
michael@0 13 return baseURL + preRedirectPath;
michael@0 14 });
michael@0 15
michael@0 16 const postRedirectPath = "/528292/post-redirect";
michael@0 17
michael@0 18 XPCOMUtils.defineLazyGetter(this, "postRedirectURL", function() {
michael@0 19 return baseURL + postRedirectPath;
michael@0 20 });
michael@0 21
michael@0 22 var httpServer = null;
michael@0 23 var receivedCookieVal = null;
michael@0 24
michael@0 25 function preRedirectHandler(metadata, response)
michael@0 26 {
michael@0 27 response.setStatusLine(metadata.httpVersion, 302, "Found");
michael@0 28 response.setHeader("Location", postRedirectURL, false);
michael@0 29 return;
michael@0 30 }
michael@0 31
michael@0 32 function postRedirectHandler(metadata, response)
michael@0 33 {
michael@0 34 receivedCookieVal = metadata.getHeader("Cookie");
michael@0 35 response.setHeader("Content-Type", "text/plain");
michael@0 36 response.bodyOutputStream.write(responseBody, responseBody.length);
michael@0 37 }
michael@0 38
michael@0 39 function run_test()
michael@0 40 {
michael@0 41 // Start the HTTP server.
michael@0 42 httpServer = new HttpServer();
michael@0 43 httpServer.registerPathHandler(preRedirectPath, preRedirectHandler);
michael@0 44 httpServer.registerPathHandler(postRedirectPath, postRedirectHandler);
michael@0 45 httpServer.start(-1);
michael@0 46
michael@0 47 // Disable third-party cookies in general.
michael@0 48 Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch).
michael@0 49 setIntPref("network.cookie.cookieBehavior", 1);
michael@0 50
michael@0 51 var ioService = Cc["@mozilla.org/network/io-service;1"].
michael@0 52 getService(Ci.nsIIOService);
michael@0 53
michael@0 54 // Set up a channel with forceAllowThirdPartyCookie set to true. We'll use
michael@0 55 // the channel both to set a cookie (since nsICookieService::setCookieString
michael@0 56 // requires such a channel in order to successfully set a cookie) and then
michael@0 57 // to load the pre-redirect URI.
michael@0 58 var chan = ioService.newChannel(preRedirectURL, "", null).
michael@0 59 QueryInterface(Ci.nsIHttpChannel).
michael@0 60 QueryInterface(Ci.nsIHttpChannelInternal);
michael@0 61 chan.forceAllowThirdPartyCookie = true;
michael@0 62
michael@0 63 // Set a cookie on one of the URIs. It doesn't matter which one, since
michael@0 64 // they're both from the same host, which is enough for the cookie service
michael@0 65 // to send the cookie with both requests.
michael@0 66 var postRedirectURI = ioService.newURI(postRedirectURL, "", null);
michael@0 67 Cc["@mozilla.org/cookieService;1"].getService(Ci.nsICookieService).
michael@0 68 setCookieString(postRedirectURI, null, sentCookieVal, chan);
michael@0 69
michael@0 70 // Load the pre-redirect URI.
michael@0 71 chan.asyncOpen(new ChannelListener(finish_test, null), null);
michael@0 72 do_test_pending();
michael@0 73 }
michael@0 74
michael@0 75 function finish_test(event)
michael@0 76 {
michael@0 77 do_check_eq(receivedCookieVal, sentCookieVal);
michael@0 78 httpServer.stop(do_test_finished);
michael@0 79 }

mercurial