1
0
Fork 0

Foxx: BaseMiddleware Tests pulled out

This commit is contained in:
Lucas Dohmen 2013-08-05 13:53:17 +02:00
parent f88f371fd5
commit b19d3c69fb
4 changed files with 197 additions and 188 deletions

View File

@ -316,6 +316,7 @@ SHELL_SERVER_ONLY = \
@top_srcdir@/js/common/tests/shell-foxx.js \
@top_srcdir@/js/common/tests/shell-foxx-repository.js \
@top_srcdir@/js/common/tests/shell-foxx-model.js \
@top_srcdir@/js/common/tests/shell-foxx-base-middleware.js \
@top_srcdir@/js/common/tests/shell-graph-traversal.js \
@top_srcdir@/js/common/tests/shell-graph-algorithms.js \
@top_srcdir@/js/common/tests/shell-graph-measurement.js \

View File

@ -0,0 +1,194 @@
require("internal").flushModuleCache();
var jsunity = require("jsunity"),
arangodb = require("org/arangodb"),
db = arangodb.db;
function BaseMiddlewareWithoutTemplateSpec () {
var BaseMiddleware, request, response, options, next;
return {
setUp: function () {
baseMiddleware = require("org/arangodb/foxx/base_middleware").BaseMiddleware().functionRepresentation;
request = {};
response = {};
options = {};
next = function () {};
},
testBodyFunctionAddedToRequest: function () {
request.requestBody = "test";
baseMiddleware(request, response, options, next);
assertEqual(request.body(), "test");
},
testParamFunctionReturnsUrlParameters: function () {
request.urlParameters = {a: 1};
baseMiddleware(request, response, options, next);
assertEqual(request.params("a"), 1);
},
testParamFunctionReturnsParameters: function () {
request.parameters = {a: 1};
baseMiddleware(request, response, options, next);
assertEqual(request.params("a"), 1);
},
testParamFunctionReturnsAllParams: function () {
request.urlParameters = {a: 1};
request.parameters = {b: 2};
baseMiddleware(request, response, options, next);
assertEqual(request.params("a"), 1);
assertEqual(request.params("b"), 2);
},
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");
},
testTemplateFunctionAddedToResponse: function () {
var error;
baseMiddleware(request, response, options, next);
try {
response.render("simple/path", { username: "moonglum" });
} catch(e) {
error = e;
}
assertEqual(error, new Error("No template collection has been provided when creating a new FoxxApplication"));
},
testMiddlewareCallsTheAction: function () {
var actionWasCalled = false;
next = function () {
actionWasCalled = true;
};
baseMiddleware(request, response, options, next);
assertTrue(actionWasCalled);
}
};
}
function BaseMiddlewareWithTemplateSpec () {
var BaseMiddleware, request, response, options, next;
return {
setUp: function () {
request = {};
response = {};
options = {};
next = function () {};
BaseMiddleware = require("org/arangodb/foxx/base_middleware").BaseMiddleware;
},
testRenderingATemplate: function () {
var myCollection, middleware;
db._drop("templateTest");
myCollection = db._create("templateTest");
myCollection.save({
path: "simple/path",
content: "hello <%= username %>",
contentType: "text/plain",
templateLanguage: "underscore"
});
middleware = new BaseMiddleware(myCollection).functionRepresentation;
middleware(request, response, options, next);
response.render("simple/path", { username: "moonglum" });
assertEqual(response.body, "hello moonglum");
assertEqual(response.contentType, "text/plain");
},
testRenderingATemplateWithAnUnknownTemplateEngine: function () {
var myCollection, error, middleware;
db._drop("templateTest");
myCollection = db._create("templateTest");
myCollection.save({
path: "simple/path",
content: "hello <%= username %>",
contentType: "text/plain",
templateLanguage: "pirateEngine"
});
middleware = new BaseMiddleware(myCollection).functionRepresentation;
middleware(request, response, options, next);
try {
response.render("simple/path", { username: "moonglum" });
} catch(e) {
error = e;
}
assertEqual(error, new Error("Unknown template language 'pirateEngine'"));
},
testRenderingATemplateWithAnNotExistingTemplate: function () {
var myCollection, error, middleware;
db._drop("templateTest");
myCollection = db._create("templateTest");
middleware = new BaseMiddleware(myCollection).functionRepresentation;
middleware(request, response, options, next);
try {
response.render("simple/path", { username: "moonglum" });
} catch(e) {
error = e;
}
assertEqual(error, new Error("Template 'simple/path' does not exist"));
}
};
}
jsunity.run(BaseMiddlewareWithoutTemplateSpec);
jsunity.run(BaseMiddlewareWithTemplateSpec);
return jsunity.done();

View File

@ -420,197 +420,13 @@ function AddMiddlewareFoxxApplicationSpec () {
};
}
function BaseMiddlewareWithoutTemplateSpec () {
var BaseMiddleware, request, response, options, next;
return {
setUp: function () {
baseMiddleware = require("org/arangodb/foxx").BaseMiddleware().functionRepresentation;
request = {};
response = {};
options = {};
next = function () {};
},
testBodyFunctionAddedToRequest: function () {
request.requestBody = "test";
baseMiddleware(request, response, options, next);
assertEqual(request.body(), "test");
},
testParamFunctionReturnsUrlParameters: function () {
request.urlParameters = {a: 1};
baseMiddleware(request, response, options, next);
assertEqual(request.params("a"), 1);
},
testParamFunctionReturnsParameters: function () {
request.parameters = {a: 1};
baseMiddleware(request, response, options, next);
assertEqual(request.params("a"), 1);
},
testParamFunctionReturnsAllParams: function () {
request.urlParameters = {a: 1};
request.parameters = {b: 2};
baseMiddleware(request, response, options, next);
assertEqual(request.params("a"), 1);
assertEqual(request.params("b"), 2);
},
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");
},
testTemplateFunctionAddedToResponse: function () {
var error;
baseMiddleware(request, response, options, next);
try {
response.render("simple/path", { username: "moonglum" });
} catch(e) {
error = e;
}
assertEqual(error, new Error("No template collection has been provided when creating a new FoxxApplication"));
},
testMiddlewareCallsTheAction: function () {
var actionWasCalled = false;
next = function () {
actionWasCalled = true;
};
baseMiddleware(request, response, options, next);
assertTrue(actionWasCalled);
}
};
}
function BaseMiddlewareWithTemplateSpec () {
var BaseMiddleware, request, response, options, next;
return {
setUp: function () {
request = {};
response = {};
options = {};
next = function () {};
BaseMiddleware = require("org/arangodb/foxx").BaseMiddleware;
},
testRenderingATemplate: function () {
var myCollection, middleware;
db._drop("templateTest");
myCollection = db._create("templateTest");
myCollection.save({
path: "simple/path",
content: "hello <%= username %>",
contentType: "text/plain",
templateLanguage: "underscore"
});
middleware = new BaseMiddleware(myCollection).functionRepresentation;
middleware(request, response, options, next);
response.render("simple/path", { username: "moonglum" });
assertEqual(response.body, "hello moonglum");
assertEqual(response.contentType, "text/plain");
},
testRenderingATemplateWithAnUnknownTemplateEngine: function () {
var myCollection, error, middleware;
db._drop("templateTest");
myCollection = db._create("templateTest");
myCollection.save({
path: "simple/path",
content: "hello <%= username %>",
contentType: "text/plain",
templateLanguage: "pirateEngine"
});
middleware = new BaseMiddleware(myCollection).functionRepresentation;
middleware(request, response, options, next);
try {
response.render("simple/path", { username: "moonglum" });
} catch(e) {
error = e;
}
assertEqual(error, new Error("Unknown template language 'pirateEngine'"));
},
testRenderingATemplateWithAnNotExistingTemplate: function () {
var myCollection, error, middleware;
db._drop("templateTest");
myCollection = db._create("templateTest");
middleware = new BaseMiddleware(myCollection).functionRepresentation;
middleware(request, response, options, next);
try {
response.render("simple/path", { username: "moonglum" });
} catch(e) {
error = e;
}
assertEqual(error, new Error("Template 'simple/path' does not exist"));
}
};
}
function ViewHelperSpec () {
var app, Middleware, request, response, options, next;
return {
setUp: function () {
app = new FoxxApplication({prefix: "", foxxes: []});
Middleware = require('org/arangodb/foxx').BaseMiddleware;
Middleware = require('org/arangodb/foxx/base_middleware').BaseMiddleware;
request = {};
response = {};
options = {};
@ -767,8 +583,6 @@ jsunity.run(CreateFoxxApplicationSpec);
jsunity.run(SetRoutesFoxxApplicationSpec);
jsunity.run(DocumentationAndConstraintsSpec);
jsunity.run(AddMiddlewareFoxxApplicationSpec);
jsunity.run(BaseMiddlewareWithoutTemplateSpec);
jsunity.run(BaseMiddlewareWithTemplateSpec);
jsunity.run(ViewHelperSpec);
jsunity.run(FormatMiddlewareSpec);

View File

@ -2,7 +2,7 @@
/*global module, require, exports */
////////////////////////////////////////////////////////////////////////////////
/// @brief Foxx application
/// @brief Foxx BaseMiddleware
///
/// @file
///