<commit_before>'use strict';

require('renasar-core/spec/helper');

var di = require('di');
global.core = require('renasar-core')(di);
global.dihelper = core.helper;

helper.startServer = function (overrides) {

    helper.setupInjector(_.flatten([
            require('renasar-tasks').injectables,
            dihelper.simpleWrapper(require('renasar-mq'), 'MQ'),
            dihelper.simpleWrapper(require('express')(), 'express-app'),
            dihelper.simpleWrapper({
                publishLog: sinon.stub().resolves()
            }, 'Protocol.Logging'),
            dihelper.simpleWrapper({
                lookupIpLease: sinon.stub().resolves('00:00:00:00:00:00')
            }, 'Protocol.Dhcp'),
            helper.requireGlob('/lib/**/*.js'),
            helper.require('/app.js')
    ].concat(overrides || [])));
    helper.setupTestConfig();
    helper.injector.get('Services.Configuration')
        .set('http', true)
        .set('https', false)
        .set('httpPort', 8089);
    return helper.injector.get('Http').start();
};

helper.stopServer = function () {
    return helper.injector.get('Http').stop();
};

helper.request = function (url, options) {
    var agent = request(url || 'http://localhost:8089', options);

    // monkeypatch supertest objects to have a "then" function so they can be used as promises
    _.methods(agent).forEach(function (method) {
        var orig = agent[method];
        agent[method] = function () {
            var test = orig.apply(agent, arguments);
            test.then = function (successCallback, errorCallback) {
                var deferred = Q.defer();
                test.end(function(err, res) {
                    if (err) {
                        deferred.reject(err);
                        return;
                    } else {
                        deferred.resolve(res);
                    }
                });
                return deferred.promise.then(successCallback, errorCallback);
            };
            return test;
        };
    });

    return agent;
};<commit_msg>Adding better output to supertest errors in the case of a status code assertion fail<commit_after>'use strict';

require('renasar-core/spec/helper');

var di = require('di');
var util = require('util');
global.core = require('renasar-core')(di);
global.dihelper = core.helper;

helper.startServer = function (overrides) {

    helper.setupInjector(_.flatten([
            require('renasar-tasks').injectables,
            dihelper.simpleWrapper(require('renasar-mq'), 'MQ'),
            dihelper.simpleWrapper(require('express')(), 'express-app'),
            dihelper.simpleWrapper({
                publishLog: sinon.stub().resolves()
            }, 'Protocol.Logging'),
            dihelper.simpleWrapper({
                lookupIpLease: sinon.stub().resolves('00:00:00:00:00:00')
            }, 'Protocol.Dhcp'),
            helper.requireGlob('/lib/**/*.js'),
            helper.require('/app.js')
    ].concat(overrides || [])));
    helper.setupTestConfig();
    helper.injector.get('Services.Configuration')
        .set('http', true)
        .set('https', false)
        .set('httpPort', 8089);
    return helper.injector.get('Http').start();
};

helper.stopServer = function () {
    return helper.injector.get('Http').stop();
};

helper.request = function (url, options) {
    var agent = request(url || 'http://localhost:8089', options);

    // monkeypatch supertest objects to have a "then" function so they can be used as promises
    _.methods(agent).forEach(function (method) {
        var orig = agent[method];
        agent[method] = function () {
            var test = orig.apply(agent, arguments);
            test.then = function (successCallback, errorCallback) {
                var deferred = Q.defer();
                test.end(function(err, res) {
                    if (err) {
                        // if a status check fails, supertest will pass the res object as well.
                        // so, append some extra verbosity to the error for the report.
                        if (res) {
                            var output = res.body || res.text;
                            err.message += '\nResponse body:\n' + util.inspect(output);
                        }
                        deferred.reject(err);
                        return;
                    } else {
                        deferred.resolve(res);
                    }
                });
                return deferred.promise.then(successCallback, errorCallback);
            };
            return test;
        };
    });

    return agent;
};
{'fraction_non_alphanumeric': 0.14093959731543623, 'fraction_numerical': 0.008659883091578264, 'mean_word_length': 1.9001883239171375, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1014721', 'n_tokens_mistral': 1274, 'n_tokens_neox': 1217, 'n_words': 253}