<commit_before>var assert = require('chai').assert;
var sinon = require('sinon');
var isoexe = require('../lib/iso-execute-server');
var isocall = require('../lib/isocall');

var cleanUp = function () {
    isocall.resetConfigs();
};

var getMiddleware = function () {
    var middleware;
    isoexe.setupMiddleware({
        put: function (path, parser, M) {
            middleware = M;
        }
    });
    return middleware;
}

describe('iso-execute-server', function () {
    beforeEach(cleanUp);
    afterEach(cleanUp);
    
    describe('.execute()', function () {
        it('will return rejected Promise when no input name', function (done) {
            isoexe.execute()['catch'](function (E) {
                assert.equal(E.message, 'iso-execute-server.execute without name!');
            }).then(done.bind(), done);
        });
        it('will return rejected Promise when no config found', function (done) {
            isoexe.execute('not found')['catch'](function (E) {
                assert.equal(E.message, 'iso-execute-server.execute but no config for the service: not found');
            }).then(done.bind(), done);
        });
        it('will return rejected Promise when it is not executable', function (done) {
            isocall.addConfigs({
                test_rpc: 'string_is_not_function'
            });

            isoexe.execute('test_rpc')['catch'](function (E) {
                assert.equal(E.message, 'iso-execute-server can not execute the service: test_rpc');
            }).then(done.bind(), done);
        });
        it('will return resolved Promise when execute correctly', function (done) {
            isocall.addConfigs({
                test_rpc: function () {
                    return 'OK!';
                }
            });

            isoexe.execute('test_rpc').then(function (R) {
                assert.equal(R, 'OK!');
            }).then(done.bind(), done);
        });
        it('will return rejected Promise when execute error', function (done) {
            isocall.addConfigs({
                test_rpc: function () {
                    throw 'internal error';
                }
            });

            isoexe.execute('test_rpc')['catch'](function (E) {
                assert.equal(E, 'internal error');
            }).then(done.bind(), done);
        });
    });

    describe('.setupMiddleware()', function () {
        it('will setup app with correct route url', function () {
            var app = {
                put: sinon.spy()
            };

            isocall.setBaseURL('haha');
            isoexe.setupMiddleware(app);
            assert.equal(app.put.getCall(0).args[0], 'haha:name');
        });
    });

    describe('middleware', function () {
        it('will run execute() with route name and request body', function (done) {
            var req = {
                params: {name: 'test'},
                body: 'OK!'
            };
            var res = {
                send: function () {}
            }

            isocall.addConfigs({
                test: function (body) {
                    assert.equal(body, 'OK!');
                    done();
                }
            });

            getMiddleware()(req, res);
        });
    });
});<commit_msg>coverage<commit_after>var assert = require('chai').assert;
var sinon = require('sinon');
var isoexe = require('../lib/iso-execute-server');
var isocall = require('../lib/isocall');

var cleanUp = function () {
    isocall.resetConfigs();
};

var getMiddleware = function () {
    var middleware;
    isoexe.setupMiddleware({
        put: function (path, parser, M) {
            middleware = M;
        }
    });
    return middleware;
}

describe('iso-execute-server', function () {
    beforeEach(cleanUp);
    afterEach(cleanUp);
    
    describe('.execute()', function () {
        it('will return rejected Promise when no input name', function (done) {
            isoexe.execute()['catch'](function (E) {
                assert.equal(E.message, 'iso-execute-server.execute without name!');
            }).then(done.bind(), done);
        });
        it('will return rejected Promise when no config found', function (done) {
            isoexe.execute('not found')['catch'](function (E) {
                assert.equal(E.message, 'iso-execute-server.execute but no config for the service: not found');
            }).then(done.bind(), done);
        });
        it('will return rejected Promise when it is not executable', function (done) {
            isocall.addConfigs({
                test_rpc: 'string_is_not_function'
            });

            isoexe.execute('test_rpc')['catch'](function (E) {
                assert.equal(E.message, 'iso-execute-server can not execute the service: test_rpc');
            }).then(done.bind(), done);
        });
        it('will return resolved Promise when execute correctly', function (done) {
            isocall.addConfigs({
                test_rpc: function () {
                    return 'OK!';
                }
            });

            isoexe.execute('test_rpc').then(function (R) {
                assert.equal(R, 'OK!');
            }).then(done.bind(), done);
        });
        it('will return rejected Promise when execute error', function (done) {
            isocall.addConfigs({
                test_rpc: function () {
                    throw 'internal error';
                }
            });

            isoexe.execute('test_rpc')['catch'](function (E) {
                assert.equal(E, 'internal error');
            }).then(done.bind(), done);
        });
    });

    describe('.setupMiddleware()', function () {
        it('will setup app with correct route url', function () {
            var app = {
                put: sinon.spy()
            };

            isocall.setBaseURL('haha');
            isoexe.setupMiddleware(app);
            assert.equal(app.put.getCall(0).args[0], 'haha:name');
        });
    });

    describe('middleware', function () {
        it('will run execute() with route name and request body', function (done) {
            var req = {
                params: {name: 'test'},
                body: 'OK!'
            };
            var res = {
                send: function () {}
            }

            isocall.addConfigs({
                test: function (body) {
                    assert.equal(body, 'OK!');
                    done();
                }
            });

            getMiddleware()(req, res);
        });
        it('will run execute() with route name and request body', function (done) {
            var req = {
                params: {name: 'test'}
            };
            var res = {
                send: function (body) {
                    assert.equal(body, 'BODY!');
                }
            }

            isocall.addConfigs({
                test: function () {
                    return 'BODY!';
                }
            });

            getMiddleware()(req, res);
    });
});
{'fraction_non_alphanumeric': 0.14444761632886097, 'fraction_numerical': 0.0005709391949757351, 'mean_word_length': 1.61455223880597, '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': '1045951', 'n_tokens_mistral': 1929, 'n_tokens_neox': 1836, 'n_words': 428}