mirror of
https://github.com/zhigang1992/interfake.git
synced 2026-04-29 04:25:17 +08:00
#23 Proper delay support. Works for non-existant routes before they've been created
This commit is contained in:
@@ -5,16 +5,11 @@ function FluentInterface(server, o) {
|
||||
function fluentModify(method, route) {
|
||||
return function (path) {
|
||||
var modifyDescriptor = {
|
||||
afterResponse: {
|
||||
endpoints: []
|
||||
routeDescriptor: {
|
||||
method: method,
|
||||
url: path
|
||||
}
|
||||
};
|
||||
// TODO This needs its own set of methods for merge rather than replace. At least, body does.
|
||||
// It will need to be possible for modifies -> creates so a whole new set of methods is defo needed
|
||||
modifyDescriptor.route = server.routeMatching({
|
||||
method: method,
|
||||
url: path
|
||||
});
|
||||
|
||||
route.addModification(modifyDescriptor);
|
||||
|
||||
|
||||
@@ -135,6 +135,13 @@ function Interfake(o) {
|
||||
debug('Response sent, making', afterSpecifiedResponse.modifications.length, 'modifications');
|
||||
while (afterSpecifiedResponse.modifications.length > 0) {
|
||||
modification = afterSpecifiedResponse.modifications.pop();
|
||||
|
||||
modification.route = this.routeMatching(modification.routeDescriptor);
|
||||
|
||||
if (!modification.route) {
|
||||
throw new Error('No route matching', modification.routeDescriptor, 'was found');
|
||||
}
|
||||
|
||||
if (modification.body) {
|
||||
debug('Modifying body');
|
||||
modification.route.mergeResponseBody(modification.body);
|
||||
@@ -151,8 +158,8 @@ function Interfake(o) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}, responseDelay);
|
||||
});
|
||||
}.bind(this), responseDelay);
|
||||
}.bind(this));
|
||||
|
||||
function addRoute(route) {
|
||||
if (expectationsLookup[route.simpleHash()]) {
|
||||
@@ -164,18 +171,6 @@ function Interfake(o) {
|
||||
|
||||
function createRoute(data) {
|
||||
var newRoute;
|
||||
// This may no longer be necessary
|
||||
// if (!data.request || !data.request.method || !data.request.url || !data.response || !data.response.code) {
|
||||
// throw createInvalidDataException(data);
|
||||
// }
|
||||
|
||||
// var numAfterResponse = (data.afterResponse && data.afterResponse.endpoints) ? data.afterResponse.endpoints.length : 0;
|
||||
|
||||
// if (data.response.body) {
|
||||
// debug('Setting up ' + data.request.method + ' ' + data.request.url + ' to return ' + data.response.code + ' with a body of length ' + JSON.stringify(data.response.body).length + ' and ' + numAfterResponse + ' after-responses');
|
||||
// } else {
|
||||
// debug('Setting up ' + data.request.method + ' ' + data.request.url + ' to return ' + data.response.code + ' with no body');
|
||||
// }
|
||||
|
||||
debug('Setting up new route');
|
||||
|
||||
@@ -185,12 +180,6 @@ function Interfake(o) {
|
||||
|
||||
debug('Setup complete');
|
||||
|
||||
// if (data.response.body) {
|
||||
// debug('Setup complete: ' + data.request.method + ' ' + data.request.url + ' to return ' + data.response.code + ' with a body of length ' + JSON.stringify(data.response.body).length + ' and ' + numAfterResponse + ' after-responses');
|
||||
// } else {
|
||||
// debug('Setup complete: ' + data.request.method + ' ' + data.request.url + ' to return ' + data.response.code + ' with no body and ' + numAfterResponse + ' after-responses');
|
||||
// }
|
||||
|
||||
return newRoute;
|
||||
}
|
||||
|
||||
|
||||
@@ -742,7 +742,6 @@ describe('Interfake JavaScript API', function () {
|
||||
|
||||
describe('#status()', function () {
|
||||
it('should create a GET endpoint which modifies its own status when it gets called', function (done) {
|
||||
interfake = new Interfake({debug:true});
|
||||
interfake.get('/fluent').body({ hello : 'there', goodbye: 'for now' }).modifies.get('/fluent').status(401);
|
||||
interfake.listen(3000);
|
||||
|
||||
@@ -806,23 +805,20 @@ describe('Interfake JavaScript API', function () {
|
||||
tookTooLong = true;
|
||||
}, 50);
|
||||
|
||||
get({url:'http://localhost:3000/needs-delay',json:true})
|
||||
get({url:'http://localhost:3000/needs-delay', json:true})
|
||||
.then(function (results) {
|
||||
if (tookTooLong) {
|
||||
throws new Error('The response took too long the first time');
|
||||
}
|
||||
assert.equal(results[0].statusCode, 200);
|
||||
assert.equal(results[1].hello, 'there');
|
||||
assert.equal(results[1].goodbye, 'for now');
|
||||
assert.equal(results[1].what, undefined);
|
||||
return get({url:'http://localhost:3000/fluent',json:true});
|
||||
if (tookTooLong) {
|
||||
throw new Error('The response took too long the first time');
|
||||
}
|
||||
return get({url:'http://localhost:3000/fluent', json:true});
|
||||
})
|
||||
.then(function (results) {
|
||||
assert.equal(results[0].statusCode, 200);
|
||||
setTimeout(function() {
|
||||
enoughTimeHasPassed = true;
|
||||
}, 50);
|
||||
return get({url:'http://localhost:3000/needs-delay',json:true});
|
||||
return get({url:'http://localhost:3000/needs-delay', json:true});
|
||||
})
|
||||
.then(function (results) {
|
||||
assert.equal(results[0].statusCode, 200);
|
||||
|
||||
Reference in New Issue
Block a user