mirror of
https://github.com/zhigang1992/interfake.git
synced 2026-01-12 17:23:07 +08:00
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:
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user