From 8c3f5e3c0b297e8a9b6fea5b8e791b3526e34d15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matti=20Ma=CC=88a=CC=88tta=CC=88?= Date: Wed, 28 Sep 2016 23:27:36 +0300 Subject: [PATCH 1/3] Add RequestBuilder to assist using superagent plugins - Change TestHelper to use new RequestBuilder --- lib/http/index.js | 3 +- lib/http/request-builder.js | 55 ++++++++++++++++++++++++++++++ lib/utils/TestHelper.js | 10 ++++-- package.json | 2 ++ tests/http/request-builder.spec.js | 31 +++++++++++++++++ 5 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 lib/http/request-builder.js create mode 100644 tests/http/request-builder.spec.js diff --git a/lib/http/index.js b/lib/http/index.js index 3903107..2694dd8 100644 --- a/lib/http/index.js +++ b/lib/http/index.js @@ -1,4 +1,5 @@ module.exports = { RangeParser: require('./range-parser').RangeParser, - request: require('./request') + request: require('./request'), + requestBuilder: require('./request-builder').requestBuilder }; diff --git a/lib/http/request-builder.js b/lib/http/request-builder.js new file mode 100644 index 0000000..429a6cb --- /dev/null +++ b/lib/http/request-builder.js @@ -0,0 +1,55 @@ +var methods = require('methods') + , _ = require('lodash'); + +function RequestBuilder (agent) { + this._agent = agent; + this._plugins = []; +} + +RequestBuilder.prototype.use = function (plugin) { + this._plugins.push(plugin); + return this; +} + +// Create prototype method for each superagent request method. +// Superagent uses similar implementation to generate http request +// verb method names. +var verbs = methods.concat('del'); +_.each(verbs, function (verb) { + RequestBuilder.prototype[verb] = function () { + var request = this._agent[verb].apply(this._agent, arguments); + + _.each(this._plugins, function (plugin) { + request.use(plugin); + }); + + return request; + } +}); + +/** + * Superagent request builder. + * + * Builder allows to chain superagent plugins before calling request + * factory method. Superagent request is created with chained plugins + * when builder's http request factory method is called. + * + * Example: + * + * var requestBuilder = require('dodo/http').requestBuilder + * , superagent = require('superagent') + * , prefix = require('superagent-prefix'); + * + * var request = requestBuilder(superagent) + * .use(prefix('http://localhost:8080')); + * + * request.get('/some/resource') + * .then(function (res) { + * console.log(res); + * }); + */ +module.exports = { + requestBuilder: function (agent) { + return new RequestBuilder(agent); + } +}; diff --git a/lib/utils/TestHelper.js b/lib/utils/TestHelper.js index b6da5a7..194a76e 100644 --- a/lib/utils/TestHelper.js +++ b/lib/utils/TestHelper.js @@ -1,7 +1,8 @@ 'use strict'; var main = require('../app/express/main') - , request = require('../http/request') + , superagent = require('superagent') + , requestBuilder = require('../http').requestBuilder , path = require('path') , _ = require('lodash'); @@ -19,10 +20,15 @@ var main = require('../app/express/main') */ function TestHelper(config) { this.serverUrl = getAppAddress(config); - this.request = request(this.serverUrl); this.app = main.createApp(config); var self = this; + + this.request = requestBuilder(superagent) + .use(function (req) { + req.url = self.serverUrl + req.url; + }); + before(function () { return main.startApp(self.app); }); diff --git a/package.json b/package.json index 373e74d..66b2785 100644 --- a/package.json +++ b/package.json @@ -40,10 +40,12 @@ "fs-extra": "0.18.2", "glob": "5.0.5", "lodash": "^3.6.0", + "methods": "^1.1.2", "minimist": "1.1.1", "moment": "2.10.2", "mv": "2.0.3", "node-uuid": "1.4.3", + "superagent": "^2.3.0", "tv4": "1.1.9", "tv4-formats": "1.0.0", "underscore.string": "^3.0.0" diff --git a/tests/http/request-builder.spec.js b/tests/http/request-builder.spec.js new file mode 100644 index 0000000..943e95c --- /dev/null +++ b/tests/http/request-builder.spec.js @@ -0,0 +1,31 @@ +var _ = require('lodash') + , methods = require('methods') + , expect = require('chai').expect + , requestBuilder = require('../../lib/http').requestBuilder; + +describe('request builder', function () { + + it('should call use for each plugin when creating request', function () { + var agent = { + args: {} + }; + + agent.get = function (url) { + agent.args.get = url; + return agent; + } + + agent.use = function (plugin) { + agent.args.use = plugin; + return agent; + } + + var request = requestBuilder(agent) + .use('foo') + .get('http://example.com'); + + expect(agent.args.use).to.equal('foo'); + expect(agent.args.get).to.equal('http://example.com'); + }); + +}); \ No newline at end of file From 2554ccbd785835b30621c03b4ae07a0a2ecda6b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matti=20Ma=CC=88a=CC=88tta=CC=88?= Date: Thu, 29 Sep 2016 09:34:08 +0300 Subject: [PATCH 2/3] Change MultiRequire export to multiRequire lib/utils/multi-require-js exports a function.. --- lib/utils/index.js | 2 +- lib/utils/multi-require.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/utils/index.js b/lib/utils/index.js index 25be3ad..a8dc200 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -1,7 +1,7 @@ module.exports = { classUtils: require('./class-utils'), mergeConfig: require('./merge-config').mergeConfig, - MultiRequire: require('./multi-require').MultiRequire, + multiRequire: require('./multi-require').multiRequire, runCommandInPromise: require('./run-command-in-promise').runCommandInPromise, TestHelper: require('./TestHelper').TestHelper }; diff --git a/lib/utils/multi-require.js b/lib/utils/multi-require.js index 37d3709..f91a37a 100644 --- a/lib/utils/multi-require.js +++ b/lib/utils/multi-require.js @@ -181,7 +181,7 @@ MultiRequire.prototype.doRequire_ = function(callback) { * @returns {MultiRequire} */ module.exports = { - MultiRequire: function(pathPattern) { + multiRequire: function(pathPattern) { return new MultiRequire(pathPattern); } }; From 83ed980a3b822e252ef2daff6e3f58c07738ac00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matti=20Ma=CC=88a=CC=88tta=CC=88?= Date: Thu, 29 Sep 2016 09:35:02 +0300 Subject: [PATCH 3/3] Add import proxies to package --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 66b2785..5870db4 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,8 @@ "files": [ "README.md", "LICENSE", - "lib/*" + "lib/*", + "*.js" ], "dependencies": { "aws-sdk": "2.1.26",