netwerk/test/unit/test_304_responses.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/unit/test_304_responses.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,95 @@
     1.4 +"use strict";
     1.5 +// https://bugzilla.mozilla.org/show_bug.cgi?id=761228
     1.6 +
     1.7 +Cu.import("resource://testing-common/httpd.js");
     1.8 +
     1.9 +XPCOMUtils.defineLazyGetter(this, "URL", function() {
    1.10 +  return "http://localhost:" + httpServer.identity.primaryPort;
    1.11 +});
    1.12 +
    1.13 +var httpServer = null;
    1.14 +const testFileName = "test_customConditionalRequest_304";
    1.15 +const basePath = "/" + testFileName + "/";
    1.16 +
    1.17 +XPCOMUtils.defineLazyGetter(this, "baseURI", function() {
    1.18 +  return URL + basePath;
    1.19 +});
    1.20 +
    1.21 +const unexpected304 = "unexpected304";
    1.22 +const existingCached304 = "existingCached304";
    1.23 +
    1.24 +function make_uri(url) {
    1.25 +  var ios = Cc["@mozilla.org/network/io-service;1"].
    1.26 +            getService(Ci.nsIIOService);
    1.27 +  return ios.newURI(url, null, null);
    1.28 +}
    1.29 +
    1.30 +function make_channel(url) {
    1.31 +  var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
    1.32 +  var chan = ios.newChannel(url, null, null).QueryInterface(Ci.nsIHttpChannel);
    1.33 +  return chan;
    1.34 +}
    1.35 +
    1.36 +function clearCache() {
    1.37 +    var service = Components.classes["@mozilla.org/netwerk/cache-storage-service;1"]
    1.38 +        .getService(Ci.nsICacheStorageService);
    1.39 +    service.clear();
    1.40 +}
    1.41 +
    1.42 +function alwaysReturn304Handler(metadata, response) {
    1.43 +  response.setStatusLine(metadata.httpVersion, 304, "Not Modified");
    1.44 +  response.setHeader("Returned-From-Handler", "1");
    1.45 +}
    1.46 +
    1.47 +function run_test() {
    1.48 +  evict_cache_entries();
    1.49 +
    1.50 +  httpServer = new HttpServer();
    1.51 +  httpServer.registerPathHandler(basePath + unexpected304,
    1.52 +                                 alwaysReturn304Handler);
    1.53 +  httpServer.registerPathHandler(basePath + existingCached304,
    1.54 +                                 alwaysReturn304Handler);
    1.55 +  httpServer.start(-1);
    1.56 +  run_next_test();
    1.57 +}
    1.58 +
    1.59 +function finish_test(request, buffer) {
    1.60 +  httpServer.stop(do_test_finished);
    1.61 +}
    1.62 +
    1.63 +function consume304(request, buffer) {
    1.64 +  request.QueryInterface(Components.interfaces.nsIHttpChannel);
    1.65 +  do_check_eq(request.responseStatus, 304);
    1.66 +  do_check_eq(request.getResponseHeader("Returned-From-Handler"), "1");
    1.67 +  run_next_test();
    1.68 +}
    1.69 +
    1.70 +// Test that we return a 304 response to the caller when we are not expecting
    1.71 +// a 304 response (i.e. when the server shouldn't have sent us one).
    1.72 +add_test(function test_unexpected_304() {
    1.73 +  var chan = make_channel(baseURI + unexpected304);
    1.74 +  chan.asyncOpen(new ChannelListener(consume304, null), null);
    1.75 +});
    1.76 +
    1.77 +// Test that we can cope with a 304 response that was (erroneously) stored in
    1.78 +// the cache.
    1.79 +add_test(function test_304_stored_in_cache() {
    1.80 +  asyncOpenCacheEntry(
    1.81 +    baseURI + existingCached304, "disk", Ci.nsICacheStorage.OPEN_NORMALLY, null,
    1.82 +    function (entryStatus, cacheEntry) {
    1.83 +      cacheEntry.setMetaDataElement("request-method", "GET");
    1.84 +      cacheEntry.setMetaDataElement("response-head",
    1.85 +                                    "HTTP/1.1 304 Not Modified\r\n" +
    1.86 +                                    "\r\n");
    1.87 +      cacheEntry.metaDataReady();
    1.88 +      cacheEntry.close();
    1.89 +
    1.90 +      var chan = make_channel(baseURI + existingCached304);
    1.91 +
    1.92 +      // make it a custom conditional request
    1.93 +      chan.QueryInterface(Components.interfaces.nsIHttpChannel);
    1.94 +      chan.setRequestHeader("If-None-Match", '"foo"', false);
    1.95 +
    1.96 +      chan.asyncOpen(new ChannelListener(consume304, null), null);
    1.97 +    });
    1.98 +});

mercurial