mirror of https://gitee.com/bigwinds/arangodb
Frank: Added some simple convenience functions to the response
Will make everything more comfy.
Amazing.
And wonderful 🦄
The middleware is not yet attached.
This commit is contained in:
parent
a29f2f7370
commit
9052dbdcbe
|
@ -1,4 +1,5 @@
|
||||||
var Frank,
|
var Frank,
|
||||||
|
baseMiddleware,
|
||||||
_ = require("underscore"),
|
_ = require("underscore"),
|
||||||
internal = {};
|
internal = {};
|
||||||
|
|
||||||
|
@ -48,32 +49,44 @@ _.extend(Frank.prototype, {
|
||||||
newRoute.handler = handler;
|
newRoute.handler = handler;
|
||||||
|
|
||||||
this.routingInfo.routes.push(newRoute);
|
this.routingInfo.routes.push(newRoute);
|
||||||
},
|
|
||||||
|
|
||||||
/*
|
|
||||||
setPublic: function () {
|
|
||||||
// Can we put 404 etc. and assets here?
|
|
||||||
var staticPagesHandler = {
|
|
||||||
type: "StaticPages",
|
|
||||||
key: "/static",
|
|
||||||
url: {
|
|
||||||
match: "/static/ *"
|
|
||||||
},
|
|
||||||
action: {
|
|
||||||
controller: "org/arangodb/actions/staticContentController",
|
|
||||||
methods: [
|
|
||||||
"GET",
|
|
||||||
"HEAD"
|
|
||||||
],
|
|
||||||
options: {
|
|
||||||
application: "org.example.simple",
|
|
||||||
contentCollection: "org_example_simple_content",
|
|
||||||
prefix: "/static"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
baseMiddleware = function (request, response, options, next) {
|
||||||
|
var responseFunctions;
|
||||||
|
|
||||||
|
responseFunctions = {
|
||||||
|
status: function (code) {
|
||||||
|
this.responseCode = code;
|
||||||
|
},
|
||||||
|
|
||||||
|
set: function (key, value) {
|
||||||
|
var attributes = {};
|
||||||
|
if (_.isUndefined(value)) {
|
||||||
|
attributes = key;
|
||||||
|
} else {
|
||||||
|
attributes[key] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
_.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);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
response = _.extend(response, responseFunctions);
|
||||||
|
};
|
||||||
|
|
||||||
exports.Frank = Frank;
|
exports.Frank = Frank;
|
||||||
|
exports.baseMiddleware = baseMiddleware;
|
||||||
|
|
|
@ -102,14 +102,66 @@ function SetRoutesFrankSpec () {
|
||||||
assertEqual(routes[0].handler, myFunc);
|
assertEqual(routes[0].handler, myFunc);
|
||||||
}
|
}
|
||||||
|
|
||||||
//app.get('/', function() {
|
};
|
||||||
// erb "index" <- from templates collection?
|
}
|
||||||
// not really erb... underscore_template is much too long
|
|
||||||
//});
|
function BaseMiddlewareSpec () {
|
||||||
|
var baseMiddleware, request, response, options, next;
|
||||||
|
|
||||||
|
return {
|
||||||
|
setUp: function () {
|
||||||
|
baseMiddleware = require("org/arangodb/frank").baseMiddleware;
|
||||||
|
request = {};
|
||||||
|
response = {};
|
||||||
|
options = {};
|
||||||
|
next = function () {};
|
||||||
|
},
|
||||||
|
|
||||||
|
testStatusFunctionAddedToResponse: function () {
|
||||||
|
baseMiddleware(request, response, options, next);
|
||||||
|
|
||||||
|
response.status(200);
|
||||||
|
assertEqual(response.responseCode, 200);
|
||||||
|
},
|
||||||
|
|
||||||
|
testSetFunctionAddedToResponse: function () {
|
||||||
|
baseMiddleware(request, response, options, next);
|
||||||
|
|
||||||
|
response.set("Content-Length", "123");
|
||||||
|
assertEqual(response.headers["content-length"], "123");
|
||||||
|
|
||||||
|
response.set("Content-Type", "text/plain");
|
||||||
|
assertEqual(response.contentType, "text/plain");
|
||||||
|
},
|
||||||
|
|
||||||
|
testSetFunctionTakingAnObjectAddedToResponse: function () {
|
||||||
|
baseMiddleware(request, response, options, next);
|
||||||
|
|
||||||
|
response.set({
|
||||||
|
"Content-Length": "123",
|
||||||
|
"Content-Type": "text/plain"
|
||||||
|
});
|
||||||
|
|
||||||
|
assertEqual(response.headers["content-length"], "123");
|
||||||
|
assertEqual(response.contentType, "text/plain");
|
||||||
|
},
|
||||||
|
|
||||||
|
testJsonFunctionAddedToResponse: function () {
|
||||||
|
var rawObject = {test: "123"};
|
||||||
|
|
||||||
|
baseMiddleware(request, response, options, next);
|
||||||
|
|
||||||
|
response.json(rawObject);
|
||||||
|
|
||||||
|
assertEqual(response.body, JSON.stringify(rawObject));
|
||||||
|
assertEqual(response.contentType, "application/json");
|
||||||
|
},
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
jsunity.run(CreateFrankSpec);
|
jsunity.run(CreateFrankSpec);
|
||||||
jsunity.run(SetRoutesFrankSpec);
|
jsunity.run(SetRoutesFrankSpec);
|
||||||
|
jsunity.run(BaseMiddlewareSpec);
|
||||||
|
|
||||||
return jsunity.done();
|
return jsunity.done();
|
||||||
|
|
Loading…
Reference in New Issue