netwerk/test/browser/browser_NetUtil.js

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

mercurial