test_redirect-caching_canceled.js (1969B)
1 "use strict"; 2 3 const { HttpServer } = ChromeUtils.importESModule( 4 "resource://testing-common/httpd.sys.mjs" 5 ); 6 7 ChromeUtils.defineLazyGetter(this, "URL", function () { 8 return "http://localhost:" + httpServer.identity.primaryPort; 9 }); 10 11 var httpServer = null; 12 // Need to randomize, because apparently no one clears our cache 13 var randomPath = "/redirect/" + Math.random(); 14 15 ChromeUtils.defineLazyGetter(this, "randomURI", function () { 16 return URL + randomPath; 17 }); 18 19 function make_channel(url) { 20 return NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true }); 21 } 22 23 const responseBody = "response body"; 24 25 function redirectHandler(metadata, response) { 26 response.setStatusLine(metadata.httpVersion, 301, "Moved"); 27 response.setHeader("Location", URL + "/content", false); 28 response.setHeader("Cache-control", "max-age=1000", false); 29 } 30 31 function contentHandler(metadata, response) { 32 response.setHeader("Content-Type", "text/plain"); 33 response.bodyOutputStream.write(responseBody, responseBody.length); 34 } 35 36 function firstTimeThrough(request, buffer) { 37 Assert.equal(buffer, responseBody); 38 var chan = make_channel(randomURI); 39 chan.asyncOpen(new ChannelListener(secondTimeThrough, null)); 40 } 41 42 function secondTimeThrough(request, buffer) { 43 Assert.equal(buffer, responseBody); 44 var chan = make_channel(randomURI); 45 chan.loadFlags |= Ci.nsIRequest.LOAD_FROM_CACHE; 46 chan.notificationCallbacks = new ChannelEventSink(ES_ABORT_REDIRECT); 47 chan.asyncOpen(new ChannelListener(finish_test, null, CL_EXPECT_FAILURE)); 48 } 49 50 function finish_test(request, buffer) { 51 Assert.equal(buffer, ""); 52 httpServer.stop(do_test_finished); 53 } 54 55 function run_test() { 56 httpServer = new HttpServer(); 57 httpServer.registerPathHandler(randomPath, redirectHandler); 58 httpServer.registerPathHandler("/content", contentHandler); 59 httpServer.start(-1); 60 61 var chan = make_channel(randomURI); 62 chan.asyncOpen(new ChannelListener(firstTimeThrough, null)); 63 do_test_pending(); 64 }