tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_cookie_header.js (2576B)


      1 // This file tests bug 250375
      2 
      3 "use strict";
      4 
      5 const { HttpServer } = ChromeUtils.importESModule(
      6  "resource://testing-common/httpd.sys.mjs"
      7 );
      8 
      9 ChromeUtils.defineLazyGetter(this, "URL", function () {
     10  return "http://localhost:" + httpserv.identity.primaryPort + "/";
     11 });
     12 
     13 function inChildProcess() {
     14  return Services.appinfo.processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
     15 }
     16 
     17 function check_request_header(chan, name, value) {
     18  var chanValue;
     19  try {
     20    chanValue = chan.getRequestHeader(name);
     21  } catch (e) {
     22    do_throw(
     23      "Expected to find header '" +
     24        name +
     25        "' but didn't find it, got exception: " +
     26        e
     27    );
     28  }
     29  dump("Value for header '" + name + "' is '" + chanValue + "'\n");
     30  Assert.equal(chanValue, value);
     31 }
     32 
     33 var cookieVal = "C1=V1";
     34 
     35 var listener = {
     36  onStartRequest: function test_onStartR(request) {
     37    try {
     38      var chan = request.QueryInterface(Ci.nsIHttpChannel);
     39      check_request_header(chan, "Cookie", cookieVal);
     40    } catch (e) {
     41      do_throw("Unexpected exception: " + e);
     42    }
     43 
     44    throw Components.Exception("", Cr.NS_ERROR_ABORT);
     45  },
     46 
     47  onDataAvailable: function test_ODA() {
     48    throw Components.Exception("", Cr.NS_ERROR_UNEXPECTED);
     49  },
     50 
     51  onStopRequest: async function test_onStopR() {
     52    if (this._iteration == 1) {
     53      await run_test_continued();
     54    } else {
     55      do_test_pending();
     56      httpserv.stop(do_test_finished);
     57    }
     58    do_test_finished();
     59  },
     60 
     61  _iteration: 1,
     62 };
     63 
     64 function makeChan() {
     65  return NetUtil.newChannel({
     66    uri: URL,
     67    loadUsingSystemPrincipal: true,
     68  }).QueryInterface(Ci.nsIHttpChannel);
     69 }
     70 
     71 var httpserv = null;
     72 
     73 function run_test() {
     74  // Allow all cookies if the pref service is available in this process.
     75  if (!inChildProcess()) {
     76    Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
     77    Services.prefs.setBoolPref(
     78      "network.cookieJarSettings.unblocked_for_testing",
     79      true
     80    );
     81  }
     82 
     83  httpserv = new HttpServer();
     84  httpserv.start(-1);
     85 
     86  var chan = makeChan();
     87 
     88  chan.setRequestHeader("Cookie", cookieVal, false);
     89 
     90  chan.asyncOpen(listener);
     91 
     92  do_test_pending();
     93 }
     94 
     95 async function run_test_continued() {
     96  var chan = makeChan();
     97 
     98  var cookie2 = "C2=V2";
     99 
    100  await CookieXPCShellUtils.setCookieToDocument(chan.URI.spec, cookie2);
    101 
    102  chan.setRequestHeader("Cookie", cookieVal, false);
    103 
    104  // We expect that the setRequestHeader overrides the
    105  // automatically-added one, so insert cookie2 in front
    106  cookieVal = cookie2 + "; " + cookieVal;
    107 
    108  listener._iteration++;
    109  chan.asyncOpen(listener);
    110 
    111  do_test_pending();
    112 }