2010-06-16 14:30:08 -07:00
|
|
|
Cu.import("resource://services-sync/auth.js");
|
|
|
|
Cu.import("resource://services-sync/identity.js");
|
|
|
|
Cu.import("resource://services-sync/log4moz.js");
|
|
|
|
Cu.import("resource://services-sync/resource.js");
|
|
|
|
Cu.import("resource://services-sync/util.js");
|
2008-11-03 14:41:39 -08:00
|
|
|
|
|
|
|
let logger;
|
|
|
|
|
|
|
|
function server_handler(metadata, response) {
|
2010-07-19 15:28:54 -07:00
|
|
|
let body, statusCode, status;
|
|
|
|
|
|
|
|
switch (metadata.getHeader("Authorization")) {
|
|
|
|
// guest:guest
|
|
|
|
case "Basic Z3Vlc3Q6Z3Vlc3Q=":
|
|
|
|
body = "This path exists and is protected";
|
|
|
|
statusCode = 200;
|
|
|
|
status = "OK";
|
|
|
|
break;
|
|
|
|
// johndoe:moneyislike$\u20ac\xa5\u5143
|
|
|
|
case "Basic am9obmRvZTptb25leWlzbGlrZSTigqzCpeWFgw==":
|
|
|
|
body = "This path exists and is protected by a UTF8 password";
|
|
|
|
statusCode = 200;
|
|
|
|
status = "OK";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
body = "This path exists and is protected - failed";
|
|
|
|
statusCode = 401;
|
|
|
|
status = "Unauthorized";
|
2008-11-03 14:41:39 -08:00
|
|
|
}
|
|
|
|
|
2010-07-19 15:28:54 -07:00
|
|
|
response.setStatusLine(metadata.httpVersion, statusCode, status);
|
|
|
|
response.setHeader("WWW-Authenticate", 'Basic realm="secret"', false);
|
2008-11-03 14:41:39 -08:00
|
|
|
response.bodyOutputStream.write(body, body.length);
|
|
|
|
}
|
|
|
|
|
2009-06-09 12:39:45 -07:00
|
|
|
function run_test() {
|
2010-08-02 22:37:13 -07:00
|
|
|
do_test_pending();
|
2008-11-03 14:41:39 -08:00
|
|
|
logger = Log4Moz.repository.getLogger('Test');
|
|
|
|
Log4Moz.repository.rootLogger.addAppender(new Log4Moz.DumpAppender());
|
|
|
|
|
2010-06-22 13:18:10 -07:00
|
|
|
let server = new nsHttpServer();
|
2008-11-03 14:41:39 -08:00
|
|
|
server.registerPathHandler("/foo", server_handler);
|
2010-07-19 15:28:54 -07:00
|
|
|
server.registerPathHandler("/bar", server_handler);
|
2008-11-03 14:41:39 -08:00
|
|
|
server.start(8080);
|
|
|
|
|
|
|
|
let auth = new BasicAuthenticator(new Identity("secret", "guest", "guest"));
|
2010-07-19 15:28:54 -07:00
|
|
|
let auth2 = new BasicAuthenticator(
|
|
|
|
new Identity("secret2", "johndoe", "moneyislike$\u20ac\xa5\u5143"));
|
2008-11-03 14:41:39 -08:00
|
|
|
Auth.defaultAuthenticator = auth;
|
2010-07-19 15:28:54 -07:00
|
|
|
Auth.registerAuthenticator("bar$", auth2);
|
|
|
|
|
|
|
|
try {
|
|
|
|
let content = new Resource("http://localhost:8080/foo").get();
|
|
|
|
do_check_eq(content, "This path exists and is protected");
|
|
|
|
do_check_eq(content.status, 200);
|
|
|
|
|
|
|
|
content = new Resource("http://localhost:8080/bar").get();
|
|
|
|
do_check_eq(content, "This path exists and is protected by a UTF8 password");
|
|
|
|
do_check_eq(content.status, 200);
|
|
|
|
} finally {
|
2010-08-02 22:37:13 -07:00
|
|
|
server.stop(do_test_finished);
|
2010-07-19 15:28:54 -07:00
|
|
|
}
|
2008-11-03 14:41:39 -08:00
|
|
|
}
|