From fc3f5a6ad76fe52b3e9fa1d2d869ff6262654245 Mon Sep 17 00:00:00 2001 From: Luiz Filipe Freitas Carneiro Date: Thu, 25 Sep 2014 00:39:47 -0300 Subject: [PATCH] Adding a way to configure a scope to factory function --- index.js | 13 ++++++- test/fixtures/defaults.json | 2 +- test/fixtures/middleware/self.js | 20 +++++++++++ test/fixtures/race.json | 2 +- test/fixtures/self.json | 11 ++++++ test/meddleware.js | 61 +++++++++++++++++++------------- 6 files changed, 81 insertions(+), 28 deletions(-) create mode 100644 test/fixtures/middleware/self.js create mode 100644 test/fixtures/self.json diff --git a/index.js b/index.js index a5ccfd5..4b9934e 100644 --- a/index.js +++ b/index.js @@ -58,6 +58,16 @@ function resolvery(basedir) { }; } +/** + * Get the scope accordingly meddleware configuration. + * @param candidate The candidate scope. + * @param config The configuration object or string describing the module and option factory method. + * @returns {*} The scope. + */ +function getScope(candidate, config) { + return config.self ? candidate : null; +} + /** * Attempts to locate a node module and get the specified middleware implementation. @@ -95,7 +105,8 @@ function resolveImpl(root, config) { } args = thing.isArray(config['arguments']) ? config['arguments'] : []; - return factory.apply(null, args); + return factory.apply(getScope(module, config), args); + } diff --git a/test/fixtures/defaults.json b/test/fixtures/defaults.json index 97bb9e5..831a23e 100644 --- a/test/fixtures/defaults.json +++ b/test/fixtures/defaults.json @@ -8,7 +8,7 @@ } }, - "static": { + "serveStatic": { "enabled": true, "priority": 30, "module": { diff --git a/test/fixtures/middleware/self.js b/test/fixtures/middleware/self.js new file mode 100644 index 0000000..e1d49ea --- /dev/null +++ b/test/fixtures/middleware/self.js @@ -0,0 +1,20 @@ +/** + * Created by luizfilipe on 24/09/14. + */ + +"use strict"; + +module.exports = { + thisMustBeingFoundByMeddleware: function () { + return function (req, res, next) { + res.locals.selfWasCalled = true; + next(null); + }; + }, + run: function () { + for (var p in this) { + console.log(p); + } + return this.thisMustBeingFoundByMeddleware(); + } +}; \ No newline at end of file diff --git a/test/fixtures/race.json b/test/fixtures/race.json index 5d94c9f..519321e 100644 --- a/test/fixtures/race.json +++ b/test/fixtures/race.json @@ -1,6 +1,6 @@ { "simultaneous": { - "enabled": true, + "enabled": true, "priority": 50, "race": { "middlewareA": { diff --git a/test/fixtures/self.json b/test/fixtures/self.json new file mode 100644 index 0000000..9bfc516 --- /dev/null +++ b/test/fixtures/self.json @@ -0,0 +1,11 @@ +{ + "testSelfProperty": { + "enabled": true, + "priority": 10, + "module": { + "name": "./fixtures/middleware/self", + "method": "run", + "self": true + } + } +} \ No newline at end of file diff --git a/test/meddleware.js b/test/meddleware.js index 56449b4..86a4556 100644 --- a/test/meddleware.js +++ b/test/meddleware.js @@ -1,6 +1,5 @@ 'use strict'; - var test = require('tape'), express = require('express'), request = require('supertest'), @@ -18,7 +17,6 @@ function Resolver() { var resolve = Resolver(); - test('meddleware', function (t) { t.test('empty config', function (t) { @@ -33,7 +31,6 @@ test('meddleware', function (t) { t.end(); }); - t.test('built-in middleware config', function (t) { var config, names, app; @@ -54,7 +51,6 @@ test('meddleware', function (t) { }); }); - t.test('not defined property', function (t) { var config, app; @@ -72,7 +68,6 @@ test('meddleware', function (t) { }); - test('priority', function (t) { t.test('no priority', function (t) { @@ -91,7 +86,7 @@ test('priority', function (t) { entry = app._router.stack[3]; t.ok(entry, 'position 3 middleware exists'); t.equal(typeof entry.handle, 'function', 'position 3 middleware is a function'); - t.equal(entry.handle.name, 'staticMiddleware', 'position 3 middleware has the expected name'); + t.equal(entry.handle.name, 'serveStatic', 'position 3 middleware has the expected name'); entry = app._router.stack[4]; t.ok(entry, 'position 4 middleware exists'); @@ -104,7 +99,6 @@ test('priority', function (t) { }); - test('module', function (t) { t.test('module not defined', function (t) { @@ -115,7 +109,7 @@ test('module', function (t) { } }; - t.throws(function() { + t.throws(function () { var app; try { app = express(); @@ -130,10 +124,9 @@ test('module', function (t) { t.end(); }); - t.test('missing module', function (t) { var app; - t.throws(function() { + t.throws(function () { try { app = express(); app.use(meddle(require('./fixtures/missing'))); @@ -149,7 +142,6 @@ test('module', function (t) { }); - test('factories', function (t) { t.test('custom middleware factories', function (t) { @@ -170,7 +162,6 @@ test('factories', function (t) { t.end(); }); - t.test('throw on invalid identifier', function (t) { var config, app; @@ -192,7 +183,6 @@ test('factories', function (t) { }); - test('enabled', function (t) { t.test('default to enabled', function (t) { @@ -208,7 +198,6 @@ test('enabled', function (t) { app.use(meddle(config)); t.equal(app._router.stack.length, 8, 'middleware stack is appropriate length'); - names.forEach(function (name, i) { var handle = app._router.stack[i + 2].handle; t.equal(typeof handle, 'function', 'position ' + i + ' middleware is a function'); @@ -233,7 +222,6 @@ test('enabled', function (t) { app.use(meddle(config)); t.equal(app._router.stack.length, 8, 'middleware stack is appropriate length'); - names.forEach(function (name, i) { var handle = app._router.stack[i + 2].handle; t.equal(typeof handle, 'function', 'position ' + i + ' middleware is a function'); @@ -244,7 +232,6 @@ test('enabled', function (t) { }); }); - t.test('disable middleware at runtime', function (t) { var config, app; @@ -284,7 +271,6 @@ test('enabled', function (t) { }); - t.test('ignore express objects', function (t) { var config, app; @@ -300,7 +286,6 @@ test('enabled', function (t) { }); - test('events', function (t) { t.test('before and after registration events', function (t) { @@ -325,7 +310,6 @@ test('events', function (t) { }); }); - t.test('before and after named registration events', function (t) { var config, events, app; @@ -352,7 +336,6 @@ test('events', function (t) { }); - test('error middleware', function (t) { t.test('arity of 4', function (t) { @@ -388,7 +371,6 @@ test('error middleware', function (t) { }); - test('routes', function (t) { t.test('route-specific middleware', function (t) { @@ -443,7 +425,6 @@ test('routes', function (t) { }); - t.test('baseroute route-specific middleware', function (t) { var config, app; @@ -500,7 +481,6 @@ test('routes', function (t) { }); - test('composition', function (t) { t.test('parallel', function (t) { @@ -538,7 +518,6 @@ test('composition', function (t) { }); }); - t.test('race', function (t) { var config, app, time; @@ -572,7 +551,6 @@ test('composition', function (t) { }); }); - t.test('fallback', function (t) { var config, app, time; @@ -604,3 +582,36 @@ test('composition', function (t) { }); }); + +test('use self module as scope in factory method', function (t) { + t.test('self', function (t) { + var config, app; + + function req(route, cb) { + var server; + server = request(app) + .get(route) + .end(function (err, res) { + t.error(err, 'no response error'); + t.equal(typeof res, 'object', 'response is defined'); + t.equal(typeof res.body, 'object', 'response body is defined'); + cb(res.body); + }); + } + + config = require('./fixtures/self'); + + app = express(); + app.use(meddle(config)); + + app.get('/', function (req, res) { + t.ok(res.locals.selfWasCalled, + 'The method was called with a scope'); + res.status(200).end(); + }); + + req('/', function () { + t.end(); + }); + }); +});