testosterone

testosterone

lib/testosterone.js

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 = {},

      _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) {
    // refactor this a little bit
    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 either serial or parallel

  • se: e #serial
  • se: e #parallel

  • 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) {
    _specs.add(function () {
      resolved.call(resolved, function (fn) {
        if (fn) {
          return _finished.add(function () {
            if (!_config.quiet) {
              _sys.print('\n' + spec.grey + '\n');
            }
            fn.apply(fn, arguments);
          });
        } else {
          // this is hackish
          _finished.add(function () {})();
          _sys.print('\n' + spec.grey + '\n');
        }
      });
    })();

    return TESTOSTERONE;
  };

Runs all the added tests in serial.

  • se: e #add
  • se: e #parallel

  • 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 = function (cb) {
    _specs.serial(function () {
      _finished.parallel(function () {
        cb.call(cb, arguments);
        _test();
      });
    });

    return TESTOSTERONE;
  };

Runs all the added tests in parallel.

  • se: e #add
  • se: e #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.parallel = function (cb) {
    _specs.parallel(function () {
      _finished.parallel(function () {
        cb.call(cb, arguments);
        _test();
      });
    });

    return TESTOSTERONE;
  };

  TESTOSTERONE.assert = _assert;

  if (!_config.quiet) {
    _sys.print(('✿ ' + (_config.title || 'Testosterone') + ' : ').yellow);
  }

  return TESTOSTERONE;
};