Add a delay parameter for responses.

Basic support for specifying that a response should be delay be an amount of millis.
This commit is contained in:
Bryce Gibson
2014-03-19 14:02:04 +11:00
parent 79aa9fba66
commit c9f1b40d88
2 changed files with 43 additions and 8 deletions

View File

@@ -85,6 +85,7 @@ function Interfake(o) {
app[specifiedRequest.method](specifiedRequest.url, function (req, res) {
var specifiedResponse = req.route.responseData;
var afterSpecifiedResponse = req.route.afterResponseData;
var responseDelay = specifiedResponse.delay || 0;
debug(req.method, 'request to', req.url, 'returning', specifiedResponse.code);
// debug('After response is', afterSpecifiedResponse);
@@ -98,15 +99,16 @@ function Interfake(o) {
if (typeof responseBody !== 'string') responseBody = JSON.stringify(responseBody);
responseBody = req.query.callback.trim() + '(' + responseBody + ');';
}
setTimeout(function() {
res.send(specifiedResponse.code, responseBody);
res.send(specifiedResponse.code, responseBody);
if (afterSpecifiedResponse && afterSpecifiedResponse.endpoints) {
debug('Response sent, setting up', afterSpecifiedResponse.endpoints.length, 'endpoints');
afterSpecifiedResponse.endpoints.forEach(function (endpoint) {
createRoute(endpoint);
});
}
if (afterSpecifiedResponse && afterSpecifiedResponse.endpoints) {
debug('Response sent, setting up', afterSpecifiedResponse.endpoints.length, 'endpoints');
afterSpecifiedResponse.endpoints.forEach(function (endpoint) {
createRoute(endpoint);
});
}
}, responseDelay)
});
setRouteProperty(specifiedRequest, 'responseData', data.response);

View File

@@ -250,6 +250,39 @@ describe('Interfake JavaScript API', function () {
done();
});
});
it('should create one GET endpoint with support for delaying the response', function (done) {
var interfake = new Interfake();
var enoughTimeHasPassed = false;
var _this = this;
this.slow(500)
interfake.createRoute({
request: {
url: '/test',
method: 'get'
},
response: {
code: 200,
delay: 200,
body: {
hi: 'there'
}
}
});
interfake.listen(3000);
timer = setTimeout(function() {
enoughTimeHasPassed = true;
}, 200)
request({ url : 'http://localhost:3000/test', json : true }, function (error, response, body) {
assert.equal(response.statusCode, 200);
assert.equal(body.hi, 'there');
interfake.stop();
if(!enoughTimeHasPassed) {
throw new Error('Response wasn\'t delay for long enough');
}
done();
});
});
});
// Testing the fluent interface