Added modules/sharing.js, which provides access to the RESTful sharing API, and a unit test suite. The unit test suite is pretty heinous right now and should get a bit of refactoring.

This commit is contained in:
Atul Varma 2008-06-09 18:55:26 -07:00
parent 6fc267b942
commit 2905492e20
2 changed files with 119 additions and 0 deletions

View File

@ -0,0 +1,57 @@
EXPORTED_SYMBOLS = ["Sharing"];
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
Cu.import("resource://weave/async.js");
Function.prototype.async = Async.sugar;
function Api(dav) {
this._dav = dav;
}
Api.prototype = {
shareWithUsers: function Api_shareWithUsers(path, users, onComplete) {
this._shareGenerator.async(this,
onComplete,
path,
users);
},
_shareGenerator: function Api__shareGenerator(path, users) {
let self = yield;
this._dav.defaultPrefix = "";
let cmd = {"version" : 1,
"directory" : path,
"share_to_users" : users};
let jsonSvc = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON);
let json = jsonSvc.encode(cmd);
this._dav.POST("share/", "cmd=" + escape(json), self.cb);
let xhr = yield;
let retval;
if (xhr.status == 200) {
if (xhr.responseText == "OK") {
retval = {wasSuccessful: true};
} else {
retval = {wasSuccessful: false,
errorText: xhr.responseText};
}
} else {
retval = {wasSuccessful: false,
errorText: "Server returned status " + xhr.status};
}
self.done(retval);
}
};
Sharing = {
Api: Api
};

View File

@ -0,0 +1,62 @@
const Cu = Components.utils;
Cu.import("resource://weave/sharing.js");
Cu.import("resource://weave/async.js");
Cu.import("resource://weave/util.js");
Cu.import("resource://weave/log4moz.js");
function runTestGenerator() {
let self = yield;
let fakeDav = {
POST: function fakeDav_POST(url, data, callback) {
let result = {status: 200, responseText: "OK"};
let cb = function() { callback(result); };
let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
timer.initWithCallback(new Utils.EventListener(cb),
0, timer.TYPE_ONE_SHOT);
}
};
let api = new Sharing.Api(fakeDav);
api.shareWithUsers("/fake/dir", ["johndoe"], self.cb);
let result = yield;
do_check_eq(result.wasSuccessful, true);
self.done();
}
function BasicFormatter() {
this.errors = 0;
}
BasicFormatter.prototype = {
format: function BF_format(message) {
let date = new Date(message.time);
if (message.level == Log4Moz.Level.Error)
this.errors += 1;
return message.loggerName + "\t" + message.levelDesc + "\t" +
message.message + "\n";
}
};
BasicFormatter.prototype.__proto__ = new Log4Moz.Formatter();
function run_test() {
var log = Log4Moz.Service.rootLogger;
var formatter = new BasicFormatter();
var appender = new Log4Moz.DumpAppender(formatter);
log.level = Log4Moz.Level.Debug;
appender.level = Log4Moz.Level.Debug;
log.addAppender(appender);
var threadManager = Cc["@mozilla.org/thread-manager;1"].getService();
let thread = threadManager.currentThread;
let gen = Async.run({}, runTestGenerator);
while (gen.generator && !formatter.errors) {
thread.processNextEvent(true);
}
if (formatter.errors)
throw new Error("Errors occurred.");
}