testosterone - Virile testing for http servers or any nodejs application.
Copyright(c) 2011 Pau Ramon masylum@gmail.com
MIT Licensed
|
module.exports = function (config) {
require('colors');
var _sys = require('sys'),
_specs = require('funk')(),
_finished = require('funk')(),
_config = config || {},
_client = require('http').createClient(
_config.port || 80,
_config.host || 'localhost'
),
_count_responses = 0,
_done_responses = 0,
_count_asserts = 0,
_passed_asserts = 0,
TESTOSTERONE = {},
_parseSpec = function (spec) {
var specs = spec.split('\n').map(function (spec) {
spec = spec.replace(/(GIVEN|WHEN|AND|THEN)/gi, '$1'.magenta + '\033[90m');
spec = spec.replace(/`([^`]*)`/g, '$1'.blue + '\033[90m');
return spec;
});
return specs.join('\n');
},
_assert = (function () {
var assert = require('assert'),
functions = Object.keys(assert),
a = {};
functions.forEach(function (fn) {
if (typeof require('assert')[fn] === 'function') {
a[fn] = function (_) {
_count_asserts += 1;
try {
require('assert')[fn].apply(this, Array.prototype.slice.call(arguments, 0));
} catch (exc) {
_sys.print(('\n✗ => ' + exc.stack + '\n').red);
process.exit();
}
_passed_asserts += 1;
_sys.print('✓ '.green);
};
}
});
return a;
}()),
|
Does a testable http call
param: String [route=undefined] http uri to test param: Object [req=undefined] Object containing request related attributes like headers or body. param: Object [res=undefined] Object to compare with the response of the http call param: Function [cb=undefined] Callback that will be called after the http call. Receives the http response object. return: s Testosterone, so you can chain http calls.
|
['get', 'post', 'head', 'put', 'delete', 'trace', 'options', 'connect'].forEach(function (http_method) {
TESTOSTERONE[http_method] = function (route, req, res, cb) {
_count_responses += 1;
if (typeof req === 'function') {
cb = req;
res = {};
req = {};
}
req.method = http_method.toUpperCase();
req.url = req.url || route;
_call(req, res, cb);
return TESTOSTERONE;
};
});
|
Adds a function to be called
param: String [spec=''] Specification. Will be printer once resolved is called. param: Function [resolved=undefined] This function prints the spec and also tracks if the test is resolved.
This allows to work with asyncronous tests. return: s Testosterone, so you can chain http calls.
|
TESTOSTERONE.add = function (spec, resolved) {
var print = function () {
_sys.print('\n\n' + _parseSpec(spec) + ' => '.yellow);
};
_specs.add(function () {
if (!_config.sync) {
_finished.add(function () {})();
if (!_config.quiet) {
print();
}
resolved.call(resolved, function () { });
} else {
resolved.call(resolved, function (fn) {
if (fn) {
return _finished.add(function () {
if (!_config.quiet) {
print();
}
fn.apply(fn, arguments);
});
} else {
_finished.add(function () {})();
if (!_config.quiet) {
print();
}
}
});
}
})();
return TESTOSTERONE;
};
|
Runs all the added tests in serial.
param: Function [cb=undefined] Callback that is run after all the resolved callbacks are run. return: s Testosterone, so you can chain http calls.
|
TESTOSTERONE.serial = TESTOSTERONE.run = function (cb) {
_specs.serial(function () {
_finished.parallel(function () {
cb.call(cb, arguments);
_test();
});
});
return TESTOSTERONE;
};
TESTOSTERONE.assert = _assert;
if (!_config.quiet) {
_sys.print(('✿ ' + (_config.title || 'Testosterone') + ' :').inverse.yellow + ' ');
}
return TESTOSTERONE;
};
|