<commit_before>'use strict';
var http = require('http');
var url = require('url');
var restitute = require('../src/index');
var sampleController = require('./sampleController');



var app = function(req, res) {

    req.app = app;
	var path = url.parse(req.url).pathname;


    function forwardToController(controller)
    {

        if (path === controller.path) {
            // this is the matching route

            controller.onRequest(req, res).then(function() {
                req.connection.destroy();
            });

            return true;
        }

        return false;
    }


    // search matching route

    for(var method in sampleController) {

        var controller = new sampleController[method]();

        if (forwardToController(controller)) {
            return;
        }
    }


    res.statusCode = 404;
    res.end(JSON.stringify({ message: 'No matching query' }));
    req.connection.destroy();
};


app.getService = function(path) {
    var apiservice = require('../src/service');
    var serviceLoader = require('./services/'+path);
    return serviceLoader(apiservice, app);
};


var server = http.createServer(app);
server.listen(3000);






describe('restitute', function RestituteTestSuite() {

	it('receive an array from a list controller', function (done){
		http.get('http://localhost:3000/rest/listTestController', function(response) {
			expect(response.statusCode).toBe(200);


            response.setEncoding('utf8');
            response.on('data', function (chunk) {
                var json = JSON.parse(chunk);
                expect(json.length).toBe(1);
                expect(json[0].name).toBe('TEST');
            });

            response.on('end', function() {
                done();
            });
		});
	});



    it('receive an object from a get controller', function (done){
		http.get('http://localhost:3000/rest/getTestController', function(response) {
			expect(response.statusCode).toBe(200);


            response.setEncoding('utf8');
            response.on('data', function (chunk) {
                var json = JSON.parse(chunk);
                expect(json.name).toBe('TEST');
            });

            response.on('end', function() {
                done();
            });
		});
	});
    


    
    
    it('receive an object from a delete controller', function (done){
		http.get('http://localhost:3000/rest/deleteTestController', function(response) {
			expect(response.statusCode).toBe(200);


            response.setEncoding('utf8');
            response.on('data', function (chunk) {
                var json = JSON.parse(chunk);
                expect(json.name).toBe('TEST');
            });

            response.on('end', function() {
                done();
            });
		});
	});
    
    
    

    function saveTest(method, next) {
        var req = http.request({
                host: 'localhost',
                port: 3000,
                path: '/rest/saveTestController',
                method: 'PUT' }, function(response) {
			expect(response.statusCode).toBe(200);


            response.setEncoding('utf8');
            response.on('data', function (chunk) {
                var json = JSON.parse(chunk);
                expect(json.name).toBe('TEST');
            });

            response.on('end', next);
		});

        req.write('data\n');
        req.end();
    }



    it('receive an object from a save controller via PUT (edit)', function (done){
		saveTest('PUT', done);<commit_msg>tests<commit_after>'use strict';
var http = require('http');
var url = require('url');
var restitute = require('../src/index');
var sampleController = require('./sampleController');



var app = function(req, res) {

    req.app = app;
	var path = url.parse(req.url).pathname;


    function forwardToController(controller)
    {

        if (path === controller.path) {
            // this is the matching route

            controller.onRequest(req, res).then(function() {
                req.connection.destroy();
            });

            return true;
        }

        return false;
    }


    // search matching route

    for(var method in sampleController) {

        var controller = new sampleController[method]();

        if (forwardToController(controller)) {
            return;
        }
    }


    res.statusCode = 404;
    res.end(JSON.stringify({ message: 'No matching query' }));
    req.connection.destroy();
};


app.getService = function(path) {
    var apiservice = require('../src/service');
    var serviceLoader = require('./services/'+path);
    return serviceLoader(apiservice, app);
};


var server = http.createServer(app);
server.listen(3000);






describe('restitute', function RestituteTestSuite() {

	it('receive an array from a list controller', function (done){
		http.get('http://localhost:3000/rest/listTestController?readonly=2&modifiable=1', function(response) {
			expect(response.statusCode).toBe(200);


            response.setEncoding('utf8');
            response.on('data', function (chunk) {
                var json = JSON.parse(chunk);
                expect(json.length).toBe(1);
                expect(json[0].name).toBe('TEST');
                expect(json.readonly).toBe('2');
            });

            response.on('end', function() {
                done();
            });
		});
	});



    it('receive an object from a get controller', function (done){
		http.get('http://localhost:3000/rest/getTestController?readonly=2&modifiable=1', function(response) {
			expect(response.statusCode).toBe(200);


            response.setEncoding('utf8');
            response.on('data', function (chunk) {
                var json = JSON.parse(chunk);
                expect(json.name).toBe('TEST');
                expect(json.readonly).toBe('2');
            });

            response.on('end', function() {
                done();
            });
		});
	});
    


    
    
    it('receive an object from a delete controller', function (done){
		var req = http.request({
                host: 'localhost',
                port: 3000,
                path: '/rest/deleteTestController',
                method: 'DELETE' }, function(response) {
			expect(response.statusCode).toBe(200);


            response.setEncoding('utf8');
            response.on('data', function (chunk) {
                var json = JSON.parse(chunk);
                expect(json.name).toBe('TEST');
                expect(json.readonly).toBe('2');
            });

            response.on('end', function() {
                done();
            });
		});

        req.write('{ readonly: 2, modifiable: 1 }\n');
        req.end();
	});
    
    
    

    function saveTest(method, next) {
        var req = http.request({
                host: 'localhost',
                port: 3000,
                path: '/rest/saveTestController',
                method: method }, function(response) {
			expect(response.statusCode).toBe(200);


            response.setEncoding('utf8');
            response.on('data', function (chunk) {
                var json = JSON.parse(chunk);
                expect(json.name).toBe('TEST');
                expect(json.readonly).toBe('2');
            });

            response.on('end', next);
		});

        req.write('{ readonly: 2, modifiable: 1 }\n');
        req.end();
    }



    it('receive an object from a save controller via PUT (edit)', function (done){
		saveTest('PUT', done);
{'fraction_non_alphanumeric': 0.14946380697050937, 'fraction_numerical': 0.01260053619302949, 'mean_word_length': 1.976067012365377, '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': '621226', 'n_tokens_mistral': 2188, 'n_tokens_neox': 1955, 'n_words': 409}