test_port_remapping.js (1599B)
1 // This test is checking the `network.socket.forcePort` preference has an effect. 2 // We remap an ilusional port `8765` to go to the port the server actually binds to. 3 4 "use strict"; 5 6 const { HttpServer } = ChromeUtils.importESModule( 7 "resource://testing-common/httpd.sys.mjs" 8 ); 9 10 function make_channel(url) { 11 return NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true }); 12 } 13 14 const REMAPPED_PORT = 8765; 15 16 add_task(async function check_protocols() { 17 function contentHandler(metadata, response) { 18 let responseBody = "The server should never return this!"; 19 response.setHeader("Content-Type", "text/plain"); 20 response.bodyOutputStream.write(responseBody, responseBody.length); 21 } 22 23 const httpserv = new HttpServer(); 24 httpserv.registerPathHandler("/content", contentHandler); 25 httpserv.start(-1); 26 27 do_get_profile(); 28 Services.prefs.setCharPref( 29 "network.socket.forcePort", 30 `${REMAPPED_PORT}=${httpserv.identity.primaryPort}` 31 ); 32 33 function get_response() { 34 return new Promise(resolve => { 35 const URL = `http://localhost:${REMAPPED_PORT}/content`; 36 const channel = make_channel(URL); 37 channel.asyncOpen( 38 new ChannelListener((request, data) => { 39 resolve(data); 40 }) 41 ); 42 }); 43 } 44 45 // We expect "Bad request" from the test server because the server doesn't 46 // have identity for the remapped port. We don't want to add it too, because 47 // that would not prove we actualy remap the port number. 48 Assert.equal(await get_response(), "Bad request\n"); 49 await new Promise(resolve => httpserv.stop(resolve)); 50 });