michael@0: Cu.import("resource://testing-common/httpd.js"); michael@0: michael@0: var httpserv = null; michael@0: michael@0: const CID = Components.ID("{5645d2c1-d6d8-4091-b117-fe7ee4027db7}"); michael@0: const contractID = "@mozilla.org/system-proxy-settings;1" michael@0: michael@0: XPCOMUtils.defineLazyGetter(this, "systemSettings", function() { michael@0: return { michael@0: QueryInterface: function (iid) { michael@0: if (iid.equals(Components.interfaces.nsISupports) || michael@0: iid.equals(Components.interfaces.nsIFactory) || michael@0: iid.equals(Components.interfaces.nsISystemProxySettings)) michael@0: return this; michael@0: throw Components.results.NS_ERROR_NO_INTERFACE; michael@0: }, michael@0: createInstance: function (outer, iid) { michael@0: if (outer) michael@0: throw Components.results.NS_ERROR_NO_AGGREGATION; michael@0: return this.QueryInterface(iid); michael@0: }, michael@0: lockFactory: function (lock) { michael@0: throw Components.results.NS_ERROR_NOT_IMPLEMENTED; michael@0: }, michael@0: michael@0: mainThreadOnly: true, michael@0: PACURI: "http://localhost:" + httpserv.identity.primaryPort + "/redirect", michael@0: getProxyForURI: function(aURI) { michael@0: throw Components.results.NS_ERROR_NOT_IMPLEMENTED; michael@0: } michael@0: }; michael@0: }); michael@0: michael@0: function checkValue(request, data, ctx) { michael@0: do_check_true(called); michael@0: do_check_eq("ok", data); michael@0: httpserv.stop(do_test_finished); michael@0: } michael@0: michael@0: function makeChan(url) { michael@0: var ios = Components.classes["@mozilla.org/network/io-service;1"] michael@0: .getService(Components.interfaces.nsIIOService); michael@0: var chan = ios.newChannel(url, null, null) michael@0: .QueryInterface(Components.interfaces.nsIHttpChannel); michael@0: michael@0: return chan; michael@0: } michael@0: michael@0: function run_test() { michael@0: httpserv = new HttpServer(); michael@0: httpserv.registerPathHandler("/redirect", redirect); michael@0: httpserv.registerPathHandler("/pac", pac); michael@0: httpserv.registerPathHandler("/target", target); michael@0: httpserv.start(-1); michael@0: michael@0: Components.manager.nsIComponentRegistrar.registerFactory( michael@0: CID, michael@0: "Fake system proxy-settings", michael@0: contractID, systemSettings); michael@0: michael@0: // Ensure we're using system-properties michael@0: const prefs = Cc["@mozilla.org/preferences-service;1"] michael@0: .getService(Ci.nsIPrefBranch); michael@0: prefs.setIntPref( michael@0: "network.proxy.type", michael@0: Components.interfaces.nsIProtocolProxyService.PROXYCONFIG_SYSTEM); michael@0: michael@0: // clear cache michael@0: evict_cache_entries(); michael@0: michael@0: var chan = makeChan("http://localhost:" + httpserv.identity.primaryPort + michael@0: "/target"); michael@0: chan.asyncOpen(new ChannelListener(checkValue, null), null); michael@0: michael@0: do_test_pending(); michael@0: } michael@0: michael@0: var called = false, failed = false; michael@0: function redirect(metadata, response) { michael@0: // If called second time, just return the PAC but set failed-flag michael@0: if (called) { michael@0: failed = true; michael@0: return pac(metadata, response); michael@0: } michael@0: michael@0: called = true; michael@0: response.setStatusLine(metadata.httpVersion, 302, "Found"); michael@0: response.setHeader("Location", "/pac", false); michael@0: var body = "Moved\n"; michael@0: response.bodyOutputStream.write(body, body.length); michael@0: } michael@0: michael@0: function pac(metadata, response) { michael@0: var PAC = 'function FindProxyForURL(url, host) { return "DIRECT"; }'; michael@0: response.setStatusLine(metadata.httpVersion, 200, "Ok"); michael@0: response.setHeader("Content-Type", "application/x-ns-proxy-autoconfig", false); michael@0: response.bodyOutputStream.write(PAC, PAC.length); michael@0: } michael@0: michael@0: function target(metadata, response) { michael@0: var retval = "ok"; michael@0: if (failed) retval = "failed"; michael@0: michael@0: response.setStatusLine(metadata.httpVersion, 200, "Ok"); michael@0: response.setHeader("Content-Type", "text/plain", false); michael@0: response.bodyOutputStream.write(retval, retval.length); michael@0: }