diff --git a/spec/all_spec.cr b/spec/all_spec.cr new file mode 100644 index 0000000..938dadf --- /dev/null +++ b/spec/all_spec.cr @@ -0,0 +1 @@ +require "./*" diff --git a/spec/frank_handler_spec.cr b/spec/frank_handler_spec.cr new file mode 100644 index 0000000..de87e7e --- /dev/null +++ b/spec/frank_handler_spec.cr @@ -0,0 +1,13 @@ +require "spec_helper" + +describe "Frank::Handler" do + it "routes" do + frank = Frank::Handler.new + frank.add_route "GET", "/" do + "hello" + end + request = HTTP::Request.new("GET", "/") + response = frank.call(request) + response.body.should eq("hello") + end +end diff --git a/spec/route_spec.cr b/spec/route_spec.cr index eeb2a4f..1a5dfdc 100644 --- a/spec/route_spec.cr +++ b/spec/route_spec.cr @@ -2,15 +2,20 @@ require "spec_helper" describe "Route" do describe "match" do - it "doesn't match" do - route = Route.new("/foo/bar", nil) - route.match(nil, "/foo/baz".split("/")).should be_nil + it "doesn't match because of route" do + route = Route.new("GET", "/foo/bar") { "" } + route.match("GET", "/foo/baz".split("/")).should be_nil + end + + it "doesn't match because of method" do + route = Route.new("GET", "/foo/bar") { "" } + route.match("POST", "/foo/bar".split("/")).should be_nil end it "matches" do - route = Route.new("/foo/:one/path/:two", nil) - request = route.match(nil, "/foo/uno/path/dos".split("/")) - request.not_nil!.params.should eq({"one" => "uno", "two" => "dos"}) + route = Route.new("GET", "/foo/:one/path/:two") { "" } + params = route.match("GET", "/foo/uno/path/dos".split("/")) + params.should eq({"one" => "uno", "two" => "dos"}) end end end diff --git a/src/frank/dsl.cr b/src/frank/dsl.cr index 3995cde..8241982 100644 --- a/src/frank/dsl.cr +++ b/src/frank/dsl.cr @@ -1,3 +1,7 @@ def get(path, &block : Frank::Request -> String) - Frank::Handler::INSTANCE.add_route(path, block) + Frank::Handler::INSTANCE.add_route("GET", path, &block) +end + +def post(path, &block : Frank::Request -> String) + Frank::Handler::INSTANCE.add_route("POST", path, &block) end diff --git a/src/frank/handler.cr b/src/frank/handler.cr index 9eea1b1..e28f432 100644 --- a/src/frank/handler.cr +++ b/src/frank/handler.cr @@ -19,15 +19,16 @@ class Frank::Handler < HTTP::Handler end end - def add_route(path, handler) - @routes << Route.new(path, handler) + def add_route(method, path, &handler : Frank::Request -> String) + @routes << Route.new(method, path, &handler) end def exec_request(request) components = request.path.split "/" @routes.each do |route| - frank_request = route.match(request, components) - if frank_request + params = route.match(request.method, components) + if params + frank_request = Request.new(params) return route.handler.call(frank_request) end end diff --git a/src/frank/route.cr b/src/frank/route.cr index ad0e4bc..fce5dae 100644 --- a/src/frank/route.cr +++ b/src/frank/route.cr @@ -1,11 +1,12 @@ class Frank::Route getter handler - def initialize(path, @handler) + def initialize(@method, path, &@handler : Frank::Request -> String) @components = path.split "/" end - def match(request, components) + def match(method, components) + return nil unless method == @method return nil unless components.length == @components.length params = nil @@ -20,6 +21,6 @@ class Frank::Route end params ||= {} of String => String - Request.new(params) + params end end