From 0a0b4eec4e46da460be2bab9e8c6117b15363f99 Mon Sep 17 00:00:00 2001 From: Joachim Seminck Date: Wed, 31 May 2017 19:41:20 +0300 Subject: [PATCH] Add .delay() API --- lib/request.js | 95 +++++++++++++++++++++++++++++---------------- test/simple-test.js | 16 ++++++++ 2 files changed, 77 insertions(+), 34 deletions(-) diff --git a/lib/request.js b/lib/request.js index b7fbd24..cf90965 100644 --- a/lib/request.js +++ b/lib/request.js @@ -9,6 +9,47 @@ function isStream(obj) { typeof obj.setEncoding === 'function'; } +function createResponse(response) { + var self = this; + + var headers = this.response.headers || this._defaultReplyHeaders; + + response.writeHead(this.response.statusCode, headers); + + if (isStream(this.response.body)) { + var readStream = this.response.body; + + if (this._maxRequests > 1) { + // Because we need to respond with this body more than once, if it is a stream, + // we make a buffer copy and use that as the body for future responses. + var data = []; + + readStream.on('readable', function () { + var chunk; + while (null !== (chunk = readStream.read())) { + data.push(chunk); + response.write(chunk); + } + }); + readStream.on('end', function(){ + self.response.body = Buffer.concat(data); + response.end(); + }); + } + else { + readStream.pipe(response); + } + } + else if ((typeof this.response.body === 'object') && !Buffer.isBuffer(this.response.body)) { + response.end(JSON.stringify(this.response.body)); + } + else { + response.end(this.response.body); + } + + return this.shouldPrune(); +}; + /** * Request class * @@ -50,6 +91,7 @@ var Request = module.exports = function (parent, options) { this._minRequests = 1; this._maxRequests = 1; this._count = 0; + this._delay = 0; }; /** @@ -184,6 +226,18 @@ Request.prototype.any = function() { return this.many({ min: 0, max: Infinity }); } +/** + * Request.delay + * + * @description Delays the requests by number of ms + * + * @returns {boolean} + */ +Request.prototype.delay = function(ms) { + this._delay = ms; + return this; +}; + /** * Request.isMatch * @@ -229,52 +283,25 @@ Request.prototype.isMatch = function(request) { /** * Request.sendResponse * - * @description send the response to the provided Hock response + * @description send the response to the provided Hock response. Applies a delay if it was set * * @param {object} response The response object from the hock server */ Request.prototype.sendResponse = function(response) { var self = this; - this._count++; - var headers = this.response.headers || this._defaultReplyHeaders; - - response.writeHead(this.response.statusCode, headers); - - if (isStream(this.response.body)) { - var readStream = this.response.body; - - if (this._maxRequests > 1) { - // Because we need to respond with this body more than once, if it is a stream, - // we make a buffer copy and use that as the body for future responses. - var data = []; - - readStream.on('readable', function () { - var chunk; - while (null !== (chunk = readStream.read())) { - data.push(chunk); - response.write(chunk); - } - }); - readStream.on('end', function(){ - self.response.body = Buffer.concat(data); - response.end(); - }); - } - else { - readStream.pipe(response); - } - } - else if ((typeof this.response.body === 'object') && !Buffer.isBuffer(this.response.body)) { - response.end(JSON.stringify(this.response.body)); + if (this._delay > 0) { + setTimeout(function() { + createResponse.call(self, response); + }, this._delay); } else { - response.end(this.response.body); + createResponse.call(this, response); } return this.shouldPrune(); -}; +} /** * Request.isDone diff --git a/test/simple-test.js b/test/simple-test.js index 9a7b02d..9fafc9a 100644 --- a/test/simple-test.js +++ b/test/simple-test.js @@ -171,6 +171,22 @@ describe('Hock HTTP Tests', function() { }); }); + it('should work with a delay configured', function(done) { + hockInstance + .get('/url') + .delay(1000) + .reply(200, { 'hock': 'ok' }); + + request('http://localhost:' + PORT + '/url', function(err, res, body) { + should.not.exist(err); + should.exist(res); + res.statusCode.should.equal(200); + JSON.parse(body).should.eql({ 'hock': 'ok' }); + done(); + + }); + }); + after(function (done) { httpServer.close(done); });