3 const { HttpServer
} = ChromeUtils
.importESModule(
4 "resource://testing-common/httpd.sys.mjs"
6 XPCOMUtils
.defineLazyServiceGetter(
9 "@mozilla.org/network/protocol-proxy-service;1",
10 "nsIProtocolProxyService"
12 var { setTimeout
} = ChromeUtils
.importESModule(
13 "resource://gre/modules/Timer.sys.mjs"
17 const proxyPort
= 4433;
19 add_setup(async
function () {
20 pacServer
= new HttpServer();
21 pacServer
.registerPathHandler(
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
);
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");
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
) {
49 // Test if we can successfully get PAC when the PAC server is available.
50 add_task(async
function testPAC() {
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");
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");