Skip to content

Commit

Permalink
Adding a way to configure a scope to factory function
Browse files Browse the repository at this point in the history
  • Loading branch information
luizfilipe committed Sep 26, 2014
1 parent 0084220 commit fc3f5a6
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 28 deletions.
13 changes: 12 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);

}


Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
}
},

"static": {
"serveStatic": {
"enabled": true,
"priority": 30,
"module": {
Expand Down
20 changes: 20 additions & 0 deletions test/fixtures/middleware/self.js
Original file line number Diff line number Diff line change
@@ -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();
}
};
2 changes: 1 addition & 1 deletion test/fixtures/race.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"simultaneous": {
"enabled": true,
"enabled": true,
"priority": 50,
"race": {
"middlewareA": {
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/self.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"testSelfProperty": {
"enabled": true,
"priority": 10,
"module": {
"name": "./fixtures/middleware/self",
"method": "run",
"self": true
}
}
}
61 changes: 36 additions & 25 deletions test/meddleware.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';


var test = require('tape'),
express = require('express'),
request = require('supertest'),
Expand All @@ -18,7 +17,6 @@ function Resolver() {

var resolve = Resolver();


test('meddleware', function (t) {

t.test('empty config', function (t) {
Expand All @@ -33,7 +31,6 @@ test('meddleware', function (t) {
t.end();
});


t.test('built-in middleware config', function (t) {
var config, names, app;

Expand All @@ -54,7 +51,6 @@ test('meddleware', function (t) {
});
});


t.test('not defined property', function (t) {
var config, app;

Expand All @@ -72,7 +68,6 @@ test('meddleware', function (t) {

});


test('priority', function (t) {

t.test('no priority', function (t) {
Expand All @@ -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');
Expand All @@ -104,7 +99,6 @@ test('priority', function (t) {

});


test('module', function (t) {

t.test('module not defined', function (t) {
Expand All @@ -115,7 +109,7 @@ test('module', function (t) {
}
};

t.throws(function() {
t.throws(function () {
var app;
try {
app = express();
Expand All @@ -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')));
Expand All @@ -149,7 +142,6 @@ test('module', function (t) {

});


test('factories', function (t) {

t.test('custom middleware factories', function (t) {
Expand All @@ -170,7 +162,6 @@ test('factories', function (t) {
t.end();
});


t.test('throw on invalid identifier', function (t) {
var config, app;

Expand All @@ -192,7 +183,6 @@ test('factories', function (t) {

});


test('enabled', function (t) {

t.test('default to enabled', function (t) {
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -244,7 +232,6 @@ test('enabled', function (t) {
});
});


t.test('disable middleware at runtime', function (t) {
var config, app;

Expand Down Expand Up @@ -284,7 +271,6 @@ test('enabled', function (t) {

});


t.test('ignore express objects', function (t) {
var config, app;

Expand All @@ -300,7 +286,6 @@ test('enabled', function (t) {

});


test('events', function (t) {

t.test('before and after registration events', function (t) {
Expand All @@ -325,7 +310,6 @@ test('events', function (t) {
});
});


t.test('before and after named registration events', function (t) {
var config, events, app;

Expand All @@ -352,7 +336,6 @@ test('events', function (t) {

});


test('error middleware', function (t) {

t.test('arity of 4', function (t) {
Expand Down Expand Up @@ -388,7 +371,6 @@ test('error middleware', function (t) {

});


test('routes', function (t) {

t.test('route-specific middleware', function (t) {
Expand Down Expand Up @@ -443,7 +425,6 @@ test('routes', function (t) {

});


t.test('baseroute route-specific middleware', function (t) {
var config, app;

Expand Down Expand Up @@ -500,7 +481,6 @@ test('routes', function (t) {

});


test('composition', function (t) {

t.test('parallel', function (t) {
Expand Down Expand Up @@ -538,7 +518,6 @@ test('composition', function (t) {
});
});


t.test('race', function (t) {
var config, app, time;

Expand Down Expand Up @@ -572,7 +551,6 @@ test('composition', function (t) {
});
});


t.test('fallback', function (t) {
var config, app, time;

Expand Down Expand Up @@ -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();
});
});
});

0 comments on commit fc3f5a6

Please sign in to comment.