netwerk/test/unit/test_304_responses.js

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

     1 "use strict";
     2 // https://bugzilla.mozilla.org/show_bug.cgi?id=761228
     4 Cu.import("resource://testing-common/httpd.js");
     6 XPCOMUtils.defineLazyGetter(this, "URL", function() {
     7   return "http://localhost:" + httpServer.identity.primaryPort;
     8 });
    10 var httpServer = null;
    11 const testFileName = "test_customConditionalRequest_304";
    12 const basePath = "/" + testFileName + "/";
    14 XPCOMUtils.defineLazyGetter(this, "baseURI", function() {
    15   return URL + basePath;
    16 });
    18 const unexpected304 = "unexpected304";
    19 const existingCached304 = "existingCached304";
    21 function make_uri(url) {
    22   var ios = Cc["@mozilla.org/network/io-service;1"].
    23             getService(Ci.nsIIOService);
    24   return ios.newURI(url, null, null);
    25 }
    27 function make_channel(url) {
    28   var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
    29   var chan = ios.newChannel(url, null, null).QueryInterface(Ci.nsIHttpChannel);
    30   return chan;
    31 }
    33 function clearCache() {
    34     var service = Components.classes["@mozilla.org/netwerk/cache-storage-service;1"]
    35         .getService(Ci.nsICacheStorageService);
    36     service.clear();
    37 }
    39 function alwaysReturn304Handler(metadata, response) {
    40   response.setStatusLine(metadata.httpVersion, 304, "Not Modified");
    41   response.setHeader("Returned-From-Handler", "1");
    42 }
    44 function run_test() {
    45   evict_cache_entries();
    47   httpServer = new HttpServer();
    48   httpServer.registerPathHandler(basePath + unexpected304,
    49                                  alwaysReturn304Handler);
    50   httpServer.registerPathHandler(basePath + existingCached304,
    51                                  alwaysReturn304Handler);
    52   httpServer.start(-1);
    53   run_next_test();
    54 }
    56 function finish_test(request, buffer) {
    57   httpServer.stop(do_test_finished);
    58 }
    60 function consume304(request, buffer) {
    61   request.QueryInterface(Components.interfaces.nsIHttpChannel);
    62   do_check_eq(request.responseStatus, 304);
    63   do_check_eq(request.getResponseHeader("Returned-From-Handler"), "1");
    64   run_next_test();
    65 }
    67 // Test that we return a 304 response to the caller when we are not expecting
    68 // a 304 response (i.e. when the server shouldn't have sent us one).
    69 add_test(function test_unexpected_304() {
    70   var chan = make_channel(baseURI + unexpected304);
    71   chan.asyncOpen(new ChannelListener(consume304, null), null);
    72 });
    74 // Test that we can cope with a 304 response that was (erroneously) stored in
    75 // the cache.
    76 add_test(function test_304_stored_in_cache() {
    77   asyncOpenCacheEntry(
    78     baseURI + existingCached304, "disk", Ci.nsICacheStorage.OPEN_NORMALLY, null,
    79     function (entryStatus, cacheEntry) {
    80       cacheEntry.setMetaDataElement("request-method", "GET");
    81       cacheEntry.setMetaDataElement("response-head",
    82                                     "HTTP/1.1 304 Not Modified\r\n" +
    83                                     "\r\n");
    84       cacheEntry.metaDataReady();
    85       cacheEntry.close();
    87       var chan = make_channel(baseURI + existingCached304);
    89       // make it a custom conditional request
    90       chan.QueryInterface(Components.interfaces.nsIHttpChannel);
    91       chan.setRequestHeader("If-None-Match", '"foo"', false);
    93       chan.asyncOpen(new ChannelListener(consume304, null), null);
    94     });
    95 });

mercurial