mirror of
https://github.com/zhigang1992/interfake.git
synced 2026-01-12 22:48:04 +08:00
Will now modify body after response is sent!!
This commit is contained in:
@@ -4,7 +4,7 @@ function FluentInterface(server, o) {
|
||||
|
||||
function forMethod(method, parent) {
|
||||
return function (originalPath) {
|
||||
var route, merge = false;
|
||||
var route, modifyDescriptor;
|
||||
var routeDescriptor = {
|
||||
request: {
|
||||
url: originalPath,
|
||||
@@ -26,13 +26,17 @@ function FluentInterface(server, o) {
|
||||
return this;
|
||||
},
|
||||
status: function (status) {
|
||||
if (modifyDescriptor) {
|
||||
modifyDescriptor.status = status;
|
||||
} else {
|
||||
route.setStatusCode(status);
|
||||
}
|
||||
return this;
|
||||
},
|
||||
body: function (body) {
|
||||
if (merge) {
|
||||
if (modifyDescriptor) {
|
||||
debug('Merging body for', route.simpleHash(), 'with', body);
|
||||
route.mergeResponseBody(body);
|
||||
modifyDescriptor.body = body;
|
||||
} else {
|
||||
debug('Changing body of', route.simpleHash(), 'to', body);
|
||||
route.setResponseBody(body);
|
||||
@@ -55,12 +59,13 @@ function FluentInterface(server, o) {
|
||||
},
|
||||
modifies: {
|
||||
get: function (path) {
|
||||
modifyDescriptor = {};
|
||||
// TODO This needs its own set of methods for merge rather than replace. At least, body does.
|
||||
route = server.routeMatching({
|
||||
modifyDescriptor.route = server.routeMatching({
|
||||
method: 'get',
|
||||
url: path
|
||||
});
|
||||
merge = true;
|
||||
route.addModification(modifyDescriptor);
|
||||
return fluentInterface;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,6 +85,14 @@ Route.prototype.addAfterResponse = function (descriptors) {
|
||||
return newRoute;
|
||||
};
|
||||
|
||||
Route.prototype.addModification = function (modifyDescriptor) {
|
||||
if (!this.afterResponse.modifications) {
|
||||
this.afterResponse.modifications = [];
|
||||
}
|
||||
|
||||
this.afterResponse.modifications.push(modifyDescriptor);
|
||||
};
|
||||
|
||||
Route.prototype.creates = function (routeDescriptor) {
|
||||
var newRoute = new Route(routeDescriptor, this.o);
|
||||
if (!this.afterResponse) {
|
||||
|
||||
@@ -118,6 +118,7 @@ function Interfake(o) {
|
||||
}
|
||||
|
||||
setTimeout(function() {
|
||||
var modification;
|
||||
debug('Expected response is', specifiedResponse);
|
||||
|
||||
res.send(specifiedResponse.code, responseBody);
|
||||
@@ -129,6 +130,20 @@ function Interfake(o) {
|
||||
addRoute(route);
|
||||
});
|
||||
}
|
||||
|
||||
if (afterSpecifiedResponse && afterSpecifiedResponse.modifications) {
|
||||
debug('Response sent, making', afterSpecifiedResponse.modifications.length, 'modifications');
|
||||
while (afterSpecifiedResponse.modifications.length > 0) {
|
||||
modification = afterSpecifiedResponse.modifications.pop();
|
||||
if (modification.body) {
|
||||
modification.route.mergeResponseBody(modification.body);
|
||||
}
|
||||
|
||||
if (modification.status) {
|
||||
modification.route.setStatusCode(modification.status);
|
||||
}
|
||||
}
|
||||
}
|
||||
}, responseDelay);
|
||||
});
|
||||
|
||||
|
||||
@@ -727,7 +727,7 @@ describe('Interfake JavaScript API', function () {
|
||||
assert.equal(results[1].hello, 'there');
|
||||
assert.equal(results[1].goodbye, 'for now');
|
||||
assert.equal(results[1].what, undefined);
|
||||
return get('http://localhost:3000/fluent');
|
||||
return get({url:'http://localhost:3000/fluent',json:true});
|
||||
})
|
||||
.then(function (results) {
|
||||
assert.equal(results[0].statusCode, 200);
|
||||
|
||||
Reference in New Issue
Block a user