Made good progress with modifies. Need to merge, not replace

This commit is contained in:
Dan Hough
2014-08-06 23:08:00 +01:00
parent 7b0e9fcead
commit e5e9a941d9
3 changed files with 57 additions and 22 deletions

View File

@@ -23,7 +23,7 @@ function FluentInterface(server, o) {
route = parent.addAfterResponse(routeDescriptor);
}
return {
var fluentInterface = {
query: function (query) {
route.setQueryStrings(query);
return this;
@@ -33,6 +33,7 @@ function FluentInterface(server, o) {
return this;
},
body: function (body) {
debug('Changing body of', route.simpleHash(), 'to', body);
route.setResponseBody(body);
return this;
},
@@ -49,8 +50,18 @@ function FluentInterface(server, o) {
put: forMethod('put', route),
post: forMethod('post', route),
delete: forMethod('delete', route)
},
modifies: {
get: function (path) {
route = server.routeMatching({
method: 'get',
url: path
});
return fluentInterface;
}
}
};
return fluentInterface;
};
}

View File

@@ -184,6 +184,28 @@ function Interfake(o) {
app.use(path, express.static(directory));
};
this.routeMatching = function (requestDescriptor) {
var incomingRoute = new Route({
request: requestDescriptor
}, o);
var expectData = expectationsLookup[incomingRoute.simpleHash()];
if (!expectData || (util.isArray(expectData.length) && expectData.length === 0)) {
return;
}
expectData = expectData.filter(function (route) {
return route.compare(incomingRoute);
});
if (expectData.length > 0) {
return expectData.pop();
} else {
return;
}
};
this.listen = function (port, callback) {
port = port || 3000;
server = app.listen(port, function () {

View File

@@ -714,28 +714,30 @@ describe('Interfake JavaScript API', function () {
});
});
// describe('#modifies', function () {
// it('should create a GET endpoint which modifies its own body when it gets called', function (done) {
// interfake.get('/fluent').body({ hello : 'there', goodbye: 'for now' }).modifies.get('/fluent').body({ what: 'ever' });
// interfake.listen(3000);
describe('#modifies', function () {
it('should create a GET endpoint which modifies its own body when it gets called', function (done) {
interfake = new Interfake({debug:true});
interfake.get('/fluent').body({ hello : 'there', goodbye: 'for now' }).modifies.get('/fluent').body({ what: 'ever' });
interfake.listen(3000);
// get('http://localhost:3000/fluent')
// .then(function (results) {
// assert.equal(results[0].statusCode, 200);
// assert.equal(results[1].hello, 'there');
// assert.equal(results[1].goodbye, 'for now');
// assert.equal(results[2].what, undefined);
// return get('http://localhost:3000/fluent');
// })
// .then(function (results) {
// assert.equal(results[0].statusCode, 200);
// assert.equal(results[1].hello, 'there');
// assert.equal(results[1].goodbye, 'for now');
// assert.equal(results[2].what, 'ever');
// done();
// });
// });
// });
get('http://localhost:3000/fluent')
.then(function (results) {
assert.equal(results[0].statusCode, 200);
assert.equal(results[1].hello, 'there');
assert.equal(results[1].goodbye, 'for now');
assert.equal(results[2].what, undefined);
return get('http://localhost:3000/fluent');
})
.then(function (results) {
assert.equal(results[0].statusCode, 200);
assert.equal(results[1].hello, 'there');
assert.equal(results[1].goodbye, 'for now');
assert.equal(results[2].what, 'ever');
done();
})
.done();
});
});
describe('#status()', function () {
it('should create one GET endpoint with a particular status code', function (done) {