<commit_before>"use strict";

require("nodeclass").registerExtension();

var expect = require("expect.js"),
    rewire = require("rewire"),
    path = require("path"),
    Request = require("../../../lib/server/request/Request.class.js"),
    config = require("../../../lib/shared/config"),
    collectMiddleware = require("../../../lib/server/collectMiddleware.js"),
    middleware = require("../../../lib/server/request/middleware.js");


describe("handleRequest", function() {

    var handleRequest;

    beforeEach(function() {
        handleRequest = rewire("../../../lib/server/request/handleRequest.js", false);
    });

    describe("#Services Request", function() {

        beforeEach(function() {
            function runServiceMock(req, res, next) {
                next();
            }

            function getMiddlewareMock() {

                return [
                    function a(req, res, next) {
                        next();
                    },
                    function b(req, res,  next) {
                        next();
                    }
                ];
            }

            handleRequest.__set__("getMiddleware", getMiddlewareMock);
            handleRequest.__set__("runService", runServiceMock);
        });

        it("should handle the request and return without an error if all middleware worked fine", function(done) {
            var req = new Request("create", "/services/blogPost", { da : "ta" });

            handleRequest(req, function(err, resReq, resRes) {
                expect(err).to.be(null);
                expect(resReq).to.eql(req);
                expect(resRes).not.to.be("undefined");
                done();
            });
        });
    });

    it("should handle the request and return without an error if all middleware worked fine", function(done) {

        function getMiddlewareMock() {
            return [
                function a(req, res, next) {
                    next(new Error("middleware a failed"));
                },
                function b(req, res,  next) {
                    next(null);
                }
            ];
        }

        handleRequest.__set__("getMiddleware", getMiddlewareMock);
        var req = new Request("create", "/services/blogPost", {});

        handleRequest(req, function(err, resReq) {
            expect(err.message).to.contain("middleware a failed");
            expect(resReq).to.eql(req);
            done();
        });
    });

    describe("#Invalid Requests", function() {

        it("should end the request if the type is not allowed", function(done) {
            var req = new Request("create", "/services/blogPost", {});
            req.getType = function() {
                return "unsupportedType";
            };

            handleRequest(req, function(err) {
                expect(err).not.to.be(null);
                done();
            });
        });
    });


    describe("#Request with Middleware", function() {
        it("should run the defined middlewares", function(done) {
            var mwPath = path.resolve(__dirname, "../../exampleApp/app/services/servicesMiddleware.js");
            collectMiddleware([], mwPath, function(err, servicesMiddleware) {

                middleware.setMiddleware("services", servicesMiddleware);
                handleRequest = rewire("../../../lib/server/request/handleRequest.js", false);
                handleRequest.__set__("getMiddleware", middleware.getMiddleware);

                var req = new Request("create", "/services/blog", {});

                handleRequest(req, function(err) {
                    expect(req.getData()).to.eql({ fancy : true });
                    expect(err).not.to.be(null);
                    done();
                });

            });
        });
    });
});<commit_msg>- fixed handleRequest test<commit_after>"use strict";

require("nodeclass").registerExtension();

var expect = require("expect.js"),
    rewire = require("rewire"),
    path = require("path"),
    Request = require("../../../lib/server/request/Request.class.js"),
    config = require("../../../lib/shared/config"),
    collectMiddleware = require("../../../lib/server/collectMiddleware.js"),
    middleware = require("../../../lib/server/request/middleware.js");


describe("handleRequest", function() {

    var handleRequest;

    beforeEach(function() {
        handleRequest = rewire("../../../lib/server/request/handleRequest.js", false);
    });

    describe("#Services Request", function() {

        beforeEach(function() {
            function runServiceMock(req, res, next) {
                next();
            }

            function getMiddlewareMock() {

                return [
                    function a(req, res, next) {
                        next();
                    },
                    function b(req, res,  next) {
                        next();
                    }
                ];
            }

            handleRequest.__set__("getMiddleware", getMiddlewareMock);
            handleRequest.__set__("runService", runServiceMock);
        });

        it("should handle the request and return without an error if all middleware worked fine", function(done) {
            var req = new Request("create", "/services/blogPost", { da : "ta" });

            handleRequest(req, function(err, resReq, resRes) {
                expect(err).to.be(null);
                expect(resReq).to.eql(req);
                expect(resRes).not.to.be("undefined");
                done();
            });
        });
    });

    it("should handle the request and return without an error if all middleware worked fine", function(done) {

        function getMiddlewareMock() {
            return [
                function a(req, res, next) {
                    next(new Error("middleware a failed"));
                },
                function b(req, res,  next) {
                    next(null);
                }
            ];
        }

        handleRequest.__set__("getMiddleware", getMiddlewareMock);
        var req = new Request("create", "/services/blogPost", {});

        handleRequest(req, function(err, resReq) {
            expect(err.message).to.contain("middleware a failed");
            expect(resReq).to.eql(req);
            done();
        });
    });

    describe("#Invalid Requests", function() {

        it("should end the request if the type is not allowed", function(done) {
            var req = new Request("create", "/services/blogPost", {});
            req.getType = function() {
                return "unsupportedType";
            };

            handleRequest(req, function(err) {
                expect(err).not.to.be(null);
                done();
            });
        });
    });


    describe("#Request with Middleware", function() {
        it("should run the defined middleware", function(done) {
            var mwPath = path.resolve(__dirname, "../../exampleApp/app/services/servicesMiddleware.js");

            var servicesMiddleware = collectMiddleware([], mwPath);

            middleware.setMiddleware("services", servicesMiddleware);

            handleRequest = rewire("../../../lib/server/request/handleRequest.js", false);
            handleRequest.__set__("getMiddleware", middleware.getMiddleware);

            var req = new Request("create", "/services/blog", {});

            handleRequest(req, function(err) {
                expect(req.getData()).to.eql({ fancy : true });
                expect(err).not.to.be(null);
                done();
            });
        });
    });
});
{'fraction_non_alphanumeric': 0.14742661576938265, 'fraction_numerical': 0.0, 'mean_word_length': 1.7223217484772484, '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': '2967061', 'n_tokens_mistral': 1932, 'n_tokens_neox': 1861, 'n_words': 387}