From e5b0e8cd7037527e3cba424495bb1dd32fd5d077 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Mon, 28 Jul 2014 09:37:50 -0700 Subject: [PATCH] Map exists to HEAD for REST --- lib/models/model.js | 17 ++++++++++++++- test/relations.integration.js | 39 +++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/lib/models/model.js b/lib/models/model.js index c5fb92c2..f9d60605 100644 --- a/lib/models/model.js +++ b/lib/models/model.js @@ -418,7 +418,22 @@ Model.hasManyRemoting = function (relationName, relation, define) { description: 'Foreign key for ' + relationName, required: true, http: {source: 'path'}}, description: 'Check the existence of ' + relationName + ' relation to an item by id', - returns: {} + returns: {arg: 'exists', type: 'boolean', root: true}, + rest: { + // After hook to map exists to 200/404 for HEAD + after: function(ctx, cb) { + if(ctx.result === false) { + var modelName = ctx.method.sharedClass.name; + var id = ctx.getArgByName('id'); + var msg = 'Unknown "' + modelName + '" id "' + id + '".'; + var error = new Error(msg); + error.statusCode = error.status = 404; + cb(error); + } else { + cb(); + } + } + } }, existsFunc); } }; diff --git a/test/relations.integration.js b/test/relations.integration.js index 84a15a6b..4a880e09 100644 --- a/test/relations.integration.js +++ b/test/relations.integration.js @@ -213,6 +213,45 @@ describe('relations - integration', function () { }); }); + describe('HEAD /physicians/:id/patients/rel/:fk', function () { + + before(function (done) { + var self = this; + setup(true, function (err, root) { + self.url = root.relUrl; + self.patient = root.patient; + self.physician = root.physician; + done(); + }); + }); + + lt.describe.whenCalledRemotely('HEAD', '/api/physicians/:id/patients/rel/:fk', function () { + it('should succeed with statusCode 200', function () { + assert.equal(this.res.statusCode, 200); + }); + }); + }); + + describe('HEAD /physicians/:id/patients/rel/:fk that does not exist', function () { + + before(function (done) { + var self = this; + setup(true, function (err, root) { + self.url = '/api/physicians/' + root.physician.id + + '/patients/rel/' + '999'; + self.patient = root.patient; + self.physician = root.physician; + done(); + }); + }); + + lt.describe.whenCalledRemotely('HEAD', '/api/physicians/:id/patients/rel/:fk', function () { + it('should succeed with statusCode 404', function () { + assert.equal(this.res.statusCode, 404); + }); + }); + }); + describe('DELETE /physicians/:id/patients/rel/:fk', function () { before(function (done) {