mirror of
https://gitea.invidious.io/iv-org/shard-kemal.git
synced 2024-08-15 00:53:36 +00:00
Fixed match bug
This commit is contained in:
parent
da23d397c6
commit
c49191e650
3 changed files with 20 additions and 2 deletions
|
@ -1,7 +1,7 @@
|
|||
require "./spec_helper"
|
||||
|
||||
describe "Route" do
|
||||
describe "match" do
|
||||
describe "match?" do
|
||||
it "doesn't match because of route" do
|
||||
route = Route.new("GET", "/foo/bar") { "" }
|
||||
request = HTTP::Request.new("GET", "/world?message=coco")
|
||||
|
@ -20,5 +20,18 @@ describe "Route" do
|
|||
match = route.match?(request)
|
||||
match.should eq true
|
||||
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
|
||||
|
|
|
@ -23,7 +23,7 @@ class Kemal::Handler < HTTP::Handler
|
|||
match = route.match?(request)
|
||||
if match
|
||||
params = Kemal::ParamParser.new(route, request).parse
|
||||
context = Context.new(request, params.not_nil!)
|
||||
context = Context.new(request, params)
|
||||
begin
|
||||
body = route.handler.call(context).to_s
|
||||
return HTTP::Response.new(context.status_code, body, context.response_headers)
|
||||
|
|
|
@ -13,6 +13,11 @@ class Kemal::Route
|
|||
components = request.path.not_nil!.split "/"
|
||||
return nil unless request.method == @method
|
||||
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
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue