Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/http-invocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand Down
33 changes: 28 additions & 5 deletions lib/rest-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -620,19 +621,41 @@ 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() {
return this.routes[0].path;
};

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) {
Expand Down
46 changes: 46 additions & 0 deletions test/rest-adapter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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' } },
Expand All @@ -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);
Expand Down Expand Up @@ -444,3 +480,13 @@ describe('RestAdapter', function() {

function someFunc() {
}

function ignoreDeprecationsInThisBlock() {
before(function() {
process.on('deprecation', NOOP);
});

after(function() {
process.removeListener('deprecation', NOOP);
});
}