test_redirect_passing.js (1586B)
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 } 29 30 function contentHandler(metadata, response) { 31 response.setHeader("Content-Type", "text/plain"); 32 response.bodyOutputStream.write(responseBody, responseBody.length); 33 } 34 35 function firstTimeThrough(request, buffer) { 36 Assert.equal(buffer, responseBody); 37 var chan = make_channel(randomURI); 38 chan.asyncOpen(new ChannelListener(finish_test, null)); 39 } 40 41 function finish_test(request, buffer) { 42 Assert.equal(buffer, responseBody); 43 httpServer.stop(do_test_finished); 44 } 45 46 function run_test() { 47 httpServer = new HttpServer(); 48 httpServer.registerPathHandler(randomPath, redirectHandler); 49 httpServer.registerPathHandler("/content", contentHandler); 50 httpServer.start(-1); 51 52 var chan = make_channel(randomURI); 53 chan.asyncOpen(new ChannelListener(firstTimeThrough, null)); 54 do_test_pending(); 55 }