Comparing version 1.0.11 to 1.0.12
@@ -88,2 +88,6 @@ "use strict"; | ||
} | ||
else { | ||
var filtered = this.filters.reduce(function (acc, next) { return next(acc); }, this.defaultNotFoundHandler); | ||
return filtered(preFilteredRequest); | ||
} | ||
}; | ||
@@ -90,0 +94,0 @@ ResourceRoutingHttpHandler.prototype.createInMemResponse = function (chunks, method, url, headers) { |
@@ -180,2 +180,23 @@ "use strict"; | ||
}); | ||
it("Post redirect.", function () { | ||
var friends = []; | ||
var routes = RoutingHttpHandler_1.getTo("/", function () { | ||
return new Response_1.Response(200, new Body_1.Body("root")); | ||
}) | ||
.withHandler("/family", "GET", function () { | ||
return new Response_1.Response(200, new Body_1.Body(friends.join(", "))); | ||
}) | ||
.withHandler("/family/{name}", "GET", function () { | ||
return new Response_1.Response(200, new Body_1.Body("fuzzy")); | ||
}) | ||
.withHandler("/family", "POST", function (req) { | ||
friends.push(req.form["name"]); | ||
return new Response_1.Response(302).setHeader("Location", "/family"); | ||
}); | ||
var postSideEffect1 = routes.match(new Request_1.Request("POST", "/family", new Body_1.Body("name=tosh"))); | ||
var postSideEffect2 = routes.match(new Request_1.Request("POST", "/family", new Body_1.Body("name=bosh"))); | ||
var postSideEffect3 = routes.match(new Request_1.Request("POST", "/family", new Body_1.Body("name=losh"))); | ||
var response = routes.match(new Request_1.Request("GET", "/family")); | ||
assert_1.equal(response.bodyString(), "tosh, bosh, losh"); | ||
}); | ||
it("extract form params", function () { | ||
@@ -182,0 +203,0 @@ var response = RoutingHttpHandler_1.postTo("/family", function (req) { |
@@ -10,2 +10,4 @@ "use strict"; | ||
describe("real request", function () { | ||
var friends = []; | ||
var baseUrl = "http://localhost:3000"; | ||
var server = RoutingHttpHandler_1.getTo("/", function (req) { | ||
@@ -20,18 +22,65 @@ var query = req.getQuery("tomQuery"); | ||
}) | ||
.withHandler("/family", "GET", function () { | ||
return new Response_1.Response(200, new Body_1.Body(friends.join(", "))); | ||
}) | ||
.withHandler("/family/{name}", "GET", function () { | ||
return new Response_1.Response(200, new Body_1.Body("fuzzy")); | ||
}) | ||
.withHandler("/family", "POST", function (req) { | ||
friends.push(req.form["name"]); | ||
return new Response_1.Response(302).setHeader("Location", "/family"); | ||
}) | ||
.asServer(3000); | ||
var anotherServer = RoutingHttpHandler_1.getTo("/", function (req) { | ||
return new Response_1.Response(200, new Body_1.Body("Hello, world!")); | ||
}) | ||
.withHandler("/friends", "GET", function (req) { | ||
var queries = req.queries; | ||
var searchTerm = queries["name"]; | ||
var filteredFriends = searchTerm | ||
? friends.filter(function (f) { return f.indexOf(searchTerm) > -1; }) | ||
: friends; | ||
var html = filteredFriends.join(", "); | ||
return new Response_1.Response(200, new Body_1.Body(html)); | ||
}) | ||
.withHandler("/friends/{name}", "GET", function (req) { | ||
var name = req.pathParams["name"]; | ||
var filter = name | ||
? friends.filter(function (it) { return it.indexOf(name) > -1; }) | ||
: friends; | ||
return new Response_1.Response(200, new Body_1.Body(filter.join(","))); | ||
}) | ||
.withHandler("/friends", "POST", function (req) { | ||
var newFriend = req.form["name"]; | ||
friends.push(newFriend); | ||
var html = "<p>" + friends.join("</p><p>") + "</p>\n <form method=\"post\"><input type=\"text\" name=\"name\"/><input type=\"submit\"></form>"; | ||
return new Response_1.Response(200, new Body_1.Body(html)); | ||
}) | ||
.withFilter(function (handler) { return function (req) { | ||
var response = handler(req); | ||
if (response.status == 404) { | ||
return new Response_1.Response(404, new Body_1.Body("Page not found")); | ||
} | ||
else { | ||
return response; | ||
} | ||
}; }) | ||
.asServer(3001); | ||
before(function () { | ||
server.start(); | ||
anotherServer.start(); | ||
}); | ||
after(function () { | ||
server.stop(); | ||
anotherServer.stop(); | ||
}); | ||
it("sets body", function () { | ||
var request = new Request_1.Request("POST", "http://localhost:3000/", new Body_1.Body("my humps")); | ||
var request = new Request_1.Request("POST", baseUrl + "/post", new Body_1.Body("my humps")); | ||
return Client_1.HttpClient(request) | ||
.then(function (succ) { | ||
assert_1.deepEqual(succ.bodyString(), "my humps"); | ||
assert_1.equal(succ.bodyString(), "my humps"); | ||
}); | ||
}); | ||
it("sets query params", function () { | ||
var request = new Request_1.Request("GET", "http://localhost:3000/") | ||
var request = new Request_1.Request("GET", baseUrl) | ||
.query("tomQuery", "likes to party"); | ||
@@ -44,3 +93,3 @@ return Client_1.HttpClient(request) | ||
it("sets multiple headers of same name", function () { | ||
var request = new Request_1.Request("GET", "http://localhost:3000/", null, { tom: ["smells", "smells more"] }); | ||
var request = new Request_1.Request("GET", baseUrl, null, { tom: ["smells", "smells more"] }); | ||
return Client_1.HttpClient(request) | ||
@@ -51,2 +100,10 @@ .then(function (succ) { | ||
}); | ||
it("Post redirect.", function () { | ||
var postSideEffect1 = new Request_1.Request("POST", "http://localhost:3001/friends", new Body_1.Body("name=tosh")); | ||
var getFriends = new Request_1.Request("GET", "http://localhost:3001/friends"); | ||
return Client_1.HttpClient(postSideEffect1) | ||
.then(function (_) { return Client_1.HttpClient(postSideEffect1).then(function (_) { | ||
return Client_1.HttpClient(getFriends).then(function (s) { return assert_1.equal(s.bodyString(), "tosh, tosh"); }); | ||
}); }); | ||
}); | ||
}); |
{ | ||
"name": "http4js", | ||
"version": "1.0.11", | ||
"version": "1.0.12", | ||
"description": "A lightweight HTTP toolkit", | ||
@@ -10,3 +10,3 @@ "main": "dist/index.js", | ||
"build": "tsc", | ||
"prepublishOnly": "tsc -p ./ --outDir dist/", | ||
"prepublishOnly": "rm -rf ./dist && tsc -p ./ --outDir dist/", | ||
"app": "tsc; node index.js" | ||
@@ -13,0 +13,0 @@ }, |
@@ -12,3 +12,3 @@ ## Http4js | ||
``` | ||
npm install --save | ||
npm install --save | ||
npm test | ||
@@ -27,5 +27,6 @@ ``` | ||
- daves advice | ||
- dont have to create a new Body for Response | ||
- other client verbs | ||
- add a type or interface for Filter, instead of referring to it as HttpHandler -> HttpHandler. | ||
Then you can add the "then" methods to it to recreate the chaining? | ||
- support express backend | ||
- other client verbs, PUT, PATCH, HEAD etc. | ||
- write docs |
Sorry, the diff of this file is not supported yet
159040
1086
31
39