From 9e9a4e5455394f7ff43b7bfe75efe450835b3a03 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Wed, 17 Sep 2014 10:36:36 -0700 Subject: [PATCH 1/4] Add support for header param --- lib/http-context.js | 3 +++ test/rest.test.js | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/lib/http-context.js b/lib/http-context.js index 77af233..289415e 100644 --- a/lib/http-context.js +++ b/lib/http-context.js @@ -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; diff --git a/test/rest.test.js b/test/rest.test.js index b9f0b3d..ac37556 100644 --- a/test/rest.test.js +++ b/test/rest.test.js @@ -322,6 +322,31 @@ 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 from http req and res', function(done) { var method = givenSharedStaticMethod( function bar(req, res, cb) { From 17cffc1077c7a0e9b0c32cdb1938c9433a6d3d90 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Wed, 17 Sep 2014 10:36:48 -0700 Subject: [PATCH 2/4] Check the name type --- lib/shared-method.js | 1 + test/shared-method.test.js | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/lib/shared-method.js b/lib/shared-method.js index 45922b0..8fea949 100644 --- a/lib/shared-method.js +++ b/lib/shared-method.js @@ -65,6 +65,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; diff --git a/test/shared-method.test.js b/test/shared-method.test.js index ac16f9e..1414d0c 100644 --- a/test/shared-method.test.js +++ b/test/shared-method.test.js @@ -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); + }); }); }); From f4000d0b3db2d53d9689938148badd538fb58631 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Thu, 18 Sep 2014 14:25:24 -0700 Subject: [PATCH 3/4] Allows implicit header param --- lib/http-context.js | 3 ++- lib/http-invocation.js | 6 ++++++ lib/shared-method.js | 1 + test/rest.test.js | 26 ++++++++++++++++++++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/http-context.js b/lib/http-context.js index 289415e..b5a3c42 100644 --- a/lib/http-context.js +++ b/lib/http-context.js @@ -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 diff --git a/lib/http-invocation.js b/lib/http-invocation.js index 5429829..307dae8 100644 --- a/lib/http-invocation.js +++ b/lib/http-invocation.js @@ -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); diff --git a/lib/shared-method.js b/lib/shared-method.js index 8fea949..bdf14f9 100644 --- a/lib/shared-method.js +++ b/lib/shared-method.js @@ -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. diff --git a/test/rest.test.js b/test/rest.test.js index ac37556..9ea7d2a 100644 --- a/test/rest.test.js +++ b/test/rest.test.js @@ -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) { From 04e582d01f1f8bcbb72336295037fda83f62deea Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Thu, 18 Sep 2014 17:48:44 -0700 Subject: [PATCH 4/4] Add a test for request header param --- test/rest.browser.test.js | 21 +++++++++++++++++++++ test/rest.test.js | 1 - 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/test/rest.browser.test.js b/test/rest.browser.test.js index 9769958..7ab9a25 100644 --- a/test/rest.browser.test.js +++ b/test/rest.browser.test.js @@ -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( diff --git a/test/rest.test.js b/test/rest.test.js index 9ea7d2a..9a39f3d 100644 --- a/test/rest.test.js +++ b/test/rest.test.js @@ -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); }, {