test_redirect_different-protocol.js (1337B)
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 redirectTargetBody = "response body"; 24 const response301Body = "redirect body"; 25 26 function redirectHandler(metadata, response) { 27 response.setStatusLine(metadata.httpVersion, 301, "Moved"); 28 response.bodyOutputStream.write(response301Body, response301Body.length); 29 response.setHeader( 30 "Location", 31 "data:text/plain," + redirectTargetBody, 32 false 33 ); 34 } 35 36 function finish_test(request, buffer) { 37 Assert.equal(buffer, redirectTargetBody); 38 httpServer.stop(do_test_finished); 39 } 40 41 function run_test() { 42 httpServer = new HttpServer(); 43 httpServer.registerPathHandler(randomPath, redirectHandler); 44 httpServer.start(-1); 45 46 var chan = make_channel(randomURI); 47 chan.asyncOpen(new ChannelListener(finish_test, null, 0)); 48 do_test_pending(); 49 }