toolkit/components/passwordmgr/test/test_xhr.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 <!DOCTYPE HTML>
michael@0 2 <html>
michael@0 3 <head>
michael@0 4 <title>Test for Login Manager</title>
michael@0 5 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 6 <script type="text/javascript" src="pwmgr_common.js"></script>
michael@0 7 <script type="text/javascript" src="prompt_common.js"></script>
michael@0 8 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
michael@0 9 </head>
michael@0 10 <body>
michael@0 11 Login Manager test: XHR prompt
michael@0 12 <p id="display"></p>
michael@0 13
michael@0 14 <div id="content" style="display: none">
michael@0 15 <iframe id="iframe"></iframe>
michael@0 16 </div>
michael@0 17
michael@0 18 <pre id="test">
michael@0 19 <script class="testbody" type="text/javascript">
michael@0 20
michael@0 21 /** Test for Login Manager: XHR prompts. **/
michael@0 22 var pwmgr, login1, login2;
michael@0 23
michael@0 24 function initLogins() {
michael@0 25 pwmgr = Cc["@mozilla.org/login-manager;1"].
michael@0 26 getService(Ci.nsILoginManager);
michael@0 27
michael@0 28 login1 = Cc["@mozilla.org/login-manager/loginInfo;1"].
michael@0 29 createInstance(Ci.nsILoginInfo);
michael@0 30 login2 = Cc["@mozilla.org/login-manager/loginInfo;1"].
michael@0 31 createInstance(Ci.nsILoginInfo);
michael@0 32
michael@0 33 login1.init("http://mochi.test:8888", null, "xhr",
michael@0 34 "xhruser1", "xhrpass1", "", "");
michael@0 35 login2.init("http://mochi.test:8888", null, "xhr2",
michael@0 36 "xhruser2", "xhrpass2", "", "");
michael@0 37
michael@0 38 pwmgr.addLogin(login1);
michael@0 39 pwmgr.addLogin(login2);
michael@0 40 }
michael@0 41
michael@0 42 function finishTest() {
michael@0 43 ok(true, "finishTest removing testing logins...");
michael@0 44 pwmgr.removeLogin(login1);
michael@0 45 pwmgr.removeLogin(login2);
michael@0 46
michael@0 47 SimpleTest.finish();
michael@0 48 }
michael@0 49
michael@0 50 function handleDialog(doc, testNum) {
michael@0 51 ok(true, "handleDialog running for test " + testNum);
michael@0 52
michael@0 53 var clickOK = true;
michael@0 54 var userfield = doc.getElementById("loginTextbox");
michael@0 55 var passfield = doc.getElementById("password1Textbox");
michael@0 56 var username = userfield.getAttribute("value");
michael@0 57 var password = passfield.getAttribute("value");
michael@0 58 var dialog = doc.getElementById("commonDialog");
michael@0 59
michael@0 60 switch(testNum) {
michael@0 61 case 1:
michael@0 62 is(username, "xhruser1", "Checking provided username");
michael@0 63 is(password, "xhrpass1", "Checking provided password");
michael@0 64 break;
michael@0 65
michael@0 66 case 2:
michael@0 67 is(username, "xhruser2", "Checking provided username");
michael@0 68 is(password, "xhrpass2", "Checking provided password");
michael@0 69
michael@0 70 // Check that the dialog has the correct parent
michael@0 71 ok(doc.defaultView.opener, "dialog has opener");
michael@0 72 // Not using is() because its "expected" text doesn't deal
michael@0 73 // with window objects very well
michael@0 74 // Disabled due to Bug 718543
michael@0 75 // ok(doc.defaultView.opener == window, "dialog's opener is correct");
michael@0 76
michael@0 77 break;
michael@0 78
michael@0 79 default:
michael@0 80 ok(false, "Uhh, unhandled switch for testNum #" + testNum);
michael@0 81 break;
michael@0 82 }
michael@0 83
michael@0 84 // Explicitly cancel the dialog and report a fail in this failure
michael@0 85 // case, rather than letting the dialog get stuck due to an auth
michael@0 86 // failure and having the test timeout.
michael@0 87 if (!username && !password) {
michael@0 88 ok(false, "No values prefilled");
michael@0 89 clickOK = false;
michael@0 90 }
michael@0 91
michael@0 92 if (clickOK)
michael@0 93 dialog.acceptDialog();
michael@0 94 else
michael@0 95 dialog.cancelDialog();
michael@0 96
michael@0 97 ok(true, "handleDialog done");
michael@0 98 didDialog = true;
michael@0 99 }
michael@0 100
michael@0 101 var newWin;
michael@0 102 function xhrLoad(xmlDoc) {
michael@0 103 ok(true, "xhrLoad running for test " + testNum);
michael@0 104
michael@0 105 // The server echos back the user/pass it received.
michael@0 106 var username = xmlDoc.getElementById("user").textContent;
michael@0 107 var password = xmlDoc.getElementById("pass").textContent;
michael@0 108 var authok = xmlDoc.getElementById("ok").textContent;
michael@0 109
michael@0 110
michael@0 111 switch(testNum) {
michael@0 112 case 1:
michael@0 113 is(username, "xhruser1", "Checking provided username");
michael@0 114 is(password, "xhrpass1", "Checking provided password");
michael@0 115 break;
michael@0 116
michael@0 117 case 2:
michael@0 118 is(username, "xhruser2", "Checking provided username");
michael@0 119 is(password, "xhrpass2", "Checking provided password");
michael@0 120
michael@0 121 newWin.close();
michael@0 122 break;
michael@0 123
michael@0 124 default:
michael@0 125 ok(false, "Uhh, unhandled switch for testNum #" + testNum);
michael@0 126 break;
michael@0 127 }
michael@0 128
michael@0 129 doTest();
michael@0 130 }
michael@0 131
michael@0 132 function doTest() {
michael@0 133 switch(++testNum) {
michael@0 134 case 1:
michael@0 135 startCallbackTimer();
michael@0 136 makeRequest("authenticate.sjs?user=xhruser1&pass=xhrpass1&realm=xhr");
michael@0 137 break;
michael@0 138
michael@0 139 case 2:
michael@0 140 // Test correct parenting, by opening another window and
michael@0 141 // making sure the prompt's opener is correct
michael@0 142 newWin = window.open();
michael@0 143 newWin.focus();
michael@0 144 startCallbackTimer();
michael@0 145 makeRequest("authenticate.sjs?user=xhruser2&pass=xhrpass2&realm=xhr2");
michael@0 146 break;
michael@0 147
michael@0 148 default:
michael@0 149 finishTest();
michael@0 150 }
michael@0 151 }
michael@0 152
michael@0 153 function makeRequest(uri) {
michael@0 154 var request = new XMLHttpRequest();
michael@0 155 request.open("GET", uri, true);
michael@0 156 request.onreadystatechange = function () {
michael@0 157 if (request.readyState == 4)
michael@0 158 xhrLoad(request.responseXML);
michael@0 159 };
michael@0 160 request.send(null);
michael@0 161 }
michael@0 162
michael@0 163
michael@0 164 initLogins();
michael@0 165
michael@0 166 // clear plain HTTP auth sessions before the test, to allow
michael@0 167 // running them more than once.
michael@0 168 var authMgr = SpecialPowers.Cc['@mozilla.org/network/http-auth-manager;1']
michael@0 169 .getService(SpecialPowers.Ci.nsIHttpAuthManager);
michael@0 170 authMgr.clearAll();
michael@0 171
michael@0 172 // start the tests
michael@0 173 testNum = 0;
michael@0 174 doTest();
michael@0 175
michael@0 176 SimpleTest.waitForExplicitFinish();
michael@0 177 </script>
michael@0 178 </pre>
michael@0 179 </body>
michael@0 180 </html>

mercurial