Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / netwerk / test / unit / test_pac_reload_after_network_change.js
blobdcd082d373f2006f49fa62ec27617bd21a1b18a1
1 "use strict";
3 const { HttpServer } = ChromeUtils.importESModule(
4 "resource://testing-common/httpd.sys.mjs"
5 );
6 XPCOMUtils.defineLazyServiceGetter(
7 this,
8 "gProxyService",
9 "@mozilla.org/network/protocol-proxy-service;1",
10 "nsIProtocolProxyService"
12 var { setTimeout } = ChromeUtils.importESModule(
13 "resource://gre/modules/Timer.sys.mjs"
16 let pacServer;
17 const proxyPort = 4433;
19 add_setup(async function () {
20 pacServer = new HttpServer();
21 pacServer.registerPathHandler(
22 "/proxy.pac",
23 function handler(metadata, response) {
24 let content = `function FindProxyForURL(url, host) { return "HTTPS localhost:${proxyPort}"; }`;
25 response.setHeader("Content-Length", `${content.length}`);
26 response.bodyOutputStream.write(content, content.length);
29 pacServer.start(-1);
30 });
32 registerCleanupFunction(async () => {
33 Services.prefs.clearUserPref("network.proxy.type");
34 Services.prefs.clearUserPref("network.proxy.autoconfig_url");
35 Services.prefs.clearUserPref("network.proxy.reload_pac_delay");
36 });
38 async function getProxyInfo() {
39 return new Promise(resolve => {
40 let uri = Services.io.newURI("http://www.mozilla.org/");
41 gProxyService.asyncResolve(uri, 0, {
42 onProxyAvailable(_req, _uri, pi, _status) {
43 resolve(pi);
45 });
46 });
49 // Test if we can successfully get PAC when the PAC server is available.
50 add_task(async function testPAC() {
51 // Configure PAC
52 Services.prefs.setIntPref("network.proxy.type", 2);
53 Services.prefs.setCharPref(
54 "network.proxy.autoconfig_url",
55 `http://localhost:${pacServer.identity.primaryPort}/proxy.pac`
58 let pi = await getProxyInfo();
59 Assert.equal(pi.port, proxyPort, "Expected proxy port to be the same");
60 Assert.equal(pi.type, "https", "Expected proxy type to be https");
61 });
63 // When PAC server is down, we should not use proxy at all.
64 add_task(async function testWhenPACServerDown() {
65 Services.prefs.setIntPref("network.proxy.reload_pac_delay", 0);
66 await new Promise(resolve => pacServer.stop(resolve));
68 Services.obs.notifyObservers(null, "network:link-status-changed", "changed");
70 // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
71 await new Promise(resolve => setTimeout(resolve, 3000));
73 let pi = await getProxyInfo();
74 Assert.equal(pi, null, "should have no proxy");
75 });