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.

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

mercurial