refactored http authentication configuration process
Showing
2 changed files
with
54 additions
and
21 deletions
... | @@ -406,6 +406,32 @@ Casper.prototype.clickLabel = function clickLabel(label, tag) { | ... | @@ -406,6 +406,32 @@ Casper.prototype.clickLabel = function clickLabel(label, tag) { |
406 | }; | 406 | }; |
407 | 407 | ||
408 | /** | 408 | /** |
409 | * Configures HTTP authentication parameters. Will try parsing auth credentials from | ||
410 | * the passed location first, then check for configured settings if any. | ||
411 | * | ||
412 | * @param String location Requested url | ||
413 | * @param Object settings Request settings | ||
414 | * @return Casper | ||
415 | */ | ||
416 | Casper.prototype.configureHttpAuth = function configureHttpAuth(location, settings) { | ||
417 | "use strict"; | ||
418 | var username, password, httpAuthMatch = location.match(/^https?:\/\/(.+):(.+)@/i); | ||
419 | this.checkStarted(); | ||
420 | if (httpAuthMatch) { | ||
421 | this.page.settings.userName = httpAuthMatch[1]; | ||
422 | this.page.settings.password = httpAuthMatch[2]; | ||
423 | } else if (utils.isObject(settings) && settings.username) { | ||
424 | this.page.settings.userName = settings.username; | ||
425 | this.page.settings.password = settings.password; | ||
426 | } else { | ||
427 | return; | ||
428 | } | ||
429 | this.emit('http.auth', username, password); | ||
430 | this.log("Setting HTTP authentication for user " + username, "info"); | ||
431 | return this; | ||
432 | }; | ||
433 | |||
434 | /** | ||
409 | * Creates a step definition. | 435 | * Creates a step definition. |
410 | * | 436 | * |
411 | * @param Function fn The step function to call | 437 | * @param Function fn The step function to call |
... | @@ -1101,20 +1127,8 @@ Casper.prototype.open = function open(location, settings) { | ... | @@ -1101,20 +1127,8 @@ Casper.prototype.open = function open(location, settings) { |
1101 | // clean location | 1127 | // clean location |
1102 | location = utils.cleanUrl(location); | 1128 | location = utils.cleanUrl(location); |
1103 | // current request url | 1129 | // current request url |
1130 | this.configureHttpAuth(location, settings); | ||
1104 | this.requestUrl = this.filter('open.location', location) || location; | 1131 | this.requestUrl = this.filter('open.location', location) || location; |
1105 | // http auth | ||
1106 | if (settings.username && settings.password) { | ||
1107 | this.setHttpAuth(settings.username, settings.password); | ||
1108 | } else { | ||
1109 | var httpAuthMatch = location.match(/^https?:\/\/(.+):(.+)@/i); | ||
1110 | if (httpAuthMatch) { | ||
1111 | var httpAuth = { | ||
1112 | username: httpAuthMatch[1], | ||
1113 | password: httpAuthMatch[2] | ||
1114 | }; | ||
1115 | this.setHttpAuth(httpAuth.username, httpAuth.password); | ||
1116 | } | ||
1117 | } | ||
1118 | this.emit('open', this.requestUrl, settings); | 1132 | this.emit('open', this.requestUrl, settings); |
1119 | this.log(f('opening url: %s, HTTP %s', this.requestUrl, settings.method.toUpperCase()), "debug"); | 1133 | this.log(f('opening url: %s, HTTP %s', this.requestUrl, settings.method.toUpperCase()), "debug"); |
1120 | this.page.openUrl(this.requestUrl, { | 1134 | this.page.openUrl(this.requestUrl, { |
... | @@ -1251,22 +1265,17 @@ Casper.prototype.runStep = function runStep(step) { | ... | @@ -1251,22 +1265,17 @@ Casper.prototype.runStep = function runStep(step) { |
1251 | }; | 1265 | }; |
1252 | 1266 | ||
1253 | /** | 1267 | /** |
1254 | * Sets HTTP authentication parameters. | 1268 | * Sets current WebPage instance the credentials for HTTP authentication. |
1255 | * | 1269 | * |
1256 | * @param String username The HTTP_AUTH_USER value | 1270 | * @param String username |
1257 | * @param String password The HTTP_AUTH_PW value | 1271 | * @param String password |
1258 | * @return Casper | 1272 | * @return Casper |
1259 | */ | 1273 | */ |
1260 | Casper.prototype.setHttpAuth = function setHttpAuth(username, password) { | 1274 | Casper.prototype.setHttpAuth = function setHttpAuth(username, password) { |
1261 | "use strict"; | 1275 | "use strict"; |
1262 | this.checkStarted(); | 1276 | this.checkStarted(); |
1263 | if (!utils.isString(username) || !utils.isString(password)) { | ||
1264 | throw new CasperError("Both username and password must be strings"); | ||
1265 | } | ||
1266 | this.page.settings.userName = username; | 1277 | this.page.settings.userName = username; |
1267 | this.page.settings.password = password; | 1278 | this.page.settings.password = password; |
1268 | this.emit('http.auth', username, password); | ||
1269 | this.log("Setting HTTP authentication for user " + username, "info"); | ||
1270 | return this; | 1279 | return this; |
1271 | }; | 1280 | }; |
1272 | 1281 | ... | ... |
tests/suites/casper/auth.js
0 → 100644
1 | /*global casper*/ | ||
2 | /*jshint strict:false maxstatements:99*/ | ||
3 | |||
4 | casper.start('tests/site/index.html'); | ||
5 | |||
6 | casper.configureHttpAuth('http://localhost/'); | ||
7 | casper.test.assertEquals(casper.page.settings.userName, undefined); | ||
8 | casper.test.assertEquals(casper.page.settings.password, undefined); | ||
9 | |||
10 | casper.configureHttpAuth('http://niko:plop@localhost/'); | ||
11 | casper.test.assertEquals(casper.page.settings.userName, 'niko'); | ||
12 | casper.test.assertEquals(casper.page.settings.password, 'plop'); | ||
13 | |||
14 | casper.configureHttpAuth('http://localhost/', {username: 'john', password: 'doe'}); | ||
15 | casper.test.assertEquals(casper.page.settings.userName, 'john'); | ||
16 | casper.test.assertEquals(casper.page.settings.password, 'doe'); | ||
17 | |||
18 | casper.configureHttpAuth('http://niko:plop@localhost/', {username: 'john', password: 'doe'}); | ||
19 | casper.test.assertEquals(casper.page.settings.userName, 'niko'); | ||
20 | casper.test.assertEquals(casper.page.settings.password, 'plop'); | ||
21 | |||
22 | casper.run(function() { | ||
23 | this.test.done(); | ||
24 | }); |
-
Please register or sign in to post a comment