2008-11-03 14:41:39 -08:00
|
|
|
Cu.import("resource://weave/log4moz.js");
|
|
|
|
Cu.import("resource://weave/util.js");
|
|
|
|
Cu.import("resource://weave/async.js");
|
|
|
|
Cu.import("resource://weave/auth.js");
|
|
|
|
Cu.import("resource://weave/identity.js");
|
|
|
|
Cu.import("resource://weave/resource.js");
|
|
|
|
|
|
|
|
Function.prototype.async = Async.sugar;
|
|
|
|
|
|
|
|
let logger;
|
|
|
|
let Httpd = {};
|
2009-03-20 00:14:21 -07:00
|
|
|
Cu.import("resource://harness/modules/httpd.js", Httpd);
|
2008-11-03 14:41:39 -08:00
|
|
|
|
|
|
|
function server_handler(metadata, response) {
|
|
|
|
let body;
|
|
|
|
|
|
|
|
// no btoa() in xpcshell. it's guest:guest
|
|
|
|
if (metadata.hasHeader("Authorization") &&
|
|
|
|
metadata.getHeader("Authorization") == "Basic Z3Vlc3Q6Z3Vlc3Q=") {
|
|
|
|
body = "This path exists and is protected";
|
|
|
|
response.setStatusLine(metadata.httpVersion, 200, "OK, authorized");
|
|
|
|
response.setHeader("WWW-Authenticate", 'Basic realm="secret"', false);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
body = "This path exists and is protected - failed";
|
|
|
|
response.setStatusLine(metadata.httpVersion, 401, "Unauthorized");
|
|
|
|
response.setHeader("WWW-Authenticate", 'Basic realm="secret"', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
response.bodyOutputStream.write(body, body.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
function async_test() {
|
|
|
|
let self = yield;
|
|
|
|
|
|
|
|
logger = Log4Moz.repository.getLogger('Test');
|
|
|
|
Log4Moz.repository.rootLogger.addAppender(new Log4Moz.DumpAppender());
|
|
|
|
|
|
|
|
let server = new Httpd.nsHttpServer();
|
|
|
|
server.registerPathHandler("/foo", server_handler);
|
|
|
|
server.start(8080);
|
|
|
|
|
|
|
|
let auth = new BasicAuthenticator(new Identity("secret", "guest", "guest"));
|
|
|
|
Auth.defaultAuthenticator = auth;
|
|
|
|
|
2009-01-06 13:54:18 -08:00
|
|
|
let res = new Resource("http://localhost:8080/foo");
|
2008-11-03 14:41:39 -08:00
|
|
|
let content = yield res.get(self.cb);
|
|
|
|
do_check_eq(content, "This path exists and is protected");
|
2009-01-28 17:51:23 -08:00
|
|
|
do_check_eq(res.lastChannel.responseStatus, 200);
|
2008-11-03 14:41:39 -08:00
|
|
|
|
|
|
|
do_test_finished();
|
|
|
|
server.stop();
|
|
|
|
self.done();
|
|
|
|
}
|
|
|
|
|
|
|
|
function run_test() {
|
|
|
|
async_test.async(this);
|
|
|
|
do_test_pending();
|
|
|
|
}
|