netwerk/test/unit/test_bug482601.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/unit/test_bug482601.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,233 @@
     1.4 +Cu.import("resource://testing-common/httpd.js");
     1.5 +
     1.6 +var httpserv = null;
     1.7 +var test_nr = 0;
     1.8 +var observers_called = "";
     1.9 +var handlers_called = "";
    1.10 +var buffer = "";
    1.11 +
    1.12 +var observer = {
    1.13 +  QueryInterface: function (aIID) {
    1.14 +    if (aIID.equals(Ci.nsISupports) ||
    1.15 +        aIID.equals(Ci.nsIObserver))
    1.16 +      return this;
    1.17 +    throw Cr.NS_ERROR_NO_INTERFACE;
    1.18 +  },
    1.19 +
    1.20 +  observe: function(subject, topic, data) {
    1.21 +    if (observers_called.length)
    1.22 +      observers_called += ",";
    1.23 +
    1.24 +    observers_called += topic;
    1.25 +  }
    1.26 +};
    1.27 +
    1.28 +var listener = {
    1.29 +  onStartRequest: function (request, ctx) {
    1.30 +    buffer = "";
    1.31 +  },
    1.32 +
    1.33 +  onDataAvailable: function (request, ctx, stream, offset, count) {
    1.34 +    buffer = buffer.concat(read_stream(stream, count));
    1.35 +  },
    1.36 +
    1.37 +  onStopRequest: function (request, ctx, status) {
    1.38 +    do_check_eq(status, Cr.NS_OK);
    1.39 +    do_check_eq(buffer, "0123456789");
    1.40 +    do_check_eq(observers_called, results[test_nr]);
    1.41 +    test_nr++;
    1.42 +    do_timeout(0, do_test);
    1.43 +  }
    1.44 +};
    1.45 +
    1.46 +function run_test() {
    1.47 +  httpserv = new HttpServer();
    1.48 +  httpserv.registerPathHandler("/bug482601/nocache", bug482601_nocache);
    1.49 +  httpserv.registerPathHandler("/bug482601/partial", bug482601_partial);
    1.50 +  httpserv.registerPathHandler("/bug482601/cached", bug482601_cached);
    1.51 +  httpserv.registerPathHandler("/bug482601/only_from_cache", bug482601_only_from_cache);
    1.52 +  httpserv.start(-1);
    1.53 +
    1.54 +  var obs = Cc["@mozilla.org/observer-service;1"].getService();
    1.55 +  obs = obs.QueryInterface(Ci.nsIObserverService);
    1.56 +  obs.addObserver(observer, "http-on-examine-response", false);
    1.57 +  obs.addObserver(observer, "http-on-examine-merged-response", false);
    1.58 +  obs.addObserver(observer, "http-on-examine-cached-response", false);
    1.59 +
    1.60 +  do_timeout(0, do_test);
    1.61 +  do_test_pending();
    1.62 +}
    1.63 +
    1.64 +function do_test() {
    1.65 +  if (test_nr < tests.length) {
    1.66 +    tests[test_nr]();
    1.67 +  }
    1.68 +  else {
    1.69 +    do_check_eq(handlers_called, "nocache,partial,cached");
    1.70 +    httpserv.stop(do_test_finished);
    1.71 +  }
    1.72 +}
    1.73 +
    1.74 +var tests = [test_nocache,
    1.75 +             test_partial,
    1.76 +             test_cached,
    1.77 +             test_only_from_cache];
    1.78 +
    1.79 +var results = ["http-on-examine-response",
    1.80 +               "http-on-examine-response,http-on-examine-merged-response",
    1.81 +               "http-on-examine-response,http-on-examine-merged-response",
    1.82 +               "http-on-examine-cached-response"];
    1.83 +
    1.84 +function makeChan(url) {
    1.85 +  var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
    1.86 +  var chan = ios.newChannel(url, null, null).QueryInterface(Ci.nsIHttpChannel);
    1.87 +  return chan;
    1.88 +}
    1.89 +
    1.90 +function storeCache(aCacheEntry, aResponseHeads, aContent) {
    1.91 +  aCacheEntry.setMetaDataElement("request-method", "GET");
    1.92 +  aCacheEntry.setMetaDataElement("response-head", aResponseHeads);
    1.93 +  aCacheEntry.setMetaDataElement("charset", "ISO-8859-1");
    1.94 +
    1.95 +  var oStream = aCacheEntry.openOutputStream(0);
    1.96 +  var written = oStream.write(aContent, aContent.length);
    1.97 +  if (written != aContent.length) {
    1.98 +    do_throw("oStream.write has not written all data!\n" +
    1.99 +             "  Expected: " + written  + "\n" +
   1.100 +             "  Actual: " + aContent.length + "\n");
   1.101 +  }
   1.102 +  oStream.close();
   1.103 +  aCacheEntry.close();
   1.104 +}
   1.105 +
   1.106 +function test_nocache() {
   1.107 +  observers_called = "";
   1.108 +
   1.109 +  var chan = makeChan("http://localhost:" + httpserv.identity.primaryPort +
   1.110 +                      "/bug482601/nocache");
   1.111 +  chan.asyncOpen(listener, null);
   1.112 +}
   1.113 +
   1.114 +function test_partial() {
   1.115 +   asyncOpenCacheEntry("http://localhost:" + httpserv.identity.primaryPort +
   1.116 +                       "/bug482601/partial",
   1.117 +                       "disk", Ci.nsICacheStorage.OPEN_NORMALLY, null,
   1.118 +                       test_partial2);
   1.119 +}
   1.120 +
   1.121 +function test_partial2(status, entry) {
   1.122 +  do_check_eq(status, Cr.NS_OK);
   1.123 +  storeCache(entry,
   1.124 +             "HTTP/1.1 200 OK\r\n" +
   1.125 +             "Date: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
   1.126 +             "Server: httpd.js\r\n" +
   1.127 +             "Last-Modified: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
   1.128 +             "Accept-Ranges: bytes\r\n" +
   1.129 +             "Content-Length: 10\r\n" +
   1.130 +             "Content-Type: text/plain\r\n",
   1.131 +             "0123");
   1.132 +
   1.133 +  observers_called = "";
   1.134 +
   1.135 +  var chan = makeChan("http://localhost:" + httpserv.identity.primaryPort +
   1.136 +                      "/bug482601/partial");
   1.137 +  chan.asyncOpen(listener, null);
   1.138 +}
   1.139 +
   1.140 +function test_cached() {
   1.141 +   asyncOpenCacheEntry("http://localhost:" + httpserv.identity.primaryPort +
   1.142 +                       "/bug482601/cached",
   1.143 +                       "disk", Ci.nsICacheStorage.OPEN_NORMALLY, null,
   1.144 +                       test_cached2);
   1.145 +}
   1.146 +
   1.147 +function test_cached2(status, entry) {
   1.148 +  do_check_eq(status, Cr.NS_OK);
   1.149 +  storeCache(entry,
   1.150 +             "HTTP/1.1 200 OK\r\n" +
   1.151 +             "Date: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
   1.152 +             "Server: httpd.js\r\n" +
   1.153 +             "Last-Modified: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
   1.154 +             "Accept-Ranges: bytes\r\n" +
   1.155 +             "Content-Length: 10\r\n" +
   1.156 +             "Content-Type: text/plain\r\n",
   1.157 +             "0123456789");
   1.158 +
   1.159 +  observers_called = "";
   1.160 +
   1.161 +  var chan = makeChan("http://localhost:" + httpserv.identity.primaryPort +
   1.162 +                      "/bug482601/cached");
   1.163 +  chan.loadFlags = Ci.nsIRequest.VALIDATE_ALWAYS;
   1.164 +  chan.asyncOpen(listener, null);
   1.165 +}
   1.166 +
   1.167 +function test_only_from_cache() {
   1.168 +   asyncOpenCacheEntry("http://localhost:" + httpserv.identity.primaryPort +
   1.169 +                       "/bug482601/only_from_cache",
   1.170 +                       "disk", Ci.nsICacheStorage.OPEN_NORMALLY, null,
   1.171 +                       test_only_from_cache2);
   1.172 +}
   1.173 +
   1.174 +function test_only_from_cache2(status, entry) {
   1.175 +  do_check_eq(status, Cr.NS_OK);
   1.176 +  storeCache(entry,
   1.177 +             "HTTP/1.1 200 OK\r\n" +
   1.178 +             "Date: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
   1.179 +             "Server: httpd.js\r\n" +
   1.180 +             "Last-Modified: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
   1.181 +             "Accept-Ranges: bytes\r\n" +
   1.182 +             "Content-Length: 10\r\n" +
   1.183 +             "Content-Type: text/plain\r\n",
   1.184 +             "0123456789");
   1.185 +
   1.186 +  observers_called = "";
   1.187 +
   1.188 +  var chan = makeChan("http://localhost:" + httpserv.identity.primaryPort +
   1.189 +                      "/bug482601/only_from_cache");
   1.190 +  chan.loadFlags = Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE;
   1.191 +  chan.asyncOpen(listener, null);
   1.192 +}
   1.193 +
   1.194 +
   1.195 +// PATHS
   1.196 +
   1.197 +// /bug482601/nocache
   1.198 +function bug482601_nocache(metadata, response) {
   1.199 +  response.setHeader("Content-Type", "text/plain", false);
   1.200 +  var body = "0123456789";
   1.201 +  response.bodyOutputStream.write(body, body.length);
   1.202 +  handlers_called += "nocache";
   1.203 +}
   1.204 +
   1.205 +// /bug482601/partial
   1.206 +function bug482601_partial(metadata, response) {
   1.207 +  do_check_true(metadata.hasHeader("If-Range"));
   1.208 +  do_check_eq(metadata.getHeader("If-Range"),
   1.209 +              "Thu, 1 Jan 2009 00:00:00 GMT");
   1.210 +  do_check_true(metadata.hasHeader("Range"));
   1.211 +  do_check_eq(metadata.getHeader("Range"), "bytes=4-");
   1.212 +
   1.213 +  response.setStatusLine(metadata.httpVersion, 206, "Partial Content");
   1.214 +  response.setHeader("Content-Range", "bytes 4-9/10", false);
   1.215 +  response.setHeader("Content-Type", "text/plain", false);
   1.216 +  response.setHeader("Last-Modified", "Thu, 1 Jan 2009 00:00:00 GMT");
   1.217 +
   1.218 +  var body = "456789";
   1.219 +  response.bodyOutputStream.write(body, body.length);
   1.220 +  handlers_called += ",partial";
   1.221 +}
   1.222 +
   1.223 +// /bug482601/cached
   1.224 +function bug482601_cached(metadata, response) {
   1.225 +  do_check_true(metadata.hasHeader("If-Modified-Since"));
   1.226 +  do_check_eq(metadata.getHeader("If-Modified-Since"),
   1.227 +              "Thu, 1 Jan 2009 00:00:00 GMT");
   1.228 +
   1.229 +  response.setStatusLine(metadata.httpVersion, 304, "Not Modified");
   1.230 +  handlers_called += ",cached";
   1.231 +}
   1.232 +
   1.233 +// /bug482601/only_from_cache
   1.234 +function bug482601_only_from_cache(metadata, response) {
   1.235 +  do_throw("This should not be reached");
   1.236 +}

mercurial