From e33d10fe445363d8470fea581399cad6d82968a5 Mon Sep 17 00:00:00 2001 From: Mohammed Essehemy Date: Mon, 24 Sep 2018 23:34:50 +0200 Subject: [PATCH] Clear handler cache when a method is added/removed --- lib/application.js | 2 ++ test/app.test.js | 11 --------- test/rest.middleware.test.js | 48 ++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 11 deletions(-) diff --git a/lib/application.js b/lib/application.js index d68102ea..bc1ba664 100644 --- a/lib/application.js +++ b/lib/application.js @@ -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); }); diff --git a/test/app.test.js b/test/app.test.js index 3f5ad4d6..f9e6843f 100644 --- a/test/app.test.js +++ b/test/app.test.js @@ -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); diff --git a/test/rest.middleware.test.js b/test/rest.middleware.test.js index 0a7191fa..84797d58 100644 --- a/test/rest.middleware.test.js +++ b/test/rest.middleware.test.js @@ -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'});