netwerk/test/browser/browser_NetUtil.js

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

michael@0 1 /*
michael@0 2 Any copyright is dedicated to the Public Domain.
michael@0 3 http://creativecommons.org/publicdomain/zero/1.0/
michael@0 4 */
michael@0 5
michael@0 6 Components.utils.import("resource://gre/modules/NetUtil.jsm");
michael@0 7
michael@0 8 function test() {
michael@0 9 waitForExplicitFinish();
michael@0 10
michael@0 11 // We overload this test to include verifying that httpd.js is
michael@0 12 // importable as a testing-only JS module.
michael@0 13 Components.utils.import("resource://testing-common/httpd.js", {});
michael@0 14
michael@0 15 nextTest();
michael@0 16 }
michael@0 17
michael@0 18 function nextTest() {
michael@0 19 if (tests.length)
michael@0 20 executeSoon(tests.shift());
michael@0 21 else
michael@0 22 executeSoon(finish);
michael@0 23 }
michael@0 24
michael@0 25 var tests = [
michael@0 26 test_asyncFetchBadCert,
michael@0 27 ];
michael@0 28
michael@0 29 var gCertErrorDialogShown = 0;
michael@0 30
michael@0 31 // We used to show a dialog box by default when we encountered an SSL
michael@0 32 // certificate error. Now we treat these errors just like other
michael@0 33 // networking errors; the dialog is no longer shown.
michael@0 34 function test_asyncFetchBadCert() {
michael@0 35 let listener = new WindowListener("chrome://pippki/content/certerror.xul", function (domwindow) {
michael@0 36 gCertErrorDialogShown++;
michael@0 37
michael@0 38 // Close the dialog
michael@0 39 domwindow.document.documentElement.cancelDialog();
michael@0 40 });
michael@0 41
michael@0 42 Services.wm.addListener(listener);
michael@0 43
michael@0 44 // Try a load from an untrusted cert, with errors supressed
michael@0 45 NetUtil.asyncFetch("https://untrusted.example.com", function (aInputStream, aStatusCode, aRequest) {
michael@0 46 ok(!Components.isSuccessCode(aStatusCode), "request failed");
michael@0 47 ok(aRequest instanceof Ci.nsIHttpChannel, "request is an nsIHttpChannel");
michael@0 48
michael@0 49 is(gCertErrorDialogShown, 0, "cert error dialog was not shown");
michael@0 50
michael@0 51 // Now try again with a channel whose notificationCallbacks doesn't suprress errors
michael@0 52 let channel = NetUtil.newChannel("https://untrusted.example.com");
michael@0 53 channel.notificationCallbacks = {
michael@0 54 QueryInterface: XPCOMUtils.generateQI([Ci.nsIProgressEventSink,
michael@0 55 Ci.nsIInterfaceRequestor]),
michael@0 56 getInterface: function (aIID) this.QueryInterface(aIID),
michael@0 57 onProgress: function () {},
michael@0 58 onStatus: function () {}
michael@0 59 };
michael@0 60 NetUtil.asyncFetch(channel, function (aInputStream, aStatusCode, aRequest) {
michael@0 61 ok(!Components.isSuccessCode(aStatusCode), "request failed");
michael@0 62 ok(aRequest instanceof Ci.nsIHttpChannel, "request is an nsIHttpChannel");
michael@0 63
michael@0 64 is(gCertErrorDialogShown, 0, "cert error dialog was not shown");
michael@0 65
michael@0 66 // Now try a valid request
michael@0 67 NetUtil.asyncFetch("https://example.com", function (aInputStream, aStatusCode, aRequest) {
michael@0 68 info("aStatusCode for valid request: " + aStatusCode);
michael@0 69 ok(Components.isSuccessCode(aStatusCode), "request succeeded");
michael@0 70 ok(aRequest instanceof Ci.nsIHttpChannel, "request is an nsIHttpChannel");
michael@0 71 ok(aRequest.requestSucceeded, "HTTP request succeeded");
michael@0 72
michael@0 73 is(gCertErrorDialogShown, 0, "cert error dialog was not shown");
michael@0 74
michael@0 75 Services.wm.removeListener(listener);
michael@0 76 nextTest();
michael@0 77 });
michael@0 78 });
michael@0 79
michael@0 80 });
michael@0 81 }
michael@0 82
michael@0 83 function WindowListener(aURL, aCallback) {
michael@0 84 this.callback = aCallback;
michael@0 85 this.url = aURL;
michael@0 86 }
michael@0 87 WindowListener.prototype = {
michael@0 88 onOpenWindow: function(aXULWindow) {
michael@0 89 var domwindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor)
michael@0 90 .getInterface(Ci.nsIDOMWindow);
michael@0 91 var self = this;
michael@0 92 domwindow.addEventListener("load", function() {
michael@0 93 domwindow.removeEventListener("load", arguments.callee, false);
michael@0 94
michael@0 95 if (domwindow.document.location.href != self.url)
michael@0 96 return;
michael@0 97
michael@0 98 // Allow other window load listeners to execute before passing to callback
michael@0 99 executeSoon(function() {
michael@0 100 self.callback(domwindow);
michael@0 101 });
michael@0 102 }, false);
michael@0 103 },
michael@0 104 onCloseWindow: function(aXULWindow) {},
michael@0 105 onWindowTitleChange: function(aXULWindow, aNewTitle) {}
michael@0 106 }

mercurial