Skip to content

Commit

Permalink
Allows implicit header param
Browse files Browse the repository at this point in the history
  • Loading branch information
Raymond Feng committed Sep 18, 2014
1 parent 17cffc1 commit f4000d0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/http-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,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 @@ -117,6 +117,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
1 change: 1 addition & 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 Down
26 changes: 26 additions & 0 deletions test/rest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,32 @@ describe('strong-remoting-rest', function(){
.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

0 comments on commit f4000d0

Please sign in to comment.