Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #34 from jseminck/delay
Add .delay() API
  • Loading branch information
mmalecki committed Aug 6, 2018
2 parents 6a65814 + 0a0b4ee commit 4ebc80f
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 34 deletions.
95 changes: 61 additions & 34 deletions lib/request.js
Expand Up @@ -15,6 +15,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
*
Expand Down Expand Up @@ -56,6 +97,7 @@ var Request = module.exports = function (parent, options) {
this._minRequests = 1;
this._maxRequests = 1;
this._count = 0;
this._delay = 0;
};

/**
Expand Down Expand Up @@ -190,6 +232,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
*
Expand Down Expand Up @@ -236,52 +290,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
Expand Down
16 changes: 16 additions & 0 deletions test/simple-test.js
Expand Up @@ -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);
});
Expand Down

0 comments on commit 4ebc80f

Please sign in to comment.