tor-browser

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

test_bug468426.js (3704B)


      1 "use strict";
      2 
      3 const { HttpServer } = ChromeUtils.importESModule(
      4  "resource://testing-common/httpd.sys.mjs"
      5 );
      6 
      7 var httpserver = new HttpServer();
      8 var index = 0;
      9 var tests = [
     10  // Initial request. Cached variant will have no cookie
     11  { url: "/bug468426", server: "0", expected: "0", cookie: null },
     12 
     13  // Cache now contains a variant with no value for cookie. If we don't
     14  // set cookie we expect to receive the cached variant
     15  { url: "/bug468426", server: "1", expected: "0", cookie: null },
     16 
     17  // Cache still contains a variant with no value for cookie. If we
     18  // set a value for cookie we expect a fresh value
     19  { url: "/bug468426", server: "2", expected: "2", cookie: "c=2" },
     20 
     21  // Cache now contains a variant with cookie "c=2". If the request
     22  // also set cookie "c=2", we expect to receive the cached variant.
     23  { url: "/bug468426", server: "3", expected: "2", cookie: "c=2" },
     24 
     25  // Cache still contains a variant with cookie "c=2". When setting
     26  // cookie "c=4" in the request we expect a fresh value
     27  { url: "/bug468426", server: "4", expected: "4", cookie: "c=4" },
     28 
     29  // Cache now contains a variant with cookie "c=4". When setting
     30  // cookie "c=4" in the request we expect the cached variant
     31  { url: "/bug468426", server: "5", expected: "4", cookie: "c=4" },
     32 
     33  // Cache still contains a variant with cookie "c=4". When setting
     34  // no cookie in the request we expect a fresh value
     35  { url: "/bug468426", server: "6", expected: "6", cookie: null },
     36 ];
     37 
     38 function setupChannel(suffix, value, cookie) {
     39  var chan = NetUtil.newChannel({
     40    uri: "http://localhost:" + httpserver.identity.primaryPort + suffix,
     41    loadUsingSystemPrincipal: true,
     42  });
     43  var httpChan = chan.QueryInterface(Ci.nsIHttpChannel);
     44  httpChan.requestMethod = "GET";
     45  httpChan.setRequestHeader("x-request", value, false);
     46  if (cookie != null) {
     47    httpChan.setRequestHeader("Cookie", cookie, false);
     48  }
     49  return httpChan;
     50 }
     51 
     52 function triggerNextTest() {
     53  var channel = setupChannel(
     54    tests[index].url,
     55    tests[index].server,
     56    tests[index].cookie
     57  );
     58  channel.asyncOpen(new ChannelListener(checkValueAndTrigger, null));
     59 }
     60 
     61 function checkValueAndTrigger(request, data) {
     62  Assert.equal(tests[index].expected, data);
     63 
     64  if (index < tests.length - 1) {
     65    index++;
     66    // This call happens in onStopRequest from the channel. Opening a new
     67    // channel to the same url here is no good idea!  Post it instead...
     68    do_timeout(1, triggerNextTest);
     69  } else {
     70    httpserver.stop(do_test_finished);
     71  }
     72 }
     73 
     74 function run_test() {
     75  httpserver.registerPathHandler("/bug468426", handler);
     76  httpserver.start(-1);
     77 
     78  // Clear cache and trigger the first test
     79  evict_cache_entries();
     80  triggerNextTest();
     81 
     82  do_test_pending();
     83 }
     84 
     85 function handler(metadata, response) {
     86  var body = "unset";
     87  try {
     88    body = metadata.getHeader("x-request");
     89  } catch (e) {}
     90  response.setStatusLine(metadata.httpVersion, 200, "Ok");
     91  response.setHeader("Content-Type", "text/plain", false);
     92  response.setHeader("Last-Modified", getDateString(-1), false);
     93  response.setHeader("Vary", "Cookie", false);
     94  response.bodyOutputStream.write(body, body.length);
     95 }
     96 
     97 function getDateString(yearDelta) {
     98  var months = [
     99    "Jan",
    100    "Feb",
    101    "Mar",
    102    "Apr",
    103    "May",
    104    "Jun",
    105    "Jul",
    106    "Aug",
    107    "Sep",
    108    "Oct",
    109    "Nov",
    110    "Dec",
    111  ];
    112  var days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
    113 
    114  var d = new Date();
    115  return (
    116    days[d.getUTCDay()] +
    117    ", " +
    118    d.getUTCDate() +
    119    " " +
    120    months[d.getUTCMonth()] +
    121    " " +
    122    (d.getUTCFullYear() + yearDelta) +
    123    " " +
    124    d.getUTCHours() +
    125    ":" +
    126    d.getUTCMinutes() +
    127    ":" +
    128    d.getUTCSeconds() +
    129    " UTC"
    130  );
    131 }