diff --git a/lib/http-invocation.js b/lib/http-invocation.js index 3ea59257..f45c6d32 100644 --- a/lib/http-invocation.js +++ b/lib/http-invocation.js @@ -164,7 +164,8 @@ HttpInvocation.prototype._processArg = function(req, verb, query, accept) { HttpInvocation.prototype.createRequest = function() { var method = this.method; - var verb = method.getHttpMethod(); + var endpoint = method.getEndpoints()[0]; + var verb = endpoint.verb; var req = this.req = {method: verb || 'GET'}; var accepts = method.accepts; var ctorAccepts = null; @@ -173,7 +174,7 @@ HttpInvocation.prototype.createRequest = function() { var auth = this.auth; // initial url is the format - req.url = this.base + method.getFullPath(); + req.url = this.base + endpoint.fullPath; var parsedUrl = urlUtil.parse(req.url); diff --git a/lib/rest-adapter.js b/lib/rest-adapter.js index 8f13ab97..21c623e3 100644 --- a/lib/rest-adapter.js +++ b/lib/rest-adapter.js @@ -16,6 +16,7 @@ RestAdapter.RestMethod = RestMethod; * Module dependencies. */ +var deprecated = require('depd')('strong-remoting'); var EventEmitter = require('events').EventEmitter; var debug = require('debug')('strong-remoting:rest-adapter'); var util = require('util'); @@ -620,11 +621,30 @@ RestMethod.prototype.acceptsSingleBodyArgument = function() { getTypeString(accepts.type) == 'object' || false; }; +RestMethod.prototype.getEndpoints = function() { + var self = this; + return this.routes.map(function(route) { + var verbResult; + var verb = route.verb; + if (verb == 'all') { + verbResult = 'POST'; + } else if (verb == 'del') { + verbResult = 'DELETE'; + } else { + verbResult = verb.toUpperCase(); + } + return { + verb: verbResult, + fullPath: joinPaths(self.restClass.getPath(), route.path), + }; + }); +}; + RestMethod.prototype.getHttpMethod = function() { - var verb = this.routes[0].verb; - if (verb == 'all') return 'POST'; - if (verb == 'del') return 'DELETE'; - return verb.toUpperCase(); + // deprecate message to let the users know what they were using + // was retuning just the first route's verb + deprecated('getHttpMethod() is deprecated, use getEndpoints()[0].verb instead.'); + return this.getEndpoints()[0].verb; }; RestMethod.prototype.getPath = function() { @@ -632,7 +652,10 @@ RestMethod.prototype.getPath = function() { }; RestMethod.prototype.getFullPath = function() { - return joinPaths(this.restClass.getPath(), this.getPath()); + // deprecate message to let the users know what they were using + // was retuning just the first route's path + deprecated('getFullPath() is deprecated, use getEndpoints()[0].fullPath instead.'); + return this.getEndpoints()[0].fullPath; }; function getTypeString(ctorOrName) { diff --git a/test/rest-adapter.test.js b/test/rest-adapter.test.js index 8c7af1c9..8ed49fe0 100644 --- a/test/rest-adapter.test.js +++ b/test/rest-adapter.test.js @@ -13,6 +13,7 @@ var SharedClass = require('../lib/shared-class'); var SharedMethod = require('../lib/shared-method'); var expect = require('chai').expect; var factory = require('./helpers/shared-objects-factory.js'); +function NOOP() {} describe('RestAdapter', function() { var remotes; @@ -218,6 +219,8 @@ describe('RestAdapter', function() { }); describe('getHttpMethod', function() { + ignoreDeprecationsInThisBlock(); + it('returns POST for `all`', function() { var method = givenRestStaticMethod({ http: { verb: 'all'} }); expect(method.getHttpMethod()).to.equal('POST'); @@ -245,6 +248,8 @@ describe('RestAdapter', function() { }); describe('getFullPath', function() { + ignoreDeprecationsInThisBlock(); + it('returns class path + method path', function() { var method = givenRestStaticMethod( { http: { path: '/a-method' } }, @@ -255,6 +260,37 @@ describe('RestAdapter', function() { }); }); + describe('getEndpoints', function() { + it('should return verb and fullPath for multiple paths', function() { + var method = givenRestStaticMethod({ http: [ + { verb: 'DEL', path: '/testMethod1' }, + { verb: 'PUT', path: '/testMethod2' }, + ] }); + + var expectedEndpoints = [ + { + fullPath: '/testClass/testMethod1', + verb: 'DELETE', + }, { + fullPath: '/testClass/testMethod2', + verb: 'PUT', + }, + ]; + + expect(method.getEndpoints()).to.eql(expectedEndpoints); + }); + + it('should return verb and fullPath for single path', function() { + var method = givenRestStaticMethod({ http: { verb: 'all' }}); + expect(method.getEndpoints()).to.eql([ + { + verb: 'POST', + fullPath: '/testClass/testMethod', + }, + ]); + }); + }); + function givenRestStaticMethod(methodConfig, classConfig) { var name = 'testMethod'; methodConfig = extend({ shared: true }, methodConfig); @@ -444,3 +480,13 @@ describe('RestAdapter', function() { function someFunc() { } + +function ignoreDeprecationsInThisBlock() { + before(function() { + process.on('deprecation', NOOP); + }); + + after(function() { + process.removeListener('deprecation', NOOP); + }); +}