netwerk/test/unit/test_bug618835.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.)

michael@0 1 //
michael@0 2 // If a response to a non-safe HTTP request-method contains the Location- or
michael@0 3 // Content-Location header, we must make sure to invalidate any cached entry
michael@0 4 // representing the URIs pointed to by either header. RFC 2616 section 13.10
michael@0 5 //
michael@0 6 // This test uses 3 URIs: "/post" is the target of a POST-request and always
michael@0 7 // redirects (301) to "/redirect". The URIs "/redirect" and "/cl" both counts
michael@0 8 // the number of loads from the server (handler). The response from "/post"
michael@0 9 // always contains the headers "Location: /redirect" and "Content-Location:
michael@0 10 // /cl", whose cached entries are to be invalidated. The tests verifies that
michael@0 11 // "/redirect" and "/cl" are loaded from server the expected number of times.
michael@0 12 //
michael@0 13
michael@0 14 Cu.import("resource://testing-common/httpd.js");
michael@0 15
michael@0 16 var httpserv;
michael@0 17
michael@0 18 function setupChannel(path) {
michael@0 19 var ios =
michael@0 20 Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
michael@0 21 return chan = ios.newChannel(path, "", null)
michael@0 22 .QueryInterface(Ci.nsIHttpChannel);
michael@0 23 }
michael@0 24
michael@0 25 // Verify that Content-Location-URI has been loaded once, load post_target
michael@0 26 function InitialListener() { }
michael@0 27 InitialListener.prototype = {
michael@0 28 onStartRequest: function(request, context) { },
michael@0 29 onStopRequest: function(request, context, status) {
michael@0 30 do_check_eq(1, numberOfCLHandlerCalls);
michael@0 31 do_execute_soon(function() {
michael@0 32 var channel = setupChannel("http://localhost:" +
michael@0 33 httpserv.identity.primaryPort + "/post");
michael@0 34 channel.requestMethod = "POST";
michael@0 35 channel.asyncOpen(new RedirectingListener(), null);
michael@0 36 });
michael@0 37 }
michael@0 38 };
michael@0 39
michael@0 40 // Verify that Location-URI has been loaded once, reload post_target
michael@0 41 function RedirectingListener() { }
michael@0 42 RedirectingListener.prototype = {
michael@0 43 onStartRequest: function(request, context) { },
michael@0 44 onStopRequest: function(request, context, status) {
michael@0 45 do_check_eq(1, numberOfHandlerCalls);
michael@0 46 do_execute_soon(function() {
michael@0 47 var channel = setupChannel("http://localhost:" +
michael@0 48 httpserv.identity.primaryPort + "/post");
michael@0 49 channel.requestMethod = "POST";
michael@0 50 channel.asyncOpen(new VerifyingListener(), null);
michael@0 51 });
michael@0 52 }
michael@0 53 };
michael@0 54
michael@0 55 // Verify that Location-URI has been loaded twice (cached entry invalidated),
michael@0 56 // reload Content-Location-URI
michael@0 57 function VerifyingListener() { }
michael@0 58 VerifyingListener.prototype = {
michael@0 59 onStartRequest: function(request, context) { },
michael@0 60 onStopRequest: function(request, context, status) {
michael@0 61 do_check_eq(2, numberOfHandlerCalls);
michael@0 62 var channel = setupChannel("http://localhost:" +
michael@0 63 httpserv.identity.primaryPort + "/cl");
michael@0 64 channel.asyncOpen(new FinalListener(), null);
michael@0 65 }
michael@0 66 };
michael@0 67
michael@0 68 // Verify that Location-URI has been loaded twice (cached entry invalidated),
michael@0 69 // stop test
michael@0 70 function FinalListener() { }
michael@0 71 FinalListener.prototype = {
michael@0 72 onStartRequest: function(request, context) { },
michael@0 73 onStopRequest: function(request, context, status) {
michael@0 74 do_check_eq(2, numberOfCLHandlerCalls);
michael@0 75 httpserv.stop(do_test_finished);
michael@0 76 }
michael@0 77 };
michael@0 78
michael@0 79 function run_test() {
michael@0 80 httpserv = new HttpServer();
michael@0 81 httpserv.registerPathHandler("/cl", content_location);
michael@0 82 httpserv.registerPathHandler("/post", post_target);
michael@0 83 httpserv.registerPathHandler("/redirect", redirect_target);
michael@0 84 httpserv.start(-1);
michael@0 85
michael@0 86 // Clear cache
michael@0 87 evict_cache_entries();
michael@0 88
michael@0 89 // Load Content-Location URI into cache and start the chain of loads
michael@0 90 var channel = setupChannel("http://localhost:" +
michael@0 91 httpserv.identity.primaryPort + "/cl");
michael@0 92 channel.asyncOpen(new InitialListener(), null);
michael@0 93
michael@0 94 do_test_pending();
michael@0 95 }
michael@0 96
michael@0 97 var numberOfCLHandlerCalls = 0;
michael@0 98 function content_location(metadata, response) {
michael@0 99 numberOfCLHandlerCalls++;
michael@0 100 response.setStatusLine(metadata.httpVersion, 200, "Ok");
michael@0 101 response.setHeader("Cache-Control", "max-age=360000", false);
michael@0 102 }
michael@0 103
michael@0 104 function post_target(metadata, response) {
michael@0 105 response.setStatusLine(metadata.httpVersion, 301, "Moved Permanently");
michael@0 106 response.setHeader("Location", "/redirect", false);
michael@0 107 response.setHeader("Content-Location", "/cl", false);
michael@0 108 response.setHeader("Cache-Control", "max-age=360000", false);
michael@0 109 }
michael@0 110
michael@0 111 var numberOfHandlerCalls = 0;
michael@0 112 function redirect_target(metadata, response) {
michael@0 113 numberOfHandlerCalls++;
michael@0 114 response.setStatusLine(metadata.httpVersion, 200, "Ok");
michael@0 115 response.setHeader("Cache-Control", "max-age=360000", false);
michael@0 116 }

mercurial