diff --git a/lib/fluent.js b/lib/fluent.js index ae758e4..229dc4b 100644 --- a/lib/fluent.js +++ b/lib/fluent.js @@ -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) { - route.setStatusCode(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; } } diff --git a/lib/route.js b/lib/route.js index 9ac0d04..a3e74c2 100644 --- a/lib/route.js +++ b/lib/route.js @@ -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) { diff --git a/lib/server.js b/lib/server.js index 1436ac8..27164e5 100644 --- a/lib/server.js +++ b/lib/server.js @@ -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); }); diff --git a/tests/javascript.test.js b/tests/javascript.test.js index 1b45487..f00ac31 100644 --- a/tests/javascript.test.js +++ b/tests/javascript.test.js @@ -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);