Permalink
Browse files

[on] resurfaced functionality

  • Loading branch information...
1 parent e8cee69 commit 7e4646b3655a747132ec39cabdd8ca8bdb5bf5b7 @dscape dscape committed Mar 27, 2012
Showing with 45 additions and 28 deletions.
  1. +3 −0 .travis.yml
  2. +2 −2 lib/director/http/index.js
  3. +6 −6 lib/director/router.js
  4. +33 −19 test/server/http/attach-test.js
  5. +1 −1 test/server/http/order-test.js
View
@@ -1,4 +1,7 @@
language: node_js
+branches:
+ only:
+ - master
node_js:
- 0.4
- 0.6
@@ -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 || {});
@@ -90,6 +90,7 @@ Router.prototype.dispatch = function (req, res, callback) {
runlist,
stream,
error;
+
if (this._attach) {
this._attach.call(thisArg);
}
@@ -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);
View
@@ -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
@@ -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
@@ -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));
@@ -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() {
@@ -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();
}
});
@@ -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
}
}),
@@ -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) {
@@ -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')
}
}
}
@@ -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}));
};

0 comments on commit 7e4646b

Please sign in to comment.