netwerk/test/unit/test_bug618835.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/unit/test_bug618835.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,116 @@
     1.4 +//
     1.5 +// If a response to a non-safe HTTP request-method contains the Location- or
     1.6 +// Content-Location header, we must make sure to invalidate any cached entry
     1.7 +// representing the URIs pointed to by either header. RFC 2616 section 13.10
     1.8 +//
     1.9 +// This test uses 3 URIs: "/post" is the target of a POST-request and always
    1.10 +// redirects (301) to "/redirect". The URIs "/redirect" and "/cl" both counts
    1.11 +// the number of loads from the server (handler). The response from "/post"
    1.12 +// always contains the headers "Location: /redirect" and "Content-Location:
    1.13 +// /cl", whose cached entries are to be invalidated. The tests verifies that
    1.14 +// "/redirect" and "/cl" are loaded from server the expected number of times.
    1.15 +//
    1.16 +
    1.17 +Cu.import("resource://testing-common/httpd.js");
    1.18 +
    1.19 +var httpserv;
    1.20 +
    1.21 +function setupChannel(path) {
    1.22 +    var ios =
    1.23 +        Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
    1.24 +    return chan = ios.newChannel(path, "", null)
    1.25 +                                .QueryInterface(Ci.nsIHttpChannel);
    1.26 +}
    1.27 +
    1.28 +// Verify that Content-Location-URI has been loaded once, load post_target
    1.29 +function InitialListener() { }
    1.30 +InitialListener.prototype = {
    1.31 +    onStartRequest: function(request, context) { },
    1.32 +    onStopRequest: function(request, context, status) {
    1.33 +        do_check_eq(1, numberOfCLHandlerCalls);
    1.34 +        do_execute_soon(function() {
    1.35 +            var channel = setupChannel("http://localhost:" +
    1.36 +                                       httpserv.identity.primaryPort + "/post");
    1.37 +            channel.requestMethod = "POST";
    1.38 +            channel.asyncOpen(new RedirectingListener(), null);
    1.39 +        });
    1.40 +    }
    1.41 +};
    1.42 +
    1.43 +// Verify that Location-URI has been loaded once, reload post_target
    1.44 +function RedirectingListener() { }
    1.45 +RedirectingListener.prototype = {
    1.46 +    onStartRequest: function(request, context) { },
    1.47 +    onStopRequest: function(request, context, status) {
    1.48 +        do_check_eq(1, numberOfHandlerCalls);
    1.49 +        do_execute_soon(function() {
    1.50 +            var channel = setupChannel("http://localhost:" +
    1.51 +                                       httpserv.identity.primaryPort + "/post");
    1.52 +            channel.requestMethod = "POST";
    1.53 +            channel.asyncOpen(new VerifyingListener(), null);
    1.54 +        });
    1.55 +    }
    1.56 +};
    1.57 +
    1.58 +// Verify that Location-URI has been loaded twice (cached entry invalidated),
    1.59 +// reload Content-Location-URI
    1.60 +function VerifyingListener() { }
    1.61 +VerifyingListener.prototype = {
    1.62 +    onStartRequest: function(request, context) { },
    1.63 +    onStopRequest: function(request, context, status) {
    1.64 +        do_check_eq(2, numberOfHandlerCalls);
    1.65 +        var channel = setupChannel("http://localhost:" +
    1.66 +                                   httpserv.identity.primaryPort + "/cl");
    1.67 +        channel.asyncOpen(new FinalListener(), null);
    1.68 +    }
    1.69 +};
    1.70 +
    1.71 +// Verify that Location-URI has been loaded twice (cached entry invalidated),
    1.72 +// stop test
    1.73 +function FinalListener() { }
    1.74 +FinalListener.prototype = {
    1.75 +    onStartRequest: function(request, context) { },
    1.76 +    onStopRequest: function(request, context, status) {
    1.77 +        do_check_eq(2, numberOfCLHandlerCalls);
    1.78 +        httpserv.stop(do_test_finished);
    1.79 +    }
    1.80 +};
    1.81 +
    1.82 +function run_test() {
    1.83 +  httpserv = new HttpServer();
    1.84 +  httpserv.registerPathHandler("/cl", content_location);
    1.85 +  httpserv.registerPathHandler("/post", post_target);
    1.86 +  httpserv.registerPathHandler("/redirect", redirect_target);
    1.87 +  httpserv.start(-1);
    1.88 +
    1.89 +  // Clear cache
    1.90 +  evict_cache_entries();
    1.91 +
    1.92 +  // Load Content-Location URI into cache and start the chain of loads
    1.93 +  var channel = setupChannel("http://localhost:" +
    1.94 +                             httpserv.identity.primaryPort + "/cl");
    1.95 +  channel.asyncOpen(new InitialListener(), null);
    1.96 +
    1.97 +  do_test_pending();
    1.98 +}
    1.99 +
   1.100 +var numberOfCLHandlerCalls = 0;
   1.101 +function content_location(metadata, response) {
   1.102 +    numberOfCLHandlerCalls++;
   1.103 +    response.setStatusLine(metadata.httpVersion, 200, "Ok");
   1.104 +    response.setHeader("Cache-Control", "max-age=360000", false);
   1.105 +}
   1.106 +
   1.107 +function post_target(metadata, response) {
   1.108 +    response.setStatusLine(metadata.httpVersion, 301, "Moved Permanently");
   1.109 +    response.setHeader("Location", "/redirect", false);
   1.110 +    response.setHeader("Content-Location", "/cl", false);
   1.111 +    response.setHeader("Cache-Control", "max-age=360000", false);
   1.112 +}
   1.113 +
   1.114 +var numberOfHandlerCalls = 0;
   1.115 +function redirect_target(metadata, response) {
   1.116 +    numberOfHandlerCalls++;
   1.117 +    response.setStatusLine(metadata.httpVersion, 200, "Ok");
   1.118 +    response.setHeader("Cache-Control", "max-age=360000", false);
   1.119 +}

mercurial