Skip to content

Commit

Permalink
Merge pull request #104 from strongloop/feature/support-header-param
Browse files Browse the repository at this point in the history
Feature/support header param
  • Loading branch information
raymondfeng committed Sep 19, 2014
2 parents 4e12af8 + 04e582d commit 6a54e62
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/http-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ HttpContext.prototype.buildArgs = function (method) {
// From the url path
val = this.req.params[name];
break;
case 'header':
val = this.req.get(name);
break;
case 'req':
// Direct access to http req
val = this.req;
Expand Down Expand Up @@ -131,11 +134,12 @@ HttpContext.prototype.getArgByName = function (name, options) {
args = {};
}

var arg = (args && args[name]) || this.req.param(name);
var arg = (args && args[name]) || this.req.param(name) || this.req.get(name);
// search these in order by name
// req.params
// req.body
// req.query
// req.header


// coerce simple types in objects
Expand Down
6 changes: 6 additions & 0 deletions lib/http-invocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ HttpInvocation.prototype.createRequest = function () {
query[name] = val;
}
break;
case 'header':
if(val !== undefined) {
req.headers = req.headers || {};
req.headers[name] = val;
}
break;
case 'path':
// From the url path
req.url = req.url.replace(':' + name, val);
Expand Down
2 changes: 2 additions & 0 deletions lib/shared-method.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var EventEmitter = require('events').EventEmitter
* - `form` - `req.body[argumentName]`
* - `query` - `req.query[argumentName]`
* - `path` - `req.params[argumentName]`
* - `header` - `req.headers[argumentName]`
* - `context` - the current `HttpContext`
* @param {Object} [options.accepts.rest] The REST mapping / settings for the
* argument.
Expand All @@ -65,6 +66,7 @@ function SharedMethod(fn, name, sc, options) {
this.fn = fn;
fn = fn || {};
this.name = name;
assert(typeof name === 'string', 'The method name must be a string');
options = options || {};
this.aliases = options.aliases || [];
var isStatic = this.isStatic = options.isStatic || false;
Expand Down
21 changes: 21 additions & 0 deletions test/rest.browser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,27 @@ describe('strong-remoting-rest', function(){
});
});

it('should allow arguments in the header', function(done) {
var method = givenSharedStaticMethod(
function bar(a, b, cb) {
cb(null, a + b);
},
{
accepts: [
{ arg: 'b', type: 'number' },
{ arg: 'a', type: 'number', http: {source: 'header' } }
],
returns: { arg: 'n', type: 'number' },
http: { path: '/' }
}
);

objects.invoke(method.name, [1, 2], function(err, n) {
assert.equal(n, 3);
done();
});
});

it('should pass undefined if the argument is not supplied', function (done) {
var called = false;
var method = givenSharedStaticMethod(
Expand Down
52 changes: 51 additions & 1 deletion test/rest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ describe('strong-remoting-rest', function(){
it('should allow string[] arg in the query', function(done) {
var method = givenSharedStaticMethod(
function bar(a, b, cb) {
console.log(a, b, typeof b);
cb(null, b.join('') + a);
},
{
Expand Down Expand Up @@ -322,6 +321,57 @@ describe('strong-remoting-rest', function(){
.expect({ n: 3 }, done);
});

it('should allow arguments in the header', function(done) {
var method = givenSharedStaticMethod(
function bar(a, b, cb) {
cb(null, a + b);
},
{
accepts: [
{ arg: 'b', type: 'number', http: {source: 'header' } },
{ arg: 'a', type: 'number', http: {source: 'header' } }
],
returns: { arg: 'n', type: 'number' },
http: { verb: 'get', path: '/' }
}
);

request(app)['get'](method.classUrl)
.set('Accept', 'application/json')
.set('Content-Type', 'application/json')
.set('a', 1)
.set('b', 2)
.send()
.expect('Content-Type', /json/)
.expect({ n: 3 }, done);
});

it('should allow arguments in the header without http source',
function(done) {
var method = givenSharedStaticMethod(
function bar(a, b, cb) {
cb(null, a + b);
},
{
accepts: [
{ arg: 'b', type: 'number' },
{ arg: 'a', type: 'number' }
],
returns: { arg: 'n', type: 'number' },
http: { verb: 'get', path: '/' }
}
);

request(app)['get'](method.classUrl)
.set('Accept', 'application/json')
.set('Content-Type', 'application/json')
.set('a', 1)
.set('b', 2)
.send()
.expect('Content-Type', /json/)
.expect({ n: 3 }, done);
});

it('should allow arguments from http req and res', function(done) {
var method = givenSharedStaticMethod(
function bar(req, res, cb) {
Expand Down
11 changes: 11 additions & 0 deletions test/shared-method.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,16 @@ describe('SharedMethod', function() {
assert.equal(sharedMethod.isDelegateFor('myAlias', true), true);
assert.equal(sharedMethod.isDelegateFor('myAlias', false), false);
});

it('checks if the given name is a string', function () {
var mockSharedClass = {};
var err;
try {
var sharedMethod = new SharedMethod(myFunction, Number, mockSharedClass);
} catch(e) {
err = e;
}
assert(err);
});
});
});

0 comments on commit 6a54e62

Please sign in to comment.