-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
83 lines (70 loc) · 2.26 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
'use strict';
var _ = require('lodash');
var when = require('when');
var nodefn = require('when/node');
var testRunner = require('./lib/testRunner');
var prepareRequest = require('./lib/prepareRequest');
var validationSuite = require('require-dir')('./validation');
function sanityCheck(requests, options){
if(!_.every(options.validation, _.isFunction)){
throw new Error('Invalid validation test included in options');
}
//TODO iterate through requests, throw on obviously invalid things
}
function prepareArgs(request){
if(_.isArray(request.args)){
return when.resolve(_.map(request.args, function(args){
return {
content: args,
history: [{content: args}]
}
}));
}else{
return when.resolve([{
content: request.args,
history: [{content: request.args}]
}]);
}
}
function prepareOptions(options){
options.validation = _.assign({}, validationSuite, options.validation);
if(options.test === undefined){
options.test = {
statusCode: 200
}
}
}
function startTests(server, requests, options){
var tests = [];
if(_.isArray(requests)){
_.forEach(requests, function(request){
tests.push(testRunner(server, prepareRequest(request, options), prepareArgs(request), options));
});
}else if(_.isObject(requests) || _.isString(requests)){
tests.push(testRunner(server, prepareRequest(requests, options), prepareArgs(requests), options));
}else{
throw new Error('Test case invalid: '+requests);
}
return when.all(_.flatten(tests));
}
function cheesefist(server, requests, testWrapper, options, callback){
if(!_.isFunction(testWrapper)){
console.log('---NOTICE: No test framework integration provided.');
console.log('---See Quickstart in readme to for details on integrating test frameworks (Mocha, Lab, ect).');
throw new Error('Please provide test framework wrapper.');
}
if(_.isFunction(options)){
callback = options;
options = {};
}else if(_.isObject(options)){
options = _.cloneDeep(options);
}else{
options = {};
}
options.testWrapper = testWrapper;
prepareOptions(options);
sanityCheck(requests, options);
var results = startTests(server, requests, options);
return nodefn.bindCallback(results, callback);
}
module.exports = cheesefist;