Skip to content

Commit

Permalink
Merge pull request #4011 from MohammedEssehemy/fix/clear-cache-handle…
Browse files Browse the repository at this point in the history
…r-after-method-add

clearHandlerCache when remote method added or disabled
  • Loading branch information
bajtos authored Oct 9, 2018
2 parents 97a55bf + e33d10f commit 0488cb7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
2 changes: 2 additions & 0 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down
11 changes: 0 additions & 11 deletions test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
48 changes: 48 additions & 0 deletions test/rest.middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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'});
Expand Down

0 comments on commit 0488cb7

Please sign in to comment.