Clear handler cache when a method is added/removed

This commit is contained in:
Mohammed Essehemy 2018-09-24 23:34:50 +02:00 committed by Miroslav Bajtoš
parent 97a55bf67a
commit e33d10fe44
No known key found for this signature in database
GPG Key ID: 6F2304BA9361C7E3
3 changed files with 50 additions and 11 deletions

View File

@ -151,9 +151,11 @@ app.model = function(Model, config) {
var self = this;
Model.on('remoteMethodDisabled', function(model, methodName) {
clearHandlerCache(self);
self.emit('remoteMethodDisabled', model, methodName);
});
Model.on('remoteMethodAdded', function(model) {
clearHandlerCache(self);
self.emit('remoteMethodAdded', model);
});

View File

@ -719,17 +719,6 @@ describe('app', function() {
expect(remoteMethodAddedClass).to.eql(Book.sharedClass);
});
it.onServer('updates REST API when a new model is added', function(done) {
app.use(loopback.rest());
request(app).get('/colors').expect(404, function(err, res) {
if (err) return done(err);
var Color = PersistedModel.extend('color', {name: String});
app.model(Color);
Color.attachTo(db);
request(app).get('/colors').expect(200, done);
});
});
it('accepts null dataSource', function(done) {
app.model(MyTestModel, {dataSource: null});
expect(MyTestModel.dataSource).to.eql(null);

View File

@ -200,6 +200,16 @@ describe('loopback.rest', function() {
}, done);
});
it('rebuilds REST endpoints after a model was added', () => {
app.use(loopback.rest());
return request(app).get('/mymodels').expect(404).then(() => {
app.model(MyModel);
return request(app).get('/mymodels').expect(200);
});
});
it('rebuilds REST endpoints after a model was deleted', () => {
app.model(MyModel);
app.use(loopback.rest());
@ -212,6 +222,44 @@ describe('loopback.rest', function() {
});
});
it('rebuilds REST endpoints after a remoteMethod was added', () => {
app.model(MyModel);
app.use(loopback.rest());
return request(app).get('/mymodels/customMethod').expect(404)
.then(() => {
MyModel.customMethod = function(req, cb) {
cb(null, true);
};
MyModel.remoteMethod('customMethod', {
http: {verb: 'get'},
accepts: [{type: 'object', http: {source: 'req'}}],
returns: [{type: 'boolean', name: 'success'}],
});
return request(app).get('/mymodels/customMethod').expect(200);
});
});
it('rebuilds REST endpoints after a remoteMethod was disabled', () => {
app.model(MyModel);
app.use(loopback.rest());
MyModel.customMethod = function(req, cb) {
cb(null, true);
};
MyModel.remoteMethod('customMethod', {
http: {verb: 'get'},
accepts: [{type: 'object', http: {source: 'req'}}],
returns: [{type: 'boolean', name: 'success'}],
});
return request(app).get('/mymodels/customMethod').expect(200)
.then(() => {
MyModel.disableRemoteMethodByName('customMethod');
return request(app).get('/mymodels/customMethod').expect(404);
});
});
function givenUserModelWithAuth() {
var AccessToken = app.registry.getModel('AccessToken');
app.model(AccessToken, {dataSource: 'db'});