tor-browser

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

test_bug669001.js (3664B)


      1 "use strict";
      2 
      3 const { HttpServer } = ChromeUtils.importESModule(
      4  "resource://testing-common/httpd.sys.mjs"
      5 );
      6 
      7 var httpServer = null;
      8 var path = "/bug699001";
      9 
     10 ChromeUtils.defineLazyGetter(this, "URI", function () {
     11  return "http://localhost:" + httpServer.identity.primaryPort + path;
     12 });
     13 
     14 function make_channel(url) {
     15  return NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true });
     16 }
     17 
     18 var fetched;
     19 
     20 // The test loads a resource that expires in one year, has an etag and varies only by User-Agent
     21 // First we load it, then check we load it only from the cache w/o even checking with the server
     22 // Then we modify our User-Agent and try it again
     23 // We have to get a new content (even though with the same etag) and again on next load only from
     24 // cache w/o accessing the server
     25 // Goal is to check we've updated User-Agent request header in cache after we've got 304 response
     26 // from the server
     27 
     28 var tests = [
     29  {
     30    prepare() {},
     31    test() {
     32      Assert.ok(fetched);
     33    },
     34  },
     35  {
     36    prepare() {},
     37    test() {
     38      Assert.ok(!fetched);
     39    },
     40  },
     41  {
     42    prepare() {
     43      setUA("A different User Agent");
     44    },
     45    test() {
     46      Assert.ok(fetched);
     47    },
     48  },
     49  {
     50    prepare() {},
     51    test() {
     52      Assert.ok(!fetched);
     53    },
     54  },
     55  {
     56    prepare() {
     57      setUA("And another User Agent");
     58    },
     59    test() {
     60      Assert.ok(fetched);
     61    },
     62  },
     63  {
     64    prepare() {},
     65    test() {
     66      Assert.ok(!fetched);
     67    },
     68  },
     69 ];
     70 
     71 function handler(metadata, response) {
     72  if (metadata.hasHeader("If-None-Match")) {
     73    response.setStatusLine(metadata.httpVersion, 304, "Not modified");
     74  } else {
     75    response.setStatusLine(metadata.httpVersion, 200, "OK");
     76    response.setHeader("Content-Type", "text/plain");
     77 
     78    var body = "body";
     79    response.bodyOutputStream.write(body, body.length);
     80  }
     81 
     82  fetched = true;
     83 
     84  response.setHeader("Expires", getDateString(+1));
     85  response.setHeader("Cache-Control", "private");
     86  response.setHeader("Vary", "User-Agent");
     87  response.setHeader("ETag", "1234");
     88 }
     89 
     90 function run_test() {
     91  httpServer = new HttpServer();
     92  httpServer.registerPathHandler(path, handler);
     93  httpServer.start(-1);
     94 
     95  do_test_pending();
     96 
     97  nextTest();
     98 }
     99 
    100 function nextTest() {
    101  fetched = false;
    102  tests[0].prepare();
    103 
    104  dump("Testing with User-Agent: " + getUA() + "\n");
    105  var chan = make_channel(URI);
    106 
    107  // Give the old channel a chance to close the cache entry first.
    108  // XXX This is actually a race condition that might be considered a bug...
    109  executeSoon(function () {
    110    chan.asyncOpen(new ChannelListener(checkAndShiftTest, null));
    111  });
    112 }
    113 
    114 function checkAndShiftTest(request, response) {
    115  tests[0].test(response);
    116 
    117  tests.shift();
    118  if (!tests.length) {
    119    httpServer.stop(tearDown);
    120    return;
    121  }
    122 
    123  nextTest();
    124 }
    125 
    126 function tearDown() {
    127  setUA("");
    128  do_test_finished();
    129 }
    130 
    131 // Helpers
    132 
    133 function getUA() {
    134  var httphandler = Cc["@mozilla.org/network/protocol;1?name=http"].getService(
    135    Ci.nsIHttpProtocolHandler
    136  );
    137  return httphandler.userAgent;
    138 }
    139 
    140 function setUA(value) {
    141  Services.prefs.setCharPref("general.useragent.override", value);
    142 }
    143 
    144 function getDateString(yearDelta) {
    145  var months = [
    146    "Jan",
    147    "Feb",
    148    "Mar",
    149    "Apr",
    150    "May",
    151    "Jun",
    152    "Jul",
    153    "Aug",
    154    "Sep",
    155    "Oct",
    156    "Nov",
    157    "Dec",
    158  ];
    159  var days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
    160 
    161  var d = new Date();
    162  return (
    163    days[d.getUTCDay()] +
    164    ", " +
    165    d.getUTCDate() +
    166    " " +
    167    months[d.getUTCMonth()] +
    168    " " +
    169    (d.getUTCFullYear() + yearDelta) +
    170    " " +
    171    d.getUTCHours() +
    172    ":" +
    173    d.getUTCMinutes() +
    174    ":" +
    175    d.getUTCSeconds() +
    176    " UTC"
    177  );
    178 }