test_inhibit_caching.js (2495B)
1 "use strict"; 2 3 const { HttpServer } = ChromeUtils.importESModule( 4 "resource://testing-common/httpd.sys.mjs" 5 ); 6 7 var first = true; 8 function contentHandler(metadata, response) { 9 response.setHeader("Content-Type", "text/plain"); 10 var body = "first"; 11 if (!first) { 12 body = "second"; 13 } 14 first = false; 15 response.bodyOutputStream.write(body, body.length); 16 } 17 18 ChromeUtils.defineLazyGetter(this, "uri", function () { 19 return "http://localhost:" + httpserver.identity.primaryPort; 20 }); 21 22 var httpserver = null; 23 24 function run_test() { 25 // setup test 26 httpserver = new HttpServer(); 27 httpserver.registerPathHandler("/test", contentHandler); 28 httpserver.start(-1); 29 30 add_test(test_first_response); 31 add_test(test_inhibit_caching); 32 33 run_next_test(); 34 } 35 36 // Makes a regular request 37 function test_first_response() { 38 var chan = NetUtil.newChannel({ 39 uri: uri + "/test", 40 loadUsingSystemPrincipal: true, 41 }); 42 chan.asyncOpen(new ChannelListener(check_first_response, null)); 43 } 44 45 // Checks that we got the appropriate response 46 function check_first_response(request, buffer) { 47 request.QueryInterface(Ci.nsIHttpChannel); 48 Assert.equal(request.responseStatus, 200); 49 Assert.equal(buffer, "first"); 50 // Open the cache entry to check its contents 51 asyncOpenCacheEntry( 52 uri + "/test", 53 "disk", 54 Ci.nsICacheStorage.OPEN_READONLY, 55 null, 56 cache_entry_callback 57 ); 58 } 59 60 // Checks that the cache entry has the correct contents 61 function cache_entry_callback(status, entry) { 62 equal(status, Cr.NS_OK); 63 var inputStream = entry.openInputStream(0); 64 pumpReadStream(inputStream, function (read) { 65 inputStream.close(); 66 equal(read, "first"); 67 run_next_test(); 68 }); 69 } 70 71 // Makes a request with the INHIBIT_CACHING load flag 72 function test_inhibit_caching() { 73 var chan = NetUtil.newChannel({ 74 uri: uri + "/test", 75 loadUsingSystemPrincipal: true, 76 }); 77 chan.QueryInterface(Ci.nsIRequest).loadFlags |= Ci.nsIRequest.INHIBIT_CACHING; 78 chan.asyncOpen(new ChannelListener(check_second_response, null)); 79 } 80 81 // Checks that we got a different response from the first request 82 function check_second_response(request, buffer) { 83 request.QueryInterface(Ci.nsIHttpChannel); 84 Assert.equal(request.responseStatus, 200); 85 Assert.equal(buffer, "second"); 86 // Checks that the cache entry still contains the content from the first request 87 asyncOpenCacheEntry( 88 uri + "/test", 89 "disk", 90 Ci.nsICacheStorage.OPEN_READONLY, 91 null, 92 cache_entry_callback 93 ); 94 }