mirror of https://gitee.com/bigwinds/arangodb
168 lines
4.2 KiB
JavaScript
168 lines
4.2 KiB
JavaScript
var jsunity = require("jsunity"),
|
|
internal = require("internal"),
|
|
console = require("console"),
|
|
Frank = require("org/arangodb/frank").Frank;
|
|
|
|
function CreateFrankSpec () {
|
|
return {
|
|
testCreationWithoutParameters: function () {
|
|
var app = new Frank(),
|
|
routingInfo = app.routingInfo;
|
|
|
|
assertEqual(routingInfo.routes.length, 0);
|
|
assertUndefined(routingInfo.urlPrefix);
|
|
},
|
|
|
|
testCreationWithUrlPrefix: function () {
|
|
var app = new Frank({urlPrefix: "/frankentest"}),
|
|
routingInfo = app.routingInfo;
|
|
|
|
assertEqual(routingInfo.routes.length, 0);
|
|
assertEqual(routingInfo.urlPrefix, "/frankentest");
|
|
}
|
|
};
|
|
}
|
|
|
|
function SetRoutesFrankSpec () {
|
|
var app;
|
|
|
|
return {
|
|
setUp: function () {
|
|
app = new Frank();
|
|
},
|
|
|
|
testSettingRoutesWithoutConstraint: function () {
|
|
var myFunc = function () {},
|
|
routes = app.routingInfo.routes;
|
|
|
|
app.get('/simple/route', myFunc);
|
|
assertEqual(routes.length, 1);
|
|
assertEqual(routes[0].url.match, '/simple/route');
|
|
assertUndefined(routes[0].url.constraint);
|
|
},
|
|
|
|
testSettingRoutesWithConstraint: function () {
|
|
var myFunc = function () {},
|
|
routes = app.routingInfo.routes,
|
|
constraint = { test: "/[a-z]+/" };
|
|
|
|
app.get('/simple/route', { constraint: constraint }, myFunc);
|
|
assertEqual(routes.length, 1);
|
|
assertEqual(routes[0].url.constraint, constraint);
|
|
},
|
|
|
|
testRefuseRoutesWithRoutesThatAreNumbers: function () {
|
|
var myFunc = function () {},
|
|
routes = app.routingInfo.routes,
|
|
error;
|
|
|
|
try {
|
|
app.get(2, myFunc);
|
|
} catch(e) {
|
|
error = e;
|
|
}
|
|
assertEqual(error, "URL has to be a String");
|
|
assertEqual(routes.length, 0);
|
|
},
|
|
|
|
testRefuseRoutesWithRoutesThatAreRegularExpressions: function () {
|
|
var myFunc = function () {},
|
|
routes = app.routingInfo.routes,
|
|
error;
|
|
|
|
try {
|
|
app.get(/[a-z]*/, myFunc);
|
|
} catch(e) {
|
|
error = e;
|
|
}
|
|
assertEqual(error, "URL has to be a String");
|
|
assertEqual(routes.length, 0);
|
|
},
|
|
|
|
testRefuseRoutesWithRoutesThatAreArrays: function () {
|
|
var myFunc = function () {},
|
|
routes = app.routingInfo.routes,
|
|
error;
|
|
|
|
try {
|
|
app.get(["/a", "/b"], myFunc);
|
|
} catch(e) {
|
|
error = e;
|
|
}
|
|
assertEqual(error, "URL has to be a String");
|
|
assertEqual(routes.length, 0);
|
|
},
|
|
|
|
testSettingHandlers: function () {
|
|
var myFunc = function () {},
|
|
routes = app.routingInfo.routes;
|
|
|
|
app.get('/simple/route', myFunc);
|
|
assertEqual(routes.length, 1);
|
|
assertEqual(routes[0].handler, myFunc);
|
|
}
|
|
|
|
};
|
|
}
|
|
|
|
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(SetRoutesFrankSpec);
|
|
jsunity.run(BaseMiddlewareSpec);
|
|
|
|
return jsunity.done();
|