Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

unbound routes return a 501

  • Loading branch information...
commit 7ba647a8f2c5ea52b5646c574c10f097c5ba50e6 1 parent bd4b34d
cloudhead authored
Showing with 25 additions and 8 deletions.
  1. +0 −1  TODO
  2. +15 −7 lib/journey.js
  3. +10 −0 test/journey-test.js
1  TODO
View
@@ -1,6 +1,5 @@
TODO
-- Unbound routes should respond with 501.
- Implement `bindResource`.
- Ability to bind multiple functions to a route.
- `Router.prototype.push`
22 lib/journey.js
View
@@ -34,6 +34,11 @@ var NotAcceptable = function (msg) {
this.headers = {};
this.body = { error: msg, only: "application/json" };
};
+var NotImplemented = function (msg) {
+ this.status = 501;
+ this.headers = {};
+ this.body = { error: msg };
+};
journey.env = 'development';
journey.version = [0, 1, 0];
@@ -88,7 +93,7 @@ journey.map = function (context) {
del: function (pattern, opts) { return this.route('DELETE', pattern, opts) },
route: function (/* variable arguments */) {
- var args = Array.prototype.slice.call(arguments).compact(),
+ var args = Array.prototype.slice.call(arguments).compact(), route,
// Defaults
pattern = /.*/,
method = journey.Router.methods,
@@ -106,13 +111,15 @@ journey.map = function (context) {
}
});
+ context.routes.push(route = {
+ patterns: Array.isArray(pattern) ? pattern : [pattern],
+ method: method, constraints: constraints
+ });
+
return {
- bind: function (handler, success) {
- return context.routes.push({
- patterns: Array.isArray(pattern) ? pattern : [pattern],
- method: method, handler: handler,
- success: success, constraints: constraints
- });
+ bind: function (handler) {
+ route.handler = handler;
+ return route;
}
};
},
@@ -223,6 +230,7 @@ journey.Router.prototype = {
});
if (match && constraints) {
+ if (! ('handler' in route)) { throw new(NotImplemented)("unbound route") }
if ((Array.isArray(route.method) && route.method.indexOf(request.method) !== -1) ||
(route.method === request.method) || !route.method) {
return function (res, params) {
10 test/journey-test.js
View
@@ -40,6 +40,7 @@ var router = new(journey.Router)(function (map) {
//map.root.bind(function (res) { res.send("Welcome to the Root") });
map.get('/home/room').bind(resources.home.room);
map.get('/undefined').bind();
+ map.get('/unbound');
map.root.bind(function (res) { return resources.home.index(res) });
@@ -212,6 +213,15 @@ vows.tell('Journey', {
assert.equal(res.status, 500);
}
},
+ // Trying to access an unbound route, will result in a 501 'Not Implemented'
+ "An unbound route": {
+ topic: function () {
+ return get('/unbound');
+ },
+ "returns a 501": function (res) {
+ assert.equal(res.status, 501);
+ }
+ },
// Here, we're trying to use the DELETE method on /
// Of course, we haven't allowed this, so Journey responds with a
// 405 'Method not Allowed', and returns the allowed methods
Please sign in to comment.
Something went wrong with that request. Please try again.