test_bug1064258.js (4633B)
1 /** 2 * Check how nsICachingChannel.cacheOnlyMetadata works. 3 * - all channels involved in this test are set cacheOnlyMetadata = true 4 * - do a previously uncached request for a long living content 5 * - check we have downloaded the content from the server (channel provides it) 6 * - check the entry has metadata, but zero-length content 7 * - load the same URL again, now cached 8 * - check the channel is giving no content (no call to OnDataAvailable) but succeeds 9 * - repeat again, but for a different URL that is not cached (immediately expires) 10 * - only difference is that we get a newer version of the content from the server during the second request 11 */ 12 13 "use strict"; 14 15 const { HttpServer } = ChromeUtils.importESModule( 16 "resource://testing-common/httpd.sys.mjs" 17 ); 18 19 ChromeUtils.defineLazyGetter(this, "URL", function () { 20 return "http://localhost:" + httpServer.identity.primaryPort; 21 }); 22 23 var httpServer = null; 24 25 function make_channel(url) { 26 return NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true }); 27 } 28 29 const responseBody1 = "response body 1"; 30 const responseBody2a = "response body 2a"; 31 const responseBody2b = "response body 2b"; 32 33 function contentHandler1(metadata, response) { 34 response.setHeader("Content-Type", "text/plain"); 35 response.setHeader("Cache-control", "max-age=999999"); 36 response.bodyOutputStream.write(responseBody1, responseBody1.length); 37 } 38 39 var content2passCount = 0; 40 41 function contentHandler2(metadata, response) { 42 response.setHeader("Content-Type", "text/plain"); 43 response.setHeader("Cache-control", "no-cache"); 44 switch (content2passCount++) { 45 case 0: 46 response.setHeader("ETag", "testetag"); 47 response.bodyOutputStream.write(responseBody2a, responseBody2a.length); 48 break; 49 case 1: 50 Assert.ok(metadata.hasHeader("If-None-Match")); 51 Assert.equal(metadata.getHeader("If-None-Match"), "testetag"); 52 response.bodyOutputStream.write(responseBody2b, responseBody2b.length); 53 break; 54 default: 55 throw new Error("Unexpected request in the test"); 56 } 57 } 58 59 function run_test() { 60 httpServer = new HttpServer(); 61 httpServer.registerPathHandler("/content1", contentHandler1); 62 httpServer.registerPathHandler("/content2", contentHandler2); 63 httpServer.start(-1); 64 65 run_test_content1a(); 66 do_test_pending(); 67 } 68 69 function run_test_content1a() { 70 var chan = make_channel(URL + "/content1"); 71 let caching = chan.QueryInterface(Ci.nsICachingChannel); 72 caching.cacheOnlyMetadata = true; 73 chan.asyncOpen(new ChannelListener(contentListener1a, null)); 74 } 75 76 function contentListener1a(request, buffer) { 77 Assert.equal(buffer, responseBody1); 78 79 asyncOpenCacheEntry(URL + "/content1", "disk", 0, null, cacheCheck1); 80 } 81 82 function cacheCheck1(status, entry) { 83 Assert.equal(status, 0); 84 Assert.equal(entry.dataSize, 0); 85 try { 86 Assert.notEqual(entry.getMetaDataElement("response-head"), null); 87 } catch (ex) { 88 do_throw("Missing response head"); 89 } 90 91 var chan = make_channel(URL + "/content1"); 92 let caching = chan.QueryInterface(Ci.nsICachingChannel); 93 caching.cacheOnlyMetadata = true; 94 chan.asyncOpen(new ChannelListener(contentListener1b, null, CL_IGNORE_CL)); 95 } 96 97 function contentListener1b(request, buffer) { 98 request.QueryInterface(Ci.nsIHttpChannel); 99 Assert.equal(request.requestMethod, "GET"); 100 Assert.equal(request.responseStatus, 200); 101 Assert.equal(request.getResponseHeader("Cache-control"), "max-age=999999"); 102 103 Assert.equal(buffer, ""); 104 run_test_content2a(); 105 } 106 107 // Now same set of steps but this time for an immediately expiring content. 108 109 function run_test_content2a() { 110 var chan = make_channel(URL + "/content2"); 111 let caching = chan.QueryInterface(Ci.nsICachingChannel); 112 caching.cacheOnlyMetadata = true; 113 chan.asyncOpen(new ChannelListener(contentListener2a, null)); 114 } 115 116 function contentListener2a(request, buffer) { 117 Assert.equal(buffer, responseBody2a); 118 119 asyncOpenCacheEntry(URL + "/content2", "disk", 0, null, cacheCheck2); 120 } 121 122 function cacheCheck2(status, entry) { 123 Assert.equal(status, 0); 124 Assert.equal(entry.dataSize, 0); 125 try { 126 Assert.notEqual(entry.getMetaDataElement("response-head"), null); 127 Assert.ok( 128 entry.getMetaDataElement("response-head").match("etag: testetag") 129 ); 130 } catch (ex) { 131 do_throw("Missing response head"); 132 } 133 134 var chan = make_channel(URL + "/content2"); 135 let caching = chan.QueryInterface(Ci.nsICachingChannel); 136 caching.cacheOnlyMetadata = true; 137 chan.asyncOpen(new ChannelListener(contentListener2b, null)); 138 } 139 140 function contentListener2b(request, buffer) { 141 Assert.equal(buffer, responseBody2b); 142 143 httpServer.stop(do_test_finished); 144 }