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