From 37646fa00804a809178476f5e2da6baf66f8fed4 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Wed, 19 Feb 2014 15:14:31 -0800 Subject: [PATCH] Allows options to be passed to strong-remoting --- lib/application.js | 11 +-- lib/middleware/rest.js | 3 +- lib/middleware/token.js | 1 - test/app.test.js | 3 +- test/fixtures/simple-integration-app/app.json | 11 ++- test/remoting.integration.js | 70 +++++++++++++++++++ 6 files changed, 85 insertions(+), 14 deletions(-) create mode 100644 test/remoting.integration.js diff --git a/lib/application.js b/lib/application.js index 527f6bb1..a66b23ee 100644 --- a/lib/application.js +++ b/lib/application.js @@ -56,7 +56,8 @@ app.remotes = function () { if(this._remotes) { return this._remotes; } else { - return (this._remotes = RemoteObjects.create()); + var options = this.get('remoting') || {}; + return (this._remotes = RemoteObjects.create(options)); } } @@ -213,14 +214,6 @@ app.remoteObjects = function () { return result; } -/** - * Get the apps set of remote objects. - */ - -app.remotes = function () { - return this._remotes || (this._remotes = RemoteObjects.create()); -} - /** * Enable swagger REST API documentation. * diff --git a/lib/middleware/rest.js b/lib/middleware/rest.js index 080add68..9ddef750 100644 --- a/lib/middleware/rest.js +++ b/lib/middleware/rest.js @@ -3,7 +3,6 @@ */ var loopback = require('../loopback'); -var RemoteObjects = require('strong-remoting'); /** * Export the middleware. @@ -23,7 +22,7 @@ function rest() { if(req.url === '/routes') { res.send(handler.adapter.allRoutes()); } else if(req.url === '/models') { - return res.send(remotes.toJSON()); + return res.send(app.remotes().toJSON()); } else { handler(req, res, next); } diff --git a/lib/middleware/token.js b/lib/middleware/token.js index 327989c1..185a71ce 100644 --- a/lib/middleware/token.js +++ b/lib/middleware/token.js @@ -3,7 +3,6 @@ */ var loopback = require('../loopback'); -var RemoteObjects = require('strong-remoting'); var assert = require('assert'); /*! diff --git a/test/app.test.js b/test/app.test.js index 835dcbdc..fe048cda 100644 --- a/test/app.test.js +++ b/test/app.test.js @@ -428,7 +428,8 @@ describe('app', function() { assert.equal(typeof res.body, 'object'); assert(res.body.started); - assert(res.body.uptime); + // The number can be 0 + assert(res.body.uptime !== undefined); var elapsed = Date.now() - Number(new Date(res.body.started)); diff --git a/test/fixtures/simple-integration-app/app.json b/test/fixtures/simple-integration-app/app.json index 0d6aed2e..c1cd9fb7 100644 --- a/test/fixtures/simple-integration-app/app.json +++ b/test/fixtures/simple-integration-app/app.json @@ -1,5 +1,14 @@ { "port": 3000, "host": "0.0.0.0", - "cookieSecret": "2d13a01d-44fb-455c-80cb-db9cb3cd3cd0" + "cookieSecret": "2d13a01d-44fb-455c-80cb-db9cb3cd3cd0", + "remoting": { + "json": { + "limit": "1kb", + "strict": false + }, + "urlencoded": { + "limit": "8kb" + } + } } \ No newline at end of file diff --git a/test/remoting.integration.js b/test/remoting.integration.js new file mode 100644 index 00000000..02fcbde0 --- /dev/null +++ b/test/remoting.integration.js @@ -0,0 +1,70 @@ +var loopback = require('../'); +var lt = require('loopback-testing'); +var path = require('path'); +var SIMPLE_APP = path.join(__dirname, 'fixtures', 'simple-integration-app'); +var app = require(path.join(SIMPLE_APP, 'app.js')); +var assert = require('assert'); + +describe('remoting - integration', function () { + + lt.beforeEach.withApp(app); + lt.beforeEach.givenModel('store'); + + afterEach(function (done) { + this.app.models.store.destroyAll(done); + }); + + describe('app.remotes.options', function () { + it("should load remoting options", function () { + var remotes = app.remotes(); + assert.deepEqual(remotes.options, {"json": {"limit": "1kb", "strict": false}, + "urlencoded": {"limit": "8kb"}}); + }); + + it("rest handler", function () { + var handler = app.handler('rest'); + assert(handler); + }); + + it('should accept request that has entity below 1kb', function (done) { + // Build an object that is smaller than 1kb + var name = ""; + for (var i = 0; i < 256; i++) { + name += "11"; + } + this.http = this.post('/api/stores'); + this.http.send({ + "name": name + }); + this.http.end(function (err) { + if (err) return done(err); + this.req = this.http.req; + this.res = this.http.res; + assert.equal(this.res.statusCode, 200); + done(); + }.bind(this)); + }); + + it('should reject request that has entity beyond 1kb', function (done) { + // Build an object that is larger than 1kb + var name = ""; + for (var i = 0; i < 2048; i++) { + name += "11111111111"; + } + this.http = this.post('/api/stores'); + this.http.send({ + "name": name + }); + this.http.end(function (err) { + if (err) return done(err); + this.req = this.http.req; + this.res = this.http.res; + // Request is rejected with 413 + assert.equal(this.res.statusCode, 413); + done(); + }.bind(this)); + }); + }); + +}) +;