netwerk/test/unit/test_bug633743.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/unit/test_bug633743.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,186 @@
     1.4 +Cu.import("resource://testing-common/httpd.js");
     1.5 +
     1.6 +const VALUE_HDR_NAME = "X-HTTP-VALUE-HEADER";
     1.7 +const VARY_HDR_NAME = "X-HTTP-VARY-HEADER";
     1.8 +const CACHECTRL_HDR_NAME = "X-CACHE-CONTROL-HEADER";
     1.9 +
    1.10 +var httpserver = null;
    1.11 +
    1.12 +function make_channel(flags, vary, value) {
    1.13 +  var ios = Cc["@mozilla.org/network/io-service;1"].
    1.14 +    getService(Ci.nsIIOService);
    1.15 +  var chan = ios.newChannel("http://localhost:" +
    1.16 +                            httpserver.identity.primaryPort +
    1.17 +                            "/bug633743", null, null);
    1.18 +  return chan.QueryInterface(Ci.nsIHttpChannel);
    1.19 +}
    1.20 +
    1.21 +function Test(flags, varyHdr, sendValue, expectValue, cacheHdr) {
    1.22 +    this._flags = flags;
    1.23 +    this._varyHdr = varyHdr;
    1.24 +    this._sendVal = sendValue;
    1.25 +    this._expectVal = expectValue;
    1.26 +    this._cacheHdr = cacheHdr;
    1.27 +}
    1.28 +
    1.29 +Test.prototype = {
    1.30 +  _buffer: "",
    1.31 +  _flags: null,
    1.32 +  _varyHdr: null,
    1.33 +  _sendVal: null,
    1.34 +  _expectVal: null,
    1.35 +  _cacheHdr: null,
    1.36 +
    1.37 +  QueryInterface: function(iid) {
    1.38 +    if (iid.equals(Ci.nsIStreamListener) ||
    1.39 +        iid.equals(Ci.nsIRequestObserver) ||
    1.40 +        iid.equals(Ci.nsISupports))
    1.41 +      return this;
    1.42 +    throw Cr.NS_ERROR_NO_INTERFACE;
    1.43 +  },
    1.44 +
    1.45 +  onStartRequest: function(request, context) { },
    1.46 +
    1.47 +  onDataAvailable: function(request, context, stream, offset, count) {
    1.48 +    this._buffer = this._buffer.concat(read_stream(stream, count));
    1.49 +  },
    1.50 +
    1.51 +  onStopRequest: function(request, context, status) {
    1.52 +    do_check_eq(this._buffer, this._expectVal);
    1.53 +    do_timeout(0, run_next_test);
    1.54 +  },
    1.55 +
    1.56 +  run: function() {
    1.57 +    var channel = make_channel();
    1.58 +    channel.loadFlags = this._flags;
    1.59 +    channel.setRequestHeader(VALUE_HDR_NAME, this._sendVal, false);
    1.60 +    channel.setRequestHeader(VARY_HDR_NAME, this._varyHdr, false);
    1.61 +    if (this._cacheHdr)
    1.62 +        channel.setRequestHeader(CACHECTRL_HDR_NAME, this._cacheHdr, false);
    1.63 +
    1.64 +    channel.asyncOpen(this, null);
    1.65 +  }
    1.66 +};
    1.67 +
    1.68 +var gTests = [
    1.69 +// Test LOAD_FROM_CACHE: Load cache-entry
    1.70 +  new Test(Ci.nsIRequest.LOAD_NORMAL,
    1.71 +          "entity-initial", // hdr-value used to vary
    1.72 +          "request1", // echoed by handler
    1.73 +          "request1"  // value expected to receive in channel
    1.74 +          ),
    1.75 +// Verify that it was cached
    1.76 +  new Test(Ci.nsIRequest.LOAD_NORMAL,
    1.77 +          "entity-initial", // hdr-value used to vary
    1.78 +          "fresh value with LOAD_NORMAL",  // echoed by handler
    1.79 +          "request1"  // value expected to receive in channel
    1.80 +          ),
    1.81 +// Load same entity with LOAD_FROM_CACHE-flag
    1.82 +  new Test(Ci.nsIRequest.LOAD_FROM_CACHE,
    1.83 +          "entity-initial", // hdr-value used to vary
    1.84 +          "fresh value with LOAD_FROM_CACHE",  // echoed by handler
    1.85 +          "request1"  // value expected to receive in channel
    1.86 +          ),
    1.87 +// Load different entity with LOAD_FROM_CACHE-flag
    1.88 +  new Test(Ci.nsIRequest.LOAD_FROM_CACHE,
    1.89 +          "entity-l-f-c", // hdr-value used to vary
    1.90 +          "request2", // echoed by handler
    1.91 +          "request2"  // value expected to receive in channel
    1.92 +          ),
    1.93 +// Verify that new value was cached
    1.94 +  new Test(Ci.nsIRequest.LOAD_NORMAL,
    1.95 +          "entity-l-f-c", // hdr-value used to vary
    1.96 +          "fresh value with LOAD_NORMAL",  // echoed by handler
    1.97 +          "request2"  // value expected to receive in channel
    1.98 +          ),
    1.99 +
   1.100 +// Test VALIDATE_NEVER: Note previous cache-entry
   1.101 +  new Test(Ci.nsIRequest.VALIDATE_NEVER,
   1.102 +          "entity-v-n", // hdr-value used to vary
   1.103 +          "request3",  // echoed by handler
   1.104 +          "request3"  // value expected to receive in channel
   1.105 +          ),
   1.106 +// Verify that cache-entry was replaced
   1.107 +  new Test(Ci.nsIRequest.LOAD_NORMAL,
   1.108 +          "entity-v-n", // hdr-value used to vary
   1.109 +          "fresh value with LOAD_NORMAL",  // echoed by handler
   1.110 +          "request3"  // value expected to receive in channel
   1.111 +          ),
   1.112 +
   1.113 +// Test combination VALIDATE_NEVER && no-store: Load new cache-entry
   1.114 +  new Test(Ci.nsIRequest.LOAD_NORMAL,
   1.115 +          "entity-2",// hdr-value used to vary
   1.116 +          "request4",  // echoed by handler
   1.117 +          "request4",  // value expected to receive in channel
   1.118 +          "no-store"   // set no-store on response
   1.119 +          ),
   1.120 +// Ensure we validate without IMS header in this case (verified in handler)
   1.121 +  new Test(Ci.nsIRequest.VALIDATE_NEVER,
   1.122 +          "entity-2-v-n",// hdr-value used to vary
   1.123 +          "request5",  // echoed by handler
   1.124 +          "request5"  // value expected to receive in channel
   1.125 +          ),
   1.126 +
   1.127 +// Test VALIDATE-ALWAYS: Load new entity
   1.128 +  new Test(Ci.nsIRequest.LOAD_NORMAL,
   1.129 +          "entity-3",// hdr-value used to vary
   1.130 +          "request6",  // echoed by handler
   1.131 +          "request6",  // value expected to receive in channel
   1.132 +          "no-cache"   // set no-cache on response
   1.133 +          ),
   1.134 +// Ensure we don't send IMS header also in this case (verified in handler)
   1.135 +  new Test(Ci.nsIRequest.VALIDATE_ALWAYS,
   1.136 +          "entity-3-v-a",// hdr-value used to vary
   1.137 +          "request7",  // echoed by handler
   1.138 +          "request7"  // value expected to receive in channel
   1.139 +          ),
   1.140 +  ];
   1.141 +
   1.142 +function run_next_test()
   1.143 +{
   1.144 +  if (gTests.length == 0) {
   1.145 +    httpserver.stop(do_test_finished);
   1.146 +    return;
   1.147 +  }
   1.148 +
   1.149 +  var test = gTests.shift();
   1.150 +  test.run();
   1.151 +}
   1.152 +
   1.153 +function handler(metadata, response) {
   1.154 +
   1.155 +  // None of the tests above should send an IMS
   1.156 +  do_check_false(metadata.hasHeader("If-Modified-Since"));
   1.157 +
   1.158 +  // Pick up requested value to echo
   1.159 +  var hdr = "default value";
   1.160 +  try {
   1.161 +    hdr = metadata.getHeader(VALUE_HDR_NAME);
   1.162 +  } catch(ex) { }
   1.163 +
   1.164 +  // Pick up requested cache-control header-value
   1.165 +  var cctrlVal = "max-age=10000";
   1.166 +  try {
   1.167 +      cctrlVal = metadata.getHeader(CACHECTRL_HDR_NAME);
   1.168 +  } catch(ex) { }
   1.169 +
   1.170 +  response.setStatusLine(metadata.httpVersion, 200, "OK");
   1.171 +  response.setHeader("Content-Type", "text/plain", false);
   1.172 +  response.setHeader("Cache-Control", cctrlVal, false);
   1.173 +  response.setHeader("Vary", VARY_HDR_NAME, false);
   1.174 +  response.setHeader("Last-Modified", "Tue, 15 Nov 1994 12:45:26 GMT", false);
   1.175 +  response.bodyOutputStream.write(hdr, hdr.length);
   1.176 +}
   1.177 +
   1.178 +function run_test() {
   1.179 +
   1.180 +  // clear the cache
   1.181 +  evict_cache_entries();
   1.182 +
   1.183 +  httpserver = new HttpServer();
   1.184 +  httpserver.registerPathHandler("/bug633743", handler);
   1.185 +  httpserver.start(-1);
   1.186 +
   1.187 +  run_next_test();
   1.188 +  do_test_pending();
   1.189 +}

mercurial