Fixed match bug

This commit is contained in:
Sdogruyol 2015-10-31 09:28:25 +02:00
parent da23d397c6
commit c49191e650
3 changed files with 20 additions and 2 deletions

View file

@ -1,7 +1,7 @@
require "./spec_helper" require "./spec_helper"
describe "Route" do describe "Route" do
describe "match" do describe "match?" do
it "doesn't match because of route" do it "doesn't match because of route" do
route = Route.new("GET", "/foo/bar") { "" } route = Route.new("GET", "/foo/bar") { "" }
request = HTTP::Request.new("GET", "/world?message=coco") request = HTTP::Request.new("GET", "/world?message=coco")
@ -20,5 +20,18 @@ describe "Route" do
match = route.match?(request) match = route.match?(request)
match.should eq true match.should eq true
end end
it "matches the correct route" do
kemal = Kemal::Handler.new
kemal.add_route "GET", "/route1" do |env|
"Route 1"
end
kemal.add_route "GET", "/route2" do |env|
"Route 2"
end
request = HTTP::Request.new("GET", "/route2")
response = kemal.call(request)
response.body.should eq("Route 2")
end
end end
end end

View file

@ -23,7 +23,7 @@ class Kemal::Handler < HTTP::Handler
match = route.match?(request) match = route.match?(request)
if match if match
params = Kemal::ParamParser.new(route, request).parse params = Kemal::ParamParser.new(route, request).parse
context = Context.new(request, params.not_nil!) context = Context.new(request, params)
begin begin
body = route.handler.call(context).to_s body = route.handler.call(context).to_s
return HTTP::Response.new(context.status_code, body, context.response_headers) return HTTP::Response.new(context.status_code, body, context.response_headers)

View file

@ -13,6 +13,11 @@ class Kemal::Route
components = request.path.not_nil!.split "/" components = request.path.not_nil!.split "/"
return nil unless request.method == @method return nil unless request.method == @method
return nil unless components.size == @components.size return nil unless components.size == @components.size
@components.zip(components) do |route_component, req_component|
unless route_component.starts_with? ':'
return nil unless route_component == req_component
end
end
true true
end end
end end