Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
John McEleney
/
casperjs
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
Commit
79aa3968
...
79aa3968b1001429aede1b034fba8111ea415e06
authored
2012-11-05 18:44:32 +0100
by
Nicolas Perriault
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
refactored http authentication configuration process
1 parent
02ecf7e4
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
21 deletions
modules/casper.js
tests/suites/casper/auth.js
modules/casper.js
View file @
79aa396
...
...
@@ -406,6 +406,32 @@ Casper.prototype.clickLabel = function clickLabel(label, tag) {
};
/**
* Configures HTTP authentication parameters. Will try parsing auth credentials from
* the passed location first, then check for configured settings if any.
*
* @param String location Requested url
* @param Object settings Request settings
* @return Casper
*/
Casper
.
prototype
.
configureHttpAuth
=
function
configureHttpAuth
(
location
,
settings
)
{
"use strict"
;
var
username
,
password
,
httpAuthMatch
=
location
.
match
(
/^https
?
:
\/\/(
.+
)
:
(
.+
)
@/i
);
this
.
checkStarted
();
if
(
httpAuthMatch
)
{
this
.
page
.
settings
.
userName
=
httpAuthMatch
[
1
];
this
.
page
.
settings
.
password
=
httpAuthMatch
[
2
];
}
else
if
(
utils
.
isObject
(
settings
)
&&
settings
.
username
)
{
this
.
page
.
settings
.
userName
=
settings
.
username
;
this
.
page
.
settings
.
password
=
settings
.
password
;
}
else
{
return
;
}
this
.
emit
(
'http.auth'
,
username
,
password
);
this
.
log
(
"Setting HTTP authentication for user "
+
username
,
"info"
);
return
this
;
};
/**
* Creates a step definition.
*
* @param Function fn The step function to call
...
...
@@ -1101,20 +1127,8 @@ Casper.prototype.open = function open(location, settings) {
// clean location
location
=
utils
.
cleanUrl
(
location
);
// current request url
this
.
configureHttpAuth
(
location
,
settings
);
this
.
requestUrl
=
this
.
filter
(
'open.location'
,
location
)
||
location
;
// http auth
if
(
settings
.
username
&&
settings
.
password
)
{
this
.
setHttpAuth
(
settings
.
username
,
settings
.
password
);
}
else
{
var
httpAuthMatch
=
location
.
match
(
/^https
?
:
\/\/(
.+
)
:
(
.+
)
@/i
);
if
(
httpAuthMatch
)
{
var
httpAuth
=
{
username
:
httpAuthMatch
[
1
],
password
:
httpAuthMatch
[
2
]
};
this
.
setHttpAuth
(
httpAuth
.
username
,
httpAuth
.
password
);
}
}
this
.
emit
(
'open'
,
this
.
requestUrl
,
settings
);
this
.
log
(
f
(
'opening url: %s, HTTP %s'
,
this
.
requestUrl
,
settings
.
method
.
toUpperCase
()),
"debug"
);
this
.
page
.
openUrl
(
this
.
requestUrl
,
{
...
...
@@ -1251,22 +1265,17 @@ Casper.prototype.runStep = function runStep(step) {
};
/**
* Sets
HTTP authentication parameters
.
* Sets
current WebPage instance the credentials for HTTP authentication
.
*
* @param String
username The HTTP_AUTH_USER valu
e
* @param String
password The HTTP_AUTH_PW value
* @param String
usernam
e
* @param String
password
* @return Casper
*/
Casper
.
prototype
.
setHttpAuth
=
function
setHttpAuth
(
username
,
password
)
{
"use strict"
;
this
.
checkStarted
();
if
(
!
utils
.
isString
(
username
)
||
!
utils
.
isString
(
password
))
{
throw
new
CasperError
(
"Both username and password must be strings"
);
}
this
.
page
.
settings
.
userName
=
username
;
this
.
page
.
settings
.
password
=
password
;
this
.
emit
(
'http.auth'
,
username
,
password
);
this
.
log
(
"Setting HTTP authentication for user "
+
username
,
"info"
);
return
this
;
};
...
...
tests/suites/casper/auth.js
0 → 100644
View file @
79aa396
/*global casper*/
/*jshint strict:false maxstatements:99*/
casper
.
start
(
'tests/site/index.html'
);
casper
.
configureHttpAuth
(
'http://localhost/'
);
casper
.
test
.
assertEquals
(
casper
.
page
.
settings
.
userName
,
undefined
);
casper
.
test
.
assertEquals
(
casper
.
page
.
settings
.
password
,
undefined
);
casper
.
configureHttpAuth
(
'http://niko:plop@localhost/'
);
casper
.
test
.
assertEquals
(
casper
.
page
.
settings
.
userName
,
'niko'
);
casper
.
test
.
assertEquals
(
casper
.
page
.
settings
.
password
,
'plop'
);
casper
.
configureHttpAuth
(
'http://localhost/'
,
{
username
:
'john'
,
password
:
'doe'
});
casper
.
test
.
assertEquals
(
casper
.
page
.
settings
.
userName
,
'john'
);
casper
.
test
.
assertEquals
(
casper
.
page
.
settings
.
password
,
'doe'
);
casper
.
configureHttpAuth
(
'http://niko:plop@localhost/'
,
{
username
:
'john'
,
password
:
'doe'
});
casper
.
test
.
assertEquals
(
casper
.
page
.
settings
.
userName
,
'niko'
);
casper
.
test
.
assertEquals
(
casper
.
page
.
settings
.
password
,
'plop'
);
casper
.
run
(
function
()
{
this
.
test
.
done
();
});
Please
register
or
sign in
to post a comment