2010-06-16 14:30:08 -07:00
|
|
|
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;
|
2011-04-18 13:26:03 -07:00
|
|
|
let guestHeader = basic_auth_header("guest", "guest");
|
|
|
|
let johnHeader = basic_auth_header("johndoe", "moneyislike$£¥");
|
2010-07-19 15:28:54 -07:00
|
|
|
|
2011-04-18 13:26:03 -07:00
|
|
|
_("Guest header: " + guestHeader);
|
|
|
|
_("John header: " + johnHeader);
|
|
|
|
|
2010-07-19 15:28:54 -07:00
|
|
|
switch (metadata.getHeader("Authorization")) {
|
2011-04-18 13:26:03 -07:00
|
|
|
case guestHeader:
|
2010-07-19 15:28:54 -07:00
|
|
|
body = "This path exists and is protected";
|
|
|
|
statusCode = 200;
|
|
|
|
status = "OK";
|
|
|
|
break;
|
2011-04-18 13:26:03 -07:00
|
|
|
case johnHeader:
|
2010-07-19 15:28:54 -07:00
|
|
|
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() {
|
2011-04-18 13:26:03 -07:00
|
|
|
initTestLogging("Trace");
|
2008-11-03 14:41:39 -08:00
|
|
|
|
2011-04-18 13:26:03 -07:00
|
|
|
do_test_pending();
|
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);
|
|
|
|
|
2011-04-18 13:26:03 -07:00
|
|
|
let guestIdentity = new Identity("secret", "guest", "guest");
|
|
|
|
let johnIdentity = new Identity("secret2", "johndoe", "moneyislike$£¥")
|
|
|
|
let guestAuth = new BasicAuthenticator(guestIdentity);
|
|
|
|
let johnAuth = new BasicAuthenticator(johnIdentity);
|
|
|
|
Auth.defaultAuthenticator = guestAuth;
|
|
|
|
Auth.registerAuthenticator("bar$", johnAuth);
|
2010-07-19 15:28:54 -07:00
|
|
|
|
|
|
|
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
|
|
|
}
|