Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/support header param #104

Merged
merged 4 commits into from
Sep 19, 2014
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
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 @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not covered by any unit-test, is it?

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);
});
});
});