mirror of
https://github.com/zhigang1992/interfake.git
synced 2026-01-12 17:23:07 +08:00
Made good progress with modifies. Need to merge, not replace
This commit is contained in:
@@ -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;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -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 () {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user