1
0
Fork 0

Merge branch 'devel' of https://github.com/triAGENS/ArangoDB into devel

This commit is contained in:
Jan Steemann 2013-01-14 14:12:26 +01:00
commit 2a3293a59e
2 changed files with 167 additions and 33 deletions

View File

@ -21,14 +21,23 @@ internal.createUrlObject = function (url, constraint, method) {
};
Frank = function (options) {
var urlPrefix, templateCollection;
options = options || {};
urlPrefix = options.urlPrefix;
templateCollection = options.templateCollection;
this.routingInfo = {
routes: []
};
if (_.isString(options.urlPrefix)) {
this.routingInfo.urlPrefix = options.urlPrefix;
if (_.isString(urlPrefix)) {
this.routingInfo.urlPrefix = urlPrefix;
}
if (_.isString(templateCollection)) {
this.routingInfo.templateCollection = db._collection(templateCollection) || db._create(templateCollection);
}
};
@ -75,41 +84,63 @@ _.extend(Frank.prototype, {
},
});
baseMiddleware = function (request, response, options, next) {
var responseFunctions;
responseFunctions = {
status: function (code) {
this.responseCode = code;
},
BaseMiddleware = function (templateCollection) {
var middleware = function (request, response, options, next) {
var responseFunctions;
set: function (key, value) {
var attributes = {};
if (_.isUndefined(value)) {
attributes = key;
} else {
attributes[key] = value;
}
responseFunctions = {
status: function (code) {
this.responseCode = code;
},
_.each(attributes, function (value, key) {
key = key.toLowerCase();
this.headers = this.headers || {};
this.headers[key] = value;
if (key === "content-type") {
this.contentType = value;
set: function (key, value) {
var attributes = {};
if (_.isUndefined(value)) {
attributes = key;
} else {
attributes[key] = value;
}
}, this);
},
json: function (obj) {
this.contentType = "application/json";
this.body = JSON.stringify(obj);
}
_.each(attributes, function (value, key) {
key = key.toLowerCase();
this.headers = this.headers || {};
this.headers[key] = value;
if (key === "content-type") {
this.contentType = value;
}
}, this);
},
json: function (obj) {
this.contentType = "application/json";
this.body = JSON.stringify(obj);
},
render: function (templatePath, data) {
var template;
if (_.isUndefined(templateCollection)) {
throw "No template collection has been provided when creating a new Frank";
}
template = templateCollection.firstExample({path: "simple/path"});
if (template.templateLanguage !== "underscore") {
throw "Unknown template language '" + template.templateLanguage + "'";
}
this.body = _.template(template.content, data);
this.contentType = template.contentType;
}
};
response = _.extend(response, responseFunctions);
};
response = _.extend(response, responseFunctions);
return middleware;
};
exports.Frank = Frank;
exports.baseMiddleware = baseMiddleware;
exports.BaseMiddleware = BaseMiddleware;

View File

@ -19,6 +19,33 @@ function CreateFrankSpec () {
assertEqual(routingInfo.routes.length, 0);
assertEqual(routingInfo.urlPrefix, "/frankentest");
},
testCreationWithTemplateCollectionIfCollectionDoesntExist: function () {
var app, routingInfo, templateCollection;
db._drop("frankentest");
app = new Frank({templateCollection: "frankentest"});
routingInfo = app.routingInfo;
templateCollection = db._collection("frankentest");
assertEqual(routingInfo.routes.length, 0);
assertNotNull(templateCollection);
assertEqual(routingInfo.templateCollection, templateCollection);
},
testCreationWithTemplateCollectionIfCollectionDoesExist: function () {
var app, routingInfo, templateCollection;
db._drop("frankentest");
db._create("frankentest");
app = new Frank({templateCollection: "frankentest"});
routingInfo = app.routingInfo;
templateCollection = db._collection("frankentest");
assertEqual(routingInfo.routes.length, 0);
assertNotNull(templateCollection);
assertEqual(routingInfo.templateCollection, templateCollection);
}
};
}
@ -152,12 +179,12 @@ function SetRoutesFrankSpec () {
};
}
function BaseMiddlewareSpec () {
function BaseMiddlewareWithoutTemplateSpec () {
var baseMiddleware, request, response, options, next;
return {
setUp: function () {
baseMiddleware = require("org/arangodb/frank").baseMiddleware;
baseMiddleware = new require("org/arangodb/frank").BaseMiddleware();
request = {};
response = {};
options = {};
@ -202,12 +229,88 @@ function BaseMiddlewareSpec () {
assertEqual(response.body, JSON.stringify(rawObject));
assertEqual(response.contentType, "application/json");
},
testTemplateFunctionAddedToResponse: function () {
var error;
baseMiddleware(request, response, options, next);
try {
response.render("simple/path", { username: "moonglum" });
} catch(e) {
error = e;
}
assertEqual(error, "No template collection has been provided when creating a new Frank");
}
};
}
function BaseMiddlewareWithTemplateSpec () {
var BaseMiddleware, request, response, options, next;
return {
setUp: function () {
request = {};
response = {};
options = {};
next = function () {};
BaseMiddleware = new require("org/arangodb/frank").BaseMiddleware;
},
testRenderingATemplate: function () {
var myCollection, middleware;
internal.db._drop("templateTest");
myCollection = internal.db._create("templateTest");
myCollection.save({
path: "simple/path",
content: "hallo <%= username %>",
contentType: "text/plain",
templateLanguage: "underscore"
});
middleware = new BaseMiddleware(myCollection);
middleware(request, response, options, next);
response.render("simple/path", { username: "moonglum" });
assertEqual(response.body, "hallo moonglum");
assertEqual(response.contentType, "text/plain");
},
testRenderingATemplateWithAnUnknownTemplateEngine: function () {
var myCollection, error, middleware;
internal.db._drop("templateTest");
myCollection = internal.db._create("templateTest");
myCollection.save({
path: "simple/path",
content: "hallo <%= username %>",
contentType: "text/plain",
templateLanguage: "pirateEngine"
});
middleware = new BaseMiddleware(myCollection);
middleware(request, response, options, next);
try {
response.render("simple/path", { username: "moonglum" });
} catch(e) {
error = e;
}
assertEqual(error, "Unknown template language 'pirateEngine'");
}
};
}
jsunity.run(CreateFrankSpec);
jsunity.run(SetRoutesFrankSpec);
jsunity.run(BaseMiddlewareSpec);
jsunity.run(BaseMiddlewareWithoutTemplateSpec);
jsunity.run(BaseMiddlewareWithTemplateSpec);
return jsunity.done();