michael@0: /* michael@0: Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ michael@0: */ michael@0: michael@0: Components.utils.import("resource://gre/modules/NetUtil.jsm"); michael@0: michael@0: function test() { michael@0: waitForExplicitFinish(); michael@0: michael@0: // We overload this test to include verifying that httpd.js is michael@0: // importable as a testing-only JS module. michael@0: Components.utils.import("resource://testing-common/httpd.js", {}); michael@0: michael@0: nextTest(); michael@0: } michael@0: michael@0: function nextTest() { michael@0: if (tests.length) michael@0: executeSoon(tests.shift()); michael@0: else michael@0: executeSoon(finish); michael@0: } michael@0: michael@0: var tests = [ michael@0: test_asyncFetchBadCert, michael@0: ]; michael@0: michael@0: var gCertErrorDialogShown = 0; michael@0: michael@0: // We used to show a dialog box by default when we encountered an SSL michael@0: // certificate error. Now we treat these errors just like other michael@0: // networking errors; the dialog is no longer shown. michael@0: function test_asyncFetchBadCert() { michael@0: let listener = new WindowListener("chrome://pippki/content/certerror.xul", function (domwindow) { michael@0: gCertErrorDialogShown++; michael@0: michael@0: // Close the dialog michael@0: domwindow.document.documentElement.cancelDialog(); michael@0: }); michael@0: michael@0: Services.wm.addListener(listener); michael@0: michael@0: // Try a load from an untrusted cert, with errors supressed michael@0: NetUtil.asyncFetch("https://untrusted.example.com", function (aInputStream, aStatusCode, aRequest) { michael@0: ok(!Components.isSuccessCode(aStatusCode), "request failed"); michael@0: ok(aRequest instanceof Ci.nsIHttpChannel, "request is an nsIHttpChannel"); michael@0: michael@0: is(gCertErrorDialogShown, 0, "cert error dialog was not shown"); michael@0: michael@0: // Now try again with a channel whose notificationCallbacks doesn't suprress errors michael@0: let channel = NetUtil.newChannel("https://untrusted.example.com"); michael@0: channel.notificationCallbacks = { michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIProgressEventSink, michael@0: Ci.nsIInterfaceRequestor]), michael@0: getInterface: function (aIID) this.QueryInterface(aIID), michael@0: onProgress: function () {}, michael@0: onStatus: function () {} michael@0: }; michael@0: NetUtil.asyncFetch(channel, function (aInputStream, aStatusCode, aRequest) { michael@0: ok(!Components.isSuccessCode(aStatusCode), "request failed"); michael@0: ok(aRequest instanceof Ci.nsIHttpChannel, "request is an nsIHttpChannel"); michael@0: michael@0: is(gCertErrorDialogShown, 0, "cert error dialog was not shown"); michael@0: michael@0: // Now try a valid request michael@0: NetUtil.asyncFetch("https://example.com", function (aInputStream, aStatusCode, aRequest) { michael@0: info("aStatusCode for valid request: " + aStatusCode); michael@0: ok(Components.isSuccessCode(aStatusCode), "request succeeded"); michael@0: ok(aRequest instanceof Ci.nsIHttpChannel, "request is an nsIHttpChannel"); michael@0: ok(aRequest.requestSucceeded, "HTTP request succeeded"); michael@0: michael@0: is(gCertErrorDialogShown, 0, "cert error dialog was not shown"); michael@0: michael@0: Services.wm.removeListener(listener); michael@0: nextTest(); michael@0: }); michael@0: }); michael@0: michael@0: }); michael@0: } michael@0: michael@0: function WindowListener(aURL, aCallback) { michael@0: this.callback = aCallback; michael@0: this.url = aURL; michael@0: } michael@0: WindowListener.prototype = { michael@0: onOpenWindow: function(aXULWindow) { michael@0: var domwindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor) michael@0: .getInterface(Ci.nsIDOMWindow); michael@0: var self = this; michael@0: domwindow.addEventListener("load", function() { michael@0: domwindow.removeEventListener("load", arguments.callee, false); michael@0: michael@0: if (domwindow.document.location.href != self.url) michael@0: return; michael@0: michael@0: // Allow other window load listeners to execute before passing to callback michael@0: executeSoon(function() { michael@0: self.callback(domwindow); michael@0: }); michael@0: }, false); michael@0: }, michael@0: onCloseWindow: function(aXULWindow) {}, michael@0: onWindowTitleChange: function(aXULWindow, aNewTitle) {} michael@0: }