Comparing version 1.0.6 to 1.0.7
@@ -23,3 +23,4 @@ import { Response } from "./Response"; | ||
match(request: Http4jsRequest): Response; | ||
private defaultNotFoundHandler; | ||
private createInMemResponse(chunks, method, url, headers); | ||
} |
@@ -24,2 +24,7 @@ "use strict"; | ||
this.filters = []; | ||
this.defaultNotFoundHandler = function (request) { | ||
var notFoundBody = request.method + " to " + request.uri.template + " did not match routes"; | ||
var body = new Body_1.Body(notFoundBody); | ||
return new Response_1.Response(404, body); | ||
}; | ||
this.path = path; | ||
@@ -86,5 +91,4 @@ this.handlers[path] = { verb: method, handler: handler }; | ||
else { | ||
var notFoundBody = request.method + " to " + request.uri.template + " did not match routes " + paths.join(" // "); | ||
var body = new Body_1.Body(notFoundBody); | ||
return new Response_1.Response(404, body); | ||
var filtered = this.filters.reduce(function (acc, next) { return next(acc); }, this.defaultNotFoundHandler); | ||
return filtered(request); | ||
} | ||
@@ -91,0 +95,0 @@ }; |
@@ -100,2 +100,28 @@ "use strict"; | ||
}); | ||
it("custom 404s using filters", function () { | ||
var response = RoutingHttpHandler_1.getTo("/", function () { | ||
return new Response_1.Response(200, new Body_1.Body("hello, world!")); | ||
}).withFilter(function (handler) { return function (req) { | ||
if (handler(req).status == 404) { | ||
return new Response_1.Response(404, new Body_1.Body("Page not found")); | ||
} | ||
else { | ||
handler(req); | ||
} | ||
}; }) | ||
.match(new Request_1.Request("GET", "/unknown")); | ||
assert_1.equal(response.status, 404); | ||
assert_1.equal(response.bodyString(), "Page not found"); | ||
}); | ||
it("ordering - filters apply in order they are declared", function () { | ||
var response = RoutingHttpHandler_1.getTo("/", function () { | ||
return new Response_1.Response(200, new Body_1.Body("hello, world!")); | ||
}).withFilter(function (handler) { return function (req) { | ||
return handler(req).replaceHeader("person", "tosh"); | ||
}; }).withFilter(function (handler) { return function (req) { | ||
return handler(req).replaceHeader("person", "bosh"); | ||
}; }) | ||
.match(new Request_1.Request("GET", "/")); | ||
assert_1.equal(response.getHeader("person"), "bosh"); | ||
}); | ||
}); |
{ | ||
"name": "http4js", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"description": "A lightweight HTTP toolkit", | ||
@@ -8,3 +8,3 @@ "main": "dist/index.js", | ||
"scripts": { | ||
"test": "mocha src/test/**", | ||
"test": "tsc; mocha dist/test/**", | ||
"build": "tsc", | ||
@@ -11,0 +11,0 @@ "prepublishOnly": "tsc -p ./ --outDir dist/", |
@@ -26,7 +26,5 @@ ## Http4js | ||
- code: | ||
- daves advice | ||
- extract form data | ||
- ordering of filters | ||
- other client verbs | ||
- daves advice | ||
- extract form data | ||
- other client verbs | ||
- write docs |
Sorry, the diff of this file is not supported yet
982
146255
30