|
1 <!DOCTYPE HTML> |
|
2 <html> |
|
3 <head> |
|
4 <title>Test bug 627616</title> |
|
5 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
|
6 <script type="text/javascript" src="prompt_common.js"></script> |
|
7 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> |
|
8 </head> |
|
9 <body> |
|
10 <script class="testbody" type="text/javascript"> |
|
11 SimpleTest.waitForExplicitFinish(); |
|
12 |
|
13 var Cc = SpecialPowers.Cc; |
|
14 |
|
15 testNum = 1; |
|
16 |
|
17 var login, login2; |
|
18 |
|
19 var resolveCallback = SpecialPowers.wrapCallbackObject({ |
|
20 |
|
21 QueryInterface : function (iid) { |
|
22 const interfaces = [Ci.nsIProtocolProxyCallback, Ci.nsISupports]; |
|
23 |
|
24 if (!interfaces.some( function(v) { return iid.equals(v) } )) |
|
25 throw SpecialPowers.Cr.NS_ERROR_NO_INTERFACE; |
|
26 return this; |
|
27 }, |
|
28 |
|
29 onProxyAvailable : function (req, uri, pi, status) { |
|
30 init2(SpecialPowers.wrap(pi).host, SpecialPowers.wrap(pi).port); |
|
31 } |
|
32 }); |
|
33 |
|
34 function init1() { |
|
35 var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService); |
|
36 var pps = Cc["@mozilla.org/network/protocol-proxy-service;1"].getService(); |
|
37 |
|
38 var uri = ios.newURI("http://example.com", null, null); |
|
39 pps.asyncResolve(uri, 0, resolveCallback); |
|
40 } |
|
41 |
|
42 function init2(proxyHost, proxyPort) { |
|
43 |
|
44 var mozproxy = "moz-proxy://" + proxyHost + ":" + proxyPort; |
|
45 |
|
46 var pwmgr = Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager); |
|
47 login = Cc["@mozilla.org/login-manager/loginInfo;1"].createInstance(Ci.nsILoginInfo); |
|
48 login.init(mozproxy, null, "proxy_realm", "proxy_user", "proxy_pass", "", ""); |
|
49 pwmgr.addLogin(login); |
|
50 |
|
51 login2 = Cc["@mozilla.org/login-manager/loginInfo;1"].createInstance(Ci.nsILoginInfo); |
|
52 login2.init("http://mochi.test:8888", null, "mochirealm", "user1name", "user1pass", "", ""); |
|
53 pwmgr.addLogin(login2); |
|
54 startCallbackTimer(); |
|
55 } |
|
56 function cleanup() { |
|
57 var pwmgr = Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager); |
|
58 pwmgr.removeLogin(login); |
|
59 pwmgr.removeLogin(login2); |
|
60 timer.cancel(); |
|
61 } |
|
62 |
|
63 function makeXHR(expectedStatus, expectedText, extra) { |
|
64 var xhr = new XMLHttpRequest(); |
|
65 xhr.open("GET", "authenticate.sjs?" + |
|
66 "proxy_user=proxy_user&" + |
|
67 "proxy_pass=proxy_pass&" + |
|
68 "proxy_realm=proxy_realm&" + |
|
69 "user=user1name&" + |
|
70 "pass=user1pass&" + |
|
71 "realm=mochirealm&" + |
|
72 extra || ""); |
|
73 xhr.onloadend = function() { |
|
74 is(xhr.status, expectedStatus, "xhr.status"); |
|
75 is(xhr.statusText, expectedText, "xhr.statusText"); |
|
76 runNextTest(); |
|
77 } |
|
78 return xhr; |
|
79 } |
|
80 |
|
81 function testNonAnonymousCredentials() { |
|
82 var xhr = makeXHR(200, "OK"); |
|
83 xhr.send(); |
|
84 } |
|
85 |
|
86 function testAnonymousCredentials() { |
|
87 // Test that an anonymous request correctly performs proxy authentication |
|
88 var xhr = makeXHR(401, "Authentication required"); |
|
89 SpecialPowers.wrap(xhr).channel.loadFlags |= Ci.nsIChannel.LOAD_ANONYMOUS; |
|
90 xhr.send(); |
|
91 } |
|
92 |
|
93 function testAnonymousNoAuth() { |
|
94 // Next, test that an anonymous request still does not include any non-proxy |
|
95 // authentication headers. |
|
96 var xhr = makeXHR(200, "Authorization header not found", "anonymous=1"); |
|
97 SpecialPowers.wrap(xhr).channel.loadFlags |= Ci.nsIChannel.LOAD_ANONYMOUS; |
|
98 xhr.send(); |
|
99 } |
|
100 |
|
101 var gExpectedDialogs = 0; |
|
102 var gCurrentTest; |
|
103 function runNextTest() { |
|
104 is(gExpectedDialogs, 0, "received expected number of auth dialogs"); |
|
105 Cc["@mozilla.org/network/http-auth-manager;1"].getService(SpecialPowers.Ci.nsIHttpAuthManager).clearAll(); |
|
106 if (pendingTests.length > 0) { |
|
107 ({expectedDialogs: gExpectedDialogs, |
|
108 test: gCurrentTest}) = pendingTests.shift(); |
|
109 gCurrentTest.call(this); |
|
110 } else { |
|
111 cleanup(); |
|
112 SimpleTest.finish(); |
|
113 } |
|
114 } |
|
115 |
|
116 var pendingTests = [{expectedDialogs: 2, test: testNonAnonymousCredentials}, |
|
117 {expectedDialogs: 1, test: testAnonymousCredentials}, |
|
118 {expectedDialogs: 0, test: testAnonymousNoAuth}]; |
|
119 init1(); |
|
120 runNextTest(); |
|
121 |
|
122 function handleDialog(doc, testNum) |
|
123 { |
|
124 var dialog = doc.getElementById("commonDialog"); |
|
125 dialog.acceptDialog(); |
|
126 gExpectedDialogs--; |
|
127 startCallbackTimer(); |
|
128 } |
|
129 </script> |
|
130 </body> |
|
131 </html> |