test_304_responses.js (2627B)
1 "use strict"; 2 // https://bugzilla.mozilla.org/show_bug.cgi?id=761228 3 4 const { HttpServer } = ChromeUtils.importESModule( 5 "resource://testing-common/httpd.sys.mjs" 6 ); 7 8 ChromeUtils.defineLazyGetter(this, "URL", function () { 9 return "http://localhost:" + httpServer.identity.primaryPort; 10 }); 11 12 var httpServer = null; 13 const testFileName = "test_customConditionalRequest_304"; 14 const basePath = "/" + testFileName + "/"; 15 16 ChromeUtils.defineLazyGetter(this, "baseURI", function () { 17 return URL + basePath; 18 }); 19 20 const unexpected304 = "unexpected304"; 21 const existingCached304 = "existingCached304"; 22 23 function make_channel(url) { 24 return NetUtil.newChannel({ 25 uri: url, 26 loadUsingSystemPrincipal: true, 27 }).QueryInterface(Ci.nsIHttpChannel); 28 } 29 30 function alwaysReturn304Handler(metadata, response) { 31 response.setStatusLine(metadata.httpVersion, 304, "Not Modified"); 32 response.setHeader("Returned-From-Handler", "1"); 33 } 34 35 function run_test() { 36 evict_cache_entries(); 37 38 httpServer = new HttpServer(); 39 httpServer.registerPathHandler( 40 basePath + unexpected304, 41 alwaysReturn304Handler 42 ); 43 httpServer.registerPathHandler( 44 basePath + existingCached304, 45 alwaysReturn304Handler 46 ); 47 httpServer.start(-1); 48 run_next_test(); 49 } 50 51 function consume304(request) { 52 request.QueryInterface(Ci.nsIHttpChannel); 53 Assert.equal(request.responseStatus, 304); 54 Assert.equal(request.getResponseHeader("Returned-From-Handler"), "1"); 55 run_next_test(); 56 } 57 58 // Test that we return a 304 response to the caller when we are not expecting 59 // a 304 response (i.e. when the server shouldn't have sent us one). 60 add_test(function test_unexpected_304() { 61 var chan = make_channel(baseURI + unexpected304); 62 chan.asyncOpen(new ChannelListener(consume304, null)); 63 }); 64 65 // Test that we can cope with a 304 response that was (erroneously) stored in 66 // the cache. 67 add_test(function test_304_stored_in_cache() { 68 asyncOpenCacheEntry( 69 baseURI + existingCached304, 70 "disk", 71 Ci.nsICacheStorage.OPEN_NORMALLY, 72 null, 73 function (entryStatus, cacheEntry) { 74 cacheEntry.setMetaDataElement("request-method", "GET"); 75 cacheEntry.setMetaDataElement( 76 "response-head", 77 // eslint-disable-next-line no-useless-concat 78 "HTTP/1.1 304 Not Modified\r\n" + "\r\n" 79 ); 80 cacheEntry.metaDataReady(); 81 82 var chan = make_channel(baseURI + existingCached304); 83 84 // make it a custom conditional request 85 chan.QueryInterface(Ci.nsIHttpChannel); 86 chan.setRequestHeader("If-None-Match", '"foo"', false); 87 88 chan.asyncOpen(new ChannelListener(consume304, null)); 89 } 90 ); 91 });