mirror of https://gitee.com/bigwinds/arangodb
Tiny additions to Frank
* Missing templates will result in an error * The middleware calls next * All JSLint warnings except one fixed
This commit is contained in:
parent
7189151f93
commit
3d59723f99
|
@ -1,12 +1,15 @@
|
||||||
|
/*jslint indent: 2, nomen: true, maxlen: 100, sloppy: true, plusplus: true */
|
||||||
|
/*global _, require, db, exports */
|
||||||
|
|
||||||
var Frank,
|
var Frank,
|
||||||
baseMiddleware,
|
BaseMiddleware,
|
||||||
_ = require("underscore"),
|
_ = require("underscore"),
|
||||||
internal = {};
|
internal = {};
|
||||||
|
|
||||||
internal.createUrlObject = function (url, constraint, method) {
|
internal.createUrlObject = function (url, constraint, method) {
|
||||||
var urlObject = {};
|
var urlObject = {};
|
||||||
|
|
||||||
if(!_.isString(url)) {
|
if (!_.isString(url)) {
|
||||||
throw "URL has to be a String";
|
throw "URL has to be a String";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,28 +40,29 @@ Frank = function (options) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_.isString(templateCollection)) {
|
if (_.isString(templateCollection)) {
|
||||||
this.routingInfo.templateCollection = db._collection(templateCollection) || db._create(templateCollection);
|
this.routingInfo.templateCollection = db._collection(templateCollection) ||
|
||||||
|
db._create(templateCollection);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
_.extend(Frank.prototype, {
|
_.extend(Frank.prototype, {
|
||||||
handleRequest: function (method, route, argument1, argument2) {
|
handleRequest: function (method, route, argument1, argument2) {
|
||||||
var newRoute = {}, options, handler;
|
var newRoute = {}, options, handler;
|
||||||
|
|
||||||
if (_.isUndefined(argument2)) {
|
if (_.isUndefined(argument2)) {
|
||||||
handler = argument1;
|
handler = argument1;
|
||||||
options = {};
|
options = {};
|
||||||
} else {
|
} else {
|
||||||
options = argument1;
|
options = argument1;
|
||||||
handler = argument2;
|
handler = argument2;
|
||||||
}
|
}
|
||||||
|
|
||||||
newRoute.url = internal.createUrlObject(route, options.constraint, method);
|
newRoute.url = internal.createUrlObject(route, options.constraint, method);
|
||||||
newRoute.handler = handler;
|
newRoute.handler = handler;
|
||||||
|
|
||||||
this.routingInfo.routes.push(newRoute);
|
this.routingInfo.routes.push(newRoute);
|
||||||
},
|
},
|
||||||
|
|
||||||
head: function (route, argument1, argument2) {
|
head: function (route, argument1, argument2) {
|
||||||
this.handleRequest("head", route, argument1, argument2);
|
this.handleRequest("head", route, argument1, argument2);
|
||||||
},
|
},
|
||||||
|
@ -74,14 +78,14 @@ _.extend(Frank.prototype, {
|
||||||
put: function (route, argument1, argument2) {
|
put: function (route, argument1, argument2) {
|
||||||
this.handleRequest("put", route, argument1, argument2);
|
this.handleRequest("put", route, argument1, argument2);
|
||||||
},
|
},
|
||||||
|
|
||||||
patch: function (route, argument1, argument2) {
|
patch: function (route, argument1, argument2) {
|
||||||
this.handleRequest("patch", route, argument1, argument2);
|
this.handleRequest("patch", route, argument1, argument2);
|
||||||
},
|
},
|
||||||
|
|
||||||
delete: function (route, argument1, argument2) {
|
delete: function (route, argument1, argument2) {
|
||||||
this.handleRequest("delete", route, argument1, argument2);
|
this.handleRequest("delete", route, argument1, argument2);
|
||||||
},
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
@ -125,7 +129,11 @@ BaseMiddleware = function (templateCollection) {
|
||||||
throw "No template collection has been provided when creating a new Frank";
|
throw "No template collection has been provided when creating a new Frank";
|
||||||
}
|
}
|
||||||
|
|
||||||
template = templateCollection.firstExample({path: "simple/path"});
|
template = templateCollection.firstExample({path: templatePath });
|
||||||
|
|
||||||
|
if (_.isNull(template)) {
|
||||||
|
throw "Template '" + templatePath + "' does not exist";
|
||||||
|
}
|
||||||
|
|
||||||
if (template.templateLanguage !== "underscore") {
|
if (template.templateLanguage !== "underscore") {
|
||||||
throw "Unknown template language '" + template.templateLanguage + "'";
|
throw "Unknown template language '" + template.templateLanguage + "'";
|
||||||
|
@ -137,6 +145,8 @@ BaseMiddleware = function (templateCollection) {
|
||||||
};
|
};
|
||||||
|
|
||||||
response = _.extend(response, responseFunctions);
|
response = _.extend(response, responseFunctions);
|
||||||
|
|
||||||
|
next();
|
||||||
};
|
};
|
||||||
|
|
||||||
return middleware;
|
return middleware;
|
||||||
|
|
|
@ -77,7 +77,7 @@ function SetRoutesFrankSpec () {
|
||||||
assertEqual(routes.length, 1);
|
assertEqual(routes.length, 1);
|
||||||
assertEqual(routes[0].url.constraint, constraint);
|
assertEqual(routes[0].url.constraint, constraint);
|
||||||
},
|
},
|
||||||
|
|
||||||
testSetMethodToHead: function () {
|
testSetMethodToHead: function () {
|
||||||
var myFunc = function () {},
|
var myFunc = function () {},
|
||||||
routes = app.routingInfo.routes;
|
routes = app.routingInfo.routes;
|
||||||
|
@ -243,6 +243,18 @@ function BaseMiddlewareWithoutTemplateSpec () {
|
||||||
}
|
}
|
||||||
|
|
||||||
assertEqual(error, "No template collection has been provided when creating a new Frank");
|
assertEqual(error, "No template collection has been provided when creating a new Frank");
|
||||||
|
},
|
||||||
|
|
||||||
|
testMiddlewareCallsTheAction: function () {
|
||||||
|
var actionWasCalled = false;
|
||||||
|
|
||||||
|
next = function () {
|
||||||
|
actionWasCalled = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
baseMiddleware(request, response, options, next);
|
||||||
|
|
||||||
|
assertTrue(actionWasCalled);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -303,11 +315,28 @@ function BaseMiddlewareWithTemplateSpec () {
|
||||||
}
|
}
|
||||||
|
|
||||||
assertEqual(error, "Unknown template language 'pirateEngine'");
|
assertEqual(error, "Unknown template language 'pirateEngine'");
|
||||||
|
},
|
||||||
|
|
||||||
|
testRenderingATemplateWithAnNotExistingTemplate: function () {
|
||||||
|
var myCollection, error, middleware;
|
||||||
|
|
||||||
|
internal.db._drop("templateTest");
|
||||||
|
myCollection = internal.db._create("templateTest");
|
||||||
|
|
||||||
|
middleware = new BaseMiddleware(myCollection);
|
||||||
|
middleware(request, response, options, next);
|
||||||
|
|
||||||
|
try {
|
||||||
|
response.render("simple/path", { username: "moonglum" });
|
||||||
|
} catch(e) {
|
||||||
|
error = e;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEqual(error, "Template 'simple/path' does not exist");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
jsunity.run(CreateFrankSpec);
|
jsunity.run(CreateFrankSpec);
|
||||||
jsunity.run(SetRoutesFrankSpec);
|
jsunity.run(SetRoutesFrankSpec);
|
||||||
jsunity.run(BaseMiddlewareWithoutTemplateSpec);
|
jsunity.run(BaseMiddlewareWithoutTemplateSpec);
|
||||||
|
|
Loading…
Reference in New Issue