netwerk/test/unit/test_bug669001.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/unit/test_bug669001.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,161 @@
     1.4 +Cu.import("resource://testing-common/httpd.js");
     1.5 +
     1.6 +var httpServer = null;
     1.7 +var path = "/bug699001";
     1.8 +
     1.9 +XPCOMUtils.defineLazyGetter(this, "URI", function() {
    1.10 +  return "http://localhost:" + httpServer.identity.primaryPort + path;
    1.11 +});
    1.12 +
    1.13 +function make_channel(url) {
    1.14 +  var ios = Cc["@mozilla.org/network/io-service;1"].
    1.15 +            getService(Ci.nsIIOService);
    1.16 +  return ios.newChannel(url, "", null);
    1.17 +}
    1.18 +
    1.19 +var fetched;
    1.20 +
    1.21 +// The test loads a resource that expires in one year, has an etag and varies only by User-Agent
    1.22 +// First we load it, then check we load it only from the cache w/o even checking with the server
    1.23 +// Then we modify our User-Agent and try it again
    1.24 +// We have to get a new content (even though with the same etag) and again on next load only from
    1.25 +// cache w/o accessing the server
    1.26 +// Goal is to check we've updated User-Agent request header in cache after we've got 304 response
    1.27 +// from the server
    1.28 +
    1.29 +var tests = [
    1.30 +{
    1.31 +  prepare: function() { },
    1.32 +  test: function(response) {
    1.33 +    do_check_true(fetched);
    1.34 +  }
    1.35 +},
    1.36 +{
    1.37 +  prepare: function() { },
    1.38 +  test: function(response) {
    1.39 +    do_check_false(fetched);
    1.40 +  }
    1.41 +},
    1.42 +{
    1.43 +  prepare: function() {
    1.44 +    setUA("A different User Agent");
    1.45 +  },
    1.46 +  test: function(response) {
    1.47 +    do_check_true(fetched);
    1.48 +  }
    1.49 +},
    1.50 +{
    1.51 +  prepare: function() { },
    1.52 +  test: function(response) {
    1.53 +    do_check_false(fetched);
    1.54 +  }
    1.55 +},
    1.56 +{
    1.57 +  prepare: function() {
    1.58 +    setUA("And another User Agent");
    1.59 +  },
    1.60 +  test: function(response) {
    1.61 +    do_check_true(fetched);
    1.62 +  }
    1.63 +},
    1.64 +{
    1.65 +  prepare: function() { },
    1.66 +  test: function(response) {
    1.67 +    do_check_false(fetched);
    1.68 +  }
    1.69 +}
    1.70 +];
    1.71 +
    1.72 +function handler(metadata, response)
    1.73 +{
    1.74 +  if (metadata.hasHeader("If-None-Match")) {
    1.75 +    response.setStatusLine(metadata.httpVersion, 304, "Not modified");
    1.76 +  }
    1.77 +  else {
    1.78 +    response.setStatusLine(metadata.httpVersion, 200, "OK");
    1.79 +    response.setHeader("Content-Type", "text/plain");
    1.80 +
    1.81 +    var body = "body";
    1.82 +    response.bodyOutputStream.write(body, body.length);
    1.83 +  }
    1.84 +
    1.85 +  fetched = true;
    1.86 +
    1.87 +  response.setHeader("Expires", getDateString(+1));
    1.88 +  response.setHeader("Cache-Control", "private");
    1.89 +  response.setHeader("Vary", "User-Agent");
    1.90 +  response.setHeader("ETag", "1234");
    1.91 +}
    1.92 +
    1.93 +function run_test()
    1.94 +{
    1.95 +  httpServer = new HttpServer();
    1.96 +  httpServer.registerPathHandler(path, handler);
    1.97 +  httpServer.start(-1);
    1.98 +
    1.99 +  do_test_pending();
   1.100 +
   1.101 +  nextTest();
   1.102 +}
   1.103 +
   1.104 +function nextTest()
   1.105 +{
   1.106 +  fetched = false;
   1.107 +  tests[0].prepare();
   1.108 +
   1.109 +  dump("Testing with User-Agent: " + getUA() + "\n");
   1.110 +  var chan = make_channel(URI);
   1.111 +
   1.112 +  // Give the old channel a chance to close the cache entry first.
   1.113 +  // XXX This is actually a race condition that might be considered a bug...
   1.114 +  do_execute_soon(function() {
   1.115 +    chan.asyncOpen(new ChannelListener(checkAndShiftTest, null), null);
   1.116 +  });
   1.117 +}
   1.118 +
   1.119 +function checkAndShiftTest(request, response)
   1.120 +{
   1.121 +  tests[0].test(response);
   1.122 +
   1.123 +  tests.shift();
   1.124 +  if (tests.length == 0) {
   1.125 +    httpServer.stop(tearDown);
   1.126 +    return;
   1.127 +  }
   1.128 +
   1.129 +  nextTest();
   1.130 +}
   1.131 +
   1.132 +function tearDown()
   1.133 +{
   1.134 +  setUA("");
   1.135 +  do_test_finished();
   1.136 +}
   1.137 +
   1.138 +// Helpers
   1.139 +
   1.140 +function getUA()
   1.141 +{
   1.142 +  var httphandler = Cc["@mozilla.org/network/protocol;1?name=http"].
   1.143 +                 getService(Ci.nsIHttpProtocolHandler);
   1.144 +  return httphandler.userAgent;
   1.145 +}
   1.146 +
   1.147 +function setUA(value)
   1.148 +{
   1.149 +  var prefs = Cc["@mozilla.org/preferences-service;1"].
   1.150 +                 getService(Ci.nsIPrefBranch);
   1.151 +  prefs.setCharPref("general.useragent.override", value);
   1.152 +}
   1.153 +
   1.154 +function getDateString(yearDelta) {
   1.155 +  var months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
   1.156 +          'Sep', 'Oct', 'Nov', 'Dec' ];
   1.157 +  var days = [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ];
   1.158 +
   1.159 +  var d = new Date();
   1.160 +  return days[d.getUTCDay()] + ", " + d.getUTCDate() + " "
   1.161 +          + months[d.getUTCMonth()] + " " + (d.getUTCFullYear() + yearDelta)
   1.162 +          + " " + d.getUTCHours() + ":" + d.getUTCMinutes() + ":"
   1.163 +          + d.getUTCSeconds() + " UTC";
   1.164 +}

mercurial