From fc1230a6ae435c91744c0b593a354d661c646907 Mon Sep 17 00:00:00 2001 From: Darrin Edelman Date: Sun, 11 Sep 2016 15:28:37 -0500 Subject: [PATCH] Updates to support override of send while retaining existing behavior to pass through object into response. --- lib/request.js | 8 +++++++- spec/request_spec.js | 27 ++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/lib/request.js b/lib/request.js index 5927674..a5d7064 100644 --- a/lib/request.js +++ b/lib/request.js @@ -72,7 +72,13 @@ Request.prototype.end = function (callback) { transformer.call(this); - this.res.send = makeFunction(callback); + var sendOverride = makeFunction(this.res.send); + var sendDefault = makeFunction(callback); + this.res.send = function() { + sendOverride.apply(this.res, arguments); + return sendDefault.apply(this.res, arguments); + }.bind(this); + action(this.req, this.res, next); }; diff --git a/spec/request_spec.js b/spec/request_spec.js index 1fc5283..4e0381f 100644 --- a/spec/request_spec.js +++ b/spec/request_spec.js @@ -76,6 +76,20 @@ describe('request', function() { request.extendRes(data); expect(request.res.additionalFn).toBe(data.additionalFn); }); + it('should safely extend the send', function () { + var expectedResponse = {}; + var data = { + send: function() {} + }; + + spyOn(data, 'send'); + new Request(function (req, res, next) { + res.send(expectedResponse); + }).extendRes(data).end(function (res) { + expect(data.send).toHaveBeenCalled(); + expect(res).toEqual(expectedResponse); + }); + }); }); describe('next', function() { @@ -243,12 +257,15 @@ describe('request', function() { }); }); - describe('end', function() { - it('should set res.send to the provided callback', function() { - function callback() {} + describe('end', function () { + it('should call the provided callback', function () { + var wrapper = { + fn: function(err, req, res) {} + }; - request.end(callback); - expect(request.res.send).toEqual(callback); + spyOn(wrapper, 'fn'); + request.end(wrapper.fn); + expect(wrapper.fn).toHaveBeenCalled(); }); }); });