Skip to content

Commit

Permalink
[on] resurfaced functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
dscape committed Mar 27, 2012
1 parent e8cee69 commit 7e4646b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 28 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
@@ -1,4 +1,7 @@
language: node_js
branches:
only:
- master
node_js:
- 0.4
- 0.6
Expand Down
4 changes: 2 additions & 2 deletions lib/director/http/index.js
Expand Up @@ -28,7 +28,7 @@ var Router = exports.Router = function (routes, config) {
this.methods = ['on', 'after', 'before'];
this.scope = [];
this._methods = {};
this.recurse = 'forward';
this.recurse = 'backward';

this.extend(exports.methods.concat(['before', 'after']));
this.configure(config || {});
Expand Down Expand Up @@ -90,6 +90,7 @@ Router.prototype.dispatch = function (req, res, callback) {
runlist,
stream,
error;

if (this._attach) {
this._attach.call(thisArg);
}
Expand All @@ -110,7 +111,6 @@ Router.prototype.dispatch = function (req, res, callback) {
if (this.recurse === 'forward') {
fns = fns.reverse();
}

stream = fns.some(function (fn) { return fn.stream === true; });
runlist = this.runlist(fns);

Expand Down
12 changes: 6 additions & 6 deletions lib/director/router.js
Expand Up @@ -403,7 +403,6 @@ Router.prototype.traverse = function (method, path, routes, regexp) {
next.captures = [];
return next;
}

for (var r in routes) {
//
// We dont have an exact match, lets explore the tree
Expand All @@ -429,9 +428,8 @@ Router.prototype.traverse = function (method, path, routes, regexp) {
if (!this.strict) {
exact += '[' + this.delimiter + ']?';
}

match = path.match(new RegExp('^' + exact));

if (!match) {
//
// If there isn't a `match` then continue. Here, the
Expand All @@ -443,17 +441,19 @@ Router.prototype.traverse = function (method, path, routes, regexp) {
continue;
}

if (match[0] && match[0] == path && routes[r][method]) {
if (match[0] && match[0] == path &&
(routes[r][method] || routes[r].on)) {
//
// ### Base case 2:
// If we had a `match` and the capture is the path itself,
// then we have completed our recursion.
//
next = [[routes[r].before, routes[r][method]].filter(Boolean)];
next = [[routes[r].before, (routes[r][method] || routes[r].on)
].filter(Boolean)];
next.after = [routes[r].after].filter(Boolean);
next.matched = true;
next.captures = match.slice(1);

if (this.recurse && routes === this.routes) {
next.push([routes.before, routes.on].filter(Boolean));
next.after = next.after.concat([routes.after].filter(Boolean));
Expand Down
52 changes: 33 additions & 19 deletions test/server/http/attach-test.js
Expand Up @@ -12,9 +12,11 @@ var assert = require('assert'),
request = require('request'),
director = require('../../../lib/director');

function helloWorld() {
this.res.writeHead(200, { 'Content-Type': 'application/json' });
this.res.end(JSON.stringify(this.data));
function hello(what) {
return function () {
this.res.writeHead(200, { 'Content-Type': 'application/json' });
this.res.end(JSON.stringify({d: this.data, w: what.toUpperCase()}));
};
}

function notFound() {
Expand All @@ -26,7 +28,7 @@ function createServer (router) {
return http.createServer(function (req, res) {
router.dispatch(req, res, function (err) {
if (err) {
res.writeHead(404);
res.writeHead(500);
res.end();
}
});
Expand All @@ -37,32 +39,46 @@ function assertMethod (uri, method) {
method = method || "GET";
return {
topic: function () {
request({ uri: 'http://localhost:9091/' + uri,
request({ uri: 'http://localhost:9098/' + uri,
method: method, json: true }, this.callback);
},
"should respond with `this.data` if not head": function (err, res, body) {
assert.isNull(err);
assert.equal(res.statusCode, 200);
if (method !== 'HEAD') {
assert.deepEqual(body, [1, 2, 3]);
assert.deepEqual(body, {d: [1, 2, 3], w: method});
}
}
};
}

function assertNotFound (uri) {
return {
topic: function () {
request({ uri: 'http://localhost:9098/' + uri, json: true },
this.callback);
},
"should respond with not found": function (err, res, body) {
assert.isNull(err);
assert.equal(res.statusCode, 404);
assert.deepEqual(body, {not: "found"});
}
};
}

vows.describe('director/server/http/attach').addBatch({
"An instance of director.http.Router": {
"instantiated with a Routing table": {
topic: new director.http.Router({
'/hello': {
get: helloWorld,
head: helloWorld,
patch: helloWorld
get: hello('get'),
head: hello('head'),
patch: hello('patch')
},
'/custom': {
on: helloWorld
on: hello('on')
},
'*': {
'/*': {
on: notFound
}
}),
Expand All @@ -71,7 +87,6 @@ vows.describe('director/server/http/attach').addBatch({
assert.isFunction(router.routes.hello.get);
assert.isObject(router.routes.custom);
assert.isFunction(router.routes.custom.on);
assert.equal(router.routes.on.toString(), notFound.toString());
},
"when passed to an http.Server instance": {
topic: function (router) {
Expand All @@ -80,14 +95,13 @@ vows.describe('director/server/http/attach').addBatch({
});

var server = createServer(router);
server.listen(9091, this.callback);
server.listen(9098, this.callback);
},
"a request to hello": assertMethod('hello')//,
//"a head request to hello": assertMethod('hello', 'HEAD'),
//"a patch request to hello": assertMethod('hello', 'PATCH'),
//"a GET request to custom": assertMethod('custom', 'GET'),
//"a custom request to custom": assertMethod('custom', 'XYZ'),
//"a request to something not found": assertMethod('notreally')
"a request to hello": assertMethod('hello'),
"a head request to hello": assertMethod('hello', 'HEAD'),
"a patch request to hello": assertMethod('hello', 'PATCH'),
"a HEAD request to custom": assertMethod('custom', 'HEAD'),
"a request to something not found": assertNotFound('notreally')
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/server/http/order-test.js
Expand Up @@ -13,7 +13,7 @@ var assert = require('assert'),
director = require('../../../lib/director');

function hello(you) {
return function () {
return function drive() {
this.res.writeHead(200, { 'Content-Type': 'text/plain' });
this.res.end(JSON.stringify({hello: you}));
};
Expand Down

0 comments on commit 7e4646b

Please sign in to comment.