From b246b797ece2100a61125a2c16f2c863cb03bd83 Mon Sep 17 00:00:00 2001 From: Pradnya Baviskar Date: Tue, 28 Apr 2015 16:47:47 +0530 Subject: [PATCH] support type string "array" --- lib/shared-method.js | 5 +++- test/rest.test.js | 60 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/lib/shared-method.js b/lib/shared-method.js index 29f43df..07768d9 100644 --- a/lib/shared-method.js +++ b/lib/shared-method.js @@ -257,7 +257,7 @@ function coerceAccepts(uarg, desc) { // ignore all other root settings like "required" return coerceAccepts(arg, { name: name + '[' + ix + ']', - type: desc.type[0] + type: targetType[0] }); }); } @@ -335,6 +335,7 @@ function convertToBasicRemotingType(type) { } type = String(type).toLowerCase(); + switch (type) { case 'string': case 'number': @@ -344,6 +345,8 @@ function convertToBasicRemotingType(type) { case 'object': case 'any': return type; + case 'array': + return ['any'].map(convertToBasicRemotingType); default: // custom types like MyModel return 'object'; diff --git a/test/rest.test.js b/test/rest.test.js index 854802b..cf9d05a 100644 --- a/test/rest.test.js +++ b/test/rest.test.js @@ -1024,6 +1024,66 @@ describe('strong-remoting-rest', function() { .end(done); }); + it('should support taget type [any]', function(done) { + var method = givenSharedStaticMethod( + function(arg, cb) { cb(null, { value: arg }); }, + { + accepts: { arg: 'arg', type: ['any'] }, + returns: { arg: 'data', type: ['any'], root: true }, + http: { method: 'POST' } + }); + + request(app).post(method.url) + .send({ arg: ['single'] }) + .expect(200, { value: ['single'] }) + .end(done); + }); + + it('should support taget type `array` - of string', function(done) { + var method = givenSharedStaticMethod( + function(arg, cb) { cb(null, { value: arg }); }, + { + accepts: { arg: 'arg', type: 'array' }, + returns: { arg: 'data', type: 'array', root: true }, + http: { method: 'POST' } + }); + + request(app).post(method.url) + .send({ arg: ['single'] }) + .expect(200, { value: ['single'] }) + .end(done); + }); + + it('should support taget type `array` - of number', function(done) { + var method = givenSharedStaticMethod( + function(arg, cb) { cb(null, { value: arg }); }, + { + accepts: { arg: 'arg', type: 'array' }, + returns: { arg: 'data', type: 'array', root: true }, + http: { method: 'POST' } + }); + + request(app).post(method.url) + .send({ arg: [1] }) + .expect(200, { value: [1] }) + .end(done); + }); + + it('should support taget type `array` - of object', function(done) { + var method = givenSharedStaticMethod( + function(arg, cb) { cb(null, { value: arg }); }, + { + accepts: { arg: 'arg', type: 'array' }, + returns: { arg: 'data', type: 'array', root: true }, + http: { method: 'POST' } + }); + + request(app).post(method.url) + .send({ arg: [{foo: 'bar'}] }) + .expect(200, { value: [{foo: 'bar'}] }) + .end(done); + }); + it('should allow empty body for json request', function(done) { remotes.foo = { bar: function(a, b, fn) {