Bug 1807268 - Re-enable verifyShowClipboardSuggestionsToggleTest UI test r=jajohnson
[gecko.git] / netwerk / test / unit / test_port_remapping.js
blob7a333a7af7d4a56f81449c87ebca669e863e2d11
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.
4 "use strict";
6 const { HttpServer } = ChromeUtils.importESModule(
7   "resource://testing-common/httpd.sys.mjs"
8 );
10 function make_channel(url) {
11   return NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true });
14 const REMAPPED_PORT = 8765;
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   }
23   const httpserv = new HttpServer();
24   httpserv.registerPathHandler("/content", contentHandler);
25   httpserv.start(-1);
27   do_get_profile();
28   Services.prefs.setCharPref(
29     "network.socket.forcePort",
30     `${REMAPPED_PORT}=${httpserv.identity.primaryPort}`
31   );
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   }
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 });